first version of gfxQtFont::GetMetrics
authorpekka.aho
Sat, 19 Apr 2008 18:37:18 +0300
changeset 16800 b06d549c0105fd2ce055840f70aea7de74232296
parent 16799 f8ed893486d261d45d900bdfe599ed9a7070d167
child 16801 e1c47edfd4f5f6dc4df9049c1ef0dc88ac0a0a72
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::GetMetrics
gfx/thebes/public/gfxQtFonts.h
gfx/thebes/src/gfxQtFonts.cpp
--- a/gfx/thebes/public/gfxQtFonts.h
+++ b/gfx/thebes/public/gfxQtFonts.h
@@ -55,39 +55,38 @@ public:
      gfxQtFont (const nsAString& aName,
                 const gfxFontStyle *aFontStyle);
      virtual ~gfxQtFont ();
 
      virtual nsString GetUniqueName ();
 
      virtual PRUint32 GetSpaceGlyph ()
      {
+
         NS_ASSERTION (GetStyle ()->size != 0,
         "forgot to short-circuit a text run with zero-sized font?");
         GetMetrics ();
        return mSpaceGlyph;
      }
 
-    static void Shutdown();
-
     virtual const gfxFont::Metrics& GetMetrics();
 
     void* GetQFont() { if (!mQFont) RealizeQFont(); return mQFont; }
 
 protected:
     void *mQFont;
     cairo_scaled_font_t *mCairoFont;
 
     PRBool mHasMetrics;
     PRUint32 mSpaceGlyph;
     Metrics mMetrics;
     gfxFloat mAdjustedSize;
 
     virtual PRBool SetupCairoFont(gfxContext *aContext);
-    void RealizeQFont();
+	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
@@ -43,18 +43,19 @@
  * ***** END LICENSE BLOCK ***** */
 
 #include "gfxPlatformQt.h"
 #include "gfxTypes.h"
 #include "gfxQtFonts.h"
 #include "qdebug.h"
 #include "qrect.h"
 #include <locale.h>
+#include <cairo.h>
+#include <QFontMetrics>
 
-#include <cairo.h>
 
 /**
  * gfxQtFontGroup
  */
 
 static int
 FFRECountHyphens (const nsAString &aFFREName)
 {
@@ -106,17 +107,21 @@ GetOrMakeFont(const nsAString& aName, co
 }
 
 void
 gfxQtFont::RealizeQFont()
 {
     // already realized?
     if (mQFont)
         return;
-    qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
+
+//	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()) {
@@ -469,24 +474,64 @@ gfxQtFont::gfxQtFont(const nsAString &aN
 
 gfxQtFont::~gfxQtFont()
 {
 }
 
 const gfxFont::Metrics&
 gfxQtFont::GetMetrics()
 {
-    if (mHasMetrics)
+     if (mHasMetrics)
         return mMetrics;
 
-    qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d, font name: %s %f %f\n", __PRETTY_FUNCTION__, __LINE__, NS_ConvertUTF16toUTF8(mName).get(), GetStyle()->size, mAdjustedSize);
+    QFont font( QString( NS_ConvertUTF16toUTF8(mName).get() ), 
+                (int) GetStyle()->size, 
+                (int) GetStyle()->weight,
+                bool( GetStyle()->style == FONT_STYLE_ITALIC ) );
+
+    QFontMetrics fontMetrics( font );
+
+    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();
+    mMetrics.maxAdvance = fontMetrics.maxWidth();
+
+    mMetrics.emHeight = mAdjustedSize ? mAdjustedSize : GetStyle()->size;
 
+    gfxFloat lineHeight = mMetrics.maxAscent + mMetrics.maxDescent;
+    if (lineHeight > mMetrics.emHeight)
+    {
+        mMetrics.externalLeading = lineHeight - mMetrics.emHeight;
+    }
+    else
+    {
+        mMetrics.externalLeading = 0;
+    }
+    mMetrics.internalLeading = 0;
+
+    mMetrics.maxHeight = lineHeight;
+
+    mMetrics.emAscent = lineHeight > 0.0 ?
+        mMetrics.maxAscent * mMetrics.emHeight / lineHeight : 0.0;
+    mMetrics.emDescent = mMetrics.emHeight - mMetrics.emAscent;
+
+    mMetrics.spaceWidth = fontMetrics.width( QChar(' ') );
+    mMetrics.xHeight = fontMetrics.xHeight();
+
+
+	mMetrics.superscriptOffset = mMetrics.xHeight;
+	mMetrics.subscriptOffset = mMetrics.xHeight;
+
+
+  
 #if 0
-    //    printf("font name: %s %f %f\n", NS_ConvertUTF16toUTF8(mName).get(), GetStyle()->size, mAdjustedSize);
-    //    printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font)));
 
     fprintf (stderr, "Font: %s\n", NS_ConvertUTF16toUTF8(mName).get());
     fprintf (stderr, "    emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent);
     fprintf (stderr, "    maxAscent: %f maxDescent: %f\n", mMetrics.maxAscent, mMetrics.maxDescent);
     fprintf (stderr, "    internalLeading: %f externalLeading: %f\n", mMetrics.externalLeading, mMetrics.internalLeading);
     fprintf (stderr, "    spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight);
     fprintf (stderr, "    uOff: %f uSize: %f stOff: %f stSize: %f suOff: %f suSize: %f\n", mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize, mMetrics.superscriptOffset, mMetrics.subscriptOffset);
 #endif
@@ -508,25 +553,27 @@ gfxQtFont::GetUniqueName()
     char *str = pango_font_description_to_string(desc);
     pango_font_description_free (desc);
 
     CopyUTF8toUTF16(str, result);
     g_free(str);*/
     return result;
 }
 
-/* static */ void
-gfxQtFont::Shutdown()
+/* static  void
+/*gfxQtFont::Shutdown()
 {
     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" );
+
     // 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;
@@ -550,33 +597,35 @@ CreateScaledFont(cairo_t *aCR, cairo_mat
     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;
         cairo_scaled_font_get_ctm(mCairoFont, &fontCTM);
         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__);
+//        qDebug("QTFONT NOT_IMPLEMENTED!!!! Func:%s::%d\n", __PRETTY_FUNCTION__, __LINE__);
         mCairoFont = CreateScaledFont(cr, &currentCTM, GetQFont());
         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;
     }