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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/painter.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
namespace views {
namespace {
class GradientPainter : public Painter {
public:
GradientPainter(bool horizontal,
SkColor* colors,
SkScalar* pos,
size_t count)
: horizontal_(horizontal),
count_(count) {
pos_.reset(new SkScalar[count_]);
colors_.reset(new SkColor[count_]);
for (size_t i = 0; i < count_; ++i) {
pos_[i] = pos[i];
colors_[i] = colors[i];
}
}
virtual ~GradientPainter() {}
// Overridden from Painter:
virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE {
SkPaint paint;
SkPoint p[2];
p[0].iset(0, 0);
if (horizontal_)
p[1].iset(size.width(), 0);
else
p[1].iset(0, size.height());
skia::RefPtr<SkShader> s = skia::AdoptRef(SkGradientShader::CreateLinear(
p, colors_.get(), pos_.get(), count_, SkShader::kClamp_TileMode, NULL));
paint.setStyle(SkPaint::kFill_Style);
paint.setShader(s.get());
// Need to unref shader, otherwise never deleted.
canvas->sk_canvas()->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
SkIntToScalar(size.width()),
SkIntToScalar(size.height()), paint);
}
private:
// If |horizontal_| is true then the gradiant is painted horizontally.
bool horizontal_;
// The gradient colors.
scoped_ptr<SkColor[]> colors_;
// The relative positions of the corresponding gradient colors.
scoped_ptr<SkScalar[]> pos_;
// The number of elements in |colors_| and |pos_|.
size_t count_;
DISALLOW_COPY_AND_ASSIGN(GradientPainter);
};
// ImagePainter stores and paints nine images as a scalable grid.
class VIEWS_EXPORT ImagePainter : public Painter {
public:
// Construct an ImagePainter with the specified image resource ids.
// See CreateImageGridPainter()'s comment regarding image ID count and order.
explicit ImagePainter(const int image_ids[]);
// Construct an ImagePainter with the specified image and insets.
ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets);
virtual ~ImagePainter();
// Returns true if the images are empty.
bool IsEmpty() const;
// Overridden from Painter:
virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE;
private:
// Images must share widths by column and heights by row as depicted below.
// Coordinates along the X and Y axes are used for construction and painting.
// x0 x1 x2 x3
// y0__|____|____|____|
// y1__|_i0_|_i1_|_i2_|
// y2__|_i3_|_i4_|_i5_|
// y3__|_i6_|_i7_|_i8_|
gfx::ImageSkia images_[9];
DISALLOW_COPY_AND_ASSIGN(ImagePainter);
};
ImagePainter::ImagePainter(const int image_ids[]) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
for (size_t i = 0; i < 9; ++i)
images_[i] = *rb.GetImageSkiaNamed(image_ids[i]);
}
ImagePainter::ImagePainter(const gfx::ImageSkia& image,
const gfx::Insets& insets) {
DCHECK_GE(image.width(), insets.width());
DCHECK_GE(image.height(), insets.height());
// Extract subsets of the original image to match the |images_| format.
const int x[] =
{ 0, insets.left(), image.width() - insets.right(), image.width() };
const int y[] =
{ 0, insets.top(), image.height() - insets.bottom(), image.height() };
for (size_t j = 0; j < 3; ++j) {
for (size_t i = 0; i < 3; ++i) {
images_[i + j * 3] = gfx::ImageSkiaOperations::ExtractSubset(image,
gfx::Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j]));
}
}
}
ImagePainter::~ImagePainter() {
}
bool ImagePainter::IsEmpty() const {
return images_[0].isNull();
}
void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
if (IsEmpty())
return;
// Paint image subsets in accordance with the |images_| format.
const gfx::Rect rect(size);
const int x[] = { rect.x(), rect.x() + images_[0].width(),
rect.right() - images_[2].width(), rect.right() };
const int y[] = { rect.y(), rect.y() + images_[0].height(),
rect.bottom() - images_[6].height(), rect.bottom() };
canvas->DrawImageInt(images_[0], x[0], y[0]);
canvas->TileImageInt(images_[1], x[1], y[0], x[2] - x[1], y[1] - y[0]);
canvas->DrawImageInt(images_[2], x[2], y[0]);
canvas->TileImageInt(images_[3], x[0], y[1], x[1] - x[0], y[2] - y[1]);
canvas->DrawImageInt(
images_[4], 0, 0, images_[4].width(), images_[4].height(),
x[1], y[1], x[2] - x[1], y[2] - y[1], false);
canvas->TileImageInt(images_[5], x[2], y[1], x[3] - x[2], y[2] - y[1]);
canvas->DrawImageInt(images_[6], 0, y[2]);
canvas->TileImageInt(images_[7], x[1], y[2], x[2] - x[1], y[3] - y[2]);
canvas->DrawImageInt(images_[8], x[2], y[2]);
}
} // namespace
// static
void Painter::PaintPainterAt(gfx::Canvas* canvas,
Painter* painter,
const gfx::Rect& rect) {
DCHECK(canvas && painter);
canvas->Save();
canvas->Translate(rect.OffsetFromOrigin());
painter->Paint(canvas, rect.size());
canvas->Restore();
}
// static
Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) {
SkColor colors[2];
colors[0] = c1;
colors[1] = c2;
SkScalar pos[] = {0, 1};
return new GradientPainter(true, colors, pos, 2);
}
// static
Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) {
SkColor colors[2];
colors[0] = c1;
colors[1] = c2;
SkScalar pos[] = {0, 1};
return new GradientPainter(false, colors, pos, 2);
}
// static
Painter* Painter::CreateVerticalMultiColorGradient(SkColor* colors,
SkScalar* pos,
size_t count) {
return new GradientPainter(false, colors, pos, count);
}
// static
Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image,
const gfx::Insets& insets) {
return new ImagePainter(image, insets);
}
// static
Painter* Painter::CreateImageGridPainter(const int image_ids[]) {
return new ImagePainter(image_ids);
}
HorizontalPainter::HorizontalPainter(const int image_resource_names[]) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
for (int i = 0; i < 3; ++i)
images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia();
height_ = images_[LEFT]->height();
DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() &&
images_[LEFT]->height() == images_[CENTER]->height());
}
void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() +
images_[RIGHT]->width())) {
// No room to paint.
return;
}
canvas->DrawImageInt(*images_[LEFT], 0, 0);
canvas->DrawImageInt(*images_[RIGHT],
size.width() - images_[RIGHT]->width(), 0);
canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0,
size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_);
}
} // namespace views
|