Unify platformWidthForGlyph across OS X and iOS
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Apr 2014 21:20:54 +0000 (21:20 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Apr 2014 21:20:54 +0000 (21:20 +0000)
https://bugs.webkit.org/show_bug.cgi?id=132036

Reviewed by Darin Adler.

This patch creates on shared SimpleFontData::platformWidthForGlyph() function for both OS X and iOS.

No new tests are necessary because there should be no behavior changes.

* platform/graphics/SimpleFontData.h: Signatures for two helper functions
* platform/graphics/ios/SimpleFontDataIOS.mm: Replace iOS implementation of platformWidthForGlyph() with
implementations of only the two helper functions
(WebCore::SimpleFontData::getRenderingStyle): Compute style argument to CGFontGetGlyphAdvancesForStyle()
(WebCore::SimpleFontData::advanceForColorBitmapFont): iOS doesn't have color bitmap fonts
(WebCore::SimpleFontData::platformWidthForGlyph): Deleted.
* platform/graphics/mac/SimpleFontDataMac.mm:
(WebCore::SimpleFontData::getRenderingStyle): Compute style argument to CGFontGetGlyphAdvancesForStyle()
(WebCore::SimpleFontData::advanceForColorBitmapFont): Use [NSFont advancementForGlyph] to compute the advance
(WebCore::hasCustomTracking): Removed #if
(WebCore::isEmoji): Only relevant on iOS
(WebCore::SimpleFontData::platformWidthForGlyph): Shared implementation. Calls helper functions.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@167773 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/SimpleFontData.h
Source/WebCore/platform/graphics/ios/SimpleFontDataIOS.mm
Source/WebCore/platform/graphics/mac/SimpleFontDataMac.mm

index 264909a2dbb0696abe90595891b9224d7c8aaf9f..436adb0661dfe28bf5d30a2a7d58eed08c070f18 100644 (file)
@@ -1,3 +1,27 @@
+2014-04-24  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        Unify platformWidthForGlyph across OS X and iOS
+        https://bugs.webkit.org/show_bug.cgi?id=132036
+
+        Reviewed by Darin Adler.
+
+        This patch creates on shared SimpleFontData::platformWidthForGlyph() function for both OS X and iOS.
+
+        No new tests are necessary because there should be no behavior changes.
+
+        * platform/graphics/SimpleFontData.h: Signatures for two helper functions
+        * platform/graphics/ios/SimpleFontDataIOS.mm: Replace iOS implementation of platformWidthForGlyph() with
+        implementations of only the two helper functions
+        (WebCore::SimpleFontData::getRenderingStyle): Compute style argument to CGFontGetGlyphAdvancesForStyle()
+        (WebCore::SimpleFontData::advanceForColorBitmapFont): iOS doesn't have color bitmap fonts
+        (WebCore::SimpleFontData::platformWidthForGlyph): Deleted.
+        * platform/graphics/mac/SimpleFontDataMac.mm:
+        (WebCore::SimpleFontData::getRenderingStyle): Compute style argument to CGFontGetGlyphAdvancesForStyle()
+        (WebCore::SimpleFontData::advanceForColorBitmapFont): Use [NSFont advancementForGlyph] to compute the advance
+        (WebCore::hasCustomTracking): Removed #if
+        (WebCore::isEmoji): Only relevant on iOS
+        (WebCore::SimpleFontData::platformWidthForGlyph): Shared implementation. Calls helper functions.
+
 2014-04-24  Zalan Bujtas  <zalan@apple.com>
 
         Subpixel rendering: Clipping on text areas when shifted by one device pixel.
index 4c2fb7a455ab6391fa113205f2e14b631ec52d3b..9df8c57d60a74202a7924367ab39397676750763 100644 (file)
 #include <cairo.h>
 #endif
 
+#if USE(CG)
+#if defined(__has_include) && __has_include(<CoreGraphics/CGFontRendering.h>)
+#include <CoreGraphics/CGFontRendering.h>
+#else
+enum {
+    kCGFontRenderingStyleAntialiasing = (1 << 0),
+    kCGFontRenderingStyleSmoothing = (1 << 1),
+    kCGFontRenderingStyleSubpixelPositioning = (1 << 2),
+    kCGFontRenderingStyleSubpixelQuantization = (1 << 3),
+    kCGFontRenderingStylePlatformNative = (1 << 9),
+    kCGFontRenderingStyleMask = 0x20F
+};
+#endif
+typedef uint32_t CGFontRenderingStyle;
+#endif
+
 namespace WebCore {
 
 class FontDescription;
@@ -245,6 +261,12 @@ private:
     float widthForGDIGlyph(Glyph glyph) const;
 #endif
 
+#if USE(CG)
+    bool canUseFastGlyphAdvanceGetter(Glyph glyph, CGSize& advance, bool& populatedAdvance) const;
+    CGFontRenderingStyle renderingStyle() const;
+    bool advanceForColorBitmapFont(Glyph, CGSize& result) const; // Returns true if the font is a color bitmap font
+#endif
+
     FontMetrics m_fontMetrics;
     float m_maxCharWidth;
     float m_avgCharWidth;
index 0b140e1dbcdf4fd935d2f8f6398b8465da6a24dd..d3ac1a532dd06ce47627d0d39963388556707303 100644 (file)
@@ -32,9 +32,7 @@
 #import "FontCache.h"
 #import "FontDescription.h"
 #import "FontServicesIOS.h"
-#import <CoreGraphics/CGFontGlyphSupport.h>
 #import <CoreGraphics/CGFontInfo.h>
-#import <CoreGraphics/CGFontRendering.h>
 #import <CoreText/CoreText.h>
 #import <float.h>
 #import <unicode/uchar.h>
@@ -187,26 +185,14 @@ void SimpleFontData::determinePitch()
     }
 }
 
