Changeset 204858 in webkit


Ignore:
Timestamp:
Aug 23, 2016, 2:11:20 PM (9 years ago)
Author:
mmaxfield@apple.com
Message:

[Cocoa] Reduce uses of CGFonts
https://bugs.webkit.org/show_bug.cgi?id=160896

Reviewed by Darin Adler.

There are a few places where we unnecessarily use CGFonts instead of
CTFonts. We have had an ongoing effort throughout the past few years
of elliminating all of our uses of CGFonts, and this patch moves us
in that direction.

Most of the changes in this patch are straightforward (e.g.
CGFontGetUnitsPerEm() -> CTFontGetUnitsPerEm()). However, there is
one notable change: from CGContextShowGlyphsWithAdvances() to
CTFontDrawGlyphs(). This migration does not cause a performance
regression.

No new tests because there is no behavior change.

  • WebCore.xcodeproj/project.pbxproj: Delete unnecessary

FontServicesIOS files.

  • platform/graphics/FontMetrics.h:

(WebCore::FontMetrics::xHeight): Migrate to Optional<float>
(WebCore::FontMetrics::setXHeight):
(WebCore::FontMetrics::hasXHeight):
(WebCore::FontMetrics::hasCapHeight): Ditto.
(WebCore::FontMetrics::floatCapHeight):
(WebCore::FontMetrics::setCapHeight):
(WebCore::FontMetrics::capHeight):
(WebCore::FontMetrics::setZeroWidth): No one ever used the
m_hasZeroWidth variable, so this patch removes it.
(WebCore::FontMetrics::reset):
(WebCore::FontMetrics::FontMetrics): Deleted.
(WebCore::FontMetrics::setHasXHeight): Deleted. No one ever calls
this, so this patch removes it.
(WebCore::FontMetrics::hasZeroWidth): Deleted.
(WebCore::FontMetrics::setHasZeroWidth): Deleted.

  • platform/graphics/cocoa/FontCascadeCocoa.mm:

(WebCore::TextMatrixRestorer::TextMatrixRestorer): Rather than leaving
the text matrix stale after using it, this class cleans it up after us.
This means that the text matrix doesn't have to be reset in other places
which draw text.
(WebCore::TextMatrixRestorer::~TextMatrixRestorer):
(WebCore::TextMatrixRestorer::savedMatrix):
(WebCore::showGlyphsWithAdvances): Migrate from
CGContextShowGlyphsWithAdvances() to CTFontDrawGlyphs()
(WebCore::FontCascade::drawGlyphs): See above comment about the text
matrix.
(WebCore::RenderingStyleSaver::RenderingStyleSaver): Deleted.
(WebCore::RenderingStyleSaver::~RenderingStyleSaver): Deleted.

  • platform/graphics/cocoa/FontCocoa.mm:

(WebCore::shouldUseAdjustment): Taken from FontServicesIOS.
(WebCore::Font::platformInit): Migrate from CG to CT for some direct
replacement API calls.
(WebCore::Font::platformCharWidthInit): Ditto.
(WebCore::Font::determinePitch): Ditto.

  • platform/graphics/cocoa/FontPlatformDataCocoa.mm:

(WebCore::FontPlatformData::openTypeTable): Ditto.
(WebCore::FontPlatformData::description): Ditto.

  • platform/graphics/ios/FontServicesIOS.h: Removed. No longer needed

after migrating from CGContextShowGlyphsWithAdvances() to
CTFontDrawGlyphs().
(WebCore::FontServicesIOS::ascent): Deleted.
(WebCore::FontServicesIOS::descent): Deleted.
(WebCore::FontServicesIOS::lineGap): Deleted.
(WebCore::FontServicesIOS::lineSpacing): Deleted.
(WebCore::FontServicesIOS::xHeight): Deleted.
(WebCore::FontServicesIOS::capHeight): Deleted.
(WebCore::FontServicesIOS::unitsPerEm): Deleted.

  • platform/graphics/ios/FontServicesIOS.mm: Removed. Moved into

Font::platformInit().
(WebCore::shouldUseAdjustment): Deleted.
(WebCore::isCourier): Deleted.
(WebCore::FontServicesIOS::FontServicesIOS): Deleted.

  • platform/graphics/opentype/OpenTypeCG.cpp:

(WebCore::OpenType::tryGetTypoMetrics): Migrate from CG to CT for some
direct replacement API calls.

  • platform/graphics/opentype/OpenTypeCG.h: Ditto.
  • platform/graphics/win/FontCGWin.cpp:

(WebCore::FontCascade::drawGlyphs): Restore the text matrix.

  • platform/graphics/win/SimpleFontDataCGWin.cpp:

(WebCore::Font::platformWidthForGlyph): Deleted.

  • platform/spi/cg/CoreGraphicsSPI.h:
  • platform/spi/cocoa/CoreTextSPI.h:
  • rendering/RenderThemeIOS.mm:

(WebCore::paintAttachmentText): Deleted.

  • rendering/RenderThemeMac.mm:

(WebCore::paintAttachmentTitle): Deleted.
(WebCore::paintAttachmentSubtitle): Deleted.

