First version of gfxQtFont
authorpekka.aho
Sat, 19 Apr 2008 18:37:38 +0300
changeset 16804 6401c60a9c04bd9ca3b16c088a42c4f41c533691
parent 16803 b4b41b917643a616f63618e2719dd6ecc8c157ab
child 16805 4a15a0a88898751d919ecfd9892207004b74fb51
push id1298
push userpavlov@mozilla.com
push dateSun, 17 Aug 2008 05:03:09 +0000
treeherderautoland@4a506fa751d8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone2.0a1pre
First version of gfxQtFont
gfx/thebes/public/gfxQtFonts.h
gfx/thebes/src/gfxQtFonts.cpp
--- a/gfx/thebes/public/gfxQtFonts.h
+++ b/gfx/thebes/public/gfxQtFonts.h
@@ -51,46 +51,40 @@
 #define ENABLE_FAST_PATH_ALWAYS 1
 
 class QFont;
 
 class gfxQtFont : public gfxFont {
 public:
      gfxQtFont (const nsAString& aName,
                 const gfxFontStyle *aFontStyle);
-     gfxQtFont(QFont *aQFont, const nsAString &aName,
-               const gfxFontStyle *aFontStyle);
      virtual ~gfxQtFont ();
 
-     virtual nsString GetUniqueName ();
-
-     virtual PRUint32 GetSpaceGlyph ()
-     {
+protected: // from gfxFont
+    virtual nsString GetUniqueName ();
+    virtual PRUint32 GetSpaceGlyph ();
+    virtual const gfxFont::Metrics& GetMetrics();
+    virtual PRBool SetupCairoFont(gfxContext *aContext);
 
-        NS_ASSERTION (GetStyle ()->size != 0,
-        "forgot to short-circuit a text run with zero-sized font?");
-        GetMetrics ();
-       return mSpaceGlyph;
-     }
+protected: // new functions
+    cairo_scaled_font_t* CreateScaledFont(cairo_t *aCR, 
+                                          cairo_matrix_t *aCTM, 
+                                          QFont &aQFont);
 
-    virtual const gfxFont::Metrics& GetMetrics();
+protected: // data
 
-    void* GetQFont() { if (!mQFont) RealizeQFont(); return mQFont; }
-
-protected:
-    void *mQFont;
+    QFont* mQFont;
     cairo_scaled_font_t *mCairoFont;
 
+    PRBool mHasSpaceGlyph;
+    PRUint32 mSpaceGlyph;
     PRBool mHasMetrics;
-    PRUint32 mSpaceGlyph;
     Metrics mMetrics;
     gfxFloat mAdjustedSize;
 
-    virtual PRBool SetupCairoFont(gfxContext *aContext);
-	void RealizeQFont();
 };
 
 class THEBES_API gfxQtFontGroup : public gfxFontGroup {
 public:
     gfxQtFontGroup (const nsAString& families,
                     const gfxFontStyle *aStyle);
     virtual ~gfxQtFontGroup ();
 
--- a/gfx/thebes/src/gfxQtFonts.cpp
+++ b/gfx/thebes/src/gfxQtFonts.cpp
@@ -42,21 +42,20 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "gfxPlatformQt.h"
 #include "gfxTypes.h"
 #include "gfxQtFonts.h"
 #include "qdebug.h"
 #include "qrect.h"
+#include <locale.h>
 #include <QFont>
-#include <locale.h>
-#include <cairo.h>
 #include <QFontMetrics>
-
+#include "cairo-ft.h"
 
 /**
  * gfxQtFontGroup
  */
 
 static int
 FFRECountHyphens (const nsAString &aFFREName)
 {
@@ -102,70 +101,16 @@ GetOrMakeFont(const nsAString& aName, co
             return nsnull;
         gfxFontCache::GetCache()->AddNew(font);
     }
     gfxFont *f = nsnull;
     font.swap(f);
     return static_cast<gfxQtFont *>(f);
 }
 
-void
-gfxQtFont::RealizeQFont()
-{
-    // already realized?
-    if (mQFont)
-        return;
-
-//	mQFont = new QFont( "times" );
-	printf( " gfxQtFont::RealizeQFont mQFont %p \n", mQFont );
-
-//    qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
-
-/*
-    PangoFontDescription *pangoFontDesc =
-        NewPangoFontDescription(mName, GetStyle());
-
-    PangoContext *pangoCtx = gdk_pango_context_get();
-
-    if (!GetStyle()->langGroup.IsEmpty()) {
-        PangoLanguage *lang = GetPangoLanguage(GetStyle()->langGroup);
-        if (lang)
-            pango_context_set_language(pangoCtx, lang);
-    }
-
-    mQFont = LoadPangoFont(pangoCtx, pangoFontDesc);
-
-    gfxFloat size = GetStyle()->size;
-    // Checking mQFont to avoid infinite recursion through GetCharSize
-    if (size != 0.0 && GetStyle()->sizeAdjust != 0.0 && mQFont) {
-        // Could try xHeight from TrueType/OpenType fonts.
-        gfxSize isz, lsz;
-        GetCharSize('x', isz, lsz);
-        if (isz.height != 0.0) {
-            gfxFloat aspect = isz.height / size;
-            size = GetStyle()->GetAdjustedSize(aspect);
-
-            pango_font_description_set_absolute_size(pangoFontDesc,
-                                                     size * PANGO_SCALE);
-            g_object_unref(mQFont);
-            mQFont = LoadPangoFont(pangoCtx, pangoFontDesc);
-        }
-    }
-
-    NS_ASSERTION(mHasMetrics == PR_FALSE, "metrics will be invalid...");
-    mAdjustedSize = size;
-    if (!g_object_get_qdata(G_OBJECT(mQFont), GetFontQuark()))
-        g_object_set_qdata(G_OBJECT(mQFont), GetFontQuark(), this);
-
-    if (pangoFontDesc)
-        pango_font_description_free(pangoFontDesc);
-    if (pangoCtx)
-        g_object_unref(pangoCtx);
-*/
-}
 
 gfxQtFontGroup::gfxQtFontGroup (const nsAString& families,
                                 const gfxFontStyle *aStyle)
     : gfxFontGroup(families, aStyle)
 {
     nsStringArray familyArray;
 
     // Leave non-existing fonts in the list so that fontconfig can get the
@@ -463,47 +408,45 @@ gfxQtFontGroup::MakeTextRun(const PRUnic
 
 /**
  * gfxQtFont
  */
 
 gfxQtFont::gfxQtFont(const nsAString &aName,
                      const gfxFontStyle *aFontStyle)
     : gfxFont(aName, aFontStyle),
-      mQFont(nsnull), mCairoFont(nsnull),
+      mCairoFont(nsnull),
       mHasMetrics(PR_FALSE), mAdjustedSize(0)
 {
-}
-
-gfxQtFont::gfxQtFont(QFont *aQFont, const nsAString &aName,
-                     const gfxFontStyle *aFontStyle)
-        : gfxFont(aName, aFontStyle),
-        mQFont(aQFont), mCairoFont(nsnull),
-        mHasMetrics(PR_FALSE), mAdjustedSize(aFontStyle->size)
-{
-//    g_object_ref(mPangoFont);
-//    g_object_set_qdata(G_OBJECT(mPangoFont), GetFontQuark(), this);
+    mQFont = new QFont();
+    mQFont->setFamily(QString( NS_ConvertUTF16toUTF8(mName).get() ) );
+    mQFont->setPixelSize((int) GetStyle()->size);
+    int weight = GetStyle()->weight/10;
+    if( weight > 99 )
+    {
+        // Max Weight for QFont is 99
+        weight = 99;
+    }
+    mQFont->setWeight(weight);
+    mQFont->setItalic(bool( GetStyle()->style == FONT_STYLE_ITALIC ));
 }
 
 gfxQtFont::~gfxQtFont()
 {
+    delete mQFont;
+    cairo_scaled_font_destroy(mCairoFont);
 }
 
 const gfxFont::Metrics&
 gfxQtFont::GetMetrics()
 {
      if (mHasMetrics)
         return mMetrics;
 
-    QFont font( QString( NS_ConvertUTF16toUTF8(mName).get() ), 
-                (int) GetStyle()->size, 
-                (int) GetStyle()->weight,
-                bool( GetStyle()->style == FONT_STYLE_ITALIC ) );
-
-    QFontMetrics fontMetrics( font );
+    QFontMetrics fontMetrics( *mQFont );
 
     mMetrics.maxAscent = fontMetrics.ascent();
     mMetrics.maxDescent = fontMetrics.descent();
     mMetrics.aveCharWidth = fontMetrics.averageCharWidth();
     mMetrics.underlineOffset = fontMetrics.underlinePos();
     mMetrics.underlineSize = fontMetrics.lineWidth();
     mMetrics.strikeoutOffset = fontMetrics.strikeOutPos(); 
     mMetrics.strikeoutSize = fontMetrics.lineWidth();
@@ -550,75 +493,64 @@ gfxQtFont::GetMetrics()
     mHasMetrics = PR_TRUE;
     return mMetrics;
 }
 
 
 nsString
 gfxQtFont::GetUniqueName()
 {
-    qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
-    nsString result;
-/*
-    PangoFont *font = GetPangoFont();
-    PangoFontDescription *desc = pango_font_describe(font);
-    pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
-    char *str = pango_font_description_to_string(desc);
-    pango_font_description_free (desc);
+    return mName;
+}
+
+PRUint32 gfxQtFont::GetSpaceGlyph ()
+{
+    NS_ASSERTION (GetStyle ()->size != 0,
+    "forgot to short-circuit a text run with zero-sized font?");
 
-    CopyUTF8toUTF16(str, result);
-    g_free(str);*/
-    return result;
+    if(!mHasSpaceGlyph)
+    {
+        FT_UInt gid = 0; // glyph ID
+        FT_Face face = mQFont->freetypeFace();
+        gid = FT_Get_Char_Index(face, ' ');
+        mSpaceGlyph = gid;
+        mHasSpaceGlyph = PR_TRUE;
+    }
+    return mSpaceGlyph;
 }
 
-/* static  void
-/*gfxQtFont::Shutdown()
+
+cairo_scaled_font_t*
+gfxQtFont::CreateScaledFont(cairo_t *aCR, cairo_matrix_t *aCTM, QFont &aQFont)
 {
-    qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
-}
-*/
-static cairo_scaled_font_t*
-CreateScaledFont(cairo_t *aCR, cairo_matrix_t *aCTM, void *aQFont)
-{
-printf( "CreateScaledFont\n" );
+    FT_Face ftFace = aQFont.freetypeFace();
+
+    double size = mAdjustedSize ? mAdjustedSize : GetStyle()->size;
+    cairo_matrix_t fontMatrix;
+    cairo_matrix_init_scale(&fontMatrix, size, size);
+    cairo_font_options_t *fontOptions = cairo_font_options_create();
 
-    // XXX is this safe really? We should probably check the font type or something.
-    // XXX does this really create the same font that Pango used for measurement?
-    // We probably need to work harder here. We should pay particular attention
-    // to the font options.
-/*
-    PangoFcFont *fcfont = PANGO_FC_FONT(aPangoFont);
-    cairo_font_face_t *face = cairo_ft_font_face_create_for_pattern(fcfont->font_pattern);
-    double size;
-    if (FcPatternGetDouble(fcfont->font_pattern, FC_PIXEL_SIZE, 0, &size) != FcResultMatch)
-        size = 12.0;
-    cairo_matrix_t fontMatrix;
-    FcMatrix *fcMatrix;
-    if (FcPatternGetMatrix(fcfont->font_pattern, FC_MATRIX, 0, &fcMatrix) == FcResultMatch)
-        cairo_matrix_init(&fontMatrix, fcMatrix->xx, -fcMatrix->yx, -fcMatrix->xy, fcMatrix->yy, 0, 0);
-    else
-        cairo_matrix_init_identity(&fontMatrix);
-    cairo_matrix_scale(&fontMatrix, size, size);
-    cairo_font_options_t *fontOptions = cairo_font_options_create();
-    cairo_get_font_options(aCR, fontOptions);
-    cairo_scaled_font_t *scaledFont =
-        cairo_scaled_font_create(face, &fontMatrix, aCTM, fontOptions);
+    cairo_font_face_t *cairoFontFace = 
+                cairo_ft_font_face_create_for_ft_face( ftFace, 0 );
+
+    cairo_scaled_font_t* scaledFont = 
+                cairo_scaled_font_create( cairoFontFace, 
+                                          &fontMatrix,
+                                          aCTM,
+                                          fontOptions);
+
     cairo_font_options_destroy(fontOptions);
-    cairo_font_face_destroy(face);
-    NS_ASSERTION(cairo_scaled_font_status(scaledFont) == CAIRO_STATUS_SUCCESS,
-                 "Failed to create scaled font");
+    cairo_font_face_destroy(cairoFontFace);
+
     return scaledFont;
-*/
-    return nsnull;
 }
 
 PRBool
 gfxQtFont::SetupCairoFont(gfxContext *aContext)
 {
-printf("gfxQtFont::SetupCairoFont\n");
 
     cairo_t *cr = aContext->GetCairo();
     cairo_matrix_t currentCTM;
     cairo_get_matrix(cr, &currentCTM);
 
     if (mCairoFont) {
         // Need to validate that its CTM is OK
         cairo_matrix_t fontCTM;
@@ -626,18 +558,17 @@ printf("gfxQtFont::SetupCairoFont\n");
         if (fontCTM.xx != currentCTM.xx || fontCTM.yy != currentCTM.yy ||
             fontCTM.xy != currentCTM.xy || fontCTM.yx != currentCTM.yx) {
             // Just recreate it from scratch, simplest way
             cairo_scaled_font_destroy(mCairoFont);
             mCairoFont = nsnull;
         }
     }
     if (!mCairoFont) {
-//        qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
-        mCairoFont = CreateScaledFont(cr, &currentCTM, GetQFont());
+        mCairoFont = CreateScaledFont(cr, &currentCTM, *mQFont);
         return PR_FALSE;
     }
     if (cairo_scaled_font_status(mCairoFont) != CAIRO_STATUS_SUCCESS) {
         // Don't cairo_set_scaled_font as that would propagate the error to
         // the cairo_t, precluding any further drawing.
         return PR_FALSE;
     }
     cairo_set_scaled_font(cr, mCairoFont);