-float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
+CGFontRenderingStyle SimpleFontData::renderingStyle() const
 {
-    CGSize advance = CGSizeZero;
-    if (platformData().orientation() == Horizontal || m_isBrokenIdeographFallback) {
-        if (platformData().m_isEmoji)
-            CTFontGetAdvancesForGlyphs(m_platformData.ctFont(), kCTFontHorizontalOrientation, &glyph, &advance, 1);
-        else {
-            float pointSize = platformData().m_size;
-            CGAffineTransform transform = CGAffineTransformMakeScale(pointSize, pointSize);
-            static const CGFontRenderingStyle renderingStyle = kCGFontRenderingStyleAntialiasing | kCGFontRenderingStyleSubpixelPositioning | kCGFontRenderingStyleSubpixelQuantization | kCGFontAntialiasingStyleUnfiltered;
-            if (!CGFontGetGlyphAdvancesForStyle(platformData().cgFont(), &transform, renderingStyle, &glyph, 1, &advance)) {
-                RetainPtr<CFStringRef> fullName = adoptCF(CGFontCopyFullName(platformData().cgFont()));
-                LOG_ERROR("Unable to cache glyph widths for %@ %f", fullName.get(), pointSize);
-                advance.width = 0;
-            }
-        }
-    } else
-        CTFontGetAdvancesForGlyphs(m_platformData.ctFont(), kCTFontVerticalOrientation, &glyph, &advance, 1);
-
-    return advance.width + m_syntheticBoldOffset;
+    return kCGFontRenderingStyleAntialiasing | kCGFontRenderingStyleSubpixelPositioning | kCGFontRenderingStyleSubpixelQuantization | kCGFontAntialiasingStyleUnfiltered;
+}
+
+bool SimpleFontData::advanceForColorBitmapFont(Glyph, CGSize&) const
+{
+    return false;
 }
 
 } // namespace WebCore
index a5eec0faa16416a2817459caa4da1ad63005dd4a..f904152607c2f9070ef4797ce9bcbf045d1cfb70 100644 (file)
 #if defined(__has_include) && __has_include(<CoreText/CTFontDescriptorPriv.h>)
 #import <CoreText/CTFontDescriptorPriv.h>
 #endif
-
 extern "C" bool CTFontDescriptorIsSystemUIFont(CTFontDescriptorRef);
 
+#if defined(__has_include) && __has_include(<CoreGraphics/CGFontGlyphSupport.h>)
+#import <CoreGraphics/CGFontGlyphSupport.h>
+#endif
+extern "C" bool CGFontGetGlyphAdvancesForStyle(CGFontRef font,
+    const CGAffineTransform *t, CGFontRenderingStyle style,
+    const CGGlyph glyphs[], size_t count, CGSize advances[]);
+
 #if !PLATFORM(IOS)
 @interface NSFont (WebAppKitSecretAPI)
 - (BOOL)_isFakeFixedPitch;
