OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "gfx/canvas_skia.h" | |
6 | |
7 #include <cairo/cairo.h> | |
8 #include <gtk/gtk.h> | |
9 #include <pango/pango.h> | |
10 #include <pango/pangocairo.h> | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "gfx/font.h" | |
15 #include "gfx/gtk_util.h" | |
16 #include "gfx/platform_font_gtk.h" | |
17 #include "gfx/rect.h" | |
18 | |
19 namespace { | |
20 | |
21 const gunichar kAcceleratorChar = '&'; | |
22 | |
23 // Font settings that we initialize once and then use when drawing text in | |
24 // DrawStringInt(). | |
25 static cairo_font_options_t* cairo_font_options = NULL; | |
26 | |
27 // Update |cairo_font_options| based on GtkSettings, allocating it if needed. | |
28 static void UpdateCairoFontOptions() { | |
29 if (!cairo_font_options) | |
30 cairo_font_options = cairo_font_options_create(); | |
31 | |
32 GtkSettings* gtk_settings = gtk_settings_get_default(); | |
33 gint antialias = 0; | |
34 gint hinting = 0; | |
35 gchar* hint_style = NULL; | |
36 gchar* rgba_style = NULL; | |
37 g_object_get(gtk_settings, | |
38 "gtk-xft-antialias", &antialias, | |
39 "gtk-xft-hinting", &hinting, | |
40 "gtk-xft-hintstyle", &hint_style, | |
41 "gtk-xft-rgba", &rgba_style, | |
42 NULL); | |
43 | |
44 // g_object_get() doesn't tell us whether the properties were present or not, | |
45 // but if they aren't (because gnome-settings-daemon isn't running), we'll get | |
46 // NULL values for the strings. | |
47 if (hint_style && rgba_style) { | |
48 if (!antialias) { | |
49 cairo_font_options_set_antialias(cairo_font_options, | |
50 CAIRO_ANTIALIAS_NONE); | |
51 } else if (strcmp(rgba_style, "none") == 0) { | |
52 cairo_font_options_set_antialias(cairo_font_options, | |
53 CAIRO_ANTIALIAS_GRAY); | |
54 } else { | |
55 cairo_font_options_set_antialias(cairo_font_options, | |
56 CAIRO_ANTIALIAS_SUBPIXEL); | |
57 cairo_subpixel_order_t cairo_subpixel_order = | |
58 CAIRO_SUBPIXEL_ORDER_DEFAULT; | |
59 if (strcmp(rgba_style, "rgb") == 0) { | |
60 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; | |
61 } else if (strcmp(rgba_style, "bgr") == 0) { | |
62 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; | |
63 } else if (strcmp(rgba_style, "vrgb") == 0) { | |
64 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; | |
65 } else if (strcmp(rgba_style, "vbgr") == 0) { | |
66 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; | |
67 } | |
68 cairo_font_options_set_subpixel_order(cairo_font_options, | |
69 cairo_subpixel_order); | |
70 } | |
71 | |
72 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; | |
73 if (hinting == 0 || strcmp(hint_style, "hintnone") == 0) { | |
74 cairo_hint_style = CAIRO_HINT_STYLE_NONE; | |
75 } else if (strcmp(hint_style, "hintslight") == 0) { | |
76 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; | |
77 } else if (strcmp(hint_style, "hintmedium") == 0) { | |
78 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; | |
79 } else if (strcmp(hint_style, "hintfull") == 0) { | |
80 cairo_hint_style = CAIRO_HINT_STYLE_FULL; | |
81 } | |
82 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); | |
83 } | |
84 | |
85 if (hint_style) | |
86 g_free(hint_style); | |
87 if (rgba_style) | |
88 g_free(rgba_style); | |
89 } | |
90 | |
91 // Pass a width > 0 to force wrapping and elliding. | |
92 static void SetupPangoLayout(PangoLayout* layout, | |
93 const string16& text, | |
94 const gfx::Font& font, | |
95 int width, | |
96 int flags) { | |
97 if (!cairo_font_options) | |
98 UpdateCairoFontOptions(); | |
99 // This needs to be done early on; it has no effect when called just before | |
100 // pango_cairo_show_layout(). | |
101 pango_cairo_context_set_font_options( | |
102 pango_layout_get_context(layout), cairo_font_options); | |
103 | |
104 // Callers of DrawStringInt handle RTL layout themselves, so tell pango to not | |
105 // scope out RTL characters. | |
106 pango_layout_set_auto_dir(layout, FALSE); | |
107 | |
108 if (width > 0) | |
109 pango_layout_set_width(layout, width * PANGO_SCALE); | |
110 | |
111 if (flags & gfx::Canvas::NO_ELLIPSIS) { | |
112 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | |
113 } else { | |
114 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | |
115 } | |
116 | |
117 if (flags & gfx::Canvas::TEXT_ALIGN_CENTER) { | |
118 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | |
119 } else if (flags & gfx::Canvas::TEXT_ALIGN_RIGHT) { | |
120 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | |
121 } | |
122 | |
123 if (flags & gfx::Canvas::MULTI_LINE) { | |
124 pango_layout_set_wrap(layout, | |
125 (flags & gfx::Canvas::CHARACTER_BREAK) ? | |
126 PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); | |
127 } | |
128 | |
129 // Set the resolution to match that used by Gtk. If we don't set the | |
130 // resolution and the resolution differs from the default, Gtk and Chrome end | |
131 // up drawing at different sizes. | |
132 double resolution = gfx::GetPangoResolution(); | |
133 if (resolution > 0) { | |
134 pango_cairo_context_set_resolution(pango_layout_get_context(layout), | |
135 resolution); | |
136 } | |
137 | |
138 PangoFontDescription* desc = font.GetNativeFont(); | |
139 pango_layout_set_font_description(layout, desc); | |
140 pango_font_description_free(desc); | |
141 | |
142 // Set text and accelerator character if needed. | |
143 std::string utf8 = UTF16ToUTF8(text); | |
144 if (flags & gfx::Canvas::SHOW_PREFIX) { | |
145 // Escape the text string to be used as markup. | |
146 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); | |
147 pango_layout_set_markup_with_accel(layout, | |
148 escaped_text, | |
149 strlen(escaped_text), | |
150 kAcceleratorChar, NULL); | |
151 g_free(escaped_text); | |
152 } else if (flags & gfx::Canvas::HIDE_PREFIX) { | |
153 // Remove the ampersand character. | |
154 utf8 = gfx::RemoveWindowsStyleAccelerators(utf8); | |
155 pango_layout_set_text(layout, utf8.data(), utf8.size()); | |
156 } else { | |
157 pango_layout_set_text(layout, utf8.data(), utf8.size()); | |
158 } | |
159 } | |
160 | |
161 // A class to encapsulate string drawing params and operations. | |
162 class DrawStringContext { | |
163 public: | |
164 DrawStringContext(gfx::CanvasSkia* canvas, | |
165 const string16& text, | |
166 const gfx::Font& font, | |
167 const gfx::Rect& bounds, | |
168 const gfx::Rect& clip, | |
169 int flags); | |
170 ~DrawStringContext(); | |
171 | |
172 void Draw(const SkColor& text_color); | |
173 void DrawWithHalo(const SkColor& text_color, | |
174 const SkColor& halo_color); | |
175 | |
176 private: | |
177 const gfx::Rect& bounds_; | |
178 int flags_; | |
179 const gfx::Font& font_; | |
180 | |
181 gfx::CanvasSkia* canvas_; | |
182 cairo_t* cr_; | |
183 PangoLayout* layout_; | |
184 | |
185 int text_x_; | |
186 int text_y_; | |
187 int text_width_; | |
188 int text_height_; | |
189 | |
190 DISALLOW_COPY_AND_ASSIGN(DrawStringContext); | |
191 }; | |
192 | |
193 DrawStringContext::DrawStringContext(gfx::CanvasSkia* canvas, | |
194 const string16& text, | |
195 const gfx::Font& font, | |
196 const gfx::Rect& bounds, | |
197 const gfx::Rect& clip, | |
198 int flags) | |
199 : bounds_(bounds), | |
200 flags_(flags), | |
201 font_(font), | |
202 canvas_(canvas), | |
203 cr_(NULL), | |
204 layout_(NULL), | |
205 text_x_(bounds.x()), | |
206 text_y_(bounds.y()), | |
207 text_width_(0), | |
208 text_height_(0) { | |
209 DCHECK(!bounds_.IsEmpty()); | |
210 | |
211 cr_ = canvas_->beginPlatformPaint(); | |
212 layout_ = pango_cairo_create_layout(cr_); | |
213 | |
214 SetupPangoLayout(layout_, text, font, bounds_.width(), flags_); | |
215 | |
216 pango_layout_set_height(layout_, bounds_.height() * PANGO_SCALE); | |
217 | |
218 cairo_save(cr_); | |
219 | |
220 cairo_rectangle(cr_, clip.x(), clip.y(), clip.width(), clip.height()); | |
221 cairo_clip(cr_); | |
222 | |
223 pango_layout_get_pixel_size(layout_, &text_width_, &text_height_); | |
224 | |
225 if (flags_ & gfx::Canvas::TEXT_VALIGN_TOP) { | |
226 // Cairo should draw from the top left corner already. | |
227 } else if (flags_ & gfx::Canvas::TEXT_VALIGN_BOTTOM) { | |
228 text_y_ += (bounds.height() - text_height_); | |
229 } else { | |
230 // Vertically centered. | |
231 text_y_ += ((bounds.height() - text_height_) / 2); | |
232 } | |
233 } | |
234 | |
235 DrawStringContext::~DrawStringContext() { | |
236 if (font_.GetStyle() & gfx::Font::UNDERLINED) { | |
237 gfx::PlatformFontGtk* platform_font = | |
238 static_cast<gfx::PlatformFontGtk*>(font_.platform_font()); | |
239 double underline_y = | |
240 static_cast<double>(text_y_) + text_height_ + | |
241 platform_font->underline_position(); | |
242 cairo_set_line_width(cr_, platform_font->underline_thickness()); | |
243 cairo_move_to(cr_, text_x_, underline_y); | |
244 cairo_line_to(cr_, text_x_ + text_width_, underline_y); | |
245 cairo_stroke(cr_); | |
246 } | |
247 cairo_restore(cr_); | |
248 | |
249 g_object_unref(layout_); | |
250 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. | |
251 } | |
252 | |
253 void DrawStringContext::Draw(const SkColor& text_color) { | |
254 cairo_set_source_rgba(cr_, | |
255 SkColorGetR(text_color) / 255.0, | |
256 SkColorGetG(text_color) / 255.0, | |
257 SkColorGetB(text_color) / 255.0, | |
258 SkColorGetA(text_color) / 255.0); | |
259 cairo_move_to(cr_, text_x_, text_y_); | |
260 pango_cairo_show_layout(cr_, layout_); | |
261 } | |
262 | |
263 void DrawStringContext::DrawWithHalo(const SkColor& text_color, | |
264 const SkColor& halo_color) { | |
265 gfx::CanvasSkia text_canvas(bounds_.width() + 2, bounds_.height() + 2, false); | |
266 text_canvas.FillRectInt(static_cast<SkColor>(0), | |
267 0, 0, bounds_.width() + 2, bounds_.height() + 2); | |
268 | |
269 cairo_t* text_cr = text_canvas.beginPlatformPaint(); | |
270 | |
271 cairo_move_to(text_cr, 1, 1); | |
272 pango_cairo_layout_path(text_cr, layout_); | |
273 | |
274 cairo_set_source_rgba(text_cr, | |
275 SkColorGetR(halo_color) / 255.0, | |
276 SkColorGetG(halo_color) / 255.0, | |
277 SkColorGetB(halo_color) / 255.0, | |
278 SkColorGetA(halo_color) / 255.0); | |
279 cairo_set_line_width(text_cr, 2.0); | |
280 cairo_set_line_join(text_cr, CAIRO_LINE_JOIN_ROUND); | |
281 cairo_stroke_preserve(text_cr); | |
282 | |
283 cairo_set_operator(text_cr, CAIRO_OPERATOR_SOURCE); | |
284 cairo_set_source_rgba(text_cr, | |
285 SkColorGetR(text_color) / 255.0, | |
286 SkColorGetG(text_color) / 255.0, | |
287 SkColorGetB(text_color) / 255.0, | |
288 SkColorGetA(text_color) / 255.0); | |
289 cairo_fill(text_cr); | |
290 | |
291 text_canvas.endPlatformPaint(); | |
292 | |
293 const SkBitmap& text_bitmap = const_cast<SkBitmap&>( | |
294 text_canvas.getTopPlatformDevice().accessBitmap(false)); | |
295 canvas_->DrawBitmapInt(text_bitmap, text_x_ - 1, text_y_ - 1); | |
296 } | |
297 | |
298 } // namespace | |
299 | |
300 namespace gfx { | |
301 | |
302 CanvasSkia::CanvasSkia(int width, int height, bool is_opaque) | |
303 : skia::PlatformCanvas(width, height, is_opaque) { | |
304 } | |
305 | |
306 CanvasSkia::CanvasSkia() : skia::PlatformCanvas() { | |
307 } | |
308 | |
309 CanvasSkia::~CanvasSkia() { | |
310 } | |
311 | |
312 // static | |
313 void CanvasSkia::SizeStringInt(const string16& text, | |
314 const gfx::Font& font, | |
315 int* width, int* height, | |
316 int flags) { | |
317 int org_width = *width; | |
318 cairo_surface_t* surface = | |
319 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); | |
320 cairo_t* cr = cairo_create(surface); | |
321 PangoLayout* layout = pango_cairo_create_layout(cr); | |
322 | |
323 SetupPangoLayout(layout, text, font, *width, flags); | |
324 | |
325 pango_layout_get_pixel_size(layout, width, height); | |
326 | |
327 if (org_width > 0 && flags & Canvas::MULTI_LINE && | |
328 pango_layout_is_wrapped(layout)) { | |
329 // The text wrapped. There seems to be a bug in Pango when this happens | |
330 // such that the width returned from pango_layout_get_pixel_size is too | |
331 // small. Using the width from pango_layout_get_pixel_size in this case | |
332 // results in wrapping across more lines, which requires a bigger height. | |
333 // As a workaround we use the original width, which is not necessarily | |
334 // exactly correct, but isn't wrong by much. | |
335 // | |
336 // It looks like Pango uses the size of whitespace in calculating wrapping | |
337 // but doesn't include the size of the whitespace when the extents are | |
338 // asked for. See the loop in pango-layout.c process_item that determines | |
339 // where to wrap. | |
340 *width = org_width; | |
341 } | |
342 | |
343 g_object_unref(layout); | |
344 cairo_destroy(cr); | |
345 cairo_surface_destroy(surface); | |
346 } | |
347 | |
348 void CanvasSkia::DrawStringWithHalo(const string16& text, | |
349 const gfx::Font& font, | |
350 const SkColor& text_color, | |
351 const SkColor& halo_color, | |
352 int x, int y, int w, int h, | |
353 int flags) { | |
354 if (w <= 0 || h <= 0) | |
355 return; | |
356 | |
357 gfx::Rect bounds(x, y, w, h); | |
358 gfx::Rect clip(x - 1, y - 1, w + 2, h + 2); // Bigger clip for halo | |
359 DrawStringContext context(this, text, font, bounds, clip,flags); | |
360 context.DrawWithHalo(text_color, halo_color); | |
361 } | |
362 | |
363 void CanvasSkia::DrawStringInt(const string16& text, | |
364 const gfx::Font& font, | |
365 const SkColor& color, | |
366 int x, int y, int w, int h, | |
367 int flags) { | |
368 if (w <= 0 || h <= 0) | |
369 return; | |
370 | |
371 gfx::Rect bounds(x, y, w, h); | |
372 DrawStringContext context(this, text, font, bounds, bounds, flags); | |
373 context.Draw(color); | |
374 } | |
375 | |
376 void CanvasSkia::DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y) { | |
377 if (!pixbuf) { | |
378 NOTREACHED(); | |
379 return; | |
380 } | |
381 | |
382 cairo_t* cr = beginPlatformPaint(); | |
383 gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y); | |
384 cairo_paint(cr); | |
385 } | |
386 | |
387 } // namespace gfx | |
OLD | NEW |