Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: app/gfx/canvas_linux.cc

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

Powered by Google App Engine
This is Rietveld 408576698