@@ -365,10 +371,39 @@ FloatRect SimpleFontData::platformBoundsForGlyph(Glyph glyph) const
     return boundingBox;
 }
 
-#if !PLATFORM(IOS)
+#if PLATFORM(MAC)
+inline CGFontRenderingStyle SimpleFontData::renderingStyle() const
+{
+    CGFontRenderingStyle style = kCGFontRenderingStyleAntialiasing | kCGFontRenderingStyleSubpixelPositioning | kCGFontRenderingStyleSubpixelQuantization;
+    NSFont *font = platformData().font();
+    if (font) {
+        switch ([font renderingMode]) {
+        case NSFontIntegerAdvancementsRenderingMode:
+            style = 0;
+            break;
+        case NSFontAntialiasedIntegerAdvancementsRenderingMode:
+            style = kCGFontRenderingStyleAntialiasing;
+            break;
+        default:
+            break;
+        }
+    }
+    return style;
+}
+
+inline bool SimpleFontData::advanceForColorBitmapFont(Glyph glyph, CGSize& advance) const
+{
+    NSFont *font = platformData().font();
+    if (!font || !platformData().isColorBitmapFont())
+        return false;
+    advance = NSSizeToCGSize([font advancementForGlyph:glyph]);
+    return true;
+}
+#endif
+
 static bool hasCustomTracking(CTFontRef font)
 {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1090
     UNUSED_PARAM(font);
     return false;
 #else
@@ -376,28 +411,50 @@ static bool hasCustomTracking(CTFontRef font)
 #endif
 }
 
+static inline bool isEmoji(const FontPlatformData& platformData)
+{
+#if PLATFORM(IOS)
+    return platformData.m_isEmoji;
+#else
+    UNUSED_PARAM(platformData);
+    return false;
+#endif
+}
+
+inline bool SimpleFontData::canUseFastGlyphAdvanceGetter(Glyph glyph, CGSize& advance, bool& populatedAdvance) const
+{
+    // Fast getter doesn't take custom tracking into account
+    if (hasCustomTracking(platformData().ctFont()))
+        return false;
+    // Fast getter doesn't work for emoji
+    if (isEmoji(platformData()))
+        return false;
+    // ... or for any bitmap fonts in general
+    if (advanceForColorBitmapFont(glyph, advance)) {
+        populatedAdvance = true;
+        return false;
+    }
+    return true;
+}
+
 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const
 {
     CGSize advance = CGSizeZero;
     bool horizontal = platformData().orientation() == Horizontal;
-    if ((horizontal || m_isBrokenIdeographFallback) && !hasCustomTracking(m_platformData.ctFont())) {
-        NSFont *font = platformData().font();
-        if (font && platformData().isColorBitmapFont())
-            advance = NSSizeToCGSize([font advancementForGlyph:glyph]);
-        else {
-            float pointSize = platformData().m_size;
-            CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
-            if (!wkGetGlyphTransformedAdvances(platformData().cgFont(), font, &m, &glyph, &advance)) {
-                LOG_ERROR("Unable to cache glyph widths for %@ %f", [font displayName], pointSize);
-                advance.width = 0;
-            }
+    bool populatedAdvance = false;
+    if ((horizontal || m_isBrokenIdeographFallback) && canUseFastGlyphAdvanceGetter(glyph, advance, populatedAdvance)) {
+        float pointSize = platformData().m_size;
+        CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
+        if (!CGFontGetGlyphAdvancesForStyle(platformData().cgFont(), &m, renderingStyle(), &glyph, 1, &advance)) {
+            RetainPtr<CFStringRef> fullName = adoptCF(CGFontCopyFullName(platformData().cgFont()));
+            LOG_ERROR("Unable to cache glyph widths for %@ %f", fullName.get(), pointSize);
+            advance.width = 0;
         }
-    } else
+    } else if (!populatedAdvance)
         CTFontGetAdvancesForGlyphs(m_platformData.ctFont(), horizontal ? kCTFontHorizontalOrientation : kCTFontVerticalOrientation, &glyph, &advance, 1);
 
     return advance.width + m_syntheticBoldOffset;
 }
-#endif // !PLATFORM(IOS)
 
 struct ProviderInfo {
     const UChar* characters;