diff --git a/CMakeLists.txt b/CMakeLists.txt index 37f833ed28..c220ed00d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2787,6 +2787,10 @@ set(NativeAssets assets/themes assets/vfpu assets/Roboto_Condensed-Regular.ttf + assets/Roboto_Condensed-Light.ttf + assets/Roboto_Condensed-Bold.ttf + assets/Roboto_Condensed-Italic.ttf + assets/Inconsolata-Regular.ttf assets/7z.png assets/compat.ini assets/infra-dns.json diff --git a/Common/Render/Text/Font.h b/Common/Render/Text/Font.h index a53ed84b96..5ceba1085e 100644 --- a/Common/Render/Text/Font.h +++ b/Common/Render/Text/Font.h @@ -8,6 +8,7 @@ #include "Common/Render/TextureAtlas.h" #include "Common/Data/Hash/Hash.h" +// NOTE: Make sure the java flags match. enum class FontStyleFlags : u8 { Default = 0, Bold = 1, diff --git a/Common/Render/Text/draw_text.cpp b/Common/Render/Text/draw_text.cpp index 4e41069d17..3bda006b18 100644 --- a/Common/Render/Text/draw_text.cpp +++ b/Common/Render/Text/draw_text.cpp @@ -283,11 +283,11 @@ struct FontDesc { // Append ".ttf" to get the actual filenames. static const FontDesc g_fontDescs[] = { - {FontFamily::SansSerif, FontStyleFlags::Default, "Roboto Condensed", "Roboto_Condensed-Regular"}, - {FontFamily::SansSerif, FontStyleFlags::Bold, "Roboto Condensed", "Roboto_Condensed-Bold"}, - {FontFamily::SansSerif, FontStyleFlags::Italic, "Roboto Condensed", "Roboto_Condensed-Italic"}, - {FontFamily::SansSerif, FontStyleFlags::Light, "Roboto Condensed", "Roboto_Condensed-Light"}, - {FontFamily::Fixed, FontStyleFlags::Default, "Inconsolata", "Inconsolata-Regular"}, + {FontFamily::SansSerif, FontStyleFlags::Default, "Roboto Condensed", "Roboto_Condensed-Regular.ttf"}, + {FontFamily::SansSerif, FontStyleFlags::Bold, "Roboto Condensed", "Roboto_Condensed-Bold.ttf"}, + {FontFamily::SansSerif, FontStyleFlags::Italic, "Roboto Condensed", "Roboto_Condensed-Italic.ttf"}, + {FontFamily::SansSerif, FontStyleFlags::Light, "Roboto Condensed", "Roboto_Condensed-Light.ttf"}, + {FontFamily::Fixed, FontStyleFlags::Default, "Inconsolata", "Inconsolata-Regular.ttf"}, }; std::map g_fontOverrides; @@ -296,34 +296,33 @@ void SetFontNameOverride(FontFamily family, std::string_view overrideFont) { g_fontOverrides[family] = std::string(overrideFont); } -void GetFilenamesForFontStyle(const FontStyle &font, std::vector *out, bool all) { +std::vector GetAllFontFilenames() { + std::vector vec; + for (const auto &desc : g_fontDescs) { + vec.emplace_back(desc.filename); + } + // uniquify idiom + std::sort(vec.begin(), vec.end()); + vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); + return vec; +} + +std::string GetFilenameForFontStyle(const FontStyle &font) { for (const auto &desc : g_fontDescs) { - bool match = all; if (desc.flags == font.flags && desc.family == font.family) { - match = true; + return std::string(desc.filename); } - if (match) { - out->push_back(join(desc.filename, ".ttf")); - } - } - - if (all) { - // uniquify idiom - std::sort(out->begin(), out->end()); - out->erase(std::unique(out->begin(), out->end()), out->end()); - return; - } - - if (!out->empty()) { - return; } // If nothing matched, just return the first font of the requested family. for (const auto &desc : g_fontDescs) { if (desc.family == font.family) { - out->push_back(join(desc.filename, ".ttf")); + return std::string(desc.filename); } } + + _dbg_assert_(false); + return ""; } std::string GetFontNameForFontStyle(const FontStyle &font, FontStyleFlags *outFlags) { diff --git a/Common/Render/Text/draw_text.h b/Common/Render/Text/draw_text.h index 51982832da..313f4629bd 100644 --- a/Common/Render/Text/draw_text.h +++ b/Common/Render/Text/draw_text.h @@ -142,7 +142,8 @@ protected: // The backends can use this to query the filenames of the fonts. // Some backends want to just load all the fonts, just pass all. // Note that the ttf file extension is included in the output. -void GetFilenamesForFontStyle(const FontStyle &font, std::vector *out, bool all); +std::vector GetAllFontFilenames(); +std::string GetFilenameForFontStyle(const FontStyle &font); std::string GetFontNameForFontStyle(const FontStyle &font, FontStyleFlags *outFlags); // Some languages use an override font, set via ini file. diff --git a/Common/Render/Text/draw_text_android.cpp b/Common/Render/Text/draw_text_android.cpp index b321655c16..2c467f0761 100644 --- a/Common/Render/Text/draw_text_android.cpp +++ b/Common/Render/Text/draw_text_android.cpp @@ -22,8 +22,10 @@ TextDrawerAndroid::TextDrawerAndroid(Draw::DrawContext *draw) : TextDrawer(draw) jclass localClass = findClass(textRendererClassName); cls_textRenderer = reinterpret_cast(env->NewGlobalRef(localClass)); if (cls_textRenderer) { - method_measureText = env->GetStaticMethodID(cls_textRenderer, "measureText", "(Ljava/lang/String;D)I"); - method_renderText = env->GetStaticMethodID(cls_textRenderer, "renderText", "(Ljava/lang/String;D)[I"); + method_allocFont = env->GetStaticMethodID(cls_textRenderer, "allocFont", "(Ljava/lang/String;)I"); + method_freeAllFonts = env->GetStaticMethodID(cls_textRenderer, "freeAllFonts", "()V"); + method_measureText = env->GetStaticMethodID(cls_textRenderer, "measureText", "(Ljava/lang/String;ID)I"); + method_renderText = env->GetStaticMethodID(cls_textRenderer, "renderText", "(Ljava/lang/String;ID)[I"); } else { ERROR_LOG(Log::G3D, "Failed to find class: '%s'", textRendererClassName); } @@ -44,59 +46,56 @@ bool TextDrawerAndroid::IsReady() const { return cls_textRenderer != nullptr && method_measureText != nullptr && method_renderText != nullptr; } -uint32_t TextDrawerAndroid::SetOrCreateFont(const FontStyle &style) { +void TextDrawerAndroid::SetOrCreateFont(const FontStyle &style) { // We will only use the default font but just for consistency let's still involve // the font name. - const uint32_t fontHash = style.Hash(); - - auto iter = fontMap_.find(fontHash); + auto iter = fontMap_.find(style); if (iter != fontMap_.end()) { - fontHash_ = fontHash; - return fontHash; + fontStyle_ = style; + return; } + std::string filename = GetFilenameForFontStyle(style); + auto fontIter = allocatedFonts_.find(filename); + if (fontIter == allocatedFonts_.end()) { + auto env = getEnv(); + jstring jstr = env->NewStringUTF(filename.c_str()); + int fontId = env->CallStaticIntMethod(cls_textRenderer, method_allocFont, jstr); + env->DeleteLocalRef(jstr); + + if (fontId >= 0) { + allocatedFonts_[filename] = fontId; + } + } + + AndroidFontEntry entry{}; + entry.font = allocatedFonts_[filename]; + entry.size = 1.4f * style.sizePts / dpiScale_; // Not sure why this formula works. + // Just chose a factor that looks good, don't know what unit size is in anyway. - AndroidFontEntry entry{}; - entry.size = ((float)style.sizePts * 1.4f) / dpiScale_; - if (style.flags & FontStyleFlags::Bold) - entry.bold = true; - if (style.flags & FontStyleFlags::Italic) - entry.italic = true; - fontMap_[fontHash] = entry; - fontHash_ = fontHash; - return fontHash; -} - -void TextDrawerAndroid::SetFont(uint32_t fontHandle) { - uint32_t fontHash = fontHandle; - auto iter = fontMap_.find(fontHash); - if (iter != fontMap_.end()) { - fontHash_ = fontHandle; - } else { - ERROR_LOG(Log::G3D, "Invalid font handle %08x", fontHandle); - } + fontMap_[style] = entry; + fontStyle_ = style; } void TextDrawerAndroid::MeasureStringInternal(std::string_view str, float *w, float *h) { - float scaledSize = 14; - auto iter = fontMap_.find(fontHash_); - if (iter != fontMap_.end()) { - scaledSize = iter->second.size; - } else { + auto iter = fontMap_.find(fontStyle_); + if (iter == fontMap_.end()) { ERROR_LOG(Log::G3D, "Missing font"); + *w = 1.0f; + *h = 1.0f; + return; } + std::string text; ConvertUTF8ToJavaModifiedUTF8(&text, str); auto env = getEnv(); jstring jstr = env->NewStringUTF(text.c_str()); - uint32_t size = env->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, scaledSize); + uint32_t size = env->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, iter->second.font, iter->second.size); env->DeleteLocalRef(jstr); *w = (float)(size >> 16); *h = (float)(size & 0xFFFF); - - // WARN_LOG(Log::G3D, "Measure Modified: '%.*s' size: %fx%f", (int)text.length(), text.data(), *w, *h); } bool TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) { @@ -105,12 +104,10 @@ bool TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextS return false; } - float size = 0.0f; - auto iter = fontMap_.find(fontHash_); - if (iter != fontMap_.end()) { - size = iter->second.size; - } else { + auto iter = fontMap_.find(fontStyle_); + if (iter == fontMap_.end()) { ERROR_LOG(Log::G3D, "Missing font"); + return false; } auto env = getEnv(); @@ -119,7 +116,7 @@ bool TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextS ConvertUTF8ToJavaModifiedUTF8(&text, str); jstring jstr = env->NewStringUTF(text.c_str()); - uint32_t textSize = env->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, size); + uint32_t textSize = env->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, iter->second.font, iter->second.size); int imageWidth = (short)(textSize >> 16); int imageHeight = (short)(textSize & 0xFFFF); if (imageWidth <= 0) @@ -129,7 +126,7 @@ bool TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextS // WARN_LOG(Log::G3D, "Text: '%.*s' (%02x)", (int)str.length(), str.data(), str[0]); // TODO: Handle bold/italic - jintArray imageData = (jintArray)env->CallStaticObjectMethod(cls_textRenderer, method_renderText, jstr, size); + jintArray imageData = (jintArray)env->CallStaticObjectMethod(cls_textRenderer, method_renderText, jstr, iter->second.font, iter->second.size); env->DeleteLocalRef(jstr); entry.texture = nullptr; diff --git a/Common/Render/Text/draw_text_android.h b/Common/Render/Text/draw_text_android.h index bf01be35b2..75dc56b5f0 100644 --- a/Common/Render/Text/draw_text_android.h +++ b/Common/Render/Text/draw_text_android.h @@ -10,9 +10,8 @@ #include struct AndroidFontEntry { + int font; float size; - bool bold; - bool italic; }; class TextDrawerAndroid : public TextDrawer { @@ -21,8 +20,7 @@ public: ~TextDrawerAndroid(); bool IsReady() const override; - uint32_t SetOrCreateFont(const FontStyle &style) override; - void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. + void SetOrCreateFont(const FontStyle &style) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; protected: @@ -34,10 +32,13 @@ protected: private: // JNI functions jclass cls_textRenderer; + jmethodID method_allocFont; + jmethodID method_freeAllFonts; jmethodID method_measureText; jmethodID method_renderText; - std::map fontMap_; + std::map fontMap_; + std::map allocatedFonts_; }; #endif diff --git a/Common/Render/Text/draw_text_uwp.cpp b/Common/Render/Text/draw_text_uwp.cpp index e4c904759e..78e569c40d 100644 --- a/Common/Render/Text/draw_text_uwp.cpp +++ b/Common/Render/Text/draw_text_uwp.cpp @@ -111,13 +111,14 @@ TextDrawerUWP::TextDrawerUWP(Draw::DrawContext *draw) : TextDrawer(draw), ctx_(n hr = m_dwriteFactory->CreateInMemoryFontFileLoader(&m_inMemoryLoader); if (FAILED(hr)) { - // handle error + _assert_msg_(false, "D2D CreateInMemoryFontFileLoader failed"); + } + hr = m_dwriteFactory->RegisterFontFileLoader(m_inMemoryLoader); + if (FAILED(hr)) { + _assert_msg_(false, "D2D RegisterFontFileLoader failed"); } - m_dwriteFactory->RegisterFontFileLoader(m_inMemoryLoader); - // Load our fonts. - std::vector fontFilenames; - GetFilenamesForFontStyle(FontStyle{}, &fontFilenames, true); + const std::vector fontFilenames = GetAllFontFilenames(); for (const auto &fname : fontFilenames) { size_t size; uint8_t *data = g_VFS.ReadFile(fname.c_str(), &size); diff --git a/Common/Render/Text/draw_text_win.cpp b/Common/Render/Text/draw_text_win.cpp index 78d8bae080..b1e2923119 100644 --- a/Common/Render/Text/draw_text_win.cpp +++ b/Common/Render/Text/draw_text_win.cpp @@ -77,8 +77,7 @@ TextDrawerWin32::TextDrawerWin32(Draw::DrawContext *draw) : TextDrawer(draw), ct ctx_->hDC = CreateCompatibleDC(NULL); // Load the font files (pass the all flags so we get all filenames); - std::vector fonts; - GetFilenamesForFontStyle(FontStyle(FontFamily::SansSerif, 16, FontStyleFlags::Default), &fonts, true); + std::vector fonts = GetAllFontFilenames(); for (const auto &iter : fonts) { std::string fn = "assets/" + iter; int numFontsAdded = AddFontResourceEx(ConvertUTF8ToWString(fn).c_str(), FR_PRIVATE, NULL); @@ -107,10 +106,9 @@ TextDrawerWin32::~TextDrawerWin32() { ClearFonts(); // Unload the fonts. - std::vector fonts; - GetFilenamesForFontStyle(FontStyle(FontFamily::SansSerif, 16, FontStyleFlags::Default), &fonts, true); + std::vector fonts = GetAllFontFilenames(); for (const auto &iter : fonts) { - std::string fn = "assets/" + iter + ".ttf"; + std::string fn = "assets/" + iter; RemoveFontResourceEx(ConvertUTF8ToWString(fn).c_str(), FR_PRIVATE, NULL); } diff --git a/UI/UIAtlas.cpp b/UI/UIAtlas.cpp index 9bd97c467d..df00ecc185 100644 --- a/UI/UIAtlas.cpp +++ b/UI/UIAtlas.cpp @@ -303,7 +303,7 @@ static bool GenerateUIAtlasImage(Atlas *atlas, float dpiScale, Image *dest) { } } - INFO_LOG(Log::G3D, " - Rasterized %d images in the svg image in %0.2f ms\n", shapeCount, svgStart.ElapsedMs()); + INFO_LOG(Log::G3D, " - Rasterized %d images in the svg image in %0.2f ms", shapeCount, svgStart.ElapsedMs()); Instant shadowStart = Instant::Now(); @@ -323,7 +323,7 @@ static bool GenerateUIAtlasImage(Atlas *atlas, float dpiScale, Image *dest) { } }, 0, (int)images.size(), 2, TaskPriority::HIGH); - INFO_LOG(Log::G3D, " - Drop-shadowed images in %0.2f ms\n", shadowStart.ElapsedMs()); + INFO_LOG(Log::G3D, " - Drop-shadowed images in %0.2f ms", shadowStart.ElapsedMs()); Instant pngStart = Instant::Now(); @@ -392,7 +392,7 @@ static bool GenerateUIAtlasImage(Atlas *atlas, float dpiScale, Image *dest) { if (SAVE_DEBUG_IMAGES) { dest->SavePNG("../ui_atlas_gen.png"); } - INFO_LOG(Log::G3D, "UI atlas generated in %.2f ms, size %dx%d with %zu images\n", svgStart.ElapsedMs(), dest->width(), dest->height(), genAtlasImages.size()); + INFO_LOG(Log::G3D, "UI atlas generated in %.2f ms, size %dx%d with %zu images", svgStart.ElapsedMs(), dest->width(), dest->height(), genAtlasImages.size()); return true; } diff --git a/android/src/org/ppsspp/ppsspp/TextRenderer.java b/android/src/org/ppsspp/ppsspp/TextRenderer.java index 63b5f2496f..8af8e207c8 100644 --- a/android/src/org/ppsspp/ppsspp/TextRenderer.java +++ b/android/src/org/ppsspp/ppsspp/TextRenderer.java @@ -7,41 +7,71 @@ import android.util.Log; import androidx.annotation.Keep; +import java.util.HashMap; + public class TextRenderer { - private static final Paint textPaint; + private static class Font { + public Paint paint; + public Typeface typeFace; + } + + private static Paint textPaint = null; + private static final Paint bg; private static final String TAG = "TextRenderer"; + private static int idGen = 1; + + private static HashMap fontMap = new HashMap<>(); + private static boolean highContrastFontsEnabled = false; + private static Context ctx; + static { - textPaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); - textPaint.setColor(Color.WHITE); bg = new Paint(); bg.setColor(0); } - public static void init(Context ctx) { + @Keep + public static int allocFont(String ttfFile) { try { - Typeface robotoCondensed = Typeface.createFromAsset(ctx.getAssets(), "Roboto-Condensed.ttf"); - if (robotoCondensed != null) { - Log.i(TAG, "Successfully loaded Roboto Condensed"); - textPaint.setTypeface(robotoCondensed); + Typeface typeFace = Typeface.createFromAsset(ctx.getAssets(), ttfFile); + if (typeFace != null) { + Log.i(TAG, "Successfully loaded typeface from " + ttfFile); } else { - Log.e(TAG, "Failed to load Roboto Condensed"); + Log.e(TAG, "Failed to load asset file " + ttfFile); } + int id = idGen++; + fontMap.put(id, typeFace); + return id; } catch (Exception e) { Log.e(TAG, "Exception when loading typeface. shouldn't happen but is reported. We just fall back." + e); + e.printStackTrace(); + return -1337; } - highContrastFontsEnabled = Settings.Secure.getInt(ctx.getContentResolver(), "high_text_contrast_enabled", 0) == 1; } - private static Point measureLine(String string, double textSize) { + @Keep + public static void freeAllFonts() { + fontMap.clear(); + } + + public static void init(Context ctx) { + Log.i(TAG, "initializing TextDrawerAndroid java side"); + textPaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); + textPaint.setColor(Color.WHITE); + highContrastFontsEnabled = Settings.Secure.getInt(ctx.getContentResolver(), "high_text_contrast_enabled", 0) == 1; + TextRenderer.ctx = ctx; + } + + private static Point measureLine(String string, int font, double textSize) { textPaint.setTextSize((float)textSize); int w = (int) textPaint.measureText(string); // Round width up to even already here to avoid annoyances from odd-width 16-bit textures // which OpenGL does not like - each line must be 4-byte aligned - w = (w + 5) & ~1; + w = (w + 1) & ~1; + w += 2; int h = (int) (textPaint.descent() - textPaint.ascent() + 2.0f); Point p = new Point(); p.x = w; @@ -49,12 +79,12 @@ public class TextRenderer { return p; } - private static Point measure(String string, double textSize) { + private static Point measure(String string, int font, double textSize) { String [] lines = string.replaceAll("\r", "").split("\n"); Point total = new Point(); total.x = 0; for (String line : lines) { - Point sz = measureLine(line, textSize); + Point sz = measureLine(line, font, textSize); total.x = Math.max(sz.x, total.x); } total.y = (int) (textPaint.descent() - textPaint.ascent()) * lines.length + 2; @@ -73,15 +103,17 @@ public class TextRenderer { } @Keep - public static int measureText(String string, double textSize) { + public static int measureText(String string, int font, double textSize) { + textPaint.setTypeface(fontMap.get(font)); textPaint.setTextSize((float) textSize); - Point s = measure(string, textSize); + Point s = measure(string, font, textSize); return (s.x << 16) | s.y; } - public static int[] renderText(String string, double textSize) { + public static int[] renderText(String string, int font, double textSize) { + textPaint.setTypeface(fontMap.get(font)); textPaint.setTextSize((float) textSize); - Point s = measure(string, textSize); + Point s = measure(string, font, textSize); int w = s.x; int h = s.y;