[Win] Clean up ColorSpace handling in Windows code
https://bugs.webkit.org/show_bug.cgi?id=124795

Reviewed by Tim Horton.

Functionality covered by existing fast/css/color test suite.

* platform/graphics/cg/GraphicsContextCG.cpp:
(WebCore::safeRGBColorSpaceRef): Handle case of Windows CG implementation not
handling sRGB correctly.
(WebCore::sRGBColorSpaceRef): Use new helper function.
* platform/graphics/win/FontCGWin.cpp:
(WebCore::Font::drawGlyphs): Pass correct color space to fill functions.
* platform/graphics/win/GraphicsContextCGWin.cpp:
(WebCore::GraphicsContext::platformInit): Initialize color space to value passed
via the style to the constructor.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@159720 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index f5a6c86..ac36841 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,22 @@
+2013-11-22  Brent Fulgham  <bfulgham@apple.com>
+
+        [Win] Clean up ColorSpace handling in Windows code
+        https://bugs.webkit.org/show_bug.cgi?id=124795
+
+        Reviewed by Tim Horton.
+
+        Functionality covered by existing fast/css/color test suite.
+
+        * platform/graphics/cg/GraphicsContextCG.cpp:
+        (WebCore::safeRGBColorSpaceRef): Handle case of Windows CG implementation not
+        handling sRGB correctly.
+        (WebCore::sRGBColorSpaceRef): Use new helper function.
+        * platform/graphics/win/FontCGWin.cpp:
+        (WebCore::Font::drawGlyphs): Pass correct color space to fill functions.
+        * platform/graphics/win/GraphicsContextCGWin.cpp:
+        (WebCore::GraphicsContext::platformInit): Initialize color space to value passed
+        via the style to the constructor.
+
 2013-11-22  Alexey Proskuryakov  <ap@apple.com>
 
         WebCrypto algorithms should check that key algorithm matches
diff --git a/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp b/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
index 4a68162..a19745e 100644
--- a/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
+++ b/Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
  * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -82,13 +82,15 @@
 
 CGColorSpaceRef sRGBColorSpaceRef()
 {
-    // FIXME: Windows should be able to use kCGColorSpaceSRGB, this is tracked by http://webkit.org/b/31363.
-#if PLATFORM(WIN)
-    return deviceRGBColorSpaceRef();
-#else
     static CGColorSpaceRef sRGBSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
-    return sRGBSpace;
+#if PLATFORM(WIN)
+    // Out-of-date CG installations will not honor kCGColorSpaceSRGB. This logic avoids
+    // causing a crash under those conditions. Since the default color space in Windows
+    // is sRGB, this all works out nicely.
+    if (!sRGBSpace)
+        sRGBSpace = deviceRGBColorSpaceRef();
 #endif
+    return sRGBSpace;
 }
 
 #if PLATFORM(WIN)
diff --git a/Source/WebCore/platform/graphics/win/FontCGWin.cpp b/Source/WebCore/platform/graphics/win/FontCGWin.cpp
index 170c9f0..b911183 100644
--- a/Source/WebCore/platform/graphics/win/FontCGWin.cpp
+++ b/Source/WebCore/platform/graphics/win/FontCGWin.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc.  All rights reserved.
+ * Copyright (C) 2006, 2007, 2008, 2009, 2013 Apple Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -191,8 +191,9 @@
         // Paint simple shadows ourselves instead of relying on CG shadows, to avoid losing subpixel antialiasing.
         graphicsContext->clearShadow();
         Color fillColor = graphicsContext->fillColor();
+        ColorSpace fillColorSpace = graphicsContext->fillColorSpace();
         Color shadowFillColor(shadowColor.red(), shadowColor.green(), shadowColor.blue(), shadowColor.alpha() * fillColor.alpha() / 255);
-        graphicsContext->setFillColor(shadowFillColor, ColorSpaceDeviceRGB);
+        graphicsContext->setFillColor(shadowFillColor, shadowColorSpace);
         float shadowTextX = point.x() + translation.width() + shadowOffset.width();
         // If shadows are ignoring transforms, then we haven't applied the Y coordinate flip yet, so down is negative.
         float shadowTextY = point.y() + translation.height() + shadowOffset.height() * (graphicsContext->shadowsIgnoreTransforms() ? -1 : 1);
@@ -202,7 +203,7 @@
             CGContextSetTextPosition(cgContext, point.x() + translation.width() + shadowOffset.width() + font->syntheticBoldOffset(), point.y() + translation.height() + shadowOffset.height());
             CGContextShowGlyphsWithAdvances(cgContext, glyphBuffer.glyphs(from), static_cast<const CGSize*>(glyphBuffer.advances(from)), numGlyphs);
         }
-        graphicsContext->setFillColor(fillColor, ColorSpaceDeviceRGB);
+        graphicsContext->setFillColor(fillColor, fillColorSpace);
     }
 
     CGContextSetTextPosition(cgContext, point.x() + translation.width(), point.y() + translation.height());
@@ -213,7 +214,7 @@
     }
 
     if (hasSimpleShadow)
-        graphicsContext->setShadow(shadowOffset, shadowBlur, shadowColor, ColorSpaceDeviceRGB);
+        graphicsContext->setShadow(shadowOffset, shadowBlur, shadowColor, shadowColorSpace);
 
     wkRestoreFontSmoothingStyle(cgContext, oldFontSmoothingStyle);
 }
diff --git a/Source/WebCore/platform/graphics/win/GraphicsContextCGWin.cpp b/Source/WebCore/platform/graphics/win/GraphicsContextCGWin.cpp
index bacc7b9..d1b5aa5 100644
--- a/Source/WebCore/platform/graphics/win/GraphicsContextCGWin.cpp
+++ b/Source/WebCore/platform/graphics/win/GraphicsContextCGWin.cpp
@@ -82,8 +82,8 @@
     setPaintingDisabled(!m_data->m_cgContext);
     if (m_data->m_cgContext) {
         // Make sure the context starts in sync with our state.
-        setPlatformFillColor(fillColor(), ColorSpaceDeviceRGB);
-        setPlatformStrokeColor(strokeColor(), ColorSpaceDeviceRGB);
+        setPlatformFillColor(fillColor(), fillColorSpace());
+        setPlatformStrokeColor(strokeColor(), strokeColorSpace());
     }
 }