From fb736b41ce8ffab8b8b67b23c67e86fa556e41e8 Mon Sep 17 00:00:00 2001 From: belong326 Date: Thu, 26 Sep 2024 21:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=93=9D=E9=BB=84=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E3=80=91=E5=90=8C=E6=AD=A5=E9=BB=84=E5=8C=BA=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: belong326 Change-Id: Ic9e4c1430ebfc34fe4bc5f685681e2605347d8b5 --- include/core/SkFontMgr.h | 8 +++++ src/core/SkFontMgr.cpp | 10 ++++++ src/gpu/ganesh/ops/DrawMeshOp.cpp | 6 +++- src/gpu/ganesh/ops/RegionOp.cpp | 9 +++++- src/ports/SkFontMgr_FontConfigInterface.cpp | 4 +++ src/ports/SkFontMgr_android.cpp | 3 ++ src/ports/skia_ohos/FontConfig_ohos.cpp | 25 ++++++++++++++ src/ports/skia_ohos/FontConfig_ohos.h | 8 +++++ src/ports/skia_ohos/FontInfo_ohos.h | 18 +++++++++++ src/ports/skia_ohos/SkFontMgr_ohos.cpp | 36 +++++++++++++++++++++ src/ports/skia_ohos/SkFontMgr_ohos.h | 4 +++ src/ports/skia_ohos/SkTypeface_ohos.cpp | 29 +++++++++++------ 12 files changed, 149 insertions(+), 11 deletions(-) mode change 100755 => 100644 src/ports/skia_ohos/FontConfig_ohos.cpp diff --git a/include/core/SkFontMgr.h b/include/core/SkFontMgr.h index eccb0a72..e2dda0f7 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 02154c9f..3af38de6 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/gpu/ganesh/ops/DrawMeshOp.cpp b/src/gpu/ganesh/ops/DrawMeshOp.cpp index fa35a6d7..f4af710a 100644 --- a/src/gpu/ganesh/ops/DrawMeshOp.cpp +++ b/src/gpu/ganesh/ops/DrawMeshOp.cpp @@ -1006,7 +1006,11 @@ GrOp::CombineResult MeshOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const Gr if (SkToBool(fIndexCount) != SkToBool(that->fIndexCount)) { return CombineResult::kCannotCombine; } - if (SkToBool(fIndexCount) && fVertexCount > UINT16_MAX - that->fVertexCount) { + if (SkToBool(fIndexCount) && + // Index count would overflow + (fIndexCount > INT32_MAX - that->fIndexCount || + // *or* combined vertex count would not be referenceable by uint16 indices + fVertexCount > SkToInt(UINT16_MAX) - that->fVertexCount)) { return CombineResult::kCannotCombine; } diff --git a/src/gpu/ganesh/ops/RegionOp.cpp b/src/gpu/ganesh/ops/RegionOp.cpp index 8dc59b55..d676515a 100644 --- a/src/gpu/ganesh/ops/RegionOp.cpp +++ b/src/gpu/ganesh/ops/RegionOp.cpp @@ -9,6 +9,7 @@ #include "include/core/SkRegion.h" #include "src/core/SkMatrixPriv.h" +#include "src/base/SkSafeMath.h" #include "src/gpu/BufferWriter.h" #include "src/gpu/ganesh/GrCaps.h" #include "src/gpu/ganesh/GrDefaultGeoProcFactory.h" @@ -116,8 +117,14 @@ private: int numRegions = fRegions.size(); int numRects = 0; + + SkSafeMath safeMath; for (int i = 0; i < numRegions; i++) { - numRects += fRegions[i].fRegion.computeRegionComplexity(); + numRects = safeMath.addInt(numRects, fRegions[i].fRegion.computeRegionComplexity()); + } + if (!safeMath) { + // This is a nonsensical draw, so we can just drop it. + return; } if (!numRects) { diff --git a/src/ports/SkFontMgr_FontConfigInterface.cpp b/src/ports/SkFontMgr_FontConfigInterface.cpp index 8de54c53..816dedb8 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 25cc9f91..49d4ddb9 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 e522dc46..02eabc40 --- a/src/ports/skia_ohos/FontConfig_ohos.cpp +++ b/src/ports/skia_ohos/FontConfig_ohos.cpp @@ -1254,3 +1254,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 8279e5e5..ad36e0d9 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/FontInfo_ohos.h b/src/ports/skia_ohos/FontInfo_ohos.h index 95cbf0ed..fd0db3d0 100755 --- a/src/ports/skia_ohos/FontInfo_ohos.h +++ b/src/ports/skia_ohos/FontInfo_ohos.h @@ -129,6 +129,24 @@ public: } } + SkFontStyle computeFontStyle() + { + int weight = style.weight(); + int width = style.width(); + auto slant = style.slant(); + for (size_t i = 0; i < axisSet.axis.size(); i++) { + auto value = SkFixedToScalar(axisSet.axis[i]); + auto tag = axisSet.range[i].fTag; + if (tag == SkSetFourByteTag('w', 'g', 'h', 't')) { + weight = SkScalarFloorToInt(value); + } else if (tag == SkSetFourByteTag('w', 'd', 't', 'h')) { + width = SkScalarFloorToInt(value); + } + } + + return SkFontStyle(weight, width, slant); + } + SkString familyName; // the real family name of the font SkString fname; // the full name of font file int index; // the index of the font in a ttc font diff --git a/src/ports/skia_ohos/SkFontMgr_ohos.cpp b/src/ports/skia_ohos/SkFontMgr_ohos.cpp index 2b790eda..545835e2 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 && 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 f8e41f85..c77caea1 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 diff --git a/src/ports/skia_ohos/SkTypeface_ohos.cpp b/src/ports/skia_ohos/SkTypeface_ohos.cpp index a3165a2f..2e5d6028 100644 --- a/src/ports/skia_ohos/SkTypeface_ohos.cpp +++ b/src/ports/skia_ohos/SkTypeface_ohos.cpp @@ -114,18 +114,29 @@ void SkTypeface_OHOS::onGetFamilyName(SkString* familyName) const */ sk_sp SkTypeface_OHOS::onMakeClone(const SkFontArguments& args) const { - std::unique_ptr data = this->cloneFontData(args); - if (!data) { - return nullptr; - } + int ttcIndex = args.getCollectionIndex(); + auto stream = openStream(&ttcIndex); FontInfo info(*(fontInfo.get())); - info.index = args.getCollectionIndex(); - info.axisSet.axis.clear(); - for (unsigned int i = 0; i < data->getAxisCount(); ++i) { - info.axisSet.axis.push_back(data->getAxis()[i]); + unsigned int axisCount = args.getVariationDesignPosition().coordinateCount; + if (axisCount > 0) { + SkTypeface_FreeType::Scanner fontScanner; + SkTypeface_FreeType::Scanner::AxisDefinitions axisDefs; + if (!fontScanner.scanFont(stream.get(), ttcIndex, &info.familyName, &info.style, + &info.isFixedWidth, &axisDefs)) { + return nullptr; + } + if (axisDefs.size() > 0) { + SkFixed axis[axisDefs.size()]; + fontScanner.computeAxisValues(axisDefs, args.getVariationDesignPosition(), + axis, info.familyName); + info.setAxisSet(axisCount, axis, axisDefs.data()); + info.style = info.computeFontStyle(); + return sk_make_sp(specifiedName, info); + } } - return sk_make_sp(specifiedName, info); + + return sk_ref_sp(this); } /*! To get the font information of the typeface -- Gitee