From 0f7750cd273b7529b1567947cd42be362b0fc3f1 Mon Sep 17 00:00:00 2001 From: Job Bautista Date: Wed, 5 Apr 2023 19:33:07 +0800 Subject: [PATCH] Issue #2191 - Require implementation of gfxFont::GetScaledFont and remove unnecessary gfxPlatform::GetScaledFontForFont. Backported from Mozilla bug 1385029. --- gfx/thebes/gfxDWriteFonts.cpp | 51 +++++++++++++--------------- gfx/thebes/gfxDWriteFonts.h | 1 - gfx/thebes/gfxFT2Fonts.cpp | 22 ++++++++++-- gfx/thebes/gfxFT2Fonts.h | 3 ++ gfx/thebes/gfxFcPlatformFontList.cpp | 14 ++++++++ gfx/thebes/gfxFcPlatformFontList.h | 3 ++ gfx/thebes/gfxFont.h | 3 +- gfx/thebes/gfxGDIFont.cpp | 21 ++++++++++++ gfx/thebes/gfxGDIFont.h | 3 ++ gfx/thebes/gfxPlatform.cpp | 23 ------------- gfx/thebes/gfxPlatform.h | 6 ---- gfx/thebes/gfxPlatformGtk.cpp | 19 ----------- gfx/thebes/gfxPlatformGtk.h | 3 -- gfx/thebes/gfxPlatformMac.cpp | 7 ---- gfx/thebes/gfxPlatformMac.h | 3 -- gfx/thebes/gfxWindowsPlatform.cpp | 38 --------------------- gfx/thebes/gfxWindowsPlatform.h | 3 -- 17 files changed, 89 insertions(+), 134 deletions(-) diff --git a/gfx/thebes/gfxDWriteFonts.cpp b/gfx/thebes/gfxDWriteFonts.cpp index 96a935245c..2db9dc5aed 100644 --- a/gfx/thebes/gfxDWriteFonts.cpp +++ b/gfx/thebes/gfxDWriteFonts.cpp @@ -672,38 +672,35 @@ gfxDWriteFont::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf, already_AddRefed gfxDWriteFont::GetScaledFont(mozilla::gfx::DrawTarget *aTarget) { - bool wantCairo = aTarget->GetBackendType() == BackendType::CAIRO; - if (mAzureScaledFont && mAzureScaledFontIsCairo == wantCairo) { - RefPtr scaledFont(mAzureScaledFont); - return scaledFont.forget(); - } + if (!mAzureScaledFont) { + gfxDWriteFontEntry *fe = + static_cast(mFontEntry.get()); + bool useEmbeddedBitmap = + fe->IsCJKFont() && + HasBitmapStrikeForSize(NS_lround(mAdjustedSize)); - NativeFont nativeFont; - nativeFont.mType = NativeFontType::DWRITE_FONT_FACE; - nativeFont.mFont = GetFontFace(); - - if (wantCairo) { - mAzureScaledFont = Factory::CreateScaledFontWithCairo(nativeFont, - GetAdjustedSize(), - GetCairoScaledFont()); - } else if (aTarget->GetBackendType() == BackendType::SKIA) { - gfxDWriteFontEntry *fe = - static_cast(mFontEntry.get()); - bool useEmbeddedBitmap = (fe->IsCJKFont() && HasBitmapStrikeForSize(NS_lround(mAdjustedSize))); - - const gfxFontStyle* fontStyle = GetStyle(); - mAzureScaledFont = + const gfxFontStyle* fontStyle = GetStyle(); + mAzureScaledFont = Factory::CreateScaledFontForDWriteFont(mFontFace, fontStyle, GetAdjustedSize(), useEmbeddedBitmap, GetForceGDIClassic()); - } else { - mAzureScaledFont = Factory::CreateScaledFontForNativeFont(nativeFont, - GetAdjustedSize()); - } - mAzureScaledFontIsCairo = wantCairo; + if (!mAzureScaledFont) { + return nullptr; + } + } - RefPtr scaledFont(mAzureScaledFont); - return scaledFont.forget(); + if (aTarget->GetBackendType() == BackendType::CAIRO) { + if (!mAzureScaledFont->GetCairoScaledFont()) { + cairo_scaled_font_t* cairoScaledFont = GetCairoScaledFont(); + if (!cairoScaledFont) { + return nullptr; + } + mAzureScaledFont->SetCairoScaledFont(cairoScaledFont); + } + } + + RefPtr scaledFont(mAzureScaledFont); + return scaledFont.forget(); } diff --git a/gfx/thebes/gfxDWriteFonts.h b/gfx/thebes/gfxDWriteFonts.h index 72ac7a2b5b..964401b43a 100644 --- a/gfx/thebes/gfxDWriteFonts.h +++ b/gfx/thebes/gfxDWriteFonts.h @@ -101,7 +101,6 @@ protected: bool mNeedsBold; bool mUseSubpixelPositions; bool mAllowManualShowGlyphs; - bool mAzureScaledFontIsCairo; }; #endif diff --git a/gfx/thebes/gfxFT2Fonts.cpp b/gfx/thebes/gfxFT2Fonts.cpp index dfb94bbb69..3fcf224e21 100644 --- a/gfx/thebes/gfxFT2Fonts.cpp +++ b/gfx/thebes/gfxFT2Fonts.cpp @@ -34,6 +34,9 @@ #include "mozilla/Preferences.h" #include "mozilla/gfx/2D.h" +using namespace mozilla; +using namespace mozilla::gfx; + /** * gfxFT2Font */ @@ -174,6 +177,21 @@ gfxFT2Font::~gfxFT2Font() { } +already_AddRefed +gfxFT2Font::GetScaledFont(DrawTarget *aTarget) +{ + if (!mAzureScaledFont) { + NativeFont nativeFont; + nativeFont.mType = NativeFontType::CAIRO_FONT_FACE; + nativeFont.mFont = GetCairoScaledFont(); + mAzureScaledFont = + Factory::CreateScaledFontForNativeFont(nativeFont, GetAdjustedSize()); + } + + RefPtr scaledFont(mAzureScaledFont); + return scaledFont.forget(); +} + void gfxFT2Font::FillGlyphDataForChar(uint32_t ch, CachedGlyphData *gd) { @@ -212,7 +230,7 @@ gfxFT2Font::FillGlyphDataForChar(uint32_t ch, CachedGlyphData *gd) } void -gfxFT2Font::AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf, +gfxFT2Font::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf, FontCacheSizes* aSizes) const { gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes); @@ -221,7 +239,7 @@ gfxFT2Font::AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf, } void -gfxFT2Font::AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf, +gfxFT2Font::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf, FontCacheSizes* aSizes) const { aSizes->mFontInstances += aMallocSizeOf(this); diff --git a/gfx/thebes/gfxFT2Fonts.h b/gfx/thebes/gfxFT2Fonts.h index 8e4691c065..ff74e98dd1 100644 --- a/gfx/thebes/gfxFT2Fonts.h +++ b/gfx/thebes/gfxFT2Fonts.h @@ -27,6 +27,9 @@ public: // new functions FT2FontEntry *GetFontEntry(); + virtual already_AddRefed + GetScaledFont(DrawTarget *aTarget) override; + struct CachedGlyphData { CachedGlyphData() : glyphIndex(0xffffffffU) { } diff --git a/gfx/thebes/gfxFcPlatformFontList.cpp b/gfx/thebes/gfxFcPlatformFontList.cpp index 4f5966d948..130b8df1ed 100644 --- a/gfx/thebes/gfxFcPlatformFontList.cpp +++ b/gfx/thebes/gfxFcPlatformFontList.cpp @@ -1103,6 +1103,20 @@ gfxFontconfigFont::~gfxFontconfigFont() { } +already_AddRefed +gfxFontconfigFont::GetScaledFont(mozilla::gfx::DrawTarget *aTarget) +{ + if (!mAzureScaledFont) { + mAzureScaledFont = + Factory::CreateScaledFontForFontconfigFont(GetCairoScaledFont(), + GetPattern(), + GetAdjustedSize()); + } + + RefPtr scaledFont(mAzureScaledFont); + return scaledFont.forget(); +} + gfxFcPlatformFontList::gfxFcPlatformFontList() : mLocalNames(64) , mGenericMappings(32) diff --git a/gfx/thebes/gfxFcPlatformFontList.h b/gfx/thebes/gfxFcPlatformFontList.h index dcad65c863..969fd95024 100644 --- a/gfx/thebes/gfxFcPlatformFontList.h +++ b/gfx/thebes/gfxFcPlatformFontList.h @@ -217,6 +217,9 @@ public: virtual FontType GetType() const override { return FONT_TYPE_FONTCONFIG; } virtual FcPattern *GetPattern() const { return mPattern; } + virtual already_AddRefed + GetScaledFont(DrawTarget *aTarget) override; + private: virtual ~gfxFontconfigFont(); diff --git a/gfx/thebes/gfxFont.h b/gfx/thebes/gfxFont.h index 36687585a9..668e0a1d5d 100644 --- a/gfx/thebes/gfxFont.h +++ b/gfx/thebes/gfxFont.h @@ -1826,8 +1826,7 @@ public: virtual FontType GetType() const = 0; - virtual already_AddRefed GetScaledFont(DrawTarget* aTarget) - { return gfxPlatform::GetPlatform()->GetScaledFontForFont(aTarget, this); } + virtual already_AddRefed GetScaledFont(DrawTarget* aTarget) = 0; bool KerningDisabled() { return mKerningSet && !mKerningEnabled; diff --git a/gfx/thebes/gfxGDIFont.cpp b/gfx/thebes/gfxGDIFont.cpp index e6ceeaf6d8..8fed1871db 100644 --- a/gfx/thebes/gfxGDIFont.cpp +++ b/gfx/thebes/gfxGDIFont.cpp @@ -22,6 +22,7 @@ #define ROUND(x) floor((x) + 0.5) using namespace mozilla; +using namespace mozilla::gfx; using namespace mozilla::unicode; static inline cairo_antialias_t @@ -130,6 +131,26 @@ gfxGDIFont::SetupCairoFont(DrawTarget* aDrawTarget) return true; } +already_AddRefed +gfxGDIFont::GetScaledFont(DrawTarget *aTarget) +{ + if (!mAzureScaledFont) { + NativeFont nativeFont; + nativeFont.mType = NativeFontType::GDI_FONT_FACE; + LOGFONT lf; + GetObject(GetHFONT(), sizeof(LOGFONT), &lf); + nativeFont.mFont = &lf; + + mAzureScaledFont = + Factory::CreateScaledFontWithCairo(nativeFont, + GetAdjustedSize(), + GetCairoScaledFont()); + } + + RefPtr scaledFont(mAzureScaledFont); + return scaledFont.forget(); +} + gfxFont::RunMetrics gfxGDIFont::Measure(const gfxTextRun *aTextRun, uint32_t aStart, uint32_t aEnd, diff --git a/gfx/thebes/gfxGDIFont.h b/gfx/thebes/gfxGDIFont.h index cf2a49e226..d8d85d08da 100644 --- a/gfx/thebes/gfxGDIFont.h +++ b/gfx/thebes/gfxGDIFont.h @@ -35,6 +35,9 @@ public: virtual bool SetupCairoFont(DrawTarget* aDrawTarget) override; + virtual already_AddRefed + GetScaledFont(DrawTarget *aTarget) override; + /* override Measure to add padding for antialiasing */ virtual RunMetrics Measure(const gfxTextRun *aTextRun, uint32_t aStart, uint32_t aEnd, diff --git a/gfx/thebes/gfxPlatform.cpp b/gfx/thebes/gfxPlatform.cpp index 31dbecc957..718fec151b 100644 --- a/gfx/thebes/gfxPlatform.cpp +++ b/gfx/thebes/gfxPlatform.cpp @@ -1058,16 +1058,6 @@ gfxPlatform::GetWrappedDataSourceSurface(gfxASurface* aSurface) return result.forget(); } -already_AddRefed -gfxPlatform::GetScaledFontForFont(DrawTarget* aTarget, gfxFont *aFont) -{ - NativeFont nativeFont; - nativeFont.mType = NativeFontType::CAIRO_FONT_FACE; - nativeFont.mFont = aFont->GetCairoScaledFont(); - return Factory::CreateScaledFontForNativeFont(nativeFont, - aFont->GetAdjustedSize()); -} - void gfxPlatform::ComputeTileSize() { @@ -2172,19 +2162,6 @@ gfxPlatform::DisableBufferRotation() sBufferRotationCheckPref = false; } -already_AddRefed -gfxPlatform::GetScaledFontForFontWithCairoSkia(DrawTarget* aTarget, gfxFont* aFont) -{ - NativeFont nativeFont; - if (aTarget->GetBackendType() == BackendType::CAIRO || aTarget->GetBackendType() == BackendType::SKIA) { - nativeFont.mType = NativeFontType::CAIRO_FONT_FACE; - nativeFont.mFont = aFont->GetCairoScaledFont(); - return Factory::CreateScaledFontForNativeFont(nativeFont, aFont->GetAdjustedSize()); - } - - return nullptr; -} - /* static */ bool gfxPlatform::UsesOffMainThreadCompositing() { diff --git a/gfx/thebes/gfxPlatform.h b/gfx/thebes/gfxPlatform.h index a4d5417a58..178d261091 100644 --- a/gfx/thebes/gfxPlatform.h +++ b/gfx/thebes/gfxPlatform.h @@ -223,9 +223,6 @@ public: static already_AddRefed GetWrappedDataSourceSurface(gfxASurface *aSurface); - virtual already_AddRefed - GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont); - already_AddRefed CreateOffscreenContentDrawTarget(const mozilla::gfx::IntSize& aSize, mozilla::gfx::SurfaceFormat aFormat); @@ -747,9 +744,6 @@ protected: */ static mozilla::gfx::BackendType BackendTypeForName(const nsCString& aName); - static already_AddRefed - GetScaledFontForFontWithCairoSkia(mozilla::gfx::DrawTarget* aTarget, gfxFont* aFont); - virtual bool CanUseHardwareVideoDecoding(); int8_t mAllowDownloadableFonts; diff --git a/gfx/thebes/gfxPlatformGtk.cpp b/gfx/thebes/gfxPlatformGtk.cpp index 476d250273..604e0703dc 100644 --- a/gfx/thebes/gfxPlatformGtk.cpp +++ b/gfx/thebes/gfxPlatformGtk.cpp @@ -575,25 +575,6 @@ gfxPlatformGtk::GetGdkDrawable(cairo_surface_t *target) } #endif -already_AddRefed -gfxPlatformGtk::GetScaledFontForFont(DrawTarget* aTarget, gfxFont *aFont) -{ - switch (aTarget->GetBackendType()) { - case BackendType::CAIRO: - case BackendType::SKIA: - if (aFont->GetType() == gfxFont::FONT_TYPE_FONTCONFIG) { - gfxFontconfigFontBase* fcFont = static_cast(aFont); - return Factory::CreateScaledFontForFontconfigFont( - fcFont->GetCairoScaledFont(), - fcFont->GetPattern(), - fcFont->GetAdjustedSize()); - } - MOZ_FALLTHROUGH; - default: - return GetScaledFontForFontWithCairoSkia(aTarget, aFont); - } -} - #ifdef GL_PROVIDER_GLX class GLXVsyncSource final : public VsyncSource diff --git a/gfx/thebes/gfxPlatformGtk.h b/gfx/thebes/gfxPlatformGtk.h index 585eeab0ac..fabb29f641 100644 --- a/gfx/thebes/gfxPlatformGtk.h +++ b/gfx/thebes/gfxPlatformGtk.h @@ -35,9 +35,6 @@ public: CreateOffscreenSurface(const IntSize& aSize, gfxImageFormat aFormat) override; - virtual already_AddRefed - GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont) override; - virtual nsresult GetFontList(nsIAtom *aLangGroup, const nsACString& aGenericFamily, nsTArray& aListOfFonts) override; diff --git a/gfx/thebes/gfxPlatformMac.cpp b/gfx/thebes/gfxPlatformMac.cpp index 75c5236a87..a5ffda452b 100644 --- a/gfx/thebes/gfxPlatformMac.cpp +++ b/gfx/thebes/gfxPlatformMac.cpp @@ -125,13 +125,6 @@ gfxPlatformMac::CreateOffscreenSurface(const IntSize& aSize, return newSurface.forget(); } -already_AddRefed -gfxPlatformMac::GetScaledFontForFont(DrawTarget* aTarget, gfxFont *aFont) -{ - gfxMacFont *font = static_cast(aFont); - return font->GetScaledFont(aTarget); -} - gfxFontGroup * gfxPlatformMac::CreateFontGroup(const FontFamilyList& aFontFamilyList, const gfxFontStyle *aStyle, diff --git a/gfx/thebes/gfxPlatformMac.h b/gfx/thebes/gfxPlatformMac.h index ea4c1a1011..16230ad122 100644 --- a/gfx/thebes/gfxPlatformMac.h +++ b/gfx/thebes/gfxPlatformMac.h @@ -30,9 +30,6 @@ public: CreateOffscreenSurface(const IntSize& aSize, gfxImageFormat aFormat) override; - already_AddRefed - GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont) override; - gfxFontGroup* CreateFontGroup(const mozilla::FontFamilyList& aFontFamilyList, const gfxFontStyle *aStyle, diff --git a/gfx/thebes/gfxWindowsPlatform.cpp b/gfx/thebes/gfxWindowsPlatform.cpp index 6cfffd5b43..aa18ae8aff 100644 --- a/gfx/thebes/gfxWindowsPlatform.cpp +++ b/gfx/thebes/gfxWindowsPlatform.cpp @@ -593,44 +593,6 @@ gfxWindowsPlatform::CreateOffscreenSurface(const IntSize& aSize, return surf.forget(); } -already_AddRefed -gfxWindowsPlatform::GetScaledFontForFont(DrawTarget* aTarget, gfxFont *aFont) -{ - if (aFont->GetType() == gfxFont::FONT_TYPE_DWRITE) { - gfxDWriteFont *font = static_cast(aFont); - - NativeFont nativeFont; - nativeFont.mType = NativeFontType::DWRITE_FONT_FACE; - nativeFont.mFont = font->GetFontFace(); - - if (aTarget->GetBackendType() == BackendType::CAIRO) { - return Factory::CreateScaledFontWithCairo(nativeFont, - font->GetAdjustedSize(), - font->GetCairoScaledFont()); - } - - return Factory::CreateScaledFontForNativeFont(nativeFont, - font->GetAdjustedSize()); - } - - NS_ASSERTION(aFont->GetType() == gfxFont::FONT_TYPE_GDI, - "Fonts on windows should be GDI or DWrite!"); - - NativeFont nativeFont; - nativeFont.mType = NativeFontType::GDI_FONT_FACE; - LOGFONT lf; - GetObject(static_cast(aFont)->GetHFONT(), sizeof(LOGFONT), &lf); - nativeFont.mFont = &lf; - - if (aTarget->GetBackendType() == BackendType::CAIRO) { - return Factory::CreateScaledFontWithCairo(nativeFont, - aFont->GetAdjustedSize(), - aFont->GetCairoScaledFont()); - } - - return Factory::CreateScaledFontForNativeFont(nativeFont, aFont->GetAdjustedSize()); -} - static const char kFontAparajita[] = "Aparajita"; static const char kFontArabicTypesetting[] = "Arabic Typesetting"; static const char kFontArial[] = "Arial"; diff --git a/gfx/thebes/gfxWindowsPlatform.h b/gfx/thebes/gfxWindowsPlatform.h index 9c7a335795..4735e9cc8e 100644 --- a/gfx/thebes/gfxWindowsPlatform.h +++ b/gfx/thebes/gfxWindowsPlatform.h @@ -121,9 +121,6 @@ public: CreateOffscreenSurface(const IntSize& aSize, gfxImageFormat aFormat) override; - virtual already_AddRefed - GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont) override; - enum RenderMode { /* Use GDI and windows surfaces */ RENDER_GDI = 0,