[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / pango_util.cc
blobed04fbbff5932e94f4aba33c5dd8cb2ffd722c5f
1 // Copyright (c) 2012 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.
5 #include "ui/gfx/pango_util.h"
7 #include <cairo/cairo.h>
8 #include <pango/pango.h>
9 #include <pango/pangocairo.h>
10 #include <string>
12 #include <algorithm>
13 #include <map>
15 #include "base/logging.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/font_list.h"
19 #include "ui/gfx/font_render_params.h"
20 #include "ui/gfx/platform_font_pango.h"
21 #include "ui/gfx/text_utils.h"
23 namespace gfx {
25 namespace {
27 // Marker for accelerators in the text.
28 const gunichar kAcceleratorChar = '&';
30 // Creates and returns a PangoContext. The caller owns the context.
31 PangoContext* GetPangoContext() {
32 PangoFontMap* font_map = pango_cairo_font_map_get_default();
33 return pango_font_map_create_context(font_map);
36 // Creates a new cairo_font_options_t based on |params|.
37 cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) {
38 cairo_font_options_t* cairo_font_options = cairo_font_options_create();
40 FontRenderParams::SubpixelRendering subpixel = params.subpixel_rendering;
41 if (!params.antialiasing) {
42 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_NONE);
43 } else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_NONE) {
44 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY);
45 } else {
46 cairo_font_options_set_antialias(cairo_font_options,
47 CAIRO_ANTIALIAS_SUBPIXEL);
48 cairo_subpixel_order_t cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
49 if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_RGB)
50 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
51 else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_BGR)
52 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
53 else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_VRGB)
54 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
55 else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_VBGR)
56 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
57 else
58 NOTREACHED() << "Unhandled subpixel rendering type " << subpixel;
59 cairo_font_options_set_subpixel_order(cairo_font_options,
60 cairo_subpixel_order);
63 if (params.hinting == FontRenderParams::HINTING_NONE ||
64 params.subpixel_positioning) {
65 cairo_font_options_set_hint_style(cairo_font_options,
66 CAIRO_HINT_STYLE_NONE);
67 cairo_font_options_set_hint_metrics(cairo_font_options,
68 CAIRO_HINT_METRICS_OFF);
69 } else {
70 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT;
71 if (params.hinting == FontRenderParams::HINTING_SLIGHT)
72 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT;
73 else if (params.hinting == FontRenderParams::HINTING_MEDIUM)
74 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM;
75 else if (params.hinting == FontRenderParams::HINTING_FULL)
76 cairo_hint_style = CAIRO_HINT_STYLE_FULL;
77 else
78 NOTREACHED() << "Unhandled hinting style " << params.hinting;
79 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style);
80 cairo_font_options_set_hint_metrics(cairo_font_options,
81 CAIRO_HINT_METRICS_ON);
84 return cairo_font_options;
87 // Returns the resolution (DPI) used by pango. A negative value means the
88 // resolution hasn't been set.
89 double GetPangoResolution() {
90 static double resolution;
91 static bool determined_resolution = false;
92 if (!determined_resolution) {
93 determined_resolution = true;
94 PangoContext* default_context = GetPangoContext();
95 resolution = pango_cairo_context_get_resolution(default_context);
96 g_object_unref(default_context);
98 return resolution;
101 // Returns the number of pixels in a point.
102 // - multiply a point size by this to get pixels ("device units")
103 // - divide a pixel size by this to get points
104 float GetPixelsInPoint() {
105 static float pixels_in_point = 1.0;
106 static bool determined_value = false;
108 if (!determined_value) {
109 // http://goo.gl/UIh5m: "This is a scale factor between points specified in
110 // a PangoFontDescription and Cairo units. The default value is 96, meaning
111 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)."
112 double pango_dpi = GetPangoResolution();
113 if (pango_dpi <= 0)
114 pango_dpi = 96.0;
115 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch
116 determined_value = true;
119 return pixels_in_point;
122 } // namespace
124 void SetUpPangoLayout(
125 PangoLayout* layout,
126 const base::string16& text,
127 const FontList& font_list,
128 base::i18n::TextDirection text_direction,
129 int flags) {
130 cairo_font_options_t* cairo_font_options = CreateCairoFontOptions(
131 font_list.GetPrimaryFont().GetFontRenderParams());
133 // If we got an explicit request to turn off subpixel rendering, disable it.
134 if ((flags & Canvas::NO_SUBPIXEL_RENDERING) &&
135 (cairo_font_options_get_antialias(cairo_font_options) ==
136 CAIRO_ANTIALIAS_SUBPIXEL))
137 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY);
139 // This needs to be done early on; it has no effect when called just before
140 // pango_cairo_show_layout().
141 pango_cairo_context_set_font_options(
142 pango_layout_get_context(layout), cairo_font_options);
143 cairo_font_options_destroy(cairo_font_options);
144 cairo_font_options = NULL;
146 // Set Pango's base text direction explicitly from |text_direction|.
147 pango_layout_set_auto_dir(layout, FALSE);
148 pango_context_set_base_dir(pango_layout_get_context(layout),
149 (text_direction == base::i18n::RIGHT_TO_LEFT ?
150 PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR));
152 if (flags & Canvas::TEXT_ALIGN_CENTER) {
153 // We don't support center aligned w/ eliding.
154 DCHECK(gfx::Canvas::NO_ELLIPSIS);
155 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
156 } else if (flags & Canvas::TEXT_ALIGN_RIGHT) {
157 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
160 if (flags & Canvas::NO_ELLIPSIS) {
161 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
162 if (flags & Canvas::MULTI_LINE) {
163 pango_layout_set_wrap(layout,
164 (flags & Canvas::CHARACTER_BREAK) ?
165 PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD);
167 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) {
168 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
169 } else {
170 // Fading the text will be handled in the draw operation.
171 // Ensure that the text is only on one line.
172 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
173 pango_layout_set_width(layout, -1);
176 // Set the resolution to match that used by Gtk. If we don't set the
177 // resolution and the resolution differs from the default, Gtk and Chrome end
178 // up drawing at different sizes.
179 double resolution = GetPangoResolution();
180 if (resolution > 0) {
181 pango_cairo_context_set_resolution(pango_layout_get_context(layout),
182 resolution);
185 // Set text and accelerator character if needed.
186 if (flags & Canvas::SHOW_PREFIX) {
187 // Escape the text string to be used as markup.
188 std::string utf8 = base::UTF16ToUTF8(text);
189 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size());
190 pango_layout_set_markup_with_accel(layout,
191 escaped_text,
192 strlen(escaped_text),
193 kAcceleratorChar, NULL);
194 g_free(escaped_text);
195 } else {
196 std::string utf8;
198 // Remove the ampersand character. A double ampersand is output as
199 // a single ampersand.
200 if (flags & Canvas::HIDE_PREFIX) {
201 DCHECK_EQ(1, g_unichar_to_utf8(kAcceleratorChar, NULL));
202 base::string16 accelerator_removed =
203 RemoveAcceleratorChar(text,
204 static_cast<base::char16>(kAcceleratorChar),
205 NULL, NULL);
206 utf8 = base::UTF16ToUTF8(accelerator_removed);
207 } else {
208 utf8 = base::UTF16ToUTF8(text);
211 pango_layout_set_text(layout, utf8.data(), utf8.size());
214 ScopedPangoFontDescription desc(pango_font_description_from_string(
215 font_list.GetFontDescriptionString().c_str()));
216 pango_layout_set_font_description(layout, desc.get());
219 int GetPangoFontSizeInPixels(PangoFontDescription* pango_font) {
220 // If the size is absolute, then it's in Pango units rather than points. There
221 // are PANGO_SCALE Pango units in a device unit (pixel).
222 if (pango_font_description_get_size_is_absolute(pango_font))
223 return pango_font_description_get_size(pango_font) / PANGO_SCALE;
225 // Otherwise, we need to convert from points.
226 return static_cast<int>(GetPixelsInPoint() *
227 pango_font_description_get_size(pango_font) / PANGO_SCALE + 0.5);
230 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) {
231 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL;
232 static PangoContext* context = NULL;
234 if (!context) {
235 context = GetPangoContext();
236 pango_context_set_language(context, pango_language_get_default());
239 if (!desc_to_metrics)
240 desc_to_metrics = new std::map<int, PangoFontMetrics*>();
242 const int desc_hash = pango_font_description_hash(desc);
243 std::map<int, PangoFontMetrics*>::iterator i =
244 desc_to_metrics->find(desc_hash);
246 if (i == desc_to_metrics->end()) {
247 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL);
248 desc_to_metrics->insert(std::make_pair(desc_hash, metrics));
249 return metrics;
251 return i->second;
254 } // namespace gfx