Bug 752380. Refactor gfxFont out of Azure. r=Bas
authorNicholas Cameron <ncameron@mozilla.com>
Thu, 17 May 2012 10:30:10 +1200
changeset 94654 303684497400762258daf1b39309e55f7f6b63a0
parent 94653 021554387271c34a3859e397b4c7d714d0cbb79a
child 94655 6c7cc13ba63a0f51178926119e9e24ea9db0f198
push id22732
push useremorley@mozilla.com
push dateWed, 23 May 2012 09:43:13 +0000
treeherdermozilla-central@aa2b52bd0374 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBas
bugs752380
milestone15.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 752380. Refactor gfxFont out of Azure. r=Bas
dom/ipc/Makefile.in
gfx/2d/2D.h
gfx/2d/Factory.cpp
gfx/2d/Makefile.in
gfx/2d/ScaledFontFreetype.cpp
gfx/2d/ScaledFontFreetype.h
gfx/2d/Types.h
gfx/thebes/Makefile.in
gfx/thebes/gfxAndroidPlatform.cpp
gfx/thebes/gfxFT2FontBase.cpp
gfx/thebes/gfxFT2FontBase.h
gfx/thebes/gfxFont.h
gfx/thebes/gfxPlatformGtk.cpp
--- a/dom/ipc/Makefile.in
+++ b/dom/ipc/Makefile.in
@@ -75,13 +75,17 @@ LOCAL_INCLUDES += \
 	-I$(topsrcdir)/dom/base \
 	-I$(topsrcdir)/toolkit/xre \
 	-I$(topsrcdir)/hal/sandbox \
 	-I$(topsrcdir)/dom/sms/src/ipc \
 	$(NULL)
 
 DEFINES += -DBIN_SUFFIX='"$(BIN_SUFFIX)"'
 
+ifeq ($(MOZ_WIDGET_TOOLKIT),$(findstring $(MOZ_WIDGET_TOOLKIT),android gtk2 gonk qt))
+DEFINES += -DMOZ_ENABLE_FREETYPE
+endif
+
 ifdef MOZ_PERMISSIONS
 DEFINES += -DMOZ_PERMISSIONS
 endif
 
 CXXFLAGS += $(TK_CFLAGS)
--- a/gfx/2d/2D.h
+++ b/gfx/2d/2D.h
@@ -10,16 +10,20 @@
 #include "Rect.h"
 #include "Matrix.h"
 #include "UserData.h"
 // This RefPtr class isn't ideal for usage in Azure, as it doesn't allow T**
 // outparams using the &-operator. But it will have to do as there's no easy
 // solution.
 #include "mozilla/RefPtr.h"
 
+#ifdef MOZ_ENABLE_FREETYPE
+#include <string>
+#endif
+
 struct _cairo_surface;
 typedef _cairo_surface cairo_surface_t;
 
 struct _cairo_scaled_font;
 typedef _cairo_scaled_font cairo_scaled_font_t;
 
 struct ID3D10Device1;
 struct ID3D10Texture2D;
@@ -459,16 +463,30 @@ public:
    * others.
    */
   virtual void CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder) = 0;
 
 protected:
   ScaledFont() {}
 };
 
