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

Side by Side Diff: ui/gfx/pango_util.cc

Issue 8392011: GTK: Step 1 of tab strip refresh. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase to ToT again. Created 9 years, 1 month 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/pango_util.h" 5 #include "ui/gfx/pango_util.h"
6 6
7 #include <cairo/cairo.h> 7 #include <cairo/cairo.h>
8 #include <pango/pango.h> 8 #include <pango/pango.h>
9 #include <pango/pangocairo.h> 9 #include <pango/pangocairo.h>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font.h" 14 #include "ui/gfx/font.h"
15 #include "ui/gfx/platform_font_pango.h"
16 #include "ui/gfx/rect.h"
15 17
16 #if !defined(USE_WAYLAND) && defined(TOOLKIT_USES_GTK) 18 #if !defined(USE_WAYLAND) && defined(TOOLKIT_USES_GTK)
17 #include <gtk/gtk.h> 19 #include <gtk/gtk.h>
18 #include "ui/gfx/gtk_util.h" 20 #include "ui/gfx/gtk_util.h"
19 #else 21 #else
20 #include "ui/gfx/linux_util.h" 22 #include "ui/gfx/linux_util.h"
21 #endif 23 #endif
22 24
23 #include "ui/gfx/skia_util.h" 25 #include "ui/gfx/skia_util.h"
24 26
25 namespace { 27 namespace {
26 28
27 // Marker for accelerators in the text. 29 // Marker for accelerators in the text.
28 const gunichar kAcceleratorChar = '&'; 30 const gunichar kAcceleratorChar = '&';
29 31
32 // Multiply by the text height to determine how much text should be faded
33 // when elliding.
34 const double kFadeWidthFactor = 1.5;
35
36 // End state of the elliding fade.
37 const double kFadeFinalAlpha = 0.15;
38
30 // Return |cairo_font_options|. If needed, allocate and update it based on 39 // Return |cairo_font_options|. If needed, allocate and update it based on
31 // GtkSettings. 40 // GtkSettings.
32 cairo_font_options_t* GetCairoFontOptions() { 41 cairo_font_options_t* GetCairoFontOptions() {
33 // Font settings that we initialize once and then use when drawing text. 42 // Font settings that we initialize once and then use when drawing text.
34 static cairo_font_options_t* cairo_font_options = NULL; 43 static cairo_font_options_t* cairo_font_options = NULL;
35 44
36 if (cairo_font_options) 45 if (cairo_font_options)
37 return cairo_font_options; 46 return cairo_font_options;
38 47
39 cairo_font_options = cairo_font_options_create(); 48 cairo_font_options = cairo_font_options_create();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 if (rgba_style) 109 if (rgba_style)
101 g_free(rgba_style); 110 g_free(rgba_style);
102 111
103 return cairo_font_options; 112 return cairo_font_options;
104 } 113 }
105 114
106 } // namespace 115 } // namespace
107 116
108 namespace gfx { 117 namespace gfx {
109 118
119 void DrawTextOntoCairoSurface(cairo_t* cr,
120 const string16& text,
121 const gfx::Font& font,
122 const gfx::Rect& bounds,
123 const gfx::Rect& clip,
124 const SkColor& text_color,
125 int flags) {
126 PangoLayout* layout = pango_cairo_create_layout(cr);
127 base::i18n::TextDirection text_direction =
128 base::i18n::GetFirstStrongCharacterDirection(text);
129 Rect text_rect(bounds.x(), bounds.y(), 0, 0);
130 DCHECK(!bounds.IsEmpty());
131
132 gfx::SetupPangoLayout(
133 layout, text, font, bounds.width(), text_direction, flags);
134
135 pango_layout_set_height(layout, bounds.height() * PANGO_SCALE);
136
137 cairo_save(cr);
138 cairo_rectangle(cr, clip.x(), clip.y(), clip.width(), clip.height());
139 cairo_clip(cr);
140
141 AdjustTextRectBasedOnLayout(layout, bounds, flags, &text_rect);
142
143 DrawPangoLayout(cr, layout, font, bounds, text_rect,
144 text_color, text_direction, flags);
145
146 cairo_restore(cr);
147 g_object_unref(layout);
148 }
149
110 // Pass a width greater than 0 to force wrapping and eliding. 150 // Pass a width greater than 0 to force wrapping and eliding.
111 void SetupPangoLayout(PangoLayout* layout, 151 void SetupPangoLayout(PangoLayout* layout,
112 const string16& text, 152 const string16& text,
113 const Font& font, 153 const Font& font,
114 int width, 154 int width,
115 base::i18n::TextDirection text_direction, 155 base::i18n::TextDirection text_direction,
116 int flags) { 156 int flags) {
117 cairo_font_options_t* cairo_font_options = GetCairoFontOptions(); 157 cairo_font_options_t* cairo_font_options = GetCairoFontOptions();
118 // This needs to be done early on; it has no effect when called just before 158 // This needs to be done early on; it has no effect when called just before
119 // pango_cairo_show_layout(). 159 // pango_cairo_show_layout().
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 NULL, NULL); 226 NULL, NULL);
187 utf8 = UTF16ToUTF8(accelerator_removed); 227 utf8 = UTF16ToUTF8(accelerator_removed);
188 } else { 228 } else {
189 utf8 = UTF16ToUTF8(text); 229 utf8 = UTF16ToUTF8(text);
190 } 230 }
191 231
192 pango_layout_set_text(layout, utf8.data(), utf8.size()); 232 pango_layout_set_text(layout, utf8.data(), utf8.size());
193 } 233 }
194 } 234 }
195 235
236 void AdjustTextRectBasedOnLayout(PangoLayout* layout,
237 const gfx::Rect& bounds,
238 int flags,
239 gfx::Rect* text_rect) {
240 int text_width, text_height;
241 pango_layout_get_pixel_size(layout, &text_width, &text_height);
242 text_rect->set_width(text_width);
243 text_rect->set_height(text_height);
244
245 if (flags & gfx::Canvas::TEXT_VALIGN_TOP) {
246 // Cairo should draw from the top left corner already.
247 } else if (flags & gfx::Canvas::TEXT_VALIGN_BOTTOM) {
248 text_rect->set_y(text_rect->y() + bounds.height() - text_rect->height());
249 } else {
250 // Vertically centered.
251 text_rect->set_y(text_rect->y() +
252 ((bounds.height() - text_rect->height()) / 2));
253 }
254 }
255
256 void DrawPangoLayout(cairo_t* cr,
257 PangoLayout* layout,
258 const Font& font,
259 const gfx::Rect& bounds,
260 const gfx::Rect& text_rect,
261 const SkColor& text_color,
262 base::i18n::TextDirection text_direction,
263 int flags) {
264 double r = SkColorGetR(text_color) / 255.0,
265 g = SkColorGetG(text_color) / 255.0,
266 b = SkColorGetB(text_color) / 255.0,
267 a = SkColorGetA(text_color) / 255.0;
268
269 cairo_pattern_t* pattern = NULL;
270
271 cairo_save(cr);
272
273 // If we're not eliding, use a fixed color.
274 // Otherwise, create a gradient pattern to use as the source.
275 if (text_direction == base::i18n::RIGHT_TO_LEFT ||
276 (flags & gfx::Canvas::NO_ELLIPSIS) ||
277 text_rect.width() <= bounds.width()) {
278 cairo_set_source_rgba(cr, r, g, b, a);
279 } else {
280 // Fade to semi-transparent to elide.
281 int fade_width = static_cast<double>(text_rect.height()) * kFadeWidthFactor;
282 if (fade_width > bounds.width() / 2) {
283 // Don't fade more than half the text.
284 fade_width = bounds.width() / 2;
285 }
286 int fade_x = bounds.x() + bounds.width() - fade_width;
287
288 pattern = cairo_pattern_create_linear(
289 fade_x, bounds.y(), bounds.x() + bounds.width(), bounds.y());
290 cairo_pattern_add_color_stop_rgba(pattern, 0, r, g, b, a);
291 cairo_pattern_add_color_stop_rgba(pattern, 1, r, g, b, kFadeFinalAlpha);
292 cairo_set_source(cr, pattern);
293 }
294
295 cairo_move_to(cr, text_rect.x(), text_rect.y());
296 pango_cairo_show_layout(cr, layout);
297
298 if (font.GetStyle() & gfx::Font::UNDERLINED) {
299 gfx::PlatformFontPango* platform_font =
300 static_cast<gfx::PlatformFontPango*>(font.platform_font());
301 DrawPangoTextUnderline(cr, platform_font, 0.0, text_rect);
302 }
303
304 if (pattern)
305 cairo_pattern_destroy(pattern);
306
307 cairo_restore(cr);
308 }
309
310 void DrawPangoTextUnderline(cairo_t* cr,
311 gfx::PlatformFontPango* platform_font,
312 double extra_edge_width,
313 const Rect& text_rect) {
314 const double underline_y =
315 static_cast<double>(text_rect.y()) + text_rect.height() +
316 platform_font->underline_position();
317 cairo_set_line_width(
318 cr, platform_font->underline_thickness() + 2 * extra_edge_width);
319 cairo_move_to(cr,
320 text_rect.x() - extra_edge_width,
321 underline_y);
322 cairo_line_to(cr,
323 text_rect.x() + text_rect.width() + extra_edge_width,
324 underline_y);
325 cairo_stroke(cr);
326 }
327
196 } // namespace gfx 328 } // namespace gfx
OLDNEW
« chrome/browser/ui/gtk/tabs/dragged_view_gtk.cc ('K') | « ui/gfx/pango_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698