OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include <string> | 10 #include <string> |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <map> | 13 #include <map> |
14 | 14 |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
18 #include "ui/gfx/font_list.h" | 18 #include "ui/gfx/font_list.h" |
19 #include "ui/gfx/font_render_params.h" | 19 #include "ui/gfx/font_render_params.h" |
20 #include "ui/gfx/linux_font_delegate.h" | |
20 #include "ui/gfx/platform_font_pango.h" | 21 #include "ui/gfx/platform_font_pango.h" |
21 #include "ui/gfx/text_utils.h" | 22 #include "ui/gfx/text_utils.h" |
22 | 23 |
23 namespace gfx { | 24 namespace gfx { |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // Marker for accelerators in the text. | 28 // Marker for accelerators in the text. |
28 const gunichar kAcceleratorChar = '&'; | 29 const gunichar kAcceleratorChar = '&'; |
29 | 30 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 else | 78 else |
78 NOTREACHED() << "Unhandled hinting style " << params.hinting; | 79 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_style(cairo_font_options, cairo_hint_style); |
80 cairo_font_options_set_hint_metrics(cairo_font_options, | 81 cairo_font_options_set_hint_metrics(cairo_font_options, |
81 CAIRO_HINT_METRICS_ON); | 82 CAIRO_HINT_METRICS_ON); |
82 } | 83 } |
83 | 84 |
84 return cairo_font_options; | 85 return cairo_font_options; |
85 } | 86 } |
86 | 87 |
87 // Returns the resolution (DPI) used by pango. A negative value means the | 88 // Returns the DPI that should be used by Pango. |
88 // resolution hasn't been set. | 89 double GetPangoDPI() { |
89 double GetPangoResolution() { | 90 static double dpi = -1.0; |
90 static double resolution; | 91 if (dpi < 0.0) { |
91 static bool determined_resolution = false; | 92 const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance(); |
92 if (!determined_resolution) { | 93 if (delegate) |
93 determined_resolution = true; | 94 dpi = delegate->GetFontDPI(); |
94 PangoContext* default_context = GetPangoContext(); | 95 if (dpi <= 0.0) |
95 resolution = pango_cairo_context_get_resolution(default_context); | 96 dpi = 96.0; |
96 g_object_unref(default_context); | |
97 } | 97 } |
98 return resolution; | 98 return dpi; |
99 } | 99 } |
100 | 100 |
101 // Returns the number of pixels in a point. | 101 // Returns the number of pixels in a point. |
102 // - multiply a point size by this to get pixels ("device units") | 102 // - multiply a point size by this to get pixels ("device units") |
103 // - divide a pixel size by this to get points | 103 // - divide a pixel size by this to get points |
104 float GetPixelsInPoint() { | 104 double GetPixelsInPoint() { |
105 static float pixels_in_point = 1.0; | 105 static double pixels_in_point = -1.0; |
106 static bool determined_value = false; | 106 if (pixels_in_point < 0.0) |
107 | 107 pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch |
msw
2014/07/18 05:02:33
nit: I guess you can inline the init with the decl
Daniel Erat
2014/07/18 14:27:04
good point. thanks, done
| |
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; | |
117 } | |
118 | |
119 return pixels_in_point; | 108 return pixels_in_point; |
120 } | 109 } |
121 | 110 |
122 } // namespace | 111 } // namespace |
123 | 112 |
124 void SetUpPangoLayout( | 113 void SetUpPangoLayout( |
125 PangoLayout* layout, | 114 PangoLayout* layout, |
126 const base::string16& text, | 115 const base::string16& text, |
127 const FontList& font_list, | 116 const FontList& font_list, |
128 base::i18n::TextDirection text_direction, | 117 base::i18n::TextDirection text_direction, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 } | 155 } |
167 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) { | 156 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) { |
168 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | 157 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); |
169 } else { | 158 } else { |
170 // Fading the text will be handled in the draw operation. | 159 // Fading the text will be handled in the draw operation. |
171 // Ensure that the text is only on one line. | 160 // Ensure that the text is only on one line. |
172 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | 161 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
173 pango_layout_set_width(layout, -1); | 162 pango_layout_set_width(layout, -1); |
174 } | 163 } |
175 | 164 |
176 // Set the resolution to match that used by Gtk. If we don't set the | 165 // Set the layout's resolution to match the resolution used to convert from |
177 // resolution and the resolution differs from the default, Gtk and Chrome end | 166 // points to pixels. |
178 // up drawing at different sizes. | 167 pango_cairo_context_set_resolution(pango_layout_get_context(layout), |
179 double resolution = GetPangoResolution(); | 168 GetPangoDPI()); |
180 if (resolution > 0) { | |
181 pango_cairo_context_set_resolution(pango_layout_get_context(layout), | |
182 resolution); | |
183 } | |
184 | 169 |
185 // Set text and accelerator character if needed. | 170 // Set text and accelerator character if needed. |
186 if (flags & Canvas::SHOW_PREFIX) { | 171 if (flags & Canvas::SHOW_PREFIX) { |
187 // Escape the text string to be used as markup. | 172 // Escape the text string to be used as markup. |
188 std::string utf8 = base::UTF16ToUTF8(text); | 173 std::string utf8 = base::UTF16ToUTF8(text); |
189 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); | 174 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); |
190 pango_layout_set_markup_with_accel(layout, | 175 pango_layout_set_markup_with_accel(layout, |
191 escaped_text, | 176 escaped_text, |
192 strlen(escaped_text), | 177 strlen(escaped_text), |
193 kAcceleratorChar, NULL); | 178 kAcceleratorChar, NULL); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 | 230 |
246 if (i == desc_to_metrics->end()) { | 231 if (i == desc_to_metrics->end()) { |
247 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); | 232 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); |
248 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); | 233 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); |
249 return metrics; | 234 return metrics; |
250 } | 235 } |
251 return i->second; | 236 return i->second; |
252 } | 237 } |
253 | 238 |
254 } // namespace gfx | 239 } // namespace gfx |
OLD | NEW |