Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jan 21, 2024
2 parents 19bc002 + cb692e3 commit 2800d5f
Show file tree
Hide file tree
Showing 66 changed files with 383 additions and 144 deletions.
124 changes: 96 additions & 28 deletions Common/Render/Text/draw_text_sdl.cpp
Expand Up @@ -18,6 +18,18 @@
#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"

static std::string getlocale() {
// setlocale is not an intuitive function...
char *curlocale = setlocale(LC_CTYPE, nullptr);
std::string loc = curlocale ? std::string(curlocale) : "en_US";
size_t ptPos = loc.find('.');
// Remove any secondary specifier.
if (ptPos != std::string::npos) {
loc.resize(ptPos);
}
return loc;
}

TextDrawerSDL::TextDrawerSDL(Draw::DrawContext *draw): TextDrawer(draw) {
if (TTF_Init() < 0) {
ERROR_LOG(G3D, "Unable to initialize SDL2_ttf");
Expand All @@ -29,7 +41,7 @@ TextDrawerSDL::TextDrawerSDL(Draw::DrawContext *draw): TextDrawer(draw) {
config = FcInitLoadConfigAndFonts();
#endif

PrepareFallbackFonts();
PrepareFallbackFonts(getlocale());
}

TextDrawerSDL::~TextDrawerSDL() {
Expand All @@ -44,22 +56,59 @@ TextDrawerSDL::~TextDrawerSDL() {
}

// If a user complains about missing characters on SDL, re-visit this!
void TextDrawerSDL::PrepareFallbackFonts() {
void TextDrawerSDL::PrepareFallbackFonts(std::string_view locale) {
#if defined(USE_SDL2_TTF_FONTCONFIG)
FcObjectSet *os = FcObjectSetBuild (FC_FILE, FC_INDEX, (char *) 0);

FcPattern *names[] = {
FcNameParse((const FcChar8 *) "Source Han Sans Medium"),
FcNameParse((const FcChar8 *) "Droid Sans Bold"),
FcNameParse((const FcChar8 *) "DejaVu Sans Condensed"),
FcNameParse((const FcChar8 *) "Noto Sans CJK Medium"),
FcNameParse((const FcChar8 *) "Noto Sans Hebrew Medium"),
FcNameParse((const FcChar8 *) "Noto Sans Lao Medium"),
FcNameParse((const FcChar8 *) "Noto Sans Thai Medium")
// To install the recommended Droid Sans fallback font in Ubuntu:
// sudo apt install fonts-droid-fallback
const char *hardcodedNames[] = {
"Droid Sans Medium",
"Droid Sans Fallback",
"Source Han Sans Medium",
"Noto Sans CJK Medium",
"Noto Sans Hebrew Medium",
"Noto Sans Lao Medium",
"Noto Sans Thai Medium",
"DejaVu Sans Condensed",
"DejaVu Sans",
"Meera Regular",
"FreeSans",
"Gargi",
"KacstDigital",
"KacstFarsi",
"Khmer OS",
"Paduak",
"Paduak",
"Jamrul",
};

for (int i = 0; i < ARRAY_SIZE(names); i++) {
FcFontSet *foundFonts = FcFontList(config, names[i], os);
std::vector<const char *> names;
if (locale == "zh_CN") {
names.push_back("Noto Sans CJK SC");
} else if (locale == "zh_TW") {
names.push_back("Noto Sans CJK TC");
names.push_back("Noto Sans CJK HK");
} else if (locale == "ja_JP") {
names.push_back("Noto Sans CJK JP");
} else if (locale == "ko_KR") {
names.push_back("Noto Sans CJK KR");
} else {
// Let's just pick one.
names.push_back("Noto Sans CJK JP");
}

// Then push all the hardcoded ones.
for (int i = 0; i < ARRAY_SIZE(hardcodedNames); i++) {
names.push_back(hardcodedNames[i]);
}

// First, add the region-specific Noto fonts according to the locale.

for (int i = 0; i < names.size(); i++) {
// printf("trying font name %s\n", names[i]);
FcPattern *name = FcNameParse((const FcChar8 *)names[i]);
FcFontSet *foundFonts = FcFontList(config, name, os);

for (int j = 0; foundFonts && j < foundFonts->nfont; ++j) {
FcPattern* font = foundFonts->fonts[j];
Expand All @@ -72,6 +121,7 @@ void TextDrawerSDL::PrepareFallbackFonts() {

if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch) {
std::string path_str((const char*)path);
// printf("fallback font: %s\n", path_str.c_str());
fallbackFontPaths_.push_back(std::make_pair(path_str, fontIndex));
}
}
Expand All @@ -80,7 +130,7 @@ void TextDrawerSDL::PrepareFallbackFonts() {
FcFontSetDestroy(foundFonts);
}

FcPatternDestroy(names[i]);
FcPatternDestroy(name);
}

if (os) {
Expand Down Expand Up @@ -152,34 +202,42 @@ uint32_t TextDrawerSDL::CheckMissingGlyph(const std::string& text) {
return missingGlyph;
}

// If this returns true, the first font in fallbackFonts_ can be used as a fallback.
bool TextDrawerSDL::FindFallbackFonts(uint32_t missingGlyph, int ptSize) {
// If we encounter a missing glyph, try to use one of the fallback fonts.
// If this returns >= 0, the nth font in fallbackFonts_ can be used as a fallback.
int TextDrawerSDL::FindFallbackFonts(uint32_t missingGlyph, int ptSize) {
auto iter = glyphFallbackFontIndex_.find(missingGlyph);

if (iter != glyphFallbackFontIndex_.end()) {
return iter->second;
}

// If we encounter a missing glyph, try to use one of already loaded fallback fonts.
for (int i = 0; i < fallbackFonts_.size(); i++) {
TTF_Font *fallbackFont = fallbackFonts_[i];
if (TTF_GlyphIsProvided32(fallbackFont, missingGlyph)) {
fallbackFonts_.erase(fallbackFonts_.begin() + i);
fallbackFonts_.insert(fallbackFonts_.begin(), fallbackFont);
return true;
glyphFallbackFontIndex_[missingGlyph] = i;
return i;
}
}

// If none of the loaded fonts can handle it, load more fonts.
// TODO: Don't retry already tried fonts.
for (int i = 0; i < fallbackFontPaths_.size(); i++) {
std::string& fontPath = fallbackFontPaths_[i].first;
int faceIndex = fallbackFontPaths_[i].second;

TTF_Font *font = TTF_OpenFontIndex(fontPath.c_str(), ptSize, faceIndex);

if (TTF_GlyphIsProvided32(font, missingGlyph)) {
fallbackFonts_.insert(fallbackFonts_.begin(), font);
return true;
fallbackFonts_.push_back(font);
return fallbackFonts_.size() - 1;
} else {
TTF_CloseFont(font);
}
}

return false;
// Not found at all? Let's remember that for this glyph.
glyphFallbackFontIndex_[missingGlyph] = -1;
return -1;
}

uint32_t TextDrawerSDL::SetFont(const char *fontName, int size, int flags) {
Expand Down Expand Up @@ -227,13 +285,17 @@ void TextDrawerSDL::MeasureString(const char *str, size_t len, float *w, float *
if (iter != sizeCache_.end()) {
entry = iter->second.get();
} else {
printf("re-measuring %s\n", str);
TTF_Font *font = fontMap_.find(fontHash_)->second;
int ptSize = TTF_FontHeight(font) / 1.35;

uint32_t missingGlyph = CheckMissingGlyph(key.text);

if (missingGlyph && FindFallbackFonts(missingGlyph, ptSize)) {
font = fallbackFonts_[0];
if (missingGlyph) {
int fallbackFont = FindFallbackFonts(missingGlyph, ptSize);
if (fallbackFont >= 0) {
font = fallbackFonts_[fallbackFont];
}
}

int width = 0;
Expand Down Expand Up @@ -263,8 +325,11 @@ void TextDrawerSDL::MeasureStringRect(const char *str, size_t len, const Bounds
int ptSize = TTF_FontHeight(font) / 1.35;
uint32_t missingGlyph = CheckMissingGlyph(toMeasure);

if (missingGlyph && FindFallbackFonts(missingGlyph, ptSize)) {
font = fallbackFonts_[0];
if (missingGlyph) {
int fallbackFont = FindFallbackFonts(missingGlyph, ptSize);
if (fallbackFont >= 0) {
font = fallbackFonts_[fallbackFont];
}
}

std::vector<std::string> lines;
Expand Down Expand Up @@ -374,8 +439,11 @@ void TextDrawerSDL::DrawStringBitmap(std::vector<uint8_t> &bitmapData, TextStrin

uint32_t missingGlyph = CheckMissingGlyph(processedStr);

if (missingGlyph && FindFallbackFonts(missingGlyph, ptSize)) {
font = fallbackFonts_[0];
if (missingGlyph) {
int fallbackFont = FindFallbackFonts(missingGlyph, ptSize);
if (fallbackFont >= 0) {
font = fallbackFonts_[fallbackFont];
}
}

#if SDL_TTF_VERSION_ATLEAST(2, 20, 0)
Expand Down
6 changes: 4 additions & 2 deletions Common/Render/Text/draw_text_sdl.h
Expand Up @@ -28,9 +28,9 @@ class TextDrawerSDL : public TextDrawer {

protected:
void ClearCache() override;
void PrepareFallbackFonts();
void PrepareFallbackFonts(std::string_view locale);
uint32_t CheckMissingGlyph(const std::string& text);
bool FindFallbackFonts(uint32_t missingGlyph, int ptSize);
int FindFallbackFonts(uint32_t missingGlyph, int ptSize);

uint32_t fontHash_;
std::map<uint32_t, _TTF_Font *> fontMap_;
Expand All @@ -41,6 +41,8 @@ class TextDrawerSDL : public TextDrawer {
std::vector<_TTF_Font *> fallbackFonts_;
std::vector<std::pair<std::string, int>> fallbackFontPaths_; // path and font face index

std::map<int, int> glyphFallbackFontIndex_;

#if defined(USE_SDL2_TTF_FONTCONFIG)
FcConfig *config;
#endif
Expand Down
1 change: 1 addition & 0 deletions Common/System/OSD.h
Expand Up @@ -14,6 +14,7 @@ enum class OSDType {
MESSAGE_ERROR,
MESSAGE_ERROR_DUMP, // displays lots of text (after the first line), small size
MESSAGE_FILE_LINK,
MESSAGE_CENTERED_WARNING,

ACHIEVEMENT_UNLOCKED,

Expand Down
1 change: 1 addition & 0 deletions Common/System/Request.cpp
Expand Up @@ -134,6 +134,7 @@ void System_CreateGameShortcut(const Path &path, const std::string &title) {
g_requestManager.MakeSystemRequest(SystemRequestType::CREATE_GAME_SHORTCUT, NO_REQUESTER_TOKEN, nullptr, nullptr, path.ToString(), title, 0);
}

// Also acts as just show folder, if you pass in a folder.
void System_ShowFileInFolder(const Path &path) {
g_requestManager.MakeSystemRequest(SystemRequestType::SHOW_FILE_IN_FOLDER, NO_REQUESTER_TOKEN, nullptr, nullptr, path.ToString(), "", 0);
}
1 change: 1 addition & 0 deletions Core/ConfigValues.h
Expand Up @@ -151,6 +151,7 @@ enum class ScreenEdgePosition {
TOP_RIGHT = 5,
CENTER_LEFT = 6,
CENTER_RIGHT = 7,
CENTER = 8, // Used for REALLY important messages! Not RetroAchievements notifications.
VALUE_COUNT,
};

Expand Down
6 changes: 5 additions & 1 deletion Core/Core.cpp
Expand Up @@ -105,7 +105,11 @@ void Core_Stop() {

bool Core_ShouldRunBehind() {
// Enforce run-behind if ad-hoc connected
return g_Config.bRunBehindPauseMenu || __NetAdhocConnected();
return g_Config.bRunBehindPauseMenu || Core_MustRunBehind();
}

bool Core_MustRunBehind() {
return __NetAdhocConnected();
}

bool Core_IsStepping() {
Expand Down
1 change: 1 addition & 0 deletions Core/Core.h
Expand Up @@ -39,6 +39,7 @@ void Core_SetGraphicsContext(GraphicsContext *ctx);
void Core_EnableStepping(bool step, const char *reason = nullptr, u32 relatedAddress = 0);

bool Core_ShouldRunBehind();
bool Core_MustRunBehind();

bool Core_NextFrame();
void Core_DoSingleStep();
Expand Down
2 changes: 1 addition & 1 deletion Core/HLE/proAdhoc.cpp
Expand Up @@ -2267,7 +2267,7 @@ int initNetwork(SceNetAdhocctlAdhocId *adhoc_id){
socklen_t addrLen = sizeof(LocalIP);
memset(&LocalIP, 0, addrLen);
getsockname((int)metasocket, &LocalIP, &addrLen);
g_OSD.Show(OSDType::MESSAGE_SUCCESS, n->T("Network Initialized"), 1.0);
g_OSD.Show(OSDType::MESSAGE_SUCCESS, n->T("Network initialized"), 1.0);
return 0;
} else {
return SOCKET_ERROR;
Expand Down
8 changes: 6 additions & 2 deletions Core/HLE/sceDisplay.cpp
Expand Up @@ -484,7 +484,9 @@ static void DoFrameIdleTiming() {
sleep_ms(1);
#else
const double left = goal - cur_time;
usleep((long)(left * 1000000));
if (left > 0.0f && left < 1.0f) { // Sanity check
usleep((long)(left * 1000000));
}
#endif
}

Expand Down Expand Up @@ -743,7 +745,9 @@ void hleLagSync(u64 userdata, int cyclesLate) {
// Tight loop on win32 - intentionally, as timing is otherwise not precise enough.
#ifndef _WIN32
const double left = goal - now;
usleep((long)(left * 1000000.0));
if (left > 0.0f && left < 1.0f) { // Sanity check
usleep((long)(left * 1000000.0));
}
#else
yield();
#endif
Expand Down
2 changes: 1 addition & 1 deletion SDL/SDLMain.cpp
Expand Up @@ -563,9 +563,9 @@ bool System_GetPropertyBool(SystemProperty prop) {
#if PPSSPP_PLATFORM(SWITCH)
case SYSPROP_HAS_TEXT_INPUT_DIALOG:
return __nx_applet_type == AppletType_Application || __nx_applet_type != AppletType_SystemApplication;
#endif
case SYSPROP_HAS_KEYBOARD:
return true;
#endif
case SYSPROP_APP_GOLD:
#ifdef GOLD
return true;
Expand Down
25 changes: 21 additions & 4 deletions Tools/langtool/src/main.rs
Expand Up @@ -27,6 +27,11 @@ enum Command {
section: String,
key: String,
},
AddNewKeyValue {
section: String,
key: String,
value: String,
},
MoveKey {
old: String,
new: String,
Expand Down Expand Up @@ -116,9 +121,9 @@ fn remove_key(target_ini: &mut IniFile, section: &str, key: &str) -> io::Result<
Ok(())
}

fn add_new_key(target_ini: &mut IniFile, section: &str, key: &str) -> io::Result<()> {
fn add_new_key(target_ini: &mut IniFile, section: &str, key: &str, value: &str) -> io::Result<()> {
if let Some(section) = target_ini.get_section_mut(section) {
section.insert_line_if_missing(&format!("{} = {}", key, key));
section.insert_line_if_missing(&format!("{} = {}", key, value));
} else {
println!("No section {}", section);
}
Expand Down Expand Up @@ -206,7 +211,12 @@ fn main() {
Command::AddNewKey {
ref section,
ref key,
} => add_new_key(&mut target_ini, section, key).unwrap(),
} => add_new_key(&mut target_ini, section, key, key).unwrap(),
Command::AddNewKeyValue {
ref section,
ref key,
ref value,
} => add_new_key(&mut target_ini, section, key, value).unwrap(),
Command::MoveKey {
ref old,
ref new,
Expand Down Expand Up @@ -235,7 +245,14 @@ fn main() {
ref section,
ref key,
} => {
add_new_key(&mut reference_ini, section, key).unwrap();
add_new_key(&mut reference_ini, section, key, key).unwrap();
}
Command::AddNewKeyValue {
ref section,
ref key,
ref value,
} => {
add_new_key(&mut reference_ini, section, key, value).unwrap();
}
Command::SortSection { ref section } => sort_section(&mut reference_ini, section).unwrap(),
Command::RenameKey {
Expand Down

0 comments on commit 2800d5f

Please sign in to comment.