| 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 <fontconfig/fontconfig.h> | |
| 9 #include <pango/pango.h> | 8 #include <pango/pango.h> |
| 10 #include <pango/pangocairo.h> | 9 #include <pango/pangocairo.h> |
| 11 #include <string> | 10 #include <string> |
| 12 | 11 |
| 13 #include <algorithm> | 12 #include <algorithm> |
| 14 #include <map> | 13 #include <map> |
| 15 #include <vector> | |
| 16 | 14 |
| 17 #include "base/logging.h" | 15 #include "base/logging.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 19 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/font.h" | 18 #include "ui/gfx/font_list.h" |
| 21 #include "ui/gfx/font_render_params_linux.h" | 19 #include "ui/gfx/font_render_params_linux.h" |
| 22 #include "ui/gfx/platform_font_pango.h" | |
| 23 #include "ui/gfx/rect.h" | |
| 24 #include "ui/gfx/text_utils.h" | 20 #include "ui/gfx/text_utils.h" |
| 25 | 21 |
| 26 namespace gfx { | 22 namespace gfx { |
| 27 | 23 |
| 28 namespace { | 24 namespace { |
| 29 | 25 |
| 30 // Marker for accelerators in the text. | 26 // Marker for accelerators in the text. |
| 31 const gunichar kAcceleratorChar = '&'; | 27 const gunichar kAcceleratorChar = '&'; |
| 32 | 28 |
| 33 // Return |cairo_font_options|. If needed, allocate and update it. | 29 // Creates and returns a PangoContext. The caller owns the context. |
| 30 PangoContext* GetPangoContext() { |
| 31 PangoFontMap* font_map = pango_cairo_font_map_get_default(); |
| 32 return pango_font_map_create_context(font_map); |
| 33 } |
| 34 |
| 35 // Returns a static cairo_font_options_t. If needed, allocates and updates it. |
| 34 // TODO(derat): Return font-specific options: http://crbug.com/125235 | 36 // TODO(derat): Return font-specific options: http://crbug.com/125235 |
| 35 cairo_font_options_t* GetCairoFontOptions() { | 37 cairo_font_options_t* GetCairoFontOptions() { |
| 36 // Font settings that we initialize once and then use when drawing text. | 38 // Font settings that we initialize once and then use when drawing text. |
| 37 static cairo_font_options_t* cairo_font_options = NULL; | 39 static cairo_font_options_t* cairo_font_options = NULL; |
| 38 if (cairo_font_options) | 40 if (cairo_font_options) |
| 39 return cairo_font_options; | 41 return cairo_font_options; |
| 40 | 42 |
| 41 cairo_font_options = cairo_font_options_create(); | 43 cairo_font_options = cairo_font_options_create(); |
| 42 | 44 |
| 43 const FontRenderParams& params = GetDefaultFontRenderParams(); | 45 const FontRenderParams& params = GetDefaultFontRenderParams(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 else | 83 else |
| 82 NOTREACHED() << "Unhandled hinting style " << params.hinting; | 84 NOTREACHED() << "Unhandled hinting style " << params.hinting; |
| 83 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); | 85 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); |
| 84 cairo_font_options_set_hint_metrics(cairo_font_options, | 86 cairo_font_options_set_hint_metrics(cairo_font_options, |
| 85 CAIRO_HINT_METRICS_ON); | 87 CAIRO_HINT_METRICS_ON); |
| 86 } | 88 } |
| 87 | 89 |
| 88 return cairo_font_options; | 90 return cairo_font_options; |
| 89 } | 91 } |
| 90 | 92 |
| 93 // Returns the resolution (DPI) used by pango. A negative value means the |
| 94 // resolution hasn't been set. |
| 95 double GetPangoResolution() { |
| 96 static double resolution; |
| 97 static bool determined_resolution = false; |
| 98 if (!determined_resolution) { |
| 99 determined_resolution = true; |
| 100 PangoContext* default_context = GetPangoContext(); |
| 101 resolution = pango_cairo_context_get_resolution(default_context); |
| 102 g_object_unref(default_context); |
| 103 } |
| 104 return resolution; |
| 105 } |
| 106 |
| 91 // Returns the number of pixels in a point. | 107 // Returns the number of pixels in a point. |
| 92 // - multiply a point size by this to get pixels ("device units") | 108 // - multiply a point size by this to get pixels ("device units") |
| 93 // - divide a pixel size by this to get points | 109 // - divide a pixel size by this to get points |
| 94 float GetPixelsInPoint() { | 110 float GetPixelsInPoint() { |
| 95 static float pixels_in_point = 1.0; | 111 static float pixels_in_point = 1.0; |
| 96 static bool determined_value = false; | 112 static bool determined_value = false; |
| 97 | 113 |
| 98 if (!determined_value) { | 114 if (!determined_value) { |
| 99 // http://goo.gl/UIh5m: "This is a scale factor between points specified in | 115 // http://goo.gl/UIh5m: "This is a scale factor between points specified in |
| 100 // a PangoFontDescription and Cairo units. The default value is 96, meaning | 116 // a PangoFontDescription and Cairo units. The default value is 96, meaning |
| 101 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)." | 117 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)." |
| 102 double pango_dpi = GetPangoResolution(); | 118 double pango_dpi = GetPangoResolution(); |
| 103 if (pango_dpi <= 0) | 119 if (pango_dpi <= 0) |
| 104 pango_dpi = 96.0; | 120 pango_dpi = 96.0; |
| 105 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch | 121 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch |
| 106 determined_value = true; | 122 determined_value = true; |
| 107 } | 123 } |
| 108 | 124 |
| 109 return pixels_in_point; | 125 return pixels_in_point; |
| 110 } | 126 } |
| 111 | 127 |
| 112 } // namespace | 128 } // namespace |
| 113 | 129 |
| 114 PangoContext* GetPangoContext() { | 130 void SetUpPangoLayout( |
| 115 PangoFontMap* font_map = pango_cairo_font_map_get_default(); | |
| 116 return pango_font_map_create_context(font_map); | |
| 117 } | |
| 118 | |
| 119 double GetPangoResolution() { | |
| 120 static double resolution; | |
| 121 static bool determined_resolution = false; | |
| 122 if (!determined_resolution) { | |
| 123 determined_resolution = true; | |
| 124 PangoContext* default_context = GetPangoContext(); | |
| 125 resolution = pango_cairo_context_get_resolution(default_context); | |
| 126 g_object_unref(default_context); | |
| 127 } | |
| 128 return resolution; | |
| 129 } | |
| 130 | |
| 131 // Pass a width greater than 0 to force wrapping and eliding. | |
| 132 static void SetupPangoLayoutWithoutFont( | |
| 133 PangoLayout* layout, | 131 PangoLayout* layout, |
| 134 const base::string16& text, | 132 const base::string16& text, |
| 135 int width, | 133 const FontList& font_list, |
| 136 base::i18n::TextDirection text_direction, | 134 base::i18n::TextDirection text_direction, |
| 137 int flags) { | 135 int flags) { |
| 136 // TODO(derat): Use rendering parameters from |font_list| instead of defaults. |
| 138 cairo_font_options_t* cairo_font_options = GetCairoFontOptions(); | 137 cairo_font_options_t* cairo_font_options = GetCairoFontOptions(); |
| 139 | 138 |
| 140 // If we got an explicit request to turn off subpixel rendering, disable it on | 139 // If we got an explicit request to turn off subpixel rendering, disable it on |
| 141 // a copy of the static font options object. | 140 // a copy of the static font options object. |
| 142 bool copied_cairo_font_options = false; | 141 bool copied_cairo_font_options = false; |
| 143 if ((flags & Canvas::NO_SUBPIXEL_RENDERING) && | 142 if ((flags & Canvas::NO_SUBPIXEL_RENDERING) && |
| 144 (cairo_font_options_get_antialias(cairo_font_options) == | 143 (cairo_font_options_get_antialias(cairo_font_options) == |
| 145 CAIRO_ANTIALIAS_SUBPIXEL)) { | 144 CAIRO_ANTIALIAS_SUBPIXEL)) { |
| 146 cairo_font_options = cairo_font_options_copy(cairo_font_options); | 145 cairo_font_options = cairo_font_options_copy(cairo_font_options); |
| 147 copied_cairo_font_options = true; | 146 copied_cairo_font_options = true; |
| 148 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); | 147 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); |
| 149 } | 148 } |
| 150 | 149 |
| 151 // This needs to be done early on; it has no effect when called just before | 150 // This needs to be done early on; it has no effect when called just before |
| 152 // pango_cairo_show_layout(). | 151 // pango_cairo_show_layout(). |
| 153 pango_cairo_context_set_font_options( | 152 pango_cairo_context_set_font_options( |
| 154 pango_layout_get_context(layout), cairo_font_options); | 153 pango_layout_get_context(layout), cairo_font_options); |
| 155 | 154 |
| 156 if (copied_cairo_font_options) { | 155 if (copied_cairo_font_options) { |
| 157 cairo_font_options_destroy(cairo_font_options); | 156 cairo_font_options_destroy(cairo_font_options); |
| 158 cairo_font_options = NULL; | 157 cairo_font_options = NULL; |
| 159 } | 158 } |
| 160 | 159 |
| 161 // Set Pango's base text direction explicitly from |text_direction|. | 160 // Set Pango's base text direction explicitly from |text_direction|. |
| 162 pango_layout_set_auto_dir(layout, FALSE); | 161 pango_layout_set_auto_dir(layout, FALSE); |
| 163 pango_context_set_base_dir(pango_layout_get_context(layout), | 162 pango_context_set_base_dir(pango_layout_get_context(layout), |
| 164 (text_direction == base::i18n::RIGHT_TO_LEFT ? | 163 (text_direction == base::i18n::RIGHT_TO_LEFT ? |
| 165 PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR)); | 164 PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR)); |
| 166 | 165 |
| 167 if (width > 0) | |
| 168 pango_layout_set_width(layout, width * PANGO_SCALE); | |
| 169 | |
| 170 if (flags & Canvas::TEXT_ALIGN_CENTER) { | 166 if (flags & Canvas::TEXT_ALIGN_CENTER) { |
| 171 // We don't support center aligned w/ eliding. | 167 // We don't support center aligned w/ eliding. |
| 172 DCHECK(gfx::Canvas::NO_ELLIPSIS); | 168 DCHECK(gfx::Canvas::NO_ELLIPSIS); |
| 173 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | 169 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); |
| 174 } else if (flags & Canvas::TEXT_ALIGN_RIGHT) { | 170 } else if (flags & Canvas::TEXT_ALIGN_RIGHT) { |
| 175 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | 171 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); |
| 176 } | 172 } |
| 177 | 173 |
| 178 if (flags & Canvas::NO_ELLIPSIS) { | 174 if (flags & Canvas::NO_ELLIPSIS) { |
| 179 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | 175 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 RemoveAcceleratorChar(text, | 217 RemoveAcceleratorChar(text, |
| 222 static_cast<base::char16>(kAcceleratorChar), | 218 static_cast<base::char16>(kAcceleratorChar), |
| 223 NULL, NULL); | 219 NULL, NULL); |
| 224 utf8 = base::UTF16ToUTF8(accelerator_removed); | 220 utf8 = base::UTF16ToUTF8(accelerator_removed); |
| 225 } else { | 221 } else { |
| 226 utf8 = base::UTF16ToUTF8(text); | 222 utf8 = base::UTF16ToUTF8(text); |
| 227 } | 223 } |
| 228 | 224 |
| 229 pango_layout_set_text(layout, utf8.data(), utf8.size()); | 225 pango_layout_set_text(layout, utf8.data(), utf8.size()); |
| 230 } | 226 } |
| 231 } | |
| 232 | 227 |
| 233 void SetupPangoLayout(PangoLayout* layout, | 228 ScopedPangoFontDescription desc(pango_font_description_from_string( |
| 234 const base::string16& text, | 229 font_list.GetFontDescriptionString().c_str())); |
| 235 const Font& font, | |
| 236 int width, | |
| 237 base::i18n::TextDirection text_direction, | |
| 238 int flags) { | |
| 239 SetupPangoLayoutWithoutFont(layout, text, width, text_direction, flags); | |
| 240 | |
| 241 ScopedPangoFontDescription desc(font.GetNativeFont()); | |
| 242 pango_layout_set_font_description(layout, desc.get()); | 230 pango_layout_set_font_description(layout, desc.get()); |
| 243 } | 231 } |
| 244 | 232 |
| 245 void SetupPangoLayoutWithFontDescription( | |
| 246 PangoLayout* layout, | |
| 247 const base::string16& text, | |
| 248 const std::string& font_description, | |
| 249 int width, | |
| 250 base::i18n::TextDirection text_direction, | |
| 251 int flags) { | |
| 252 SetupPangoLayoutWithoutFont(layout, text, width, text_direction, flags); | |
| 253 | |
| 254 ScopedPangoFontDescription desc( | |
| 255 pango_font_description_from_string(font_description.c_str())); | |
| 256 pango_layout_set_font_description(layout, desc.get()); | |
| 257 } | |
| 258 | |
| 259 size_t GetPangoFontSizeInPixels(PangoFontDescription* pango_font) { | 233 size_t GetPangoFontSizeInPixels(PangoFontDescription* pango_font) { |
| 260 size_t size_in_pixels = pango_font_description_get_size(pango_font); | 234 size_t size_in_pixels = pango_font_description_get_size(pango_font); |
| 261 if (pango_font_description_get_size_is_absolute(pango_font)) { | 235 if (pango_font_description_get_size_is_absolute(pango_font)) { |
| 262 // If the size is absolute, then it's in Pango units rather than points. | 236 // If the size is absolute, then it's in Pango units rather than points. |
| 263 // There are PANGO_SCALE Pango units in a device unit (pixel). | 237 // There are PANGO_SCALE Pango units in a device unit (pixel). |
| 264 size_in_pixels /= PANGO_SCALE; | 238 size_in_pixels /= PANGO_SCALE; |
| 265 } else { | 239 } else { |
| 266 // Otherwise, we need to convert from points. | 240 // Otherwise, we need to convert from points. |
| 267 size_in_pixels = size_in_pixels * GetPixelsInPoint() / PANGO_SCALE; | 241 size_in_pixels = size_in_pixels * GetPixelsInPoint() / PANGO_SCALE; |
| 268 } | 242 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 287 | 261 |
| 288 if (i == desc_to_metrics->end()) { | 262 if (i == desc_to_metrics->end()) { |
| 289 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); | 263 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); |
| 290 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); | 264 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); |
| 291 return metrics; | 265 return metrics; |
| 292 } | 266 } |
| 293 return i->second; | 267 return i->second; |
| 294 } | 268 } |
| 295 | 269 |
| 296 } // namespace gfx | 270 } // namespace gfx |
| OLD | NEW |