| 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> | 8 #include <fontconfig/fontconfig.h> |
| 9 #include <pango/pango.h> | 9 #include <pango/pango.h> |
| 10 #include <pango/pangocairo.h> | 10 #include <pango/pangocairo.h> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <map> | 14 #include <map> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/font.h" | 20 #include "ui/gfx/font.h" |
| 21 #include "ui/gfx/font_render_params_linux.h" | 21 #include "ui/gfx/font_render_params_linux.h" |
| 22 #include "ui/gfx/platform_font_pango.h" | 22 #include "ui/gfx/platform_font_pango.h" |
| 23 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
| 24 #include "ui/gfx/skia_util.h" | 24 #include "ui/gfx/skia_util.h" |
| 25 | 25 |
| 26 #if defined(TOOLKIT_GTK) | 26 #if defined(TOOLKIT_GTK) |
| 27 #include <gdk/gdk.h> | 27 #include <gdk/gdk.h> |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 namespace gfx { |
| 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 // Marker for accelerators in the text. | 34 // Marker for accelerators in the text. |
| 33 const gunichar kAcceleratorChar = '&'; | 35 const gunichar kAcceleratorChar = '&'; |
| 34 | 36 |
| 35 // Multiply by the text height to determine how much text should be faded | 37 // Multiply by the text height to determine how much text should be faded |
| 36 // when elliding. | 38 // when elliding. |
| 37 const double kFadeWidthFactor = 1.5; | 39 const double kFadeWidthFactor = 1.5; |
| 38 | 40 |
| 39 // End state of the elliding fade. | 41 // End state of the elliding fade. |
| 40 const double kFadeFinalAlpha = 0.15; | 42 const double kFadeFinalAlpha = 0.15; |
| 41 | 43 |
| 42 // Return |cairo_font_options|. If needed, allocate and update it. | 44 // Return |cairo_font_options|. If needed, allocate and update it. |
| 43 // TODO(derat): Return font-specific options: http://crbug.com/125235 | 45 // TODO(derat): Return font-specific options: http://crbug.com/125235 |
| 44 cairo_font_options_t* GetCairoFontOptions() { | 46 cairo_font_options_t* GetCairoFontOptions() { |
| 45 // Font settings that we initialize once and then use when drawing text. | 47 // Font settings that we initialize once and then use when drawing text. |
| 46 static cairo_font_options_t* cairo_font_options = NULL; | 48 static cairo_font_options_t* cairo_font_options = NULL; |
| 47 if (cairo_font_options) | 49 if (cairo_font_options) |
| 48 return cairo_font_options; | 50 return cairo_font_options; |
| 49 | 51 |
| 50 cairo_font_options = cairo_font_options_create(); | 52 cairo_font_options = cairo_font_options_create(); |
| 51 | 53 |
| 52 const gfx::FontRenderParams& params = gfx::GetDefaultFontRenderParams(); | 54 const FontRenderParams& params = GetDefaultFontRenderParams(); |
| 53 gfx::FontRenderParams::SubpixelRendering subpixel = params.subpixel_rendering; | 55 FontRenderParams::SubpixelRendering subpixel = params.subpixel_rendering; |
| 54 if (!params.antialiasing) { | 56 if (!params.antialiasing) { |
| 55 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_NONE); | 57 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_NONE); |
| 56 } else if (subpixel == gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE) { | 58 } else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_NONE) { |
| 57 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); | 59 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); |
| 58 } else { | 60 } else { |
| 59 cairo_font_options_set_antialias(cairo_font_options, | 61 cairo_font_options_set_antialias(cairo_font_options, |
| 60 CAIRO_ANTIALIAS_SUBPIXEL); | 62 CAIRO_ANTIALIAS_SUBPIXEL); |
| 61 cairo_subpixel_order_t cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT; | 63 cairo_subpixel_order_t cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT; |
| 62 if (subpixel == gfx::FontRenderParams::SUBPIXEL_RENDERING_RGB) | 64 if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_RGB) |
| 63 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; | 65 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; |
| 64 else if (subpixel == gfx::FontRenderParams::SUBPIXEL_RENDERING_BGR) | 66 else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_BGR) |
| 65 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; | 67 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; |
| 66 else if (subpixel == gfx::FontRenderParams::SUBPIXEL_RENDERING_VRGB) | 68 else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_VRGB) |
| 67 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; | 69 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; |
| 68 else if (subpixel == gfx::FontRenderParams::SUBPIXEL_RENDERING_VBGR) | 70 else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_VBGR) |
| 69 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; | 71 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; |
| 70 else | 72 else |
| 71 NOTREACHED() << "Unhandled subpixel rendering type " << subpixel; | 73 NOTREACHED() << "Unhandled subpixel rendering type " << subpixel; |
| 72 cairo_font_options_set_subpixel_order(cairo_font_options, | 74 cairo_font_options_set_subpixel_order(cairo_font_options, |
| 73 cairo_subpixel_order); | 75 cairo_subpixel_order); |
| 74 } | 76 } |
| 75 | 77 |
| 76 if (params.hinting == gfx::FontRenderParams::HINTING_NONE || | 78 if (params.hinting == FontRenderParams::HINTING_NONE || |
| 77 params.subpixel_positioning) { | 79 params.subpixel_positioning) { |
| 78 cairo_font_options_set_hint_style(cairo_font_options, | 80 cairo_font_options_set_hint_style(cairo_font_options, |
| 79 CAIRO_HINT_STYLE_NONE); | 81 CAIRO_HINT_STYLE_NONE); |
| 80 cairo_font_options_set_hint_metrics(cairo_font_options, | 82 cairo_font_options_set_hint_metrics(cairo_font_options, |
| 81 CAIRO_HINT_METRICS_OFF); | 83 CAIRO_HINT_METRICS_OFF); |
| 82 } else { | 84 } else { |
| 83 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; | 85 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; |
| 84 if (params.hinting == gfx::FontRenderParams::HINTING_SLIGHT) | 86 if (params.hinting == FontRenderParams::HINTING_SLIGHT) |
| 85 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; | 87 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; |
| 86 else if (params.hinting == gfx::FontRenderParams::HINTING_MEDIUM) | 88 else if (params.hinting == FontRenderParams::HINTING_MEDIUM) |
| 87 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; | 89 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; |
| 88 else if (params.hinting == gfx::FontRenderParams::HINTING_FULL) | 90 else if (params.hinting == FontRenderParams::HINTING_FULL) |
| 89 cairo_hint_style = CAIRO_HINT_STYLE_FULL; | 91 cairo_hint_style = CAIRO_HINT_STYLE_FULL; |
| 90 else | 92 else |
| 91 NOTREACHED() << "Unhandled hinting style " << params.hinting; | 93 NOTREACHED() << "Unhandled hinting style " << params.hinting; |
| 92 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); | 94 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); |
| 93 cairo_font_options_set_hint_metrics(cairo_font_options, | 95 cairo_font_options_set_hint_metrics(cairo_font_options, |
| 94 CAIRO_HINT_METRICS_ON); | 96 CAIRO_HINT_METRICS_ON); |
| 95 } | 97 } |
| 96 | 98 |
| 97 return cairo_font_options; | 99 return cairo_font_options; |
| 98 } | 100 } |
| 99 | 101 |
| 100 // Returns the number of pixels in a point. | 102 // Returns the number of pixels in a point. |
| 101 // - multiply a point size by this to get pixels ("device units") | 103 // - multiply a point size by this to get pixels ("device units") |
| 102 // - divide a pixel size by this to get points | 104 // - divide a pixel size by this to get points |
| 103 float GetPixelsInPoint() { | 105 float GetPixelsInPoint() { |
| 104 static float pixels_in_point = 1.0; | 106 static float pixels_in_point = 1.0; |
| 105 static bool determined_value = false; | 107 static bool determined_value = false; |
| 106 | 108 |
| 107 if (!determined_value) { | 109 if (!determined_value) { |
| 108 // http://goo.gl/UIh5m: "This is a scale factor between points specified in | 110 // http://goo.gl/UIh5m: "This is a scale factor between points specified in |
| 109 // a PangoFontDescription and Cairo units. The default value is 96, meaning | 111 // a PangoFontDescription and Cairo units. The default value is 96, meaning |
| 110 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)." | 112 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)." |
| 111 double pango_dpi = gfx::GetPangoResolution(); | 113 double pango_dpi = GetPangoResolution(); |
| 112 if (pango_dpi <= 0) | 114 if (pango_dpi <= 0) |
| 113 pango_dpi = 96.0; | 115 pango_dpi = 96.0; |
| 114 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch | 116 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch |
| 115 determined_value = true; | 117 determined_value = true; |
| 116 } | 118 } |
| 117 | 119 |
| 118 return pixels_in_point; | 120 return pixels_in_point; |
| 119 } | 121 } |
| 120 | 122 |
| 121 } // namespace | 123 } // namespace |
| 122 | 124 |
| 123 namespace gfx { | |
| 124 | |
| 125 PangoContext* GetPangoContext() { | 125 PangoContext* GetPangoContext() { |
| 126 #if defined(USE_AURA) | 126 #if defined(USE_AURA) |
| 127 PangoFontMap* font_map = pango_cairo_font_map_get_default(); | 127 PangoFontMap* font_map = pango_cairo_font_map_get_default(); |
| 128 return pango_font_map_create_context(font_map); | 128 return pango_font_map_create_context(font_map); |
| 129 #else | 129 #else |
| 130 return gdk_pango_context_get(); | 130 return gdk_pango_context_get(); |
| 131 #endif | 131 #endif |
| 132 } | 132 } |
| 133 | 133 |
| 134 double GetPangoResolution() { | 134 double GetPangoResolution() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 146 void DrawTextOntoCairoSurface(cairo_t* cr, | 146 void DrawTextOntoCairoSurface(cairo_t* cr, |
| 147 const string16& text, | 147 const string16& text, |
| 148 const gfx::Font& font, | 148 const gfx::Font& font, |
| 149 const gfx::Rect& bounds, | 149 const gfx::Rect& bounds, |
| 150 const gfx::Rect& clip, | 150 const gfx::Rect& clip, |
| 151 SkColor text_color, | 151 SkColor text_color, |
| 152 int flags) { | 152 int flags) { |
| 153 PangoLayout* layout = pango_cairo_create_layout(cr); | 153 PangoLayout* layout = pango_cairo_create_layout(cr); |
| 154 base::i18n::TextDirection text_direction = | 154 base::i18n::TextDirection text_direction = |
| 155 base::i18n::GetFirstStrongCharacterDirection(text); | 155 base::i18n::GetFirstStrongCharacterDirection(text); |
| 156 Rect text_rect(bounds.x(), bounds.y(), 0, 0); | |
| 157 DCHECK(!bounds.IsEmpty()); | 156 DCHECK(!bounds.IsEmpty()); |
| 158 | 157 |
| 159 gfx::SetupPangoLayout( | 158 gfx::SetupPangoLayout( |
| 160 layout, text, font, bounds.width(), text_direction, flags); | 159 layout, text, font, bounds.width(), text_direction, flags); |
| 161 | 160 |
| 162 pango_layout_set_height(layout, bounds.height() * PANGO_SCALE); | 161 pango_layout_set_height(layout, bounds.height() * PANGO_SCALE); |
| 163 | 162 |
| 164 cairo_save(cr); | 163 cairo_save(cr); |
| 165 cairo_rectangle(cr, clip.x(), clip.y(), clip.width(), clip.height()); | 164 cairo_rectangle(cr, clip.x(), clip.y(), clip.width(), clip.height()); |
| 166 cairo_clip(cr); | 165 cairo_clip(cr); |
| 167 | 166 |
| 168 AdjustTextRectBasedOnLayout(layout, bounds, flags, &text_rect); | 167 int width = 0, height = 0; |
| 168 pango_layout_get_pixel_size(layout, &width, &height); |
| 169 Rect text_rect(bounds.x(), bounds.y(), width, height); |
| 170 // Vertically center |text_rect| in |bounds|. |
| 171 text_rect.Offset(0, (bounds.height() - text_rect.height()) / 2); |
| 169 | 172 |
| 170 DrawPangoLayout(cr, layout, font, bounds, text_rect, | 173 DrawPangoLayout(cr, layout, font, bounds, text_rect, |
| 171 text_color, text_direction, flags); | 174 text_color, text_direction, flags); |
| 172 | 175 |
| 173 cairo_restore(cr); | 176 cairo_restore(cr); |
| 174 g_object_unref(layout); | 177 g_object_unref(layout); |
| 175 } | 178 } |
| 176 | 179 |
| 177 // Pass a width greater than 0 to force wrapping and eliding. | 180 // Pass a width greater than 0 to force wrapping and eliding. |
| 178 static void SetupPangoLayoutWithoutFont( | 181 static void SetupPangoLayoutWithoutFont( |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 int width, | 297 int width, |
| 295 base::i18n::TextDirection text_direction, | 298 base::i18n::TextDirection text_direction, |
| 296 int flags) { | 299 int flags) { |
| 297 SetupPangoLayoutWithoutFont(layout, text, width, text_direction, flags); | 300 SetupPangoLayoutWithoutFont(layout, text, width, text_direction, flags); |
| 298 | 301 |
| 299 ScopedPangoFontDescription desc( | 302 ScopedPangoFontDescription desc( |
| 300 pango_font_description_from_string(font_description.c_str())); | 303 pango_font_description_from_string(font_description.c_str())); |
| 301 pango_layout_set_font_description(layout, desc.get()); | 304 pango_layout_set_font_description(layout, desc.get()); |
| 302 } | 305 } |
| 303 | 306 |
| 304 void AdjustTextRectBasedOnLayout(PangoLayout* layout, | |
| 305 const gfx::Rect& bounds, | |
| 306 int flags, | |
| 307 gfx::Rect* text_rect) { | |
| 308 int text_width, text_height; | |
| 309 pango_layout_get_pixel_size(layout, &text_width, &text_height); | |
| 310 text_rect->set_width(text_width); | |
| 311 text_rect->set_height(text_height); | |
| 312 | |
| 313 if (flags & gfx::Canvas::TEXT_VALIGN_TOP) { | |
| 314 // Cairo should draw from the top left corner already. | |
| 315 } else if (flags & gfx::Canvas::TEXT_VALIGN_BOTTOM) { | |
| 316 text_rect->set_y(text_rect->y() + bounds.height() - text_rect->height()); | |
| 317 } else { | |
| 318 // Vertically centered. | |
| 319 text_rect->set_y(text_rect->y() + | |
| 320 ((bounds.height() - text_rect->height()) / 2)); | |
| 321 } | |
| 322 } | |
| 323 | |
| 324 void DrawPangoLayout(cairo_t* cr, | 307 void DrawPangoLayout(cairo_t* cr, |
| 325 PangoLayout* layout, | 308 PangoLayout* layout, |
| 326 const Font& font, | 309 const Font& font, |
| 327 const gfx::Rect& bounds, | 310 const gfx::Rect& bounds, |
| 328 const gfx::Rect& text_rect, | 311 const gfx::Rect& text_rect, |
| 329 SkColor text_color, | 312 SkColor text_color, |
| 330 base::i18n::TextDirection text_direction, | 313 base::i18n::TextDirection text_direction, |
| 331 int flags) { | 314 int flags) { |
| 332 double r = SkColorGetR(text_color) / 255.0, | 315 double r = SkColorGetR(text_color) / 255.0, |
| 333 g = SkColorGetG(text_color) / 255.0, | 316 g = SkColorGetG(text_color) / 255.0, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 407 |
| 425 if (i == desc_to_metrics->end()) { | 408 if (i == desc_to_metrics->end()) { |
| 426 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); | 409 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); |
| 427 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); | 410 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); |
| 428 return metrics; | 411 return metrics; |
| 429 } | 412 } |
| 430 return i->second; | 413 return i->second; |
| 431 } | 414 } |
| 432 | 415 |
| 433 } // namespace gfx | 416 } // namespace gfx |
| OLD | NEW |