+#ifdef MOZ_ENABLE_FREETYPE
+/**
+ * Describes a font
+ * Used to pass the key informatin from a gfxFont into Azure
+ * XXX Should be replaced by a more long term solution, perhaps Bug 738014
+ */
+struct FontOptions
+{
+  std::string mName;
+  FontStyle mStyle;
+};
+#endif
+
+
 /* This class is designed to allow passing additional glyph rendering
  * parameters to the glyph drawing functions. This is an empty wrapper class
  * merely used to allow holding on to and passing around platform specific
  * parameters. This is because different platforms have unique rendering
  * parameters.
  */
 class GlyphRenderingOptions : public RefCounted<GlyphRenderingOptions>
 {
--- a/gfx/2d/Factory.cpp
+++ b/gfx/2d/Factory.cpp
@@ -8,18 +8,20 @@
 #ifdef USE_CAIRO
 #include "DrawTargetCairo.h"
 #include "ScaledFontBase.h"
 #endif
 
 #ifdef USE_SKIA
 #include "DrawTargetSkia.h"
 #include "ScaledFontBase.h"
+#ifdef MOZ_ENABLE_FREETYPE
 #include "ScaledFontFreetype.h"
 #endif
+#endif
 
 #if defined(WIN32) && defined(USE_SKIA)
 #include "ScaledFontWin.h"
 #endif
 
 #ifdef XP_MACOSX
 #include "ScaledFontMac.h"
 #endif
@@ -246,21 +248,23 @@ Factory::CreateScaledFontForNativeFont(c
 #endif
 #ifdef USE_SKIA
 #ifdef WIN32
   case NATIVE_FONT_GDI_FONT_FACE:
     {
       return new ScaledFontWin(static_cast<LOGFONT*>(aNativeFont.mFont), aSize);
     }
 #endif
+#ifdef MOZ_ENABLE_FREETYPE
   case NATIVE_FONT_SKIA_FONT_FACE:
     {
-      return new ScaledFontFreetype(static_cast<gfxFont*>(aNativeFont.mFont), aSize);
+      return new ScaledFontFreetype(static_cast<FontOptions*>(aNativeFont.mFont), aSize);
     }
 #endif
+#endif
 #ifdef USE_CAIRO
   case NATIVE_FONT_CAIRO_FONT_FACE:
     {
       return new ScaledFontBase(aSize);
     }
 #endif
   default:
     gfxWarning() << "Invalid native font type specified.";
--- a/gfx/2d/Makefile.in
+++ b/gfx/2d/Makefile.in
@@ -56,32 +56,38 @@ endif
 
 DEFINES += -DMOZ_GFX -DUSE_CAIRO -DGFX2D_INTERNAL
 
 ifdef MOZ_ENABLE_SKIA
 CPPSRCS	+= \
         SourceSurfaceSkia.cpp \
         DrawTargetSkia.cpp \
         PathSkia.cpp \
-        ScaledFontFreetype.cpp
         $(NULL)
 
 DEFINES += -DUSE_SKIA
 
 endif
 
 ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
 ifdef MOZ_ENABLE_SKIA
 CPPSRCS += \
         ScaledFontMac.cpp \
         $(NULL)
 
 endif
 endif
 
+ifeq ($(MOZ_WIDGET_TOOLKIT),$(findstring $(MOZ_WIDGET_TOOLKIT),android gtk2 gonk qt))
+CPPSRCS	+= \
+        ScaledFontFreetype.cpp \
+        $(NULL)
+DEFINES += -DMOZ_ENABLE_FREETYPE
+endif
+
 ifeq (android,$(MOZ_WIDGET_TOOLKIT))
 DEFINES += -DSK_BUILD_FOR_ANDROID_NDK
 endif
 
 DEFINES += -DSK_A32_SHIFT=24 -DSK_R32_SHIFT=16 -DSK_G32_SHIFT=8 -DSK_B32_SHIFT=0
 
 ifdef MOZ_DEBUG
 DEFINES += -DGFX_LOG_DEBUG -DGFX_LOG_WARNING
--- a/gfx/2d/ScaledFontFreetype.cpp
+++ b/gfx/2d/ScaledFontFreetype.cpp
@@ -1,48 +1,53 @@
 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "ScaledFontFreetype.h"
+#include "Logging.h"
 
 #include "gfxFont.h"
 
 #ifdef USE_SKIA
 #include "skia/SkTypeface.h"
 #endif
 
+#include <string>
+
 using namespace std;
 
 namespace mozilla {
 namespace gfx {
 
 #ifdef USE_SKIA
 static SkTypeface::Style
-gfxFontStyleToSkia(const gfxFontStyle* aStyle)
+fontStyleToSkia(FontStyle aStyle)
 {
-  if (aStyle->style == NS_FONT_STYLE_ITALIC) {
-    if (aStyle->weight == NS_FONT_WEIGHT_BOLD) {
-      return SkTypeface::kBoldItalic;
-    }
+  switch (aStyle) {
+  case FONT_STYLE_NORMAL:
+    return SkTypeface::kNormal;
+  case FONT_STYLE_ITALIC:
     return SkTypeface::kItalic;
-  }
-  if (aStyle->weight == NS_FONT_WEIGHT_BOLD) {
+  case FONT_STYLE_BOLD:
     return SkTypeface::kBold;
-  }
+  case FONT_STYLE_BOLD_ITALIC:
+    return SkTypeface::kBoldItalic;
+   }
+
+  gfxWarning() << "Unknown font style";
   return SkTypeface::kNormal;
 }
 #endif
 
 // Ideally we want to use FT_Face here but as there is currently no way to get
 // an SkTypeface from an FT_Face we do this.
-ScaledFontFreetype::ScaledFontFreetype(gfxFont* aFont, Float aSize)
+ScaledFontFreetype::ScaledFontFreetype(FontOptions* aFont, Float aSize)
   : ScaledFontBase(aSize)
 {
 #ifdef USE_SKIA
-  NS_LossyConvertUTF16toASCII name(aFont->GetName());
-  mTypeface = SkTypeface::CreateFromName(name.get(), gfxFontStyleToSkia(aFont->GetStyle()));
+  mTypeface = SkTypeface::CreateFromName(aFont->mName.c_str(), fontStyleToSkia(aFont->mStyle));
 #endif
 }
 
 }
 }
--- a/gfx/2d/ScaledFontFreetype.h
+++ b/gfx/2d/ScaledFontFreetype.h
@@ -10,15 +10,15 @@
 
 namespace mozilla {
 namespace gfx {
 
 class ScaledFontFreetype : public ScaledFontBase
 {
 public:
 
-  ScaledFontFreetype(gfxFont* aFont, Float aSize);
+  ScaledFontFreetype(FontOptions* aFont, Float aSize);
 };
 
 }
 }
 
 #endif /* MOZILLA_GFX_SCALEDFONTFREETYPE_H_ */
--- a/gfx/2d/Types.h
+++ b/gfx/2d/Types.h
@@ -66,16 +66,24 @@ enum NativeFontType
 {
   NATIVE_FONT_DWRITE_FONT_FACE,
   NATIVE_FONT_GDI_FONT_FACE,
   NATIVE_FONT_MAC_FONT_FACE,
   NATIVE_FONT_SKIA_FONT_FACE,
   NATIVE_FONT_CAIRO_FONT_FACE
 };
 
+enum FontStyle
+{
+  FONT_STYLE_NORMAL,
+  FONT_STYLE_ITALIC,
+  FONT_STYLE_BOLD,
+  FONT_STYLE_BOLD_ITALIC
+};
+
 enum CompositionOp { OP_OVER, OP_ADD, OP_ATOP, OP_OUT, OP_IN, OP_SOURCE, OP_DEST_IN, OP_DEST_OUT, OP_DEST_OVER, OP_DEST_ATOP, OP_XOR, OP_COUNT };
 enum ExtendMode { EXTEND_CLAMP, EXTEND_REPEAT, EXTEND_REFLECT };
 enum FillRule { FILL_WINDING, FILL_EVEN_ODD };
 enum AntialiasMode { AA_NONE, AA_GRAY, AA_SUBPIXEL };
 enum Snapping { SNAP_NONE, SNAP_ALIGNED };
 enum Filter { FILTER_LINEAR, FILTER_POINT };
 enum PatternType { PATTERN_COLOR, PATTERN_SURFACE, PATTERN_LINEAR_GRADIENT, PATTERN_RADIAL_GRADIENT };
 enum JoinStyle { JOIN_BEVEL, JOIN_ROUND, JOIN_MITER, JOIN_MITER_OR_BEVEL };
--- a/gfx/thebes/Makefile.in
+++ b/gfx/thebes/Makefile.in
@@ -171,16 +171,20 @@ CPPSRCS	= \
 	gfxUserFontSet.cpp \
 	gfxUtils.cpp \
 	gfxScriptItemizer.cpp \
 	gfxHarfBuzzShaper.cpp \
 	gfxSharedImageSurface.cpp \
 	gfxReusableSurfaceWrapper.cpp \
 	$(NULL)
 
+ifeq ($(MOZ_WIDGET_TOOLKIT),$(findstring $(MOZ_WIDGET_TOOLKIT),android gtk2 gonk qt))
+DEFINES += -DMOZ_ENABLE_FREETYPE
+endif
+
 ifdef MOZ_GRAPHITE
 DEFINES += -DGRAPHITE2_STATIC
 CPPSRCS	+= \
 	gfxGraphiteShaper.cpp \
 	$(NULL)
 endif
 
 # Are we targeting x86 or x64?  If so, build gfxAlphaRecoverySSE2.cpp.
--- a/gfx/thebes/gfxAndroidPlatform.cpp
+++ b/gfx/thebes/gfxAndroidPlatform.cpp
@@ -161,19 +161,20 @@ gfxAndroidPlatform::MakePlatformFont(con
     return gfxPlatformFontList::PlatformFontList()->MakePlatformFont(aProxyEntry,
                                                                      aFontData,
                                                                      aLength);
 }
 
 RefPtr<ScaledFont>
 gfxAndroidPlatform::GetScaledFontForFont(gfxFont *aFont)
 {
+    NS_ASSERTION(aFont->GetType() == gfxFont::FontType::FONT_TYPE_FT2, "Expecting Freetype font");
     NativeFont nativeFont;
     nativeFont.mType = NATIVE_FONT_SKIA_FONT_FACE;
-    nativeFont.mFont = aFont;
+    nativeFont.mFont = static_cast<gfxFT2FontBase*>(aFont)->GetFontOptions();
     RefPtr<ScaledFont> scaledFont =
       Factory::CreateScaledFontForNativeFont(nativeFont, aFont->GetAdjustedSize());
 
     return scaledFont;
 }
 
 bool
 gfxAndroidPlatform::FontHintingEnabled()
--- a/gfx/thebes/gfxFT2FontBase.cpp
+++ b/gfx/thebes/gfxFT2FontBase.cpp
@@ -10,16 +10,17 @@
 gfxFT2FontBase::gfxFT2FontBase(cairo_scaled_font_t *aScaledFont,
                                gfxFontEntry *aFontEntry,
                                const gfxFontStyle *aFontStyle)
     : gfxFont(aFontEntry, aFontStyle, kAntialiasDefault, aScaledFont),
       mSpaceGlyph(0),
       mHasMetrics(false)
 {
     cairo_scaled_font_reference(mScaledFont);
+    ConstructFontOptions();
 }
 
 gfxFT2FontBase::~gfxFT2FontBase()
 {
     cairo_scaled_font_destroy(mScaledFont);
 }
 
 PRUint32
@@ -215,8 +216,30 @@ gfxFT2FontBase::SetupCairoFont(gfxContex
     //
     // I can't see any significant difference in printing, irrespective of
     // what is set here.  It's too late to change things here as measuring has
     // already taken place.  We should really be measuring with a different
     // font for pdf and ps surfaces (bug 403513).
     cairo_set_scaled_font(cr, cairoFont);
     return true;
 }
+
+void
+gfxFT2FontBase::ConstructFontOptions()
+{
+  NS_LossyConvertUTF16toASCII name(this->GetName());
+  mFontOptions.mName = name.get();
+
+  const gfxFontStyle* style = this->GetStyle();
+  if (style->style == NS_FONT_STYLE_ITALIC) {
+    if (style->weight == NS_FONT_WEIGHT_BOLD) {
+      mFontOptions.mStyle = mozilla::gfx::FontStyle::FONT_STYLE_BOLD_ITALIC;
+    } else {
+      mFontOptions.mStyle = mozilla::gfx::FontStyle::FONT_STYLE_ITALIC;
+    }
+  } else {
+    if (style->weight == NS_FONT_WEIGHT_BOLD) {
+      mFontOptions.mStyle = mozilla::gfx::FontStyle::FONT_STYLE_BOLD;
+    } else {
+      mFontOptions.mStyle = mozilla::gfx::FontStyle::FONT_STYLE_NORMAL;
+    }
+  }
+}
--- a/gfx/thebes/gfxFT2FontBase.h
+++ b/gfx/thebes/gfxFT2FontBase.h
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GFX_FT2FONTBASE_H
 #define GFX_FT2FONTBASE_H
 
 #include "cairo.h"
 #include "gfxContext.h"
 #include "gfxFont.h"
+#include "mozilla/gfx/2D.h"
 
 class gfxFT2FontBase : public gfxFont {
 public:
     gfxFT2FontBase(cairo_scaled_font_t *aScaledFont,
                    gfxFontEntry *aFontEntry,
                    const gfxFontStyle *aFontStyle);
     virtual ~gfxFT2FontBase();
 
@@ -27,15 +28,21 @@ public:
     virtual PRUint32 GetGlyph(PRUint32 unicode, PRUint32 variation_selector);
     virtual bool ProvidesGlyphWidths() { return true; }
     virtual PRInt32 GetGlyphWidth(gfxContext *aCtx, PRUint16 aGID);
 
     cairo_scaled_font_t *CairoScaledFont() { return mScaledFont; };
     virtual bool SetupCairoFont(gfxContext *aContext);
 
     virtual FontType GetType() const { return FONT_TYPE_FT2; }
+
+    mozilla::gfx::FontOptions* GetFontOptions() { return &mFontOptions; }
 protected:
     PRUint32 mSpaceGlyph;
     bool mHasMetrics;
     Metrics mMetrics;
+
+    // Azure font description
+    mozilla::gfx::FontOptions  mFontOptions;
+    void ConstructFontOptions();
 };
 
 #endif /* GFX_FT2FONTBASE_H */
--- a/gfx/thebes/gfxFont.h
+++ b/gfx/thebes/gfxFont.h
@@ -21,16 +21,17 @@
 #include "gfxFontConstants.h"
 #include "gfxPlatform.h"
 #include "nsIAtom.h"
 #include "nsISupportsImpl.h"
 #include "gfxPattern.h"
 #include "mozilla/HashFunctions.h"
 #include "nsIMemoryReporter.h"
 #include "gfxFontFeatures.h"
+#include "mozilla/gfx/Types.h"
 
 typedef struct _cairo_scaled_font cairo_scaled_font_t;
 
 #ifdef DEBUG
 #include <stdio.h>
 #endif
 
 class gfxContext;
--- a/gfx/thebes/gfxPlatformGtk.cpp
+++ b/gfx/thebes/gfxPlatformGtk.cpp
@@ -12,16 +12,17 @@
 
 #include "nsUnicharUtils.h"
 #include "nsUnicodeProperties.h"
 #include "gfxFontconfigUtils.h"
 #ifdef MOZ_PANGO
 #include "gfxPangoFonts.h"
 #include "gfxContext.h"
 #include "gfxUserFontSet.h"
+#include "gfxFT2FontBase.h"
 #else
 #include <ft2build.h>
 #include FT_FREETYPE_H
 #include "gfxFT2Fonts.h"
 #endif
 
 #include "mozilla/gfx/2D.h"
 
@@ -736,24 +737,25 @@ gfxPlatformGtk::GetGdkDrawable(gfxASurfa
 #endif
 
     return NULL;
 }
 
 RefPtr<ScaledFont>
 gfxPlatformGtk::GetScaledFontForFont(gfxFont *aFont)
 {
-  NativeFont nativeFont;
-  nativeFont.mType = NATIVE_FONT_SKIA_FONT_FACE;
-  nativeFont.mFont = aFont;
-  RefPtr<ScaledFont> scaledFont =
-    Factory::CreateScaledFontForNativeFont(nativeFont, aFont->GetAdjustedSize());
+    NS_ASSERTION(aFont->GetType() == gfxFont::FontType::FONT_TYPE_FT2, "Expecting Freetype font");
+    NativeFont nativeFont;
+    nativeFont.mType = NATIVE_FONT_SKIA_FONT_FACE;
+    nativeFont.mFont = static_cast<gfxFT2FontBase*>(aFont)->GetFontOptions();
+    RefPtr<ScaledFont> scaledFont =
+      Factory::CreateScaledFontForNativeFont(nativeFont, aFont->GetAdjustedSize());
 
-  return scaledFont;
+    return scaledFont;
 }
 
 bool
 gfxPlatformGtk::SupportsAzure(BackendType& aBackend)
 {
-  aBackend = BACKEND_SKIA;
-  return true;
+    aBackend = BACKEND_SKIA;
+    return true;
 }