From 8a5e880418f510d35d34fadef006be10031d859b Mon Sep 17 00:00:00 2001 From: roytam1 Date: Fri, 7 Apr 2023 10:39:31 +0800 Subject: [PATCH] ported from UXP: Issue #2191 - Require implementation of gfxFont::GetScaledFont and remove unnecessary gfxPlatform::GetScaledFontForFont. (0f7750cd) --- 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 ea8e879fb..d695533ca 100644 --- a/gfx/thebes/gfxDWriteFonts.cpp +++ b/gfx/thebes/gfxDWriteFonts.cpp @@ -682,38 +682,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 cc39f6c20..b7f5be657 100644 --- a/gfx/thebes/gfxDWriteFonts.h +++ b/gfx/thebes/gfxDWriteFonts.h @@ -103,7 +103,6 @@ protected: bool mNeedsBold; bool mUseSubpixelPositions; bool mAllowManualShowGlyphs; - bool mAzureScaledFontIsCairo; static bool mUseClearType; }; diff --git a/gfx/thebes/gfxFT2Fonts.cpp b/gfx/thebes/gfxFT2Fonts.cpp index dfb94bbb6..3fcf224e2 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 8e4691c06..ff74e98dd 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 f710209c2..d111b122f 100644 --- a/gfx/thebes/gfxFcPlatformFontList.cpp +++ b/gfx/thebes/gfxFcPlatformFontList.cpp @@ -1105,6 +1105,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 dcad65c86..969fd9502 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 6f90bae4e..55c645f4c 100644 --- a/gfx/thebes/gfxFont.h +++ b/gfx/thebes/gfxFont.h @@ -1842,8 +1842,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 0db13f422..5999554f6 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 25bafcdcb..923ea1e98 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 1cafae7f5..61503c087 100644 --- a/gfx/thebes/gfxPlatform.cpp +++ b/gfx/thebes/gfxPlatform.cpp @@ -1152,16 +1152,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() { @@ -2285,19 +2275,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 c021f3884..08bc0752f 100644 --- a/gfx/thebes/gfxPlatform.h +++ b/gfx/thebes/gfxPlatform.h @@ -229,9 +229,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); @@ -765,9 +762,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 4d1133fea..2c4e9d449 100644 --- a/gfx/thebes/gfxPlatformGtk.cpp +++ b/gfx/thebes/gfxPlatformGtk.cpp @@ -585,25 +585,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 ab7055549..8eb8c8dc6 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 36ba3ec57..797ae7495 100644 --- a/gfx/thebes/gfxPlatformMac.cpp +++ b/gfx/thebes/gfxPlatformMac.cpp @@ -135,13 +135,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 6f69f7159..f356948bb 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 0af4e54dd..e80246712 100644 --- a/gfx/thebes/gfxWindowsPlatform.cpp +++ b/gfx/thebes/gfxWindowsPlatform.cpp @@ -602,44 +602,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 81d89baf9..a611e9ea8 100644 --- a/gfx/thebes/gfxWindowsPlatform.h +++ b/gfx/thebes/gfxWindowsPlatform.h @@ -122,9 +122,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,