diff --git a/include/core/SkFontMgr.h b/include/core/SkFontMgr.h index eccb0a729cba039b649245b52c112df7ed05e7d8..e2dda0f7a13f770d2e93377259fb178a12f7df62 100644 --- a/include/core/SkFontMgr.h +++ b/include/core/SkFontMgr.h @@ -113,6 +113,10 @@ public: sk_sp legacyMakeTypeface(const char familyName[], SkFontStyle style) const; +#ifdef OHOS_THEME_FONT + void InvalidateThemeFont(int fd); +#endif + /** Return the default fontmgr. */ static sk_sp RefDefault(); @@ -143,6 +147,10 @@ protected: virtual sk_sp onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0; +#ifdef OHOS_THEME_FONT + virtual void onInvalidateThemeFont(int fd) = 0; +#endif + private: /** Implemented by porting layer to return the default factory. */ static sk_sp Factory(); diff --git a/src/core/SkFontMgr.cpp b/src/core/SkFontMgr.cpp index 02154c9fd47ec23c944327329dd2f716c55abf2e..3af38de6ce4967d3efe593fd0aa580cee4cd16b1 100644 --- a/src/core/SkFontMgr.cpp +++ b/src/core/SkFontMgr.cpp @@ -78,6 +78,10 @@ protected: sk_sp onLegacyMakeTypeface(const char [], SkFontStyle) const override { return nullptr; } + +#ifdef OHOS_THEME_FONT + void onInvalidateThemeFont(int fd) override {} +#endif }; static sk_sp emptyOnNull(sk_sp&& fsset) { @@ -148,6 +152,12 @@ sk_sp SkFontMgr::legacyMakeTypeface(const char familyName[], SkFontS return this->onLegacyMakeTypeface(familyName, style); } +#ifdef OHOS_THEME_FONT +void SkFontMgr::InvalidateThemeFont(int fd) { + this->onInvalidateThemeFont(fd); +} +#endif + sk_sp SkFontMgr::RefEmpty() { static SkEmptyFontMgr singleton; return sk_ref_sp(&singleton); diff --git a/src/ports/SkFontMgr_FontConfigInterface.cpp b/src/ports/SkFontMgr_FontConfigInterface.cpp index 8de54c5359d179f92b255d3ed9b73511241c44be..816dedb81dd4c6d5c9bc3ab79c975737de5a85b5 100644 --- a/src/ports/SkFontMgr_FontConfigInterface.cpp +++ b/src/ports/SkFontMgr_FontConfigInterface.cpp @@ -281,6 +281,10 @@ protected: return face; } + +#ifdef OHOS_THEME_FONT + void onInvalidateThemeFont(int fd) override {} +#endif }; SK_API sk_sp SkFontMgr_New_FCI(sk_sp fci) { diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp index 25cc9f9172d9c7e17e815ae766717537f77c2574..49d4ddb9a208a802a8ff7a48ad3c7ec4df13f5c9 100644 --- a/src/ports/SkFontMgr_android.cpp +++ b/src/ports/SkFontMgr_android.cpp @@ -435,6 +435,9 @@ protected: return sk_sp(fDefaultStyleSet->matchStyle(style)); } +#ifdef OHOS_THEME_FONT + void onInvalidateThemeFont(int fd) override {} +#endif private: diff --git a/src/ports/skia_ohos/FontConfig_ohos.cpp b/src/ports/skia_ohos/FontConfig_ohos.cpp old mode 100755 new mode 100644 index 530ee23d24a5156070f940930f493db850b37122..a65ba05d158ff3448029de9a7975e674f5ef3d8d --- a/src/ports/skia_ohos/FontConfig_ohos.cpp +++ b/src/ports/skia_ohos/FontConfig_ohos.cpp @@ -1251,3 +1251,28 @@ int FontConfig_OHOS::logErrInfo(int err, const char* key, Json::ValueType expect } return err; } + +#ifdef OHOS_THEME_FONT +void FontConfig_OHOS::InvalidateThemeFont(const SkTypeface_FreeType::Scanner& fontScanner, int fd) { + sk_sp data(SkData::MakeFromFD(fd)); + std::unique_ptr stream = + (data ? std::make_unique(std::move(data)) : nullptr); + + FontInfo font; + int count = 0; + if (stream == nullptr || !fontScanner.recognizedFont(stream.get(), &count) || + !fontScanner.scanFont( + stream.get(), 0, &font.familyName, &font.style, &font.isFixedWidth, nullptr)) { + themeFontTypeface.reset(); + LOGE("[themefont] InvalidateThemeFont failed, stream(%p) count(%d).\n", + stream.get(), + count); + return; + } + + font.stream = std::move(stream); + themeFontTypeface = sk_make_sp(SkString(), font); +} + +SkTypeface_OHOS* FontConfig_OHOS::getThemeFontTypeface() const { return themeFontTypeface.get(); } +#endif diff --git a/src/ports/skia_ohos/FontConfig_ohos.h b/src/ports/skia_ohos/FontConfig_ohos.h index 8279e5e59faafe21cb3c786552461ae3994caaf2..ad36e0d9d5222e5bbc518205b883927e267f20d0 100755 --- a/src/ports/skia_ohos/FontConfig_ohos.h +++ b/src/ports/skia_ohos/FontConfig_ohos.h @@ -113,6 +113,11 @@ public: SkTypeface_OHOS* getTypeface(int styleIndex, const SkFontStyle& style, bool isFallback = false) const; +#ifdef OHOS_THEME_FONT + void InvalidateThemeFont(const SkTypeface_FreeType::Scanner& fontScanner, int fd); + SkTypeface_OHOS* getThemeFontTypeface() const; +#endif + #if ENABLE_DEBUG void dumpFont(const FontInfo& font) const; void dumpGeneric() const; @@ -184,6 +189,9 @@ private: FallbackForMap fallbackForMap; // a hash table to save the fallbackFor pairs GenericFamilySet genericFamilySet; // the font style set list of generic family FallbackSet fallbackSet; // the font style set list of fallback family +#ifdef OHOS_THEME_FONT + sk_sp themeFontTypeface; +#endif NamesMap genericNames; // a map to store the index of a family for generic family NamesMap fallbackNames; // a map to store the index of a family for fallback family diff --git a/src/ports/skia_ohos/SkFontMgr_ohos.cpp b/src/ports/skia_ohos/SkFontMgr_ohos.cpp index 2b790edab220916c84729dd79cc23523ab14920e..74739786afced73aa7b57ea00f59e0c9da19b5ad 100644 --- a/src/ports/skia_ohos/SkFontMgr_ohos.cpp +++ b/src/ports/skia_ohos/SkFontMgr_ohos.cpp @@ -95,7 +95,26 @@ sk_sp SkFontMgr_OHOS::onMatchFamilyStyle(const char familyName[], int styleIndex = 0; if (familyName) { styleIndex = fontConfig->getStyleIndex(familyName, isFallback); + if (styleIndex < 0 && SkString(familyName) == SkString("sans-serif")) { + // TODO: remove this |if| when fontconfig.json support sans-serif. + SkString sansFamilyName("HarmonyOS-Sans"); + styleIndex = fontConfig->getStyleIndex(sansFamilyName.c_str(), isFallback); + } + } + +#ifdef OHOS_THEME_FONT + auto* themeFontTypeface = fontConfig->getThemeFontTypeface(); + if (styleIndex >= 0 && !isFallback && themeFontTypeface) { + return sk_ref_sp(themeFontTypeface); } + if (styleIndex < 0 && themeFontTypeface) { + const FontInfo* fontInfo = themeFontTypeface->getFontInfo(); + if (fontInfo && SkString(familyName) == fontInfo->familyName) { + return sk_ref_sp(themeFontTypeface); + } + } +#endif + return sk_ref_sp(fontConfig->getTypeface(styleIndex, style, isFallback)); } @@ -119,6 +138,14 @@ sk_sp SkFontMgr_OHOS::onMatchFamilyStyleCharacter(const char familyN if (fontConfig == nullptr) { return nullptr; } + +#ifdef OHOS_THEME_FONT + auto* themeFontTypeface = fontConfig->getThemeFontTypeface(); + if (themeFontTypeface && themeFontTypeface->unicharToGlyph(character) != 0) { + return sk_ref_sp(themeFontTypeface); + } +#endif + const FallbackForMap& fallbackForMap = fontConfig->getFallbackForMap(); const FallbackSet& fallbackSet = fontConfig->getFallbackSet(); SkString defaultFamily(""); @@ -139,6 +166,7 @@ sk_sp SkFontMgr_OHOS::onMatchFamilyStyleCharacter(const char familyN FontConfig_OHOS::errToString(ERROR_FAMILY_NOT_FOUND), defaultFamily.c_str()); return nullptr; } + while (true) { if (bcp47Count > 0) { SkTypeface* retTp = findTypeface(*item, style, bcp47, bcp47Count, character); @@ -441,6 +469,14 @@ sk_sp SkFontMgr_OHOS::makeTypeface(SkFontData* fontData) const return sk_make_sp(fontInfo); } +#ifdef OHOS_THEME_FONT +void SkFontMgr_OHOS::onInvalidateThemeFont(int fd) { + if (fontConfig) { + fontConfig->InvalidateThemeFont(fontScanner, fd); + } +} +#endif + /*! To create SkFontMgr object for Harmony platform * \param fname the full name of system font configuration documents * \return The object of SkFontMgr_OHOS diff --git a/src/ports/skia_ohos/SkFontMgr_ohos.h b/src/ports/skia_ohos/SkFontMgr_ohos.h index f8e41f85cdcd041484f487aa2a8894f8e9378957..c77caea1bd04042f424a3cf812f377ea8b5fb89f 100644 --- a/src/ports/skia_ohos/SkFontMgr_ohos.h +++ b/src/ports/skia_ohos/SkFontMgr_ohos.h @@ -47,6 +47,10 @@ protected: virtual sk_sp onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override; +#ifdef OHOS_THEME_FONT + virtual void onInvalidateThemeFont(int fd) override; +#endif + private: std::shared_ptr fontConfig = nullptr; // the pointer of FontConfig_OHOS SkTypeface_FreeType::Scanner fontScanner; // the scanner to parse a font file