Home | History | Annotate | Download | only in gfx
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ui/gfx/pango_util.h"
      6 
      7 #include <cairo/cairo.h>
      8 #include <fontconfig/fontconfig.h>
      9 #include <pango/pango.h>
     10 #include <pango/pangocairo.h>
     11 #include <string>
     12 
     13 #include <algorithm>
     14 #include <map>
     15 #include <vector>
     16 
     17 #include "base/logging.h"
     18 #include "base/strings/utf_string_conversions.h"
     19 #include "ui/gfx/canvas.h"
     20 #include "ui/gfx/font.h"
     21 #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"
     25 
     26 namespace gfx {
     27 
     28 namespace {
     29 
     30 // Marker for accelerators in the text.
     31 const gunichar kAcceleratorChar = '&';
     32 
     33 // Return |cairo_font_options|. If needed, allocate and update it.
     34 // TODO(derat): Return font-specific options: http://crbug.com/125235
     35 cairo_font_options_t* GetCairoFontOptions() {
     36   // Font settings that we initialize once and then use when drawing text.
     37   static cairo_font_options_t* cairo_font_options = NULL;
     38   if (cairo_font_options)
     39     return cairo_font_options;
     40 
     41   cairo_font_options = cairo_font_options_create();
     42 
     43   const FontRenderParams& params = GetDefaultFontRenderParams();
     44   FontRenderParams::SubpixelRendering subpixel = params.subpixel_rendering;
     45   if (!params.antialiasing) {
     46     cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_NONE);
     47   } else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_NONE) {
     48     cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY);
     49   } else {
     50     cairo_font_options_set_antialias(cairo_font_options,
     51                                      CAIRO_ANTIALIAS_SUBPIXEL);
     52     cairo_subpixel_order_t cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
     53     if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_RGB)
     54       cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
     55     else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_BGR)
     56       cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
     57     else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_VRGB)
     58       cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
     59     else if (subpixel == FontRenderParams::SUBPIXEL_RENDERING_VBGR)
     60       cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
     61     else
     62       NOTREACHED() << "Unhandled subpixel rendering type " << subpixel;
     63     cairo_font_options_set_subpixel_order(cairo_font_options,
     64                                           cairo_subpixel_order);
     65   }
     66 
     67   if (params.hinting == FontRenderParams::HINTING_NONE ||
     68       params.subpixel_positioning) {
     69     cairo_font_options_set_hint_style(cairo_font_options,
     70                                       CAIRO_HINT_STYLE_NONE);
     71     cairo_font_options_set_hint_metrics(cairo_font_options,
     72                                         CAIRO_HINT_METRICS_OFF);
     73   } else {
     74     cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT;
     75     if (params.hinting == FontRenderParams::HINTING_SLIGHT)
     76       cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT;
     77     else if (params.hinting == FontRenderParams::HINTING_MEDIUM)
     78       cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM;
     79     else if (params.hinting == FontRenderParams::HINTING_FULL)
     80       cairo_hint_style = CAIRO_HINT_STYLE_FULL;
     81     else
     82       NOTREACHED() << "Unhandled hinting style " << params.hinting;
     83     cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style);
     84     cairo_font_options_set_hint_metrics(cairo_font_options,
     85                                         CAIRO_HINT_METRICS_ON);
     86   }
     87 
     88   return cairo_font_options;
     89 }
     90 
     91 // Returns the number of pixels in a point.
     92 // - multiply a point size by this to get pixels ("device units")
     93 // - divide a pixel size by this to get points
     94 float GetPixelsInPoint() {
     95   static float pixels_in_point = 1.0;
     96   static bool determined_value = false;
     97 
     98   if (!determined_value) {
     99     // 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
    101     // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)."
    102     double pango_dpi = GetPangoResolution();
    103     if (pango_dpi <= 0)
    104       pango_dpi = 96.0;
    105     pixels_in_point = pango_dpi / 72.0;  // 72 points in an inch
    106     determined_value = true;
    107   }
    108 
    109   return pixels_in_point;
    110 }
    111 
    112 }  // namespace
    113 
    114 PangoContext* GetPangoContext() {
    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,
    134     const base::string16& text,
    135     int width,
    136     base::i18n::TextDirection text_direction,
    137     int flags) {
    138   cairo_font_options_t* cairo_font_options = GetCairoFontOptions();
    139 
    140   // If we got an explicit request to turn off subpixel rendering, disable it on
    141   // a copy of the static font options object.
    142   bool copied_cairo_font_options = false;
    143   if ((flags & Canvas::NO_SUBPIXEL_RENDERING) &&
    144       (cairo_font_options_get_antialias(cairo_font_options) ==
    145        CAIRO_ANTIALIAS_SUBPIXEL)) {
    146     cairo_font_options = cairo_font_options_copy(cairo_font_options);
    147     copied_cairo_font_options = true;
    148     cairo_font_options_set_antialias(cairo_font_options, CAIRO_ANTIALIAS_GRAY);
    149   }
    150 
    151   // This needs to be done early on; it has no effect when called just before
    152   // pango_cairo_show_layout().
    153   pango_cairo_context_set_font_options(
    154       pango_layout_get_context(layout), cairo_font_options);
    155 
    156   if (copied_cairo_font_options) {
    157     cairo_font_options_destroy(cairo_font_options);
    158     cairo_font_options = NULL;
    159   }
    160 
    161   // Set Pango's base text direction explicitly from |text_direction|.
    162   pango_layout_set_auto_dir(layout, FALSE);
    163   pango_context_set_base_dir(pango_layout_get_context(layout),
    164       (text_direction == base::i18n::RIGHT_TO_LEFT ?
    165        PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR));
    166 
    167   if (width > 0)
    168     pango_layout_set_width(layout, width * PANGO_SCALE);
    169 
    170   if (flags & Canvas::TEXT_ALIGN_CENTER) {
    171     // We don't support center aligned w/ eliding.
    172     DCHECK(gfx::Canvas::NO_ELLIPSIS);
    173     pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
    174   } else if (flags & Canvas::TEXT_ALIGN_RIGHT) {
    175     pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
    176   }
    177 
    178   if (flags & Canvas::NO_ELLIPSIS) {
    179     pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
    180     if (flags & Canvas::MULTI_LINE) {
    181       pango_layout_set_wrap(layout,
    182           (flags & Canvas::CHARACTER_BREAK) ?
    183               PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD);
    184     }
    185   } else if (text_direction == base::i18n::RIGHT_TO_LEFT) {
    186     pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
    187   } else {
    188     // Fading the text will be handled in the draw operation.
    189     // Ensure that the text is only on one line.
    190     pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
    191     pango_layout_set_width(layout, -1);
    192   }
    193 
    194   // Set the resolution to match that used by Gtk. If we don't set the
    195   // resolution and the resolution differs from the default, Gtk and Chrome end
    196   // up drawing at different sizes.
    197   double resolution = GetPangoResolution();
    198   if (resolution > 0) {
    199     pango_cairo_context_set_resolution(pango_layout_get_context(layout),
    200                                        resolution);
    201   }
    202 
    203   // Set text and accelerator character if needed.
    204   if (flags & Canvas::SHOW_PREFIX) {
    205     // Escape the text string to be used as markup.
    206     std::string utf8 = base::UTF16ToUTF8(text);
    207     gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size());
    208     pango_layout_set_markup_with_accel(layout,
    209                                        escaped_text,
    210                                        strlen(escaped_text),
    211                                        kAcceleratorChar, NULL);
    212     g_free(escaped_text);
    213   } else {
    214     std::string utf8;
    215 
    216     // Remove the ampersand character.  A double ampersand is output as
    217     // a single ampersand.
    218     if (flags & Canvas::HIDE_PREFIX) {
    219       DCHECK_EQ(1, g_unichar_to_utf8(kAcceleratorChar, NULL));
    220       base::string16 accelerator_removed =
    221           RemoveAcceleratorChar(text,
    222                                 static_cast<base::char16>(kAcceleratorChar),
    223                                 NULL, NULL);
    224       utf8 = base::UTF16ToUTF8(accelerator_removed);
    225     } else {
    226       utf8 = base::UTF16ToUTF8(text);
    227     }
    228 
    229     pango_layout_set_text(layout, utf8.data(), utf8.size());
    230   }
    231 }
    232 
    233 void SetupPangoLayout(PangoLayout* layout,
    234                       const base::string16& text,
    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());
    243 }
    244 
    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) {
    260   size_t size_in_pixels = pango_font_description_get_size(pango_font);
    261   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.
    263     // There are PANGO_SCALE Pango units in a device unit (pixel).
    264     size_in_pixels /= PANGO_SCALE;
    265   } else {
    266     // Otherwise, we need to convert from points.
    267     size_in_pixels = size_in_pixels * GetPixelsInPoint() / PANGO_SCALE;
    268   }
    269   return size_in_pixels;
    270 }
    271 
    272 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) {
    273   static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL;
    274   static PangoContext* context = NULL;
    275 
    276   if (!context) {
    277     context = GetPangoContext();
    278     pango_context_set_language(context, pango_language_get_default());
    279   }
    280 
    281   if (!desc_to_metrics)
    282     desc_to_metrics = new std::map<int, PangoFontMetrics*>();
    283 
    284   const int desc_hash = pango_font_description_hash(desc);
    285   std::map<int, PangoFontMetrics*>::iterator i =
    286       desc_to_metrics->find(desc_hash);
    287 
    288   if (i == desc_to_metrics->end()) {
    289     PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL);
    290     desc_to_metrics->insert(std::make_pair(desc_hash, metrics));
    291     return metrics;
    292   }
    293   return i->second;
    294 }
    295 
    296 }  // namespace gfx
    297