1/*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "GraphicsContextCG.h"
28
29#include "AffineTransform.h"
30#include "Path.h"
31
32#include <CoreGraphics/CGBitmapContext.h>
33#include <WebKitSystemInterface/WebKitSystemInterface.h>
34#include "GraphicsContextPlatformPrivateCG.h"
35
36using namespace std;
37
38namespace WebCore {
39
40static CGContextRef CGContextWithHDC(HDC hdc, bool hasAlpha)
41{
42    HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
43
44    DIBPixelData pixelData(bitmap);
45
46    // FIXME: We can get here because we asked for a bitmap that is too big
47    // when we have a tiled layer and we're compositing. In that case
48    // bmBitsPixel will be 0. This seems to be benign, so for now we will
49    // exit gracefully and look at it later:
50    //  https://bugs.webkit.org/show_bug.cgi?id=52041
51    // ASSERT(bitmapBits.bitsPerPixel() == 32);
52    if (pixelData.bitsPerPixel() != 32)
53        return 0;
54
55    CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little | (hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst);
56    CGContextRef context = CGBitmapContextCreate(pixelData.buffer(), pixelData.size().width(), pixelData.size().height(), 8,
57                                                 pixelData.bytesPerRow(), deviceRGBColorSpaceRef(), bitmapInfo);
58
59    // Flip coords
60    CGContextTranslateCTM(context, 0, pixelData.size().height());
61    CGContextScaleCTM(context, 1, -1);
62
63    // Put the HDC In advanced mode so it will honor affine transforms.
64    SetGraphicsMode(hdc, GM_ADVANCED);
65
66    return context;
67}
68
69GraphicsContext::GraphicsContext(HDC hdc, bool hasAlpha)
70    : m_updatingControlTints(false)
71{
72    platformInit(hdc, hasAlpha);
73}
74
75void GraphicsContext::platformInit(HDC hdc, bool hasAlpha)
76{
77    m_data = new GraphicsContextPlatformPrivate(CGContextWithHDC(hdc, hasAlpha));
78    CGContextRelease(m_data->m_cgContext.get());
79    m_data->m_hdc = hdc;
80    setPaintingDisabled(!m_data->m_cgContext);
81    if (m_data->m_cgContext) {
82        // Make sure the context starts in sync with our state.
83        setPlatformFillColor(fillColor(), ColorSpaceDeviceRGB);
84        setPlatformStrokeColor(strokeColor(), ColorSpaceDeviceRGB);
85    }
86}
87
88// FIXME: Is it possible to merge getWindowsContext and createWindowsBitmap into a single API
89// suitable for all clients?
90void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
91{
92    bool createdBitmap = mayCreateBitmap && (!m_data->m_hdc || inTransparencyLayer());
93    if (!createdBitmap) {
94        m_data->restore();
95        return;
96    }
97
98    if (dstRect.isEmpty())
99        return;
100
101    OwnPtr<HBITMAP> bitmap = adoptPtr(static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP)));
102
103    DIBPixelData pixelData(bitmap.get());
104
105    ASSERT(pixelData.bitsPerPixel() == 32);
106
107    CGContextRef bitmapContext = CGBitmapContextCreate(pixelData.buffer(), pixelData.size().width(), pixelData.size().height(), 8,
108                                                       pixelData.bytesPerRow(), deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little |
109                                                       (supportAlphaBlend ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst));
110
111    CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
112    CGContextDrawImage(m_data->m_cgContext.get(), dstRect, image);
113
114    // Delete all our junk.
115    CGImageRelease(image);
116    CGContextRelease(bitmapContext);
117    ::DeleteDC(hdc);
118}
119
120void GraphicsContext::drawWindowsBitmap(WindowsBitmap* image, const IntPoint& point)
121{
122    // FIXME: Creating CFData is non-optimal, but needed to avoid crashing when printing.  Ideally we should
123    // make a custom CGDataProvider that controls the WindowsBitmap lifetime.  see <rdar://6394455>
124    RetainPtr<CFDataRef> imageData(AdoptCF, CFDataCreate(kCFAllocatorDefault, image->buffer(), image->bufferLength()));
125    RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(imageData.get()));
126    RetainPtr<CGImageRef> cgImage(AdoptCF, CGImageCreate(image->size().width(), image->size().height(), 8, 32, image->bytesPerRow(), deviceRGBColorSpaceRef(),
127                                                         kCGBitmapByteOrder32Little | kCGImageAlphaFirst, dataProvider.get(), 0, true, kCGRenderingIntentDefault));
128    CGContextDrawImage(m_data->m_cgContext.get(), CGRectMake(point.x(), point.y(), image->size().width(), image->size().height()), cgImage.get());
129}
130
131void GraphicsContext::drawFocusRing(const Path& path, int width, int offset, const Color& color)
132{
133    // FIXME: implement
134}
135
136// FIXME: This is nearly identical to the GraphicsContext::drawFocusRing function in GraphicsContextMac.mm.
137// The code could move to GraphicsContextCG.cpp and be shared.
138void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int width, int offset, const Color& color)
139{
140    if (paintingDisabled())
141        return;
142
143    float radius = (width - 1) / 2.0f;
144    offset += radius;
145    CGColorRef colorRef = color.isValid() ? cachedCGColor(color, ColorSpaceDeviceRGB) : 0;
146
147    CGMutablePathRef focusRingPath = CGPathCreateMutable();
148    unsigned rectCount = rects.size();
149    for (unsigned i = 0; i < rectCount; i++)
150        CGPathAddRect(focusRingPath, 0, CGRectInset(rects[i], -offset, -offset));
151
152    CGContextRef context = platformContext();
153    CGContextSaveGState(context);
154
155    CGContextBeginPath(context);
156    CGContextAddPath(context, focusRingPath);
157
158    wkDrawFocusRing(context, colorRef, radius);
159
160    CGPathRelease(focusRingPath);
161
162    CGContextRestoreGState(context);
163}
164
165// Pulled from GraphicsContextCG
166static void setCGStrokeColor(CGContextRef context, const Color& color)
167{
168    CGFloat red, green, blue, alpha;
169    color.getRGBA(red, green, blue, alpha);
170    CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
171}
172
173static const Color& spellingPatternColor() {
174    static const Color spellingColor(255, 0, 0);
175    return spellingColor;
176}
177
178static const Color& grammarPatternColor() {
179    static const Color grammarColor(0, 128, 0);
180    return grammarColor;
181}
182
183void GraphicsContext::drawLineForTextChecking(const FloatPoint& point, float width, TextCheckingLineStyle style)
184{
185    if (paintingDisabled())
186        return;
187
188    if (style != TextCheckingSpellingLineStyle && style != TextCheckingGrammarLineStyle)
189        return;
190
191    // These are the same for misspelling or bad grammar
192    const int patternHeight = 3; // 3 rows
193    ASSERT(cMisspellingLineThickness == patternHeight);
194    const int patternWidth = 4; // 4 pixels
195    ASSERT(patternWidth == cMisspellingLinePatternWidth);
196
197    // Make sure to draw only complete dots.
198    // NOTE: Code here used to shift the underline to the left and increase the width
199    // to make sure everything gets underlined, but that results in drawing out of
200    // bounds (e.g. when at the edge of a view) and could make it appear that the
201    // space between adjacent misspelled words was underlined.
202    // allow slightly more considering that the pattern ends with a transparent pixel
203    float widthMod = fmodf(width, patternWidth);
204    if (patternWidth - widthMod > cMisspellingLinePatternGapWidth)
205        width -= widthMod;
206
207    // Draw the underline
208    CGContextRef context = platformContext();
209    CGContextSaveGState(context);
210
211    const Color& patternColor = style == TextCheckingGrammarLineStyle ? grammarPatternColor() : spellingPatternColor();
212    setCGStrokeColor(context, patternColor);
213
214    wkSetPatternPhaseInUserSpace(context, point);
215    CGContextSetBlendMode(context, kCGBlendModeNormal);
216
217    // 3 rows, each offset by half a pixel for blending purposes
218    const CGPoint upperPoints [] = {{point.x(), point.y() + patternHeight - 2.5 }, {point.x() + width, point.y() + patternHeight - 2.5}};
219    const CGPoint middlePoints [] = {{point.x(), point.y() + patternHeight - 1.5 }, {point.x() + width, point.y() + patternHeight - 1.5}};
220    const CGPoint lowerPoints [] = {{point.x(), point.y() + patternHeight - 0.5 }, {point.x() + width, point.y() + patternHeight - 0.5 }};
221
222    // Dash lengths for the top and bottom of the error underline are the same.
223    // These are magic.
224    static const float edge_dash_lengths[] = {2.0f, 2.0f};
225    static const float middle_dash_lengths[] = {2.76f, 1.24f};
226    static const float edge_offset = -(edge_dash_lengths[1] - 1.0f) / 2.0f;
227    static const float middle_offset = -(middle_dash_lengths[1] - 1.0f) / 2.0f;
228
229    // Line opacities.  Once again, these are magic.
230    const float upperOpacity = 0.33f;
231    const float middleOpacity = 0.75f;
232    const float lowerOpacity = 0.88f;
233
234    //Top line
235    CGContextSetLineDash(context, edge_offset, edge_dash_lengths, WTF_ARRAY_LENGTH(edge_dash_lengths));
236    CGContextSetAlpha(context, upperOpacity);
237    CGContextStrokeLineSegments(context, upperPoints, 2);
238
239    // Middle line
240    CGContextSetLineDash(context, middle_offset, middle_dash_lengths, WTF_ARRAY_LENGTH(middle_dash_lengths));
241    CGContextSetAlpha(context, middleOpacity);
242    CGContextStrokeLineSegments(context, middlePoints, 2);
243
244    // Bottom line
245    CGContextSetLineDash(context, edge_offset, edge_dash_lengths, WTF_ARRAY_LENGTH(edge_dash_lengths));
246    CGContextSetAlpha(context, lowerOpacity);
247    CGContextStrokeLineSegments(context, lowerPoints, 2);
248
249    CGContextRestoreGState(context);
250}
251
252void GraphicsContextPlatformPrivate::flush()
253{
254    CGContextFlush(m_cgContext.get());
255}
256
257}
258