1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GraphicsContext.h"
#include "AffineTransform.h"
#include "NotImplemented.h"
#include "Path.h"
#include <CoreGraphics/CGBitmapContext.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#include "GraphicsContextPlatformPrivateCG.h"
using namespace std;
namespace WebCore {
static CGContextRef CGContextWithHDC(HDC hdc)
{
HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
BITMAP info;
GetObject(bitmap, sizeof(info), &info);
ASSERT(info.bmBitsPixel == 32);
CGContextRef context = CGBitmapContextCreate(info.bmBits, info.bmWidth, info.bmHeight, 8,
info.bmWidthBytes, deviceRGB, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
CGColorSpaceRelease(deviceRGB);
// Flip coords
CGContextTranslateCTM(context, 0, info.bmHeight);
CGContextScaleCTM(context, 1, -1);
// Put the HDC In advanced mode so it will honor affine transforms.
SetGraphicsMode(hdc, GM_ADVANCED);
return context;
}
GraphicsContext::GraphicsContext(HDC hdc)
: m_common(createGraphicsContextPrivate())
, m_data(new GraphicsContextPlatformPrivate(CGContextWithHDC(hdc)))
{
CGContextRelease(m_data->m_cgContext);
m_data->m_hdc = hdc;
setPaintingDisabled(!m_data->m_cgContext);
if (m_data->m_cgContext) {
// Make sure the context starts in sync with our state.
setPlatformFillColor(fillColor());
setPlatformStrokeColor(strokeColor());
}
}
bool GraphicsContext::inTransparencyLayer() const { return m_data->m_transparencyCount; }
HDC GraphicsContext::getWindowsContext(const IntRect& dstRect, bool supportAlphaBlend)
{
if (inTransparencyLayer()) {
if (dstRect.isEmpty())
return 0;
// Create a bitmap DC in which to draw.
BITMAPINFO bitmapInfo;
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = dstRect.width();
bitmapInfo.bmiHeader.biHeight = dstRect.height();
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = 0;
bitmapInfo.bmiHeader.biXPelsPerMeter = 0;
bitmapInfo.bmiHeader.biYPelsPerMeter = 0;
bitmapInfo.bmiHeader.biClrUsed = 0;
bitmapInfo.bmiHeader.biClrImportant = 0;
void* pixels = 0;
HBITMAP bitmap = ::CreateDIBSection(NULL, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0);
if (!bitmap)
return 0;
HDC bitmapDC = ::CreateCompatibleDC(m_data->m_hdc);
::SelectObject(bitmapDC, bitmap);
// Fill our buffer with clear if we're going to alpha blend.
if (supportAlphaBlend) {
BITMAP bmpInfo;
GetObject(bitmap, sizeof(bmpInfo), &bmpInfo);
int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight;
memset(bmpInfo.bmBits, 0, bufferSize);
}
// Make sure we can do world transforms.
SetGraphicsMode(bitmapDC, GM_ADVANCED);
// Apply a translation to our context so that the drawing done will be at (0,0) of the bitmap.
XFORM xform;
xform.eM11 = 1.0f;
xform.eM12 = 0.0f;
xform.eM21 = 0.0f;
xform.eM22 = 1.0f;
xform.eDx = -dstRect.x();
xform.eDy = -dstRect.y();
::SetWorldTransform(bitmapDC, &xform);
return bitmapDC;
}
CGContextFlush(platformContext());
m_data->save();
return m_data->m_hdc;
}
void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend)
{
if (hdc && inTransparencyLayer()) {
if (dstRect.isEmpty())
return;
HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
// Need to make a CGImage out of the bitmap's pixel buffer and then draw
// it into our context.
BITMAP info;
GetObject(bitmap, sizeof(info), &info);
ASSERT(info.bmBitsPixel == 32);
CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(info.bmBits, info.bmWidth, info.bmHeight, 8,
info.bmWidthBytes, deviceRGB, kCGBitmapByteOrder32Little |
(supportAlphaBlend ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst));
CGColorSpaceRelease(deviceRGB);
CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
CGContextDrawImage(m_data->m_cgContext, dstRect, image);
// Delete all our junk.
CGImageRelease(image);
CGContextRelease(bitmapContext);
::DeleteDC(hdc);
::DeleteObject(bitmap);
return;
}
m_data->restore();
}
void GraphicsContextPlatformPrivate::concatCTM(const AffineTransform& transform)
{
if (!m_hdc)
return;
CGAffineTransform mat = transform;
XFORM xform;
xform.eM11 = mat.a;
xform.eM12 = mat.b;
xform.eM21 = mat.c;
xform.eM22 = mat.d;
xform.eDx = mat.tx;
xform.eDy = mat.ty;
ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY);
}
void GraphicsContext::drawFocusRing(const Color& color)
{
if (paintingDisabled())
return;
float radius = (focusRingWidth() - 1) / 2.0f;
int offset = radius + focusRingOffset();
CGColorRef colorRef = color.isValid() ? cgColor(color) : 0;
CGMutablePathRef focusRingPath = CGPathCreateMutable();
const Vector<IntRect>& rects = focusRingRects();
unsigned rectCount = rects.size();
for (unsigned i = 0; i < rectCount; i++)
CGPathAddRect(focusRingPath, 0, CGRectInset(rects[i], -offset, -offset));
CGContextRef context = platformContext();
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextAddPath(context, focusRingPath);
wkDrawFocusRing(context, colorRef, radius);
CGColorRelease(colorRef);
CGPathRelease(focusRingPath);
CGContextRestoreGState(context);
}
// Pulled from GraphicsContextCG
static void setCGStrokeColor(CGContextRef context, const Color& color)
{
CGFloat red, green, blue, alpha;
color.getRGBA(red, green, blue, alpha);
CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
}
static const Color& spellingPatternColor() {
static const Color spellingColor(255, 0, 0);
return spellingColor;
}
static const Color& grammarPatternColor() {
static const Color grammarColor(0, 128, 0);
return grammarColor;
}
void GraphicsContext::drawLineForMisspellingOrBadGrammar(const IntPoint& point, int width, bool grammar)
{
if (paintingDisabled())
return;
// These are the same for misspelling or bad grammar
const int patternHeight = 3; // 3 rows
ASSERT(cMisspellingLineThickness == patternHeight);
const int patternWidth = 4; // 4 pixels
ASSERT(patternWidth == cMisspellingLinePatternWidth);
// Make sure to draw only complete dots.
// NOTE: Code here used to shift the underline to the left and increase the width
// to make sure everything gets underlined, but that results in drawing out of
// bounds (e.g. when at the edge of a view) and could make it appear that the
// space between adjacent misspelled words was underlined.
// allow slightly more considering that the pattern ends with a transparent pixel
int widthMod = width % patternWidth;
if (patternWidth - widthMod > cMisspellingLinePatternGapWidth)
width -= widthMod;
// Draw the underline
CGContextRef context = platformContext();
CGContextSaveGState(context);
const Color& patternColor = grammar ? grammarPatternColor() : spellingPatternColor();
setCGStrokeColor(context, patternColor);
wkSetPatternPhaseInUserSpace(context, point);
CGContextSetBlendMode(context, kCGBlendModeNormal);
// 3 rows, each offset by half a pixel for blending purposes
const CGPoint upperPoints [] = {{point.x(), point.y() + patternHeight - 2.5 }, {point.x() + width, point.y() + patternHeight - 2.5}};
const CGPoint middlePoints [] = {{point.x(), point.y() + patternHeight - 1.5 }, {point.x() + width, point.y() + patternHeight - 1.5}};
const CGPoint lowerPoints [] = {{point.x(), point.y() + patternHeight - 0.5 }, {point.x() + width, point.y() + patternHeight - 0.5 }};
// Dash lengths for the top and bottom of the error underline are the same.
// These are magic.
static const float edge_dash_lengths[] = {2.0f, 2.0f};
static const float middle_dash_lengths[] = {2.76f, 1.24f};
static const float edge_offset = -(edge_dash_lengths[1] - 1.0f) / 2.0f;
static const float middle_offset = -(middle_dash_lengths[1] - 1.0f) / 2.0f;
// Line opacities. Once again, these are magic.
const float upperOpacity = 0.33f;
const float middleOpacity = 0.75f;
const float lowerOpacity = 0.88f;
//Top line
CGContextSetLineDash(context, edge_offset, edge_dash_lengths,
sizeof(edge_dash_lengths) / sizeof(edge_dash_lengths[0]));
CGContextSetAlpha(context, upperOpacity);
CGContextStrokeLineSegments(context, upperPoints, 2);
// Middle line
CGContextSetLineDash(context, middle_offset, middle_dash_lengths,
sizeof(middle_dash_lengths) / sizeof(middle_dash_lengths[0]));
CGContextSetAlpha(context, middleOpacity);
CGContextStrokeLineSegments(context, middlePoints, 2);
// Bottom line
CGContextSetLineDash(context, edge_offset, edge_dash_lengths,
sizeof(edge_dash_lengths) / sizeof(edge_dash_lengths[0]));
CGContextSetAlpha(context, lowerOpacity);
CGContextStrokeLineSegments(context, lowerPoints, 2);
CGContextRestoreGState(context);
}
}
|