[OS X] Text dilation parameters are not restored after being set
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 1 May 2015 23:25:41 +0000 (23:25 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 1 May 2015 23:25:41 +0000 (23:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=144507
<rdar://problem/19446938>

Patch by Myles C. Maxfield <mmaxfield@apple.com> on 2015-05-01
Reviewed by Simon Fraser and Ned Holbrook.

Source/WebCore:

Create a RAII class who is in charge of saving, applying, and restoring dilation parameters. Use this class
around our text drawing routines.

Note that this migrates our use of CTFontSetRenderingParameters() to CTFontSetRenderingStyle().

Test: fast/text/dilation-save-restore.html

* platform/graphics/cocoa/FontCascadeCocoa.mm:
(WebCore::RenderingStyleSaver::RenderingStyleSaver):
(WebCore::RenderingStyleSaver::~RenderingStyleSaver):
(WebCore::showGlyphsWithAdvances):
* platform/spi/cg/CoreGraphicsSPI.h:
* platform/spi/cocoa/CoreTextSPI.h:

LayoutTests:

See below.

* fast/text/dilation-save-restore-expected.html: Added. Never sets dilation parameters, draws two strings.
* fast/text/dilation-save-restore.html: Added. Draws one string without dilation, sets the dilation, then draws a
second string. If saving and restoring doesn't work correctly, these two lines will appear different.

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

LayoutTests/ChangeLog
LayoutTests/fast/text/dilation-save-restore-expected.html [new file with mode: 0644]
LayoutTests/fast/text/dilation-save-restore.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/cocoa/FontCascadeCocoa.mm
Source/WebCore/platform/spi/cg/CoreGraphicsSPI.h
Source/WebCore/platform/spi/cocoa/CoreTextSPI.h

index b99efc9..b7bbf7f 100644 (file)
@@ -1,3 +1,17 @@
+2015-05-01  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [OS X] Text dilation parameters are not restored after being set
+        https://bugs.webkit.org/show_bug.cgi?id=144507
+        <rdar://problem/19446938>
+
+        Reviewed by Simon Fraser and Ned Holbrook.
+
+        See below.
+
+        * fast/text/dilation-save-restore-expected.html: Added. Never sets dilation parameters, draws two strings.
+        * fast/text/dilation-save-restore.html: Added. Draws one string without dilation, sets the dilation, then draws a
+        second string. If saving and restoring doesn't work correctly, these two lines will appear different.
+
 2015-05-01  Jordan Harband  <ljharb@gmail.com>
 
         String#startsWith/endsWith/includes don't handle Infinity position/endPosition args correctly
diff --git a/LayoutTests/fast/text/dilation-save-restore-expected.html b/LayoutTests/fast/text/dilation-save-restore-expected.html
new file mode 100644 (file)
index 0000000..85e3b20
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body>
+This test makes sure that we save and restore state properly when issuing dilation rendering parameters.
+<div>Hello, World!</div>
+<br>
+<div>Hello, World!</div>
+</body>
+</html>
diff --git a/LayoutTests/fast/text/dilation-save-restore.html b/LayoutTests/fast/text/dilation-save-restore.html
new file mode 100644 (file)
index 0000000..1276990
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body>
+This test makes sure that we save and restore state properly when issuing dilation rendering parameters.
+<div>Hello, World!</div>
+<div style="margin-top: -100px; color: white;">Hello, World!</div>
+<div style="margin-top: 100px;">Hello, World!</div>
+</body>
+</html>
index 4cf1e3c..250047a 100644 (file)
@@ -1,3 +1,25 @@
+2015-05-01  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [OS X] Text dilation parameters are not restored after being set
+        https://bugs.webkit.org/show_bug.cgi?id=144507
+        <rdar://problem/19446938>
+
+        Reviewed by Simon Fraser and Ned Holbrook.
+
+        Create a RAII class who is in charge of saving, applying, and restoring dilation parameters. Use this class
+        around our text drawing routines.
+
+        Note that this migrates our use of CTFontSetRenderingParameters() to CTFontSetRenderingStyle().
+
+        Test: fast/text/dilation-save-restore.html
+
+        * platform/graphics/cocoa/FontCascadeCocoa.mm:
+        (WebCore::RenderingStyleSaver::RenderingStyleSaver):
+        (WebCore::RenderingStyleSaver::~RenderingStyleSaver):
+        (WebCore::showGlyphsWithAdvances):
+        * platform/spi/cg/CoreGraphicsSPI.h:
+        * platform/spi/cocoa/CoreTextSPI.h:
+
 2015-05-01  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r183687.
index c977388..3bcc323 100644 (file)
@@ -131,6 +131,56 @@ static void showLetterpressedGlyphsWithAdvances(const FloatPoint& point, const F
 #endif
 }
 
+class RenderingStyleSaver {
+public:
+#if !PLATFORM(MAC) || __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
+
+    RenderingStyleSaver(CTFontRef, CGContextRef) { }
+
+#elif !defined(CORETEXT_HAS_CTFontSetRenderingStyle) || CORETEXT_HAS_CTFontSetRenderingStyle != 1
+
+    // This is very slow, but it's just a holdover until everyone migrates to CTFontSetRenderingStyle()
+    // FIXME: Delete this implementation when everyone has migrated off
+    RenderingStyleSaver(CTFontRef font, CGContextRef context)
+        : m_context(context)
+    {
+        CGContextSaveGState(context);
+        CTFontSetRenderingParameters(font, context);
+    }
+
+    ~RenderingStyleSaver()
+    {
+        CGContextRestoreGState(m_context);
+    }
+
+private:
+    CGContextRef m_context;
+
+#else
+
+    RenderingStyleSaver(CTFontRef font, CGContextRef context)
+        : m_context(context)
+    {
+        m_changed = CTFontSetRenderingStyle(font, context, &m_originalStyle, &m_originalDilation);
+    }
+
+    ~RenderingStyleSaver()
+    {
+        if (!m_changed)
+            return;
+        CGContextSetFontRenderingStyle(m_context, m_originalStyle);
+        CGContextSetFontDilation(m_context, m_originalDilation);
+    }
+
+private:
+    bool m_changed;
+    CGContextRef m_context;
+    CGFontRenderingStyle m_originalStyle;
+    CGSize m_originalDilation;
+
+#endif
+};
+
 static void showGlyphsWithAdvances(const FloatPoint& point, const Font* font, CGContextRef context, const CGGlyph* glyphs, const CGSize* advances, size_t count)
 {
     if (!count)
@@ -162,18 +212,14 @@ static void showGlyphsWithAdvances(const FloatPoint& point, const Font* font, CG
             position.y += advances[i].height;
         }
         if (!platformData.isColorBitmapFont()) {
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED > 101000
-            CTFontSetRenderingParameters(platformData.ctFont(), context);
-#endif
+            RenderingStyleSaver saver(platformData.ctFont(), context);
             CGContextShowGlyphsAtPositions(context, glyphs, positions.data(), count);
         } else
             CTFontDrawGlyphs(platformData.ctFont(), glyphs, positions.data(), count, context);
         CGContextSetTextMatrix(context, savedMatrix);
     } else {
         if (!platformData.isColorBitmapFont()) {
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED > 101000
-            CTFontSetRenderingParameters(platformData.ctFont(), context);
-#endif
+            RenderingStyleSaver saver(platformData.ctFont(), context);
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
             CGContextShowGlyphsWithAdvances(context, glyphs, advances, count);
index cdb04b5..198efb2 100644 (file)
@@ -144,6 +144,7 @@ void CGContextSetCompositeOperation(CGContextRef, CGCompositeOperation);
 void CGContextSetShouldAntialiasFonts(CGContextRef, bool shouldAntialiasFonts);
 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100
 void CGContextSetFontDilation(CGContextRef, CGSize);
+void CGContextSetFontRenderingStyle(CGContextRef, CGFontRenderingStyle);
 #endif
 
 CFStringRef CGFontCopyFamilyName(CGFontRef);
index b1c44ec..e128d4e 100644 (file)
@@ -66,8 +66,12 @@ CTLineRef CTLineCreateWithUniCharProvider(CTUniCharProviderCallback provide, CTU
 CTTypesetterRef CTTypesetterCreateWithUniCharProviderAndOptions(CTUniCharProviderCallback provide, CTUniCharDisposeCallback dispose, void* refCon, CFDictionaryRef options);
 bool CTFontGetVerticalGlyphsForCharacters(CTFontRef, const UniChar characters[], CGGlyph glyphs[], CFIndex count);
 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED > 101000
+#if defined(CORETEXT_HAS_CTFontSetRenderingStyle) && CORETEXT_HAS_CTFontSetRenderingStyle == 1
+bool CTFontSetRenderingStyle(CTFontRef, CGContextRef, CGFontRenderingStyle* originalStyle, CGSize* originalDilation);
+#else
 void CTFontSetRenderingParameters(CTFontRef, CGContextRef);
 #endif
+#endif
 
 CTFontDescriptorRef CTFontDescriptorCreateForUIType(CTFontUIFontType, CGFloat size, CFStringRef language);
 CTFontDescriptorRef CTFontDescriptorCreateWithTextStyle(CFStringRef style, CFStringRef size, CFStringRef language);