From ddc80400c7ec62b69b9180d79cadc271a64639d5 Mon Sep 17 00:00:00 2001 From: Karl Tomlinson Date: Thu, 18 Sep 2008 13:44:14 +1200 Subject: [PATCH] b=454951 lazy creation of gfxPangoFont for gfxPangoFontGroup r=roc --- gfx/thebes/public/gfxPangoFonts.h | 8 +-- gfx/thebes/src/gfxPangoFonts.cpp | 87 +++++++++++++++++-------------- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/gfx/thebes/public/gfxPangoFonts.h b/gfx/thebes/public/gfxPangoFonts.h index 782e5bc4f3da..448ccb7cb520 100644 --- a/gfx/thebes/public/gfxPangoFonts.h +++ b/gfx/thebes/public/gfxPangoFonts.h @@ -128,9 +128,7 @@ public: virtual gfxTextRun *MakeTextRun(const PRUint8 *aString, PRUint32 aLength, const Parameters *aParams, PRUint32 aFlags); - gfxPangoFont *GetFontAt(PRInt32 i) { - return static_cast(static_cast(mFonts[i])); - } + virtual gfxPangoFont *GetFontAt(PRInt32 i); protected: // ****** Textrun glyph conversion helpers ****** @@ -163,9 +161,7 @@ protected: const gchar *aUTF8, PRUint32 aUTF8Length); #endif - static PRBool FontCallback (const nsAString& fontName, - const nsACString& genericName, - void *closure); + void GetFcFamilies(nsAString &aFcFamilies); }; class gfxPangoFontWrapper { diff --git a/gfx/thebes/src/gfxPangoFonts.cpp b/gfx/thebes/src/gfxPangoFonts.cpp index 13a20cbaeef8..89de87720cff 100644 --- a/gfx/thebes/src/gfxPangoFonts.cpp +++ b/gfx/thebes/src/gfxPangoFonts.cpp @@ -460,10 +460,9 @@ FFRECountHyphens (const nsAString &aFFREName) return h; } -PRBool -gfxPangoFontGroup::FontCallback (const nsAString& fontName, - const nsACString& genericName, - void *closure) +static PRBool +FontCallback (const nsAString& fontName, const nsACString& genericName, + void *closure) { nsStringArray *sa = static_cast(closure); @@ -503,40 +502,7 @@ gfxPangoFontGroup::gfxPangoFontGroup (const nsAString& families, const gfxFontStyle *aStyle) : gfxFontGroup(families, aStyle) { - g_type_init(); - - nsStringArray familyArray; - - // Leave non-existing fonts in the list so that fontconfig can get the - // best match. - ForEachFontInternal(families, aStyle->langGroup, PR_TRUE, PR_FALSE, - FontCallback, &familyArray); - - // Construct a string suitable for fontconfig - nsAutoString fcFamilies; - if (familyArray.Count()) { - int i = 0; - while (1) { - fcFamilies.Append(*familyArray[i]); - ++i; - if (i >= familyArray.Count()) - break; - fcFamilies.Append(NS_LITERAL_STRING(",")); - } - } - else { - // XXX If there are no fonts, we should use dummy family. - // Pango will resolve from this. - // behdad: yep, looks good. - // printf("%s(%s)\n", NS_ConvertUTF16toUTF8(families).get(), - // aStyle->langGroup.get()); - fcFamilies.Append(NS_LITERAL_STRING("sans-serif")); - } - - nsRefPtr font = GetOrMakeFont(fcFamilies, &mStyle); - if (font) { - mFonts.AppendElement(font); - } + mFonts.AppendElements(1); } gfxPangoFontGroup::~gfxPangoFontGroup() @@ -549,6 +515,51 @@ gfxPangoFontGroup::Copy(const gfxFontStyle *aStyle) return new gfxPangoFontGroup(mFamilies, aStyle); } +// A string of family names suitable for fontconfig +void +gfxPangoFontGroup::GetFcFamilies(nsAString& aFcFamilies) +{ + nsStringArray familyArray; + + // Leave non-existing fonts in the list so that fontconfig can get the + // best match. + ForEachFontInternal(mFamilies, mStyle.langGroup, PR_TRUE, PR_FALSE, + FontCallback, &familyArray); + + if (familyArray.Count()) { + int i = 0; + while (1) { + aFcFamilies.Append(*familyArray[i]); + ++i; + if (i >= familyArray.Count()) + break; + aFcFamilies.Append(NS_LITERAL_STRING(",")); + } + } + else { + // XXX If there are no fonts, we should use dummy family. + // Pango will resolve from this. + // behdad: yep, looks good. + // printf("%s(%s)\n", NS_ConvertUTF16toUTF8(families).get(), + // aStyle->langGroup.get()); + aFcFamilies.Append(NS_LITERAL_STRING("sans-serif")); + } +} + +gfxPangoFont * +gfxPangoFontGroup::GetFontAt(PRInt32 i) { + NS_PRECONDITION(i == 0, "Only have one font"); + + if (!mFonts[0]) { + nsAutoString fcFamilies; + GetFcFamilies(fcFamilies); + nsRefPtr font = GetOrMakeFont(fcFamilies, &mStyle); + mFonts[0] = font; + } + + return static_cast(mFonts[i].get()); +} + /** ** gfxPangoFont **/