| 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/platform_font_pango.h" |
| 20 #include "ui/gfx/text_utils.h" | 21 #include "ui/gfx/text_utils.h" |
| 21 | 22 |
| 22 namespace gfx { | 23 namespace gfx { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Marker for accelerators in the text. | 27 // Marker for accelerators in the text. |
| 27 const gunichar kAcceleratorChar = '&'; | 28 const gunichar kAcceleratorChar = '&'; |
| 28 | 29 |
| 29 // Creates and returns a PangoContext. The caller owns the context. | 30 // Creates and returns a PangoContext. The caller owns the context. |
| 30 PangoContext* GetPangoContext() { | 31 PangoContext* GetPangoContext() { |
| 31 PangoFontMap* font_map = pango_cairo_font_map_get_default(); | 32 PangoFontMap* font_map = pango_cairo_font_map_get_default(); |
| 32 return pango_font_map_create_context(font_map); | 33 return pango_font_map_create_context(font_map); |
| 33 } | 34 } |
| 34 | 35 |
| 35 // Returns a static cairo_font_options_t. If needed, allocates and updates it. | 36 // Creates a new cairo_font_options_t based on |params|. |
| 36 // TODO(derat): Return font-specific options: http://crbug.com/125235 | 37 cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) { |
| 37 cairo_font_options_t* GetCairoFontOptions() { | 38 cairo_font_options_t* cairo_font_options = cairo_font_options_create(); |
| 38 // Font settings that we initialize once and then use when drawing text. | |
| 39 static cairo_font_options_t* cairo_font_options = NULL; | |
| 40 if (cairo_font_options) | |
| 41 return cairo_font_options; | |
| 42 | 39 |
| 43 cairo_font_options = cairo_font_options_create(); | |
| 44 | |
| 45 const FontRenderParams& params = GetDefaultFontRenderParams(); | |
| 46 FontRenderParams::SubpixelRendering subpixel = params.subpixel_rendering; | 40 FontRenderParams::SubpixelRendering subpixel = params.subpixel_rendering; |
| 47 if (!params.antialiasing) { | 41 if (!params.antialiasing) { |
| 48 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_NONE); | 42 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_NONE); |
| 49 } else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_NONE) { | 43 } else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_NONE) { |
| 50 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); | 44 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); |
| 51 } else { | 45 } else { |
| 52 cairo_font_options_set_antialias(cairo_font_options, | 46 cairo_font_options_set_antialias(cairo_font_options, |
| 53 CAIRO_ANTIALIAS_SUBPIXEL); | 47 CAIRO_ANTIALIAS_SUBPIXEL); |
| 54 cairo_subpixel_order_t cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT; | 48 cairo_subpixel_order_t cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT; |
| 55 if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_RGB) | 49 if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_RGB) |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 } | 120 } |
| 127 | 121 |
| 128 } // namespace | 122 } // namespace |
| 129 | 123 |
| 130 void SetUpPangoLayout( | 124 void SetUpPangoLayout( |
| 131 PangoLayout* layout, | 125 PangoLayout* layout, |
| 132 const base::string16& text, | 126 const base::string16& text, |
| 133 const FontList& font_list, | 127 const FontList& font_list, |
| 134 base::i18n::TextDirection text_direction, | 128 base::i18n::TextDirection text_direction, |
| 135 int flags) { | 129 int flags) { |
| 136 // TODO(derat): Use rendering parameters from |font_list| instead of defaults. | 130 cairo_font_options_t* cairo_font_options = |
| 137 cairo_font_options_t* cairo_font_options = GetCairoFontOptions(); | 131 CreateCairoFontOptions(font_list.GetFontRenderParams()); |
| 138 | 132 |
| 139 // If we got an explicit request to turn off subpixel rendering, disable it on | 133 // If we got an explicit request to turn off subpixel rendering, disable it. |
| 140 // a copy of the static font options object. | |
| 141 bool copied_cairo_font_options = false; | |
| 142 if ((flags & Canvas::NO_SUBPIXEL_RENDERING) && | 134 if ((flags & Canvas::NO_SUBPIXEL_RENDERING) && |
| 143 (cairo_font_options_get_antialias(cairo_font_options) == | 135 (cairo_font_options_get_antialias(cairo_font_options) == |
| 144 CAIRO_ANTIALIAS_SUBPIXEL)) { | 136 CAIRO_ANTIALIAS_SUBPIXEL)) |
| 145 cairo_font_options = cairo_font_options_copy(cairo_font_options); | |
| 146 copied_cairo_font_options = true; | |
| 147 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); | 137 cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY); |
| 148 } | |
| 149 | 138 |
| 150 // This needs to be done early on; it has no effect when called just before | 139 // This needs to be done early on; it has no effect when called just before |
| 151 // pango_cairo_show_layout(). | 140 // pango_cairo_show_layout(). |
| 152 pango_cairo_context_set_font_options( | 141 pango_cairo_context_set_font_options( |
| 153 pango_layout_get_context(layout), cairo_font_options); | 142 pango_layout_get_context(layout), cairo_font_options); |
| 154 | 143 cairo_font_options_destroy(cairo_font_options); |
| 155 if (copied_cairo_font_options) { | 144 cairo_font_options = NULL; |
| 156 cairo_font_options_destroy(cairo_font_options); | |
| 157 cairo_font_options = NULL; | |
| 158 } | |
| 159 | 145 |
| 160 // Set Pango's base text direction explicitly from |text_direction|. | 146 // Set Pango's base text direction explicitly from |text_direction|. |
| 161 pango_layout_set_auto_dir(layout, FALSE); | 147 pango_layout_set_auto_dir(layout, FALSE); |
| 162 pango_context_set_base_dir(pango_layout_get_context(layout), | 148 pango_context_set_base_dir(pango_layout_get_context(layout), |
| 163 (text_direction == base::i18n::RIGHT_TO_LEFT ? | 149 (text_direction == base::i18n::RIGHT_TO_LEFT ? |
| 164 PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR)); | 150 PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR)); |
| 165 | 151 |
| 166 if (flags & Canvas::TEXT_ALIGN_CENTER) { | 152 if (flags & Canvas::TEXT_ALIGN_CENTER) { |
| 167 // We don't support center aligned w/ eliding. | 153 // We don't support center aligned w/ eliding. |
| 168 DCHECK(gfx::Canvas::NO_ELLIPSIS); | 154 DCHECK(gfx::Canvas::NO_ELLIPSIS); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 245 |
| 260 if (i == desc_to_metrics->end()) { | 246 if (i == desc_to_metrics->end()) { |
| 261 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); | 247 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); |
| 262 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); | 248 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); |
| 263 return metrics; | 249 return metrics; |
| 264 } | 250 } |
| 265 return i->second; | 251 return i->second; |
| 266 } | 252 } |
| 267 | 253 |
| 268 } // namespace gfx | 254 } // namespace gfx |
| OLD | NEW |