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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
// Copyright 2008, Google 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:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * 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.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "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 THE COPYRIGHT
// OWNER 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 "chrome/common/gfx/chrome_canvas.h"
#include <limits>
#include "base/gfx/rect.h"
#include "base/logging.h"
#include "skia/include/SkShader.h"
#include "chrome/common/gfx/chrome_font.h"
#include "chrome/common/l10n_util.h"
ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque)
: gfx::PlatformCanvas(width, height, is_opaque) {
}
ChromeCanvas::ChromeCanvas() : gfx::PlatformCanvas() {
}
ChromeCanvas::~ChromeCanvas() {
}
bool ChromeCanvas::GetClipRect(gfx::Rect* r) {
SkRect clip;
if (!getClipBounds(&clip)) {
if (r)
r->SetRect(0, 0, 0, 0);
return false;
}
r->SetRect(SkScalarRound(clip.fLeft), SkScalarRound(clip.fTop),
SkScalarRound(clip.fRight - clip.fLeft),
SkScalarRound(clip.fBottom - clip.fTop));
return true;
}
bool ChromeCanvas::ClipRectInt(int x, int y, int w, int h) {
SkRect new_clip;
new_clip.set(SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(x + w), SkIntToScalar(y + h));
return clipRect(new_clip);
}
bool ChromeCanvas::IntersectsClipRectInt(int x, int y, int w, int h) {
SkRect clip;
return getClipBounds(&clip) &&
clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w),
SkIntToScalar(y + h));
}
void ChromeCanvas::TranslateInt(int x, int y) {
translate(SkIntToScalar(x), SkIntToScalar(y));
}
void ChromeCanvas::ScaleInt(int x, int y) {
scale(SkIntToScalar(x), SkIntToScalar(y));
}
void ChromeCanvas::FillRectInt(const SkColor& color,
int x, int y, int w, int h) {
SkPaint paint;
paint.setColor(color);
paint.setStyle(SkPaint::kFill_Style);
paint.setPorterDuffXfermode(SkPorterDuff::kSrcOver_Mode);
FillRectInt(x, y, w, h, paint);
}
void ChromeCanvas::FillRectInt(int x, int y, int w, int h,
const SkPaint& paint) {
SkRect rc = {SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(x + w), SkIntToScalar(y + h) };
drawRect(rc, paint);
}
void ChromeCanvas::DrawRectInt(const SkColor& color,
int x, int y, int w, int h) {
DrawRectInt(color, x, y, w, h, SkPorterDuff::kSrcOver_Mode);
}
void ChromeCanvas::DrawRectInt(const SkColor& color, int x, int y, int w, int h,
SkPorterDuff::Mode mode) {
SkPaint paint;
paint.setColor(color);
paint.setStyle(SkPaint::kStroke_Style);
// Contrary to the docs, a width of 0 results in nothing.
paint.setStrokeWidth(SkIntToScalar(1));
paint.setPorterDuffXfermode(mode);
SkRect rc = {SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(x + w), SkIntToScalar(y + h) };
drawRect(rc, paint);
}
void ChromeCanvas::DrawFocusRect(int x, int y, int width, int height) {
// Create a 2D bitmap containing alternating on/off pixels - we do this
// so that you never get two pixels of the same color around the edges
// of the focus rect (this may mean that opposing edges of the rect may
// have a dot pattern out of phase to each other).
static SkBitmap* dots = NULL;
if (!dots) {
int col_pixels = 32;
int row_pixels = 32;
dots = new SkBitmap;
dots->setConfig(SkBitmap::kARGB_8888_Config, col_pixels, row_pixels);
dots->allocPixels();
dots->eraseARGB(0, 0, 0, 0);
uint32_t* dot = dots->getAddr32(0, 0);
for (int i = 0; i < row_pixels; i++) {
for (int u = 0; u < col_pixels; u++) {
if ((u % 2 + i % 2) % 2 != 0) {
dot[i * row_pixels + u] = SK_ColorGRAY;
}
}
}
}
// First the horizontal lines.
// Make a shader for the bitmap with an origin of the box we'll draw. This
// shader is refcounted and will have an initial refcount of 1.
SkShader* shader = SkShader::CreateBitmapShader(
*dots, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
// Assign the shader to the paint & release our reference. The paint will
// now own the shader and the shader will be destroyed when the paint goes
// out of scope.
SkPaint paint;
paint.setShader(shader);
shader->unref();
SkRect rect;
rect.set(SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(x + width), SkIntToScalar(y + 1));
drawRect(rect, paint);
rect.set(SkIntToScalar(x), SkIntToScalar(y + height - 1),
SkIntToScalar(x + width), SkIntToScalar(y + height));
drawRect(rect, paint);
rect.set(SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(x + 1), SkIntToScalar(y + height));
drawRect(rect, paint);
rect.set(SkIntToScalar(x + width - 1), SkIntToScalar(y),
SkIntToScalar(x + width), SkIntToScalar(y + height));
drawRect(rect, paint);
}
void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y) {
drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y));
}
void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int x, int y,
const SkPaint& paint) {
drawBitmap(bitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
}
void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y,
int src_w, int src_h, int dest_x, int dest_y,
int dest_w, int dest_h,
bool filter) {
SkPaint p;
DrawBitmapInt(bitmap, src_x, src_y, src_w, src_h, dest_x, dest_y,
dest_w, dest_h, filter, p);
}
void ChromeCanvas::DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y,
int src_w, int src_h, int dest_x, int dest_y,
int dest_w, int dest_h,
bool filter, const SkPaint& paint) {
DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
src_y + src_h < std::numeric_limits<int16_t>::max());
if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) {
NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!";
return;
}
if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
return;
SkRect dest_rect = { SkIntToScalar(dest_x),
SkIntToScalar(dest_y),
SkIntToScalar(dest_x + dest_w),
SkIntToScalar(dest_y + dest_h) };
if (src_w == dest_w && src_h == dest_h) {
// Workaround for apparent bug in Skia that causes image to occasionally
// shift.
SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h };
drawBitmapRect(bitmap, &src_rect, dest_rect, &paint);
return;
}
// Make a bitmap shader that contains the bitmap we want to draw. This is
// basically what SkCanvas.drawBitmap does internally, but it gives us
// more control over quality and will use the mipmap in the source image if
// it has one, whereas drawBitmap won't.
SkShader* shader = SkShader::CreateBitmapShader(bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix shader_scale;
shader_scale.setScale(
SkFloatToScalar(static_cast<float>(dest_w) / src_w),
SkFloatToScalar(static_cast<float>(dest_h) / src_h));
shader_scale.postTranslate(SkIntToScalar(dest_x - src_x),
SkIntToScalar(dest_y - src_y));
shader->setLocalMatrix(shader_scale);
// Set up our paint to use the shader & release our reference (now just owned
// by the paint).
SkPaint p(paint);
p.setFilterBitmap(filter);
p.setShader(shader);
shader->unref();
// The rect will be filled by the bitmap.
drawRect(dest_rect, p);
}
int ChromeCanvas::ComputeFormatFlags(int flags) {
int f = 0;
// Setting the text alignment explicitly in case it hasn't already been set.
// This will make sure that we don't align text to the left on RTL locales
// just because no alignment flag was passed to DrawStringInt().
if (!(flags & (TEXT_ALIGN_CENTER | TEXT_ALIGN_RIGHT | TEXT_ALIGN_LEFT))) {
flags |= l10n_util::DefaultCanvasTextAlignment();
}
if (flags & HIDE_PREFIX)
f |= DT_HIDEPREFIX;
else if ((flags & SHOW_PREFIX) == 0)
f |= DT_NOPREFIX;
if (flags & MULTI_LINE)
f |= DT_WORDBREAK;
else
f |= DT_SINGLELINE | DT_END_ELLIPSIS | DT_VCENTER;
// vertical alignment
if (flags & TEXT_VALIGN_TOP)
f |= DT_TOP;
else if (flags & TEXT_VALIGN_BOTTOM)
f |= DT_BOTTOM;
else
f |= DT_VCENTER;
// horizontal alignment
if (flags & TEXT_ALIGN_CENTER)
f |= DT_CENTER;
else if (flags & TEXT_ALIGN_RIGHT)
f |= DT_RIGHT;
else
f |= DT_LEFT;
// In order to make sure RTL/BiDi strings are rendered correctly, we must
// pass the flag DT_RTLREADING to DrawText (when the locale's language is
// a right-to-left language) so that Windows does the right thing.
//
// In addition to correctly displaying text containing both RTL and LTR
// elements (for example, a string containing a telephone number within a
// sentence in Hebrew, or a sentence in Hebrew that contains a word in
// English) this flag also makes sure that if there is not enough space to
// display the entire string, the ellipsis is displayed on the left hand side
// of the truncated string and not on the right hand side.
if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) {
f |= DT_RTLREADING;
}
return f;
}
void ChromeCanvas::SizeStringInt(const std::wstring& text,
const ChromeFont& font,
int *width, int *height, int flags) {
HDC dc = beginPlatformPaint();
HFONT old_font = static_cast<HFONT>(SelectObject(dc, font.hfont()));
RECT b;
b.left = 0;
b.top = 0;
b.right = *width;
b.bottom = *height;
DoDrawText(dc, text, &b, ComputeFormatFlags(flags) | DT_CALCRECT);
endPlatformPaint();
// Restore the old font. This way we don't have to worry if the caller
// deletes the font and the DC lives longer.
SelectObject(dc, old_font);
*width = b.right;
*height = b.bottom;
}
void ChromeCanvas::DrawStringInt(const std::wstring& text, HFONT font,
const SkColor& color, int x, int y, int w,
int h, int flags) {
if (!IntersectsClipRectInt(x, y, w, h))
return;
getTopPlatformDevice().prepareForGDI(x, y, w, h);
RECT text_bounds = { x, y, x + w, y + h };
HDC dc = beginPlatformPaint();
SetBkMode(dc, TRANSPARENT);
HFONT old_font = (HFONT)SelectObject(dc, font);
COLORREF brush_color = RGB(SkColorGetR(color), SkColorGetG(color),
SkColorGetB(color));
SetTextColor(dc, brush_color);
int f = ComputeFormatFlags(flags);
DoDrawText(dc, text, &text_bounds, f);
endPlatformPaint();
// Restore the old font. This way we don't have to worry if the caller
// deletes the font and the DC lives longer.
SelectObject(dc, old_font);
getTopPlatformDevice().postProcessGDI(x, y, w, h);
}
void ChromeCanvas::DrawStringInt(const std::wstring& text,
const ChromeFont& font,
const SkColor& color,
int x, int y,
int w, int h) {
DrawStringInt(text, font, color, x, y, w, h,
l10n_util::DefaultCanvasTextAlignment());
}
// We make sure that LTR text we draw in an RTL context is modified
// appropriately to make sure it maintains it LTR orientation.
void ChromeCanvas::DoDrawText(HDC hdc, const std::wstring& text,
RECT* text_bounds, int flags) {
std::wstring localized_text;
const wchar_t* string_ptr = text.c_str();
int string_size = static_cast<int>(text.length());
if (l10n_util::AdjustStringForLocaleDirection(text, &localized_text)) {
string_ptr = localized_text.c_str();
string_size = static_cast<int>(localized_text.length());
}
DrawText(hdc, string_ptr, string_size, text_bounds, flags);
}
void ChromeCanvas::DrawStringInt(const std::wstring& text,
const ChromeFont& font,
const SkColor& color,
int x, int y, int w, int h, int flags) {
DrawStringInt(text, font.hfont(), color, x, y, w, h, flags);
}
void ChromeCanvas::TileImageInt(const SkBitmap& bitmap,
int x, int y, int w, int h,
SkPorterDuff::Mode mode) {
if (!IntersectsClipRectInt(x, y, w, h))
return;
SkPaint paint;
int bitmap_width = bitmap.width();
int bitmap_height = bitmap.height();
SkShader* shader = SkShader::CreateBitmapShader(bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
paint.setShader(shader);
paint.setPorterDuffXfermode(mode);
// CreateBitmapShader returns a Shader with a reference count of one, we
// need to unref after paint takes ownership of the shader.
shader->unref();
save();
translate(SkIntToScalar(x), SkIntToScalar(y));
ClipRectInt(0, 0, w, h);
drawPaint(paint);
restore();
}
void ChromeCanvas::TileImageInt(const SkBitmap& bitmap,
int x, int y, int w, int h) {
TileImageInt(bitmap, x, y, w, h, SkPorterDuff::kSrcOver_Mode);
}
SkBitmap ChromeCanvas::ExtractBitmap() {
const SkBitmap& device_bitmap = getDevice()->accessBitmap(false);
// Make a bitmap to return, and a canvas to draw into it. We don't just want
// to call extractSubset or the copy constuctor, since we want an actual copy
// of the bitmap.
SkBitmap result;
device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config);
return result;
}
|