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.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/rect.h" | |
17 | |
18 namespace { | |
19 | |
20 const gunichar kAcceleratorChar = '&'; | |
21 | |
22 // Font settings that we initialize once and then use when drawing text in | |
23 // DrawStringInt(). | |
24 static cairo_font_options_t* cairo_font_options = NULL; | |
25 | |
26 // Update |cairo_font_options| based on GtkSettings, allocating it if needed. | |
27 static void UpdateCairoFontOptions() { | |
28 if (!cairo_font_options) | |
29 cairo_font_options = cairo_font_options_create(); | |
30 | |
31 GtkSettings* gtk_settings = gtk_settings_get_default(); | |
32 gint antialias = 0; | |
33 gint hinting = 0; | |
34 gchar* hint_style = NULL; | |
35 gchar* rgba_style = NULL; | |
36 g_object_get(gtk_settings, | |
37 "gtk-xft-antialias", &antialias, | |
38 "gtk-xft-hinting", &hinting, | |
39 "gtk-xft-hintstyle", &hint_style, | |
40 "gtk-xft-rgba", &rgba_style, | |
41 NULL); | |
42 | |
43 // g_object_get() doesn't tell us whether the properties were present or not, | |
44 // but if they aren't (because gnome-settings-daemon isn't running), we'll get | |
45 // NULL values for the strings. | |
46 if (hint_style && rgba_style) { | |
47 if (!antialias) { | |
48 cairo_font_options_set_antialias(cairo_font_options, | |
49 CAIRO_ANTIALIAS_NONE); | |
50 } else if (strcmp(rgba_style, "none") == 0) { | |
51 cairo_font_options_set_antialias(cairo_font_options, | |
52 CAIRO_ANTIALIAS_GRAY); | |
53 } else { | |
54 cairo_font_options_set_antialias(cairo_font_options, | |
55 CAIRO_ANTIALIAS_SUBPIXEL); | |
56 cairo_subpixel_order_t cairo_subpixel_order = | |
57 CAIRO_SUBPIXEL_ORDER_DEFAULT; | |
58 if (strcmp(rgba_style, "rgb") == 0) { | |
59 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; | |
60 } else if (strcmp(rgba_style, "bgr") == 0) { | |
61 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; | |
62 } else if (strcmp(rgba_style, "vrgb") == 0) { | |
63 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; | |
64 } else if (strcmp(rgba_style, "vbgr") == 0) { | |
65 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; | |
66 } | |
67 cairo_font_options_set_subpixel_order(cairo_font_options, | |
68 cairo_subpixel_order); | |
69 } | |
70 | |
71 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; | |
72 if (hinting == 0 || strcmp(hint_style, "hintnone") == 0) { | |
73 cairo_hint_style = CAIRO_HINT_STYLE_NONE; | |
74 } else if (strcmp(hint_style, "hintslight") == 0) { | |
75 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; | |
76 } else if (strcmp(hint_style, "hintmedium") == 0) { | |
77 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; | |
78 } else if (strcmp(hint_style, "hintfull") == 0) { | |
79 cairo_hint_style = CAIRO_HINT_STYLE_FULL; | |
80 } | |
81 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); | |
82 } | |
83 | |
84 if (hint_style) | |
85 g_free(hint_style); | |
86 if (rgba_style) | |
87 g_free(rgba_style); | |
88 } | |
89 | |
90 } // namespace | |
91 | |
92 namespace gfx { | |
93 | |
94 Canvas::Canvas(int width, int height, bool is_opaque) | |
95 : skia::PlatformCanvas(width, height, is_opaque) { | |
96 } | |
97 | |
98 Canvas::Canvas() : skia::PlatformCanvas() { | |
99 } | |
100 | |
101 Canvas::~Canvas() { | |
102 } | |
103 | |
104 // Pass a width > 0 to force wrapping and elliding. | |
105 static void SetupPangoLayout(PangoLayout* layout, | |
106 const std::wstring& text, | |
107 const gfx::Font& font, | |
108 int width, | |
109 int flags) { | |
110 if (!cairo_font_options) | |
111 UpdateCairoFontOptions(); | |
112 // This needs to be done early on; it has no effect when called just before | |
113 // pango_cairo_show_layout(). | |
114 pango_cairo_context_set_font_options( | |
115 pango_layout_get_context(layout), cairo_font_options); | |
116 | |
117 // Callers of DrawStringInt handle RTL layout themselves, so tell pango to not | |
118 // scope out RTL characters. | |
119 pango_layout_set_auto_dir(layout, FALSE); | |
120 | |
121 if (width > 0) | |
122 pango_layout_set_width(layout, width * PANGO_SCALE); | |
123 | |
124 if (flags & Canvas::NO_ELLIPSIS) { | |
125 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | |
126 } else { | |
127 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | |
128 } | |
129 | |
130 if (flags & Canvas::TEXT_ALIGN_CENTER) { | |
131 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | |
132 } else if (flags & Canvas::TEXT_ALIGN_RIGHT) { | |
133 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | |
134 } | |
135 | |
136 if (flags & Canvas::MULTI_LINE) { | |
137 pango_layout_set_wrap(layout, | |
138 (flags & Canvas::CHARACTER_BREAK) ? | |
139 PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); | |
140 } | |
141 | |
142 // Set the resolution to match that used by Gtk. If we don't set the | |
143 // resolution and the resolution differs from the default, Gtk and Chrome end | |
144 // up drawing at different sizes. | |
145 double resolution = gfx::GetPangoResolution(); | |
146 if (resolution > 0) { | |
147 pango_cairo_context_set_resolution(pango_layout_get_context(layout), | |
148 resolution); | |
149 } | |
150 | |
151 PangoFontDescription* desc = gfx::Font::PangoFontFromGfxFont(font); | |
152 pango_layout_set_font_description(layout, desc); | |
153 pango_font_description_free(desc); | |
154 | |
155 // Set text and accelerator character if needed. | |
156 std::string utf8 = WideToUTF8(text); | |
157 if (flags & gfx::Canvas::HIDE_PREFIX) { | |
158 // Escape the text string to be used as markup. | |
159 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); | |
160 pango_layout_set_markup_with_accel(layout, | |
161 escaped_text, | |
162 strlen(escaped_text), | |
163 kAcceleratorChar, NULL); | |
164 g_free(escaped_text); | |
165 } else { | |
166 pango_layout_set_text(layout, utf8.data(), utf8.size()); | |
167 } | |
168 } | |
169 | |
170 // static | |
171 void Canvas::SizeStringInt(const std::wstring& text, | |
172 const gfx::Font& font, | |
173 int* width, int* height, int flags) { | |
174 int org_width = *width; | |
175 cairo_surface_t* surface = | |
176 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); | |
177 cairo_t* cr = cairo_create(surface); | |
178 PangoLayout* layout = pango_cairo_create_layout(cr); | |
179 | |
180 SetupPangoLayout(layout, text, font, *width, flags); | |
181 | |
182 pango_layout_get_pixel_size(layout, width, height); | |
183 | |
184 if (org_width > 0 && flags & Canvas::MULTI_LINE && | |
185 pango_layout_is_wrapped(layout)) { | |
186 // The text wrapped. There seems to be a bug in Pango when this happens | |
187 // such that the width returned from pango_layout_get_pixel_size is too | |
188 // small. Using the width from pango_layout_get_pixel_size in this case | |
189 // results in wrapping across more lines, which requires a bigger height. | |
190 // As a workaround we use the original width, which is not necessarily | |
191 // exactly correct, but isn't wrong by much. | |
192 // | |
193 // It looks like Pango uses the size of whitespace in calculating wrapping | |
194 // but doesn't include the size of the whitespace when the extents are | |
195 // asked for. See the loop in pango-layout.c process_item that determines | |
196 // where to wrap. | |
197 *width = org_width; | |
198 } | |
199 | |
200 g_object_unref(layout); | |
201 cairo_destroy(cr); | |
202 cairo_surface_destroy(surface); | |
203 } | |
204 | |
205 void Canvas::DrawStringInt(const std::wstring& text, | |
206 const gfx::Font& font, | |
207 const SkColor& color, | |
208 int x, int y, int w, int h, | |
209 int flags) { | |
210 if (w <= 0 || h <= 0) | |
211 return; | |
212 | |
213 cairo_t* cr = beginPlatformPaint(); | |
214 PangoLayout* layout = pango_cairo_create_layout(cr); | |
215 | |
216 SetupPangoLayout(layout, text, font, w, flags); | |
217 | |
218 pango_layout_set_height(layout, h * PANGO_SCALE); | |
219 | |
220 cairo_save(cr); | |
221 cairo_set_source_rgba(cr, | |
222 SkColorGetR(color) / 255.0, | |
223 SkColorGetG(color) / 255.0, | |
224 SkColorGetB(color) / 255.0, | |
225 SkColorGetA(color) / 255.0); | |
226 | |
227 int width, height; | |
228 pango_layout_get_pixel_size(layout, &width, &height); | |
229 | |
230 cairo_rectangle(cr, x, y, w, h); | |
231 cairo_clip(cr); | |
232 | |
233 if (flags & Canvas::TEXT_VALIGN_TOP) { | |
234 // Cairo should draw from the top left corner already. | |
235 } else if (flags & Canvas::TEXT_VALIGN_BOTTOM) { | |
236 y += (h - height); | |
237 } else { | |
238 // Vertically centered. | |
239 y += ((h - height) / 2); | |
240 } | |
241 | |
242 cairo_move_to(cr, x, y); | |
243 pango_cairo_show_layout(cr, layout); | |
244 if (font.style() & gfx::Font::UNDERLINED) { | |
245 double underline_y = | |
246 static_cast<double>(y) + height + font.underline_position(); | |
247 cairo_set_line_width(cr, font.underline_thickness()); | |
248 cairo_move_to(cr, x, underline_y); | |
249 cairo_line_to(cr, x + width, underline_y); | |
250 cairo_stroke(cr); | |
251 } | |
252 cairo_restore(cr); | |
253 | |
254 g_object_unref(layout); | |
255 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. | |
256 } | |
257 | |
258 void Canvas::DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y) { | |
259 if (!pixbuf) { | |
260 NOTREACHED(); | |
261 return; | |
262 } | |
263 | |
264 cairo_t* cr = beginPlatformPaint(); | |
265 gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y); | |
266 cairo_paint(cr); | |
267 } | |
268 | |
269 } // namespace gfx | |
OLD | NEW |