Location:
trunk/Source/WebCore
Files:
2 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r204855 r204858  
     12016-08-23  Myles C. Maxfield  <mmaxfield@apple.com>
     2
     3        [Cocoa] Reduce uses of CGFonts
     4        https://bugs.webkit.org/show_bug.cgi?id=160896
     5
     6        Reviewed by Darin Adler.
     7
     8        There are a few places where we unnecessarily use CGFonts instead of
     9        CTFonts. We have had an ongoing effort throughout the past few years
     10        of elliminating all of our uses of CGFonts, and this patch moves us
     11        in that direction.
     12
     13        Most of the changes in this patch are straightforward (e.g.
     14        CGFontGetUnitsPerEm() -> CTFontGetUnitsPerEm()). However, there is
     15        one notable change: from CGContextShowGlyphsWithAdvances() to
     16        CTFontDrawGlyphs(). This migration does not cause a performance
     17        regression.
     18
     19        No new tests because there is no behavior change.
     20
     21        * WebCore.xcodeproj/project.pbxproj: Delete unnecessary
     22        FontServicesIOS files.
     23        * platform/graphics/FontMetrics.h:
     24        (WebCore::FontMetrics::xHeight): Migrate to Optional<float>
     25        (WebCore::FontMetrics::setXHeight):
     26        (WebCore::FontMetrics::hasXHeight):
     27        (WebCore::FontMetrics::hasCapHeight): Ditto.
     28        (WebCore::FontMetrics::floatCapHeight):
     29        (WebCore::FontMetrics::setCapHeight):
     30        (WebCore::FontMetrics::capHeight):
     31        (WebCore::FontMetrics::setZeroWidth): No one ever used the
     32        m_hasZeroWidth variable, so this patch removes it.
     33        (WebCore::FontMetrics::reset):
     34        (WebCore::FontMetrics::FontMetrics): Deleted.
     35        (WebCore::FontMetrics::setHasXHeight): Deleted. No one ever calls
     36        this, so this patch removes it.
     37        (WebCore::FontMetrics::hasZeroWidth): Deleted.
     38        (WebCore::FontMetrics::setHasZeroWidth): Deleted.
     39        * platform/graphics/cocoa/FontCascadeCocoa.mm:
     40        (WebCore::TextMatrixRestorer::TextMatrixRestorer): Rather than leaving
     41        the text matrix stale after using it, this class cleans it up after us.
     42        This means that the text matrix doesn't have to be reset in other places
     43        which draw text.
     44        (WebCore::TextMatrixRestorer::~TextMatrixRestorer):
     45        (WebCore::TextMatrixRestorer::savedMatrix):
     46        (WebCore::showGlyphsWithAdvances): Migrate from
     47        CGContextShowGlyphsWithAdvances() to CTFontDrawGlyphs()
     48        (WebCore::FontCascade::drawGlyphs): See above comment about the text
     49        matrix.
     50        (WebCore::RenderingStyleSaver::RenderingStyleSaver): Deleted.
     51        (WebCore::RenderingStyleSaver::~RenderingStyleSaver): Deleted.
     52        * platform/graphics/cocoa/FontCocoa.mm:
     53        (WebCore::shouldUseAdjustment): Taken from FontServicesIOS.
     54        (WebCore::Font::platformInit): Migrate from CG to CT for some direct
     55        replacement API calls.
     56        (WebCore::Font::platformCharWidthInit): Ditto.
     57        (WebCore::Font::determinePitch): Ditto.
     58        * platform/graphics/cocoa/FontPlatformDataCocoa.mm:
     59        (WebCore::FontPlatformData::openTypeTable): Ditto.
     60        (WebCore::FontPlatformData::description): Ditto.
     61        * platform/graphics/ios/FontServicesIOS.h: Removed. No longer needed
     62        after migrating from CGContextShowGlyphsWithAdvances() to
     63        CTFontDrawGlyphs().
     64        (WebCore::FontServicesIOS::ascent): Deleted.
     65        (WebCore::FontServicesIOS::descent): Deleted.
     66        (WebCore::FontServicesIOS::lineGap): Deleted.
     67        (WebCore::FontServicesIOS::lineSpacing): Deleted.
     68        (WebCore::FontServicesIOS::xHeight): Deleted.
     69        (WebCore::FontServicesIOS::capHeight): Deleted.
     70        (WebCore::FontServicesIOS::unitsPerEm): Deleted.
     71        * platform/graphics/ios/FontServicesIOS.mm: Removed. Moved into
     72        Font::platformInit().
     73        (WebCore::shouldUseAdjustment): Deleted.
     74        (WebCore::isCourier): Deleted.
     75        (WebCore::FontServicesIOS::FontServicesIOS): Deleted.
     76        * platform/graphics/opentype/OpenTypeCG.cpp:
     77        (WebCore::OpenType::tryGetTypoMetrics): Migrate from CG to CT for some
     78        direct replacement API calls.
     79        * platform/graphics/opentype/OpenTypeCG.h: Ditto.
     80        * platform/graphics/win/FontCGWin.cpp:
     81        (WebCore::FontCascade::drawGlyphs): Restore the text matrix.
     82        * platform/graphics/win/SimpleFontDataCGWin.cpp:
     83        (WebCore::Font::platformWidthForGlyph): Deleted.
     84        * platform/spi/cg/CoreGraphicsSPI.h:
     85        * platform/spi/cocoa/CoreTextSPI.h:
     86        * rendering/RenderThemeIOS.mm:
     87        (WebCore::paintAttachmentText): Deleted.
     88        * rendering/RenderThemeMac.mm:
     89        (WebCore::paintAttachmentTitle): Deleted.
     90        (WebCore::paintAttachmentSubtitle): Deleted.
     91
    1922016-08-23  Alex Christensen  <achristensen@webkit.org>
    293
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r204853 r204858  
    54415441                C572EE1F1201C9BC007D8F82 /* JSIDBIndex.h in Headers */ = {isa = PBXBuildFile; fileRef = C572EE1D1201C9BC007D8F82 /* JSIDBIndex.h */; };
    54425442                C57FEDE11212EE9C0097BE65 /* FileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C57FEDE01212EE9C0097BE65 /* FileSystem.cpp */; };
    5443                 C58361A91744523F00173511 /* FontServicesIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = C58361A71744523F00173511 /* FontServicesIOS.h */; };
    5444                 C58361AA1744523F00173511 /* FontServicesIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = C58361A81744523F00173511 /* FontServicesIOS.mm */; };
    54455443                C585A66211D4FAC5004C3E4B /* IDBBindingUtilities.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C585A66011D4FAC5004C3E4B /* IDBBindingUtilities.cpp */; };
    54465444                C585A66311D4FAC5004C3E4B /* IDBBindingUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = C585A66111D4FAC5004C3E4B /* IDBBindingUtilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    1288612884                C572EE1D1201C9BC007D8F82 /* JSIDBIndex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSIDBIndex.h; sourceTree = "<group>"; };
    1288712885                C57FEDE01212EE9C0097BE65 /* FileSystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FileSystem.cpp; sourceTree = "<group>"; };
    12888                 C58361A71744523F00173511 /* FontServicesIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FontServicesIOS.h; sourceTree = "<group>"; };
    12889                 C58361A81744523F00173511 /* FontServicesIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FontServicesIOS.mm; sourceTree = "<group>"; };
    1289012886                C585A66011D4FAC5004C3E4B /* IDBBindingUtilities.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IDBBindingUtilities.cpp; sourceTree = "<group>"; };
    1289112887                C585A66111D4FAC5004C3E4B /* IDBBindingUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDBBindingUtilities.h; sourceTree = "<group>"; };
     
    1602016016                                1AC2D89C1B1E291F00D52E87 /* FontAntialiasingStateSaver.h */,
    1602116017                                3727DFD4142AAE4500D449CB /* FontCacheIOS.mm */,
    16022                                 C58361A71744523F00173511 /* FontServicesIOS.h */,
    16023                                 C58361A81744523F00173511 /* FontServicesIOS.mm */,
    1602416018                                31AB4FFF122878A2001A7DB0 /* GraphicsContext3DIOS.h */,
    1602516019                                FED13D3F0CEA939400D89466 /* IconIOS.mm */,
     
    2433324327                                B2C3DA6B0D006CD600EF6F26 /* FontSelector.h in Headers */,
    2433424328                                E419041F1CC6486B00C35F5D /* FontSelectorClient.h in Headers */,
    24335                                 C58361A91744523F00173511 /* FontServicesIOS.h in Headers */,
    2433624329                                C2015C0A1BE6FEB200822389 /* FontVariantBuilder.h in Headers */,
    2433724330                                4A0DA2FF129B241900AB61E1 /* FormAssociatedElement.h in Headers */,
     
    2795027943                                B5320D6C122A24E9002D1440 /* FontPlatformDataCocoa.mm in Sources */,
    2795127944                                371F4FFD0D25E7F300ECE0D5 /* FontRanges.cpp in Sources */,
    27952                                 C58361AA1744523F00173511 /* FontServicesIOS.mm in Sources */,
    2795327945                                1C3249111C6D6A3B007EDB32 /* FontVariantBuilder.cpp in Sources */,
    2795427946                                4A0DA2FE129B241900AB61E1 /* FormAssociatedElement.cpp in Sources */,
  • trunk/Source/WebCore/platform/graphics/FontMetrics.h

    r178940 r204858  
    2323#include "FontBaseline.h"
    2424#include <wtf/MathExtras.h>
     25#include <wtf/Optional.h>
    2526
    2627namespace WebCore {
    2728
    28 const unsigned gDefaultUnitsPerEm = 1000;
    29 
    3029class FontMetrics {
    3130public:
    32     FontMetrics()
    33         : m_unitsPerEm(gDefaultUnitsPerEm)
    34         , m_ascent(0)
    35         , m_descent(0)
    36         , m_lineGap(0)
    37         , m_lineSpacing(0)
    38         , m_xHeight(0)
    39         , m_zeroWidth(0)
    40         , m_hasXHeight(false)
    41         , m_hasZeroWidth(false)
    42     {
    43     }
     31    static const unsigned defaultUnitsPerEm = 1000;
    4432
    4533    unsigned unitsPerEm() const { return m_unitsPerEm; }
     
    7664
    7765    float xHeight() const { return m_xHeight; }
    78     void setXHeight(float xHeight)
    79     {
    80         m_xHeight = xHeight;
    81         m_hasXHeight = true;
    82     }
    83 
    84     bool hasXHeight() const { return m_hasXHeight && m_xHeight > 0; }
    85     void setHasXHeight(bool hasXHeight) { m_hasXHeight = hasXHeight; }
     66    void setXHeight(float xHeight) { m_xHeight = xHeight; }
     67    bool hasXHeight() const { return m_xHeight > 0; }
    8668   
    87     bool hasCapHeight() const { return m_hasCapHeight && m_capHeight > 0; }
     69    bool hasCapHeight() const { return m_capHeight > 0; }
    8870    float floatCapHeight() const { return m_capHeight; }
    89     void setCapHeight(float capHeight)
    90     {
    91         m_capHeight = capHeight;
    92         m_hasCapHeight = true;
    93     }
     71    void setCapHeight(float capHeight) { m_capHeight = capHeight; }
    9472   
    9573    // Integer variants of certain metrics, used for HTML rendering.
     
    124102
    125103    float zeroWidth() const { return m_zeroWidth; }
    126     void setZeroWidth(float zeroWidth)
    127     {
    128         m_zeroWidth = zeroWidth;
    129         m_hasZeroWidth = true;
    130     }
    131 
    132     bool hasZeroWidth() const { return m_hasZeroWidth; }
    133     void setHasZeroWidth(bool hasZeroWidth) { m_hasZeroWidth = hasZeroWidth; }
     104    void setZeroWidth(float zeroWidth) { m_zeroWidth = zeroWidth; }
    134105
    135106private:
     
    138109    void reset()
    139110    {
    140         m_unitsPerEm = gDefaultUnitsPerEm;
     111        m_unitsPerEm = defaultUnitsPerEm;
    141112        m_ascent = 0;
    142113        m_descent = 0;
     
    144115        m_lineSpacing = 0;
    145116        m_xHeight = 0;
    146         m_hasXHeight = false;
     117        m_capHeight = 0;
     118        m_zeroWidth = 0;
    147119    }
    148120
    149     unsigned m_unitsPerEm;
    150     float m_ascent;
    151     float m_descent;
    152     float m_lineGap;
    153     float m_lineSpacing;
    154     float m_xHeight;
    155     float m_capHeight;
    156     float m_zeroWidth;
    157     bool m_hasXHeight;
    158     bool m_hasCapHeight;
    159     bool m_hasZeroWidth;
     121    unsigned m_unitsPerEm { defaultUnitsPerEm };
     122    float m_ascent { 0 };
     123    float m_descent { 0 };
     124    float m_lineGap { 0 };
     125    float m_lineSpacing { 0 };
     126    float m_zeroWidth { 0 };
     127    float m_xHeight { 0 };
     128    float m_capHeight { 0 };
    160129};
    161130
  • trunk/Source/WebCore/platform/graphics/FontPlatformData.h

    r200563 r204858  
    4545
    4646#if PLATFORM(COCOA)
    47 #if PLATFORM(IOS)
    48 #import <CoreGraphics/CoreGraphics.h>
    49 #endif
    5047#if USE(APPKIT)
    5148OBJC_CLASS NSFont;
     
    5754
    5855#if USE(CG)
    59 typedef struct CGFont* CGFontRef;
     56#include <CoreGraphics/CoreGraphics.h>
    6057#endif
    6158
     
    293290#endif
    294291
     292#if USE(CG)
     293class ScopedTextMatrix {
     294public:
     295    ScopedTextMatrix(CGAffineTransform newMatrix, CGContextRef context)
     296        : m_context(context)
     297        , m_textMatrix(CGContextGetTextMatrix(context))
     298    {
     299        CGContextSetTextMatrix(m_context, newMatrix);
     300    }
     301
     302    ~ScopedTextMatrix()
     303    {
     304        CGContextSetTextMatrix(m_context, m_textMatrix);
     305    }
     306
     307    CGAffineTransform savedMatrix() const
     308    {
     309        return m_textMatrix;
     310    }
     311
     312private:
     313    CGContextRef m_context;
     314    CGAffineTransform m_textMatrix;
     315};
     316#endif
     317
    295318} // namespace WebCore
    296319
  • trunk/Source/WebCore/platform/graphics/cocoa/FontCascadeCocoa.mm

    r204400 r204858  
    132132}
    133133
    134 class RenderingStyleSaver {
    135 public:
    136 #if !PLATFORM(MAC) || __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
    137     RenderingStyleSaver(CTFontRef, CGContextRef) { }
    138 #else
    139     RenderingStyleSaver(CTFontRef font, CGContextRef context)
    140         : m_context(context)
    141     {
    142         m_changed = CTFontSetRenderingStyle(font, context, &m_originalStyle, &m_originalDilation);
    143     }
    144 
    145     ~RenderingStyleSaver()
    146     {
    147         if (!m_changed)
    148             return;
    149         CGContextSetFontRenderingStyle(m_context, m_originalStyle);
    150         CGContextSetFontDilation(m_context, m_originalDilation);
    151     }
    152 
    153 private:
    154     bool m_changed;
    155     CGContextRef m_context;
    156     CGFontRenderingStyle m_originalStyle;
    157     CGSize m_originalDilation;
    158 #endif
    159 };
    160 
    161134static void showGlyphsWithAdvances(const FloatPoint& point, const Font& font, CGContextRef context, const CGGlyph* glyphs, const CGSize* advances, unsigned count)
    162135{
     
    168141    const FontPlatformData& platformData = font.platformData();
    169142    Vector<CGPoint, 256> positions(count);
    170     if (platformData.isColorBitmapFont())
    171         fillVectorWithHorizontalGlyphPositions(positions, context, advances, count);
    172143    if (platformData.orientation() == Vertical) {
    173         CGAffineTransform savedMatrix;
    174144        CGAffineTransform rotateLeftTransform = CGAffineTransformMake(0, -1, 1, 0, 0, 0);
    175         savedMatrix = CGContextGetTextMatrix(context);
    176         CGAffineTransform runMatrix = CGAffineTransformConcat(savedMatrix, rotateLeftTransform);
     145        CGAffineTransform textMatrix = CGContextGetTextMatrix(context);
     146        CGAffineTransform runMatrix = CGAffineTransformConcat(textMatrix, rotateLeftTransform);
    177147        CGContextSetTextMatrix(context, runMatrix);
    178148
     
    189159            position.y += advances[i].height;
    190160        }
    191         if (!platformData.isColorBitmapFont()) {
    192             RenderingStyleSaver saver(platformData.ctFont(), context);
    193             CGContextShowGlyphsAtPositions(context, glyphs, positions.data(), count);
    194         } else
    195             CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
    196         CGContextSetTextMatrix(context, savedMatrix);
     161        CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
    197162    } else {
    198         if (!platformData.isColorBitmapFont()) {
    199             RenderingStyleSaver saver(platformData.ctFont(), context);
    200 #pragma clang diagnostic push
    201 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
    202             CGContextShowGlyphsWithAdvances(context, glyphs, advances, count);
    203 #pragma clang diagnostic pop
    204         } else
    205             CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
     163        fillVectorWithHorizontalGlyphPositions(positions, context, advances, count);
     164        CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
    206165    }
    207166}
     
    270229    }
    271230#endif
    272 
    273     CGContextSetFont(cgContext, platformData.cgFont());
    274231
    275232    bool useLetterpressEffect = shouldUseLetterpressEffect(context);
     
    288245            matrix = CGAffineTransformConcat(matrix, CGAffineTransformMake(1, 0, -obliqueSkew, 1, 0, 0));
    289246    }
    290     CGContextSetTextMatrix(cgContext, matrix);
     247    ScopedTextMatrix restorer(matrix, cgContext);
    291248
    292249    setCGFontRenderingMode(context);
  • trunk/Source/WebCore/platform/graphics/cocoa/FontCocoa.mm

    r204466 r204858  
    3535#import "FontCascade.h"
    3636#import "FontDescription.h"
     37#import "OpenTypeCG.h"
    3738#import "SharedBuffer.h"
    3839#import "WebCoreSystemInterface.h"
     
    4748#import <ApplicationServices/ApplicationServices.h>
    4849#else
    49 #import "FontServicesIOS.h"
    5050#import <CoreText/CoreText.h>
    5151#endif
     
    5757#endif
    5858
    59 #if USE(APPKIT)
    60 #import "OpenTypeCG.h"
    61 #endif
    62 
    6359namespace WebCore {
     60
     61static inline bool caseInsensitiveCompare(CFStringRef a, CFStringRef b)
     62{
     63    return a && CFStringCompare(a, b, kCFCompareCaseInsensitive) == kCFCompareEqualTo;
     64}
    6465
    6566static bool fontHasVerticalGlyphs(CTFontRef ctFont)
     
    7879}
    7980
    80 #if !USE(APPKIT)
     81#if PLATFORM(IOS)
    8182bool fontFamilyShouldNotBeUsedForArabic(CFStringRef fontFamilyName)
    8283{
     
    8990        || (CFStringCompare(CFSTR("Arial"), fontFamilyName, 0) == kCFCompareEqualTo);
    9091}
     92
     93static const float kLineHeightAdjustment = 0.15f;
     94
     95static bool shouldUseAdjustment(CTFontRef font)
     96{
     97    RetainPtr<CFStringRef> familyName = adoptCF(CTFontCopyFamilyName(font));
     98
     99    if (!familyName || !CFStringGetLength(familyName.get()))
     100        return false;
     101
     102    return caseInsensitiveCompare(familyName.get(), CFSTR("Times"))
     103        || caseInsensitiveCompare(familyName.get(), CFSTR("Helvetica"))
     104        || caseInsensitiveCompare(familyName.get(), CFSTR(".Helvetica NeueUI"));
     105}
     106
     107#else
     108
     109static bool needsAscentAdjustment(CFStringRef familyName)
     110{
     111    return familyName && (caseInsensitiveCompare(familyName, CFSTR("Times"))
     112        || caseInsensitiveCompare(familyName, CFSTR("Helvetica"))
     113        || caseInsensitiveCompare(familyName, CFSTR("Courier")));
     114}
     115
    91116#endif
    92117
    93118void Font::platformInit()
    94119{
    95     // FIXME: Unify these two codepaths
    96 #if USE(APPKIT)
     120#if PLATFORM(IOS)
     121    m_syntheticBoldOffset = m_platformData.syntheticBold() ? ceilf(m_platformData.size() / 24.0f) : 0.f;
     122#else
    97123    m_syntheticBoldOffset = m_platformData.syntheticBold() ? 1.0f : 0.f;
     124#endif
    98125
    99126#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101100
     
    104131#endif
    105132
    106     unsigned unitsPerEm = CGFontGetUnitsPerEm(m_platformData.cgFont());
    107 
    108     // Some fonts erroneously specify a positive descender value. We follow Core Text in assuming that
    109     // such fonts meant the same distance, but in the reverse direction.
     133    unsigned unitsPerEm = CTFontGetUnitsPerEm(m_platformData.font());
    110134    float pointSize = m_platformData.size();
     135    float capHeight = CTFontGetCapHeight(m_platformData.font());
     136    float lineGap = CTFontGetLeading(m_platformData.font());
     137#if PLATFORM(IOS)
     138    CGFloat ascent = CTFontGetAscent(m_platformData.font());
     139    CGFloat descent = CTFontGetDescent(m_platformData.font());
     140#else
    111141    float ascent = scaleEmToUnits(CGFontGetAscent(m_platformData.cgFont()), unitsPerEm) * pointSize;
    112142    float descent = -scaleEmToUnits(-abs(CGFontGetDescent(m_platformData.cgFont())), unitsPerEm) * pointSize;
    113     float capHeight = scaleEmToUnits(CGFontGetCapHeight(m_platformData.cgFont()), unitsPerEm) * pointSize;
    114    
    115     float lineGap = scaleEmToUnits(CGFontGetLeading(m_platformData.cgFont()), unitsPerEm) * pointSize;
     143#endif
    116144
    117145    // The Open Font Format describes the OS/2 USE_TYPO_METRICS flag as follows:
     
    120148    if (OpenType::fontHasMathTable(m_platformData.ctFont())) {
    121149        short typoAscent, typoDescent, typoLineGap;
    122         if (OpenType::tryGetTypoMetrics(m_platformData.cgFont(), typoAscent, typoDescent, typoLineGap)) {
     150        if (OpenType::tryGetTypoMetrics(m_platformData.font(), typoAscent, typoDescent, typoLineGap)) {
    123151            ascent = scaleEmToUnits(typoAscent, unitsPerEm) * pointSize;
    124152            descent = -scaleEmToUnits(typoDescent, unitsPerEm) * pointSize;
     
    127155    }
    128156
     157    auto familyName = adoptCF(CTFontCopyFamilyName(m_platformData.font()));
     158#if PLATFORM(MAC)
    129159    // We need to adjust Times, Helvetica, and Courier to closely match the
    130160    // vertical metrics of their Microsoft counterparts that are the de facto
     
    132162    // incorrectly added to line spacing, so we use a 15% adjustment instead
    133163    // and add it to the ascent.
    134     RetainPtr<CFStringRef> familyName = adoptCF(CTFontCopyFamilyName(m_platformData.font()));
    135     if (!m_isCustomFont && familyName && (CFStringCompare(familyName.get(), CFSTR("Times"), kCFCompareCaseInsensitive) == kCFCompareEqualTo
    136         || CFStringCompare(familyName.get(), CFSTR("Helvetica"), kCFCompareCaseInsensitive) == kCFCompareEqualTo
    137         || CFStringCompare(familyName.get(), CFSTR("Courier"), kCFCompareCaseInsensitive) == kCFCompareEqualTo))
    138         ascent += floorf(((ascent + descent) * 0.15f) + 0.5f);
    139 
    140     // Compute and store line spacing, before the line metrics hacks are applied.
    141     m_fontMetrics.setLineSpacing(lroundf(ascent) + lroundf(descent) + lroundf(lineGap));
    142 
     164    if (!m_isCustomFont && needsAscentAdjustment(familyName.get()))
     165        ascent += std::round((ascent + descent) * 0.15f);
     166#endif
     167
     168    // Compute line spacing before the line metrics hacks are applied.
     169    float lineSpacing = lroundf(ascent) + lroundf(descent) + lroundf(lineGap);
     170
     171#if !PLATFORM(IOS)
    143172    // Hack Hiragino line metrics to allow room for marked text underlines.
    144173    // <rdar://problem/5386183>
     
    147176        descent = 3;
    148177    }
     178#endif
    149179   
    150180    if (platformData().orientation() == Vertical && !isTextOrientationFallback())
    151181        m_hasVerticalGlyphs = fontHasVerticalGlyphs(m_platformData.ctFont());
    152182
     183#if PLATFORM(IOS)
     184    CGFloat adjustment = shouldUseAdjustment(m_platformData.font()) ? ceil((ascent + descent) * kLineHeightAdjustment) : 0;
     185
     186    CGFontDescriptor descriptor;
     187    float xHeight = CGFontGetDescriptor(m_platformData.cgFont(), &descriptor) ? (descriptor.xHeight / 1000) * CTFontGetSize(m_platformData.font()) : 0;
     188    lineGap = ceilf(lineGap);
     189    lineSpacing = ceil(ascent) + adjustment + ceil(descent) + lineGap;
     190    ascent = ceilf(ascent + adjustment);
     191    descent = ceilf(descent);
     192
     193    m_shouldNotBeUsedForArabic = fontFamilyShouldNotBeUsedForArabic(adoptCF(CTFontCopyFamilyName(m_platformData.font())).get());
     194#else
    153195    float xHeight;
    154 
    155196    if (platformData().orientation() == Horizontal) {
    156197        // Measure the actual character "x", since it's possible for it to extend below the baseline, and we need the
     
    160201            xHeight = -CGRectGetMinY(platformBoundsForGlyph(xGlyph));
    161202        else
    162             xHeight = scaleEmToUnits(CGFontGetXHeight(m_platformData.cgFont()), unitsPerEm) * pointSize;
     203            xHeight = CTFontGetXHeight(m_platformData.font());
    163204    } else
    164205        xHeight = verticalRightOrientationFont().fontMetrics().xHeight();
     206#endif
    165207
    166208    m_fontMetrics.setUnitsPerEm(unitsPerEm);
     
    170212    m_fontMetrics.setLineGap(lineGap);
    171213    m_fontMetrics.setXHeight(xHeight);
    172 
    173 #else
    174 
    175     m_syntheticBoldOffset = m_platformData.syntheticBold() ? ceilf(m_platformData.size()  / 24.0f) : 0.f;
    176 
    177     CTFontRef ctFont = m_platformData.font();
    178     FontServicesIOS fontService(ctFont);
    179     m_fontMetrics.setUnitsPerEm(fontService.unitsPerEm());
    180     m_fontMetrics.setAscent(ceilf(fontService.ascent()));
    181     m_fontMetrics.setDescent(ceilf(fontService.descent()));
    182     m_fontMetrics.setLineGap(fontService.lineGap());
    183     m_fontMetrics.setLineSpacing(fontService.lineSpacing());
    184     m_fontMetrics.setXHeight(fontService.xHeight());
    185     m_fontMetrics.setCapHeight(fontService.capHeight());
    186     m_shouldNotBeUsedForArabic = fontFamilyShouldNotBeUsedForArabic(adoptCF(CTFontCopyFamilyName(ctFont)).get());
    187 
    188     if (platformData().orientation() == Vertical && !isTextOrientationFallback())
    189         m_hasVerticalGlyphs = fontHasVerticalGlyphs(m_platformData.ctFont());
    190 #endif
     214    m_fontMetrics.setLineSpacing(lineSpacing);
    191215}
    192216
     
    197221   
    198222#if PLATFORM(MAC)
    199     RetainPtr<CFDataRef> os2Table = adoptCF(CGFontCopyTableForTag(m_platformData.cgFont(), 'OS/2'));
     223    auto os2Table = adoptCF(CTFontCopyTable(m_platformData.font(), kCTFontTableOS2, kCTFontTableOptionNoOptions));
    200224    if (os2Table && CFDataGetLength(os2Table.get()) >= 4) {
    201225        const UInt8* os2 = CFDataGetBytePtr(os2Table.get());
     
    204228    }
    205229
    206     RetainPtr<CFDataRef> headTable = adoptCF(CGFontCopyTableForTag(m_platformData.cgFont(), 'head'));
     230    auto headTable = adoptCF(CTFontCopyTable(m_platformData.font(), kCTFontTableHead, kCTFontTableOptionNoOptions));
    207231    if (headTable && CFDataGetLength(headTable.get()) >= 42) {
    208232        const UInt8* head = CFDataGetBytePtr(headTable.get());
     
    524548}
    525549
    526 static inline bool caseInsensitiveCompare(CFStringRef a, CFStringRef b)
    527 {
    528     return a && CFStringCompare(a, b, kCFCompareCaseInsensitive) == kCFCompareEqualTo;
     550static int extractNumber(CFNumberRef number)
     551{
     552    int result = 0;
     553    if (number)
     554        CFNumberGetValue(number, kCFNumberIntType, &result);
     555    return result;
    529556}
    530557
     
    546573    // Note that AppKit does report MonotypeCorsiva as fixed pitch.
    547574
    548     RetainPtr<CFStringRef> fullName = adoptCF(CTFontCopyFullName(ctFont));
    549     RetainPtr<CFStringRef> familyName = adoptCF(CTFontCopyFamilyName(ctFont));
    550 
    551     m_treatAsFixedPitch = (CTFontGetSymbolicTraits(ctFont) & kCTFontMonoSpaceTrait) || CGFontIsFixedPitch(m_platformData.cgFont()) || (caseInsensitiveCompare(fullName.get(), CFSTR("Osaka-Mono")) || caseInsensitiveCompare(fullName.get(), CFSTR("MS-PGothic")) || caseInsensitiveCompare(fullName.get(), CFSTR("MonotypeCorsiva")));
     575    auto fullName = adoptCF(CTFontCopyFullName(ctFont));
     576    auto familyName = adoptCF(CTFontCopyFamilyName(ctFont));
     577
     578    int fixedPitch = extractNumber(adoptCF(static_cast<CFNumberRef>(CTFontCopyAttribute(m_platformData.font(), kCTFontFixedAdvanceAttribute))).get());
     579    m_treatAsFixedPitch = (CTFontGetSymbolicTraits(ctFont) & kCTFontMonoSpaceTrait) || fixedPitch || (caseInsensitiveCompare(fullName.get(), CFSTR("Osaka-Mono")) || caseInsensitiveCompare(fullName.get(), CFSTR("MS-PGothic")) || caseInsensitiveCompare(fullName.get(), CFSTR("MonotypeCorsiva")));
    552580#if PLATFORM(IOS)
    553     if (familyName && CFStringCompare(familyName.get(), CFSTR("Courier New"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
     581    if (familyName && caseInsensitiveCompare(familyName.get(), CFSTR("Courier New"))) {
    554582        // Special case Courier New to not be treated as fixed pitch, as this will make use of a hacked space width which is undesireable for iPhone (see rdar://6269783).
    555583        m_treatAsFixedPitch = false;
  • trunk/Source/WebCore/platform/graphics/cocoa/FontPlatformDataCocoa.mm

    r200563 r204858  
    158158RefPtr<SharedBuffer> FontPlatformData::openTypeTable(uint32_t table) const
    159159{
    160     if (RetainPtr<CFDataRef> data = adoptCF(CGFontCopyTableForTag(cgFont(), table)))
     160    if (RetainPtr<CFDataRef> data = adoptCF(CTFontCopyTable(font(), table, kCTFontTableOptionNoOptions)))
    161161        return SharedBuffer::wrapCFData(data.get());
    162162   
     
    167167String FontPlatformData::description() const
    168168{
    169     RetainPtr<CFStringRef> cgFontDescription = adoptCF(CFCopyDescription(cgFont()));
    170     return String(cgFontDescription.get()) + " " + String::number(m_size)
     169    auto fontDescription = adoptCF(CFCopyDescription(font()));
     170    return String(fontDescription.get()) + " " + String::number(m_size)
    171171            + (m_syntheticBold ? " synthetic bold" : "") + (m_syntheticOblique ? " synthetic oblique" : "") + (m_orientation ? " vertical orientation" : "");
    172172}
  • trunk/Source/WebCore/platform/graphics/opentype/OpenTypeCG.cpp

    r193484 r204858  
    3636#endif
    3737
    38 #if PLATFORM(COCOA)
    3938bool fontHasMathTable(CTFontRef ctFont)
    4039{
     
    5049    return false;
    5150}
    52 #endif
    5351
    5452static inline short readShortFromTable(const UInt8* os2Data, CFIndex offset)
     
    5755}
    5856
    59 bool tryGetTypoMetrics(CGFontRef cgFont, short& ascent, short& descent, short& lineGap)
     57bool tryGetTypoMetrics(CTFontRef font, short& ascent, short& descent, short& lineGap)
    6058{
    6159    bool result = false;
    62     if (CFDataRef os2Table = CGFontCopyTableForTag(cgFont, kCTFontTableOS2)) {
     60    if (auto os2Table = adoptCF(CTFontCopyTable(font, kCTFontTableOS2, kCTFontTableOptionNoOptions))) {
    6361        // For the structure of the OS/2 table, see
    6462        // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html
     
    6765        const CFIndex sTypoDescenderOffset = sTypoAscenderOffset + 2;
    6866        const CFIndex sTypoLineGapOffset = sTypoDescenderOffset + 2;
    69         if (CFDataGetLength(os2Table) >= sTypoLineGapOffset + 2) {
    70             const UInt8* os2Data = CFDataGetBytePtr(os2Table);
     67        if (CFDataGetLength(os2Table.get()) >= sTypoLineGapOffset + 2) {
     68            const UInt8* os2Data = CFDataGetBytePtr(os2Table.get());
    7169            // We test the use typo bit on the least significant byte of fsSelection.
    7270            const UInt8 useTypoMetricsMask = 1 << 7;
     
    7876            }
    7977        }
    80         CFRelease(os2Table);
    8178    }
    8279    return result;
  • trunk/Source/WebCore/platform/graphics/opentype/OpenTypeCG.h

    r192017 r204858  
    2828
    2929#include <CoreGraphics/CoreGraphics.h>
    30 #if PLATFORM(COCOA)
    3130#include <CoreText/CoreText.h>
     31
     32#if PLATFORM(WIN)
     33#include "CoreTextSPIWin.h"
    3234#endif
    3335
     
    3537namespace OpenType {
    3638
    37 #if PLATFORM(COCOA)
    3839bool fontHasMathTable(CTFontRef);
    39 #endif
    40 bool tryGetTypoMetrics(CGFontRef, short& ascent, short& descent, short& lineGap);
     40bool tryGetTypoMetrics(CTFontRef, short& ascent, short& descent, short& lineGap);
    4141
    4242} // namespace OpenType
  • trunk/Source/WebCore/platform/graphics/win/FontCGWin.cpp

    r204400 r204858  
    173173    }
    174174
     175    CGAffineTransform savedMatrix = CGContextGetTextMatrix(cgContext);
    175176    CGContextSetTextMatrix(cgContext, matrix);
    176177
     
    216217
    217218    wkRestoreFontSmoothingStyle(cgContext, oldFontSmoothingStyle);
    218 }
    219 
    220 }
     219    CGContextSetTextMatrix(cgContext, savedMatrix);
     220}
     221
     222}
  • trunk/Source/WebCore/platform/graphics/win/SimpleFontDataCGWin.cpp

    r199890 r204858  
    3030#include "Font.h"
    3131
     32#include "CoreTextSPIWin.h"
    3233#include "FloatRect.h"
    3334#include "FontCache.h"
     
    6869    // "If set, it is strongly recommended to use OS/2.sTypoAscender - OS/2.sTypoDescender+ OS/2.sTypoLineGap as a value for default line spacing for this font."
    6970    short typoAscent, typoDescent, typoLineGap;
    70     if (OpenType::tryGetTypoMetrics(m_platformData.cgFont(), typoAscent, typoDescent, typoLineGap)) {
     71    if (OpenType::tryGetTypoMetrics(adoptCF(CTFontCreateWithGraphicsFont(m_platformData.cgFont(), m_platformData.size(), nullptr, nullptr)).get(), typoAscent, typoDescent, typoLineGap)) {
    7172        iAscent = typoAscent;
    7273        iDescent = typoDescent;
     
    145146    CGAffineTransform m = CGAffineTransformMakeScale(pointSize, pointSize);
    146147 
    147     // FIXME: Need to add real support for printer fonts.
    148148    bool isPrinterFont = false;
    149149    wkGetGlyphAdvances(font, m, m_platformData.isSystemFont(), isPrinterFont, glyph, advance);
  • trunk/Source/WebCore/platform/spi/cg/CoreGraphicsSPI.h

    r203424 r204858  
    171171void CGContextResetClip(CGContextRef);
    172172CGContextType CGContextGetType(CGContextRef);
    173 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100
    174 void CGContextSetFontDilation(CGContextRef, CGSize);
    175 void CGContextSetFontRenderingStyle(CGContextRef, CGFontRenderingStyle);
    176 #endif
    177173
    178174CFStringRef CGFontCopyFamilyName(CGFontRef);
  • trunk/Source/WebCore/platform/spi/cocoa/CoreTextSPI.h

    r204456 r204858  
    6868CTTypesetterRef CTTypesetterCreateWithUniCharProviderAndOptions(CTUniCharProviderCallback provide, CTUniCharDisposeCallback dispose, void* refCon, CFDictionaryRef options);
    6969bool CTFontGetVerticalGlyphsForCharacters(CTFontRef, const UniChar characters[], CGGlyph glyphs[], CFIndex count);
    70 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED > 101000
    71 bool CTFontSetRenderingStyle(CTFontRef, CGContextRef, CGFontRenderingStyle* originalStyle, CGSize* originalDilation);
    72 #endif
    7370
    7471CTFontDescriptorRef CTFontDescriptorCreateForUIType(CTFontUIFontType, CGFloat size, CFStringRef language);
  • trunk/Source/WebCore/platform/spi/win/CoreTextSPIWin.h

    r204592 r204858  
    3434typedef const struct __CTFont* CTFontRef;
    3535typedef const struct __CTLine* CTLineRef;
     36typedef UInt32 FourCharCode;
     37typedef FourCharCode CTFontTableTag;
    3638
    3739extern const CFStringRef kCTFontAttributeName;
    3840extern const CFStringRef kCTForegroundColorFromContextAttributeName;
    3941
    40 CTFontRef CTFontCreateWithName(CFStringRef name, CGFloat size, const CGAffineTransform* matrix);
     42typedef CF_OPTIONS(uint32_t, CTFontTableOptions)
     43{
     44    kCTFontTableOptionNoOptions = 0,
     45    kCTFontTableOptionExcludeSynthetic = (1 << 0)
     46};
     47
     48CTFontRef CTFontCreateWithName(CFStringRef, CGFloat size, const CGAffineTransform*);
     49CTFontRef CTFontCreateWithGraphicsFont(CGFontRef, CGFloat size, const CGAffineTransform*, CTFontDescriptorRef attributes);
    4150CTLineRef CTLineCreateWithAttributedString(CFAttributedStringRef);
    4251void CTLineDraw(CTLineRef, CGContextRef);
     52CFDataRef CTFontCopyTable(CTFontRef, CTFontTableTag, CTFontTableOptions);
     53CFArrayRef CTFontCopyAvailableTables(CTFontRef, CTFontTableOptions);
    4354
    4455}
  • trunk/Source/WebCore/rendering/RenderThemeIOS.mm

    r204005 r204858  
    16531653        context.scale(FloatSize(1, -1));
    16541654
    1655         CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
     1655        CGContextSetTextPosition(context.platformContext(), 0, 0);
    16561656        CTLineDraw(line.line.get(), context.platformContext());
    16571657    }
  • trunk/Source/WebCore/rendering/RenderThemeMac.mm

    r203324 r204858  
    25112511        context.scale(FloatSize(1, -1));
    25122512
    2513         CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
     2513        CGContextSetTextPosition(context.platformContext(), 0, 0);
    25142514        CTLineDraw(line.line.get(), context.platformContext());
    25152515    }
     
    25232523    context.scale(FloatSize(1, -1));
    25242524
    2525     CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
     2525    CGContextSetTextPosition(context.platformContext(), 0, 0);
    25262526    CTLineDraw(layout.subtitleLine.get(), context.platformContext());
    25272527}
  • trunk/Source/WebCore/svg/SVGFontFaceElement.cpp

    r203324 r204858  
    7777    const AtomicString& value = attributeWithoutSynchronization(units_per_emAttr);
    7878    if (value.isEmpty())
    79         return gDefaultUnitsPerEm;
     79        return FontMetrics::defaultUnitsPerEm;
    8080
    8181    return static_cast<unsigned>(ceilf(value.toFloat()));
Note: See TracChangeset for help on using the changeset viewer.