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> |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 else | 78 else |
79 NOTREACHED() << "Unhandled hinting style " << params.hinting; | 79 NOTREACHED() << "Unhandled hinting style " << params.hinting; |
80 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); |
81 cairo_font_options_set_hint_metrics(cairo_font_options, | 81 cairo_font_options_set_hint_metrics(cairo_font_options, |
82 CAIRO_HINT_METRICS_ON); | 82 CAIRO_HINT_METRICS_ON); |
83 } | 83 } |
84 | 84 |
85 return cairo_font_options; | 85 return cairo_font_options; |
86 } | 86 } |
87 | 87 |
88 // Returns the DPI that should be used by Pango. | |
89 double GetPangoDPI() { | |
90 static double dpi = -1.0; | |
91 if (dpi < 0.0) { | |
92 const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance(); | |
93 if (delegate) | |
94 dpi = delegate->GetFontDPI(); | |
95 if (dpi <= 0.0) | |
96 dpi = 96.0; | |
97 } | |
98 return dpi; | |
99 } | |
100 | |
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 double GetPixelsInPoint() { | |
105 static double pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch | |
106 return pixels_in_point; | |
107 } | |
108 | |
109 } // namespace | 88 } // namespace |
110 | 89 |
111 void SetUpPangoLayout( | 90 void SetUpPangoLayout( |
112 PangoLayout* layout, | 91 PangoLayout* layout, |
113 const base::string16& text, | 92 const base::string16& text, |
114 const FontList& font_list, | 93 const FontList& font_list, |
115 base::i18n::TextDirection text_direction, | 94 base::i18n::TextDirection text_direction, |
116 int flags) { | 95 int flags) { |
117 cairo_font_options_t* cairo_font_options = CreateCairoFontOptions( | 96 cairo_font_options_t* cairo_font_options = CreateCairoFontOptions( |
118 font_list.GetPrimaryFont().GetFontRenderParams()); | 97 font_list.GetPrimaryFont().GetFontRenderParams()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | 134 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); |
156 } else { | 135 } else { |
157 // Fading the text will be handled in the draw operation. | 136 // Fading the text will be handled in the draw operation. |
158 // Ensure that the text is only on one line. | 137 // Ensure that the text is only on one line. |
159 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | 138 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
160 pango_layout_set_width(layout, -1); | 139 pango_layout_set_width(layout, -1); |
161 } | 140 } |
162 | 141 |
163 // Set the layout's resolution to match the resolution used to convert from | 142 // Set the layout's resolution to match the resolution used to convert from |
164 // points to pixels. | 143 // points to pixels. |
165 pango_cairo_context_set_resolution(pango_layout_get_context(layout), | 144 pango_cairo_context_set_resolution(pango_layout_get_context(layout), 96.0); |
166 GetPangoDPI()); | |
167 | 145 |
168 // Set text and accelerator character if needed. | 146 // Set text and accelerator character if needed. |
169 if (flags & Canvas::SHOW_PREFIX) { | 147 if (flags & Canvas::SHOW_PREFIX) { |
170 // Escape the text string to be used as markup. | 148 // Escape the text string to be used as markup. |
171 std::string utf8 = base::UTF16ToUTF8(text); | 149 std::string utf8 = base::UTF16ToUTF8(text); |
172 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); | 150 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); |
173 pango_layout_set_markup_with_accel(layout, | 151 pango_layout_set_markup_with_accel(layout, |
174 escaped_text, | 152 escaped_text, |
175 strlen(escaped_text), | 153 strlen(escaped_text), |
176 kAcceleratorChar, NULL); | 154 kAcceleratorChar, NULL); |
(...skipping 22 matching lines...) Expand all Loading... |
199 pango_layout_set_font_description(layout, desc.get()); | 177 pango_layout_set_font_description(layout, desc.get()); |
200 } | 178 } |
201 | 179 |
202 int GetPangoFontSizeInPixels(PangoFontDescription* pango_font) { | 180 int GetPangoFontSizeInPixels(PangoFontDescription* pango_font) { |
203 // If the size is absolute, then it's in Pango units rather than points. There | 181 // If the size is absolute, then it's in Pango units rather than points. There |
204 // are PANGO_SCALE Pango units in a device unit (pixel). | 182 // are PANGO_SCALE Pango units in a device unit (pixel). |
205 if (pango_font_description_get_size_is_absolute(pango_font)) | 183 if (pango_font_description_get_size_is_absolute(pango_font)) |
206 return pango_font_description_get_size(pango_font) / PANGO_SCALE; | 184 return pango_font_description_get_size(pango_font) / PANGO_SCALE; |
207 | 185 |
208 // Otherwise, we need to convert from points. | 186 // Otherwise, we need to convert from points. |
209 return static_cast<int>(GetPixelsInPoint() * | 187 return static_cast<int>((96.0 / 72.0) * |
210 pango_font_description_get_size(pango_font) / PANGO_SCALE + 0.5); | 188 pango_font_description_get_size(pango_font) / PANGO_SCALE + 0.5); |
211 } | 189 } |
212 | 190 |
213 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { | 191 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { |
214 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; | 192 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; |
215 static PangoContext* context = NULL; | 193 static PangoContext* context = NULL; |
216 | 194 |
217 if (!context) { | 195 if (!context) { |
218 context = GetPangoContext(); | 196 context = GetPangoContext(); |
219 pango_context_set_language(context, pango_language_get_default()); | 197 pango_context_set_language(context, pango_language_get_default()); |
220 } | 198 } |
221 | 199 |
222 if (!desc_to_metrics) | 200 if (!desc_to_metrics) |
223 desc_to_metrics = new std::map<int, PangoFontMetrics*>(); | 201 desc_to_metrics = new std::map<int, PangoFontMetrics*>(); |
224 | 202 |
225 const int desc_hash = pango_font_description_hash(desc); | 203 const int desc_hash = pango_font_description_hash(desc); |
226 std::map<int, PangoFontMetrics*>::iterator i = | 204 std::map<int, PangoFontMetrics*>::iterator i = |
227 desc_to_metrics->find(desc_hash); | 205 desc_to_metrics->find(desc_hash); |
228 | 206 |
229 if (i == desc_to_metrics->end()) { | 207 if (i == desc_to_metrics->end()) { |
230 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); | 208 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); |
231 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); | 209 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); |
232 return metrics; | 210 return metrics; |
233 } | 211 } |
234 return i->second; | 212 return i->second; |
235 } | 213 } |
236 | 214 |
237 } // namespace gfx | 215 } // namespace gfx |
OLD | NEW |