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 #ifndef UI_GFX_PANGO_UTIL_H_ 6 #define UI_GFX_PANGO_UTIL_H_ 7 8 #include <cairo/cairo.h> 9 #include <pango/pango.h> 10 #include <string> 11 12 #include "base/i18n/rtl.h" 13 #include "base/logging.h" 14 #include "base/strings/string16.h" 15 #include "third_party/skia/include/core/SkColor.h" 16 #include "ui/base/ui_export.h" 17 18 typedef struct _PangoContext PangoContext; 19 20 namespace gfx { 21 22 class Font; 23 class PlatformFontPango; 24 class Rect; 25 26 // Creates and returns a PangoContext. The caller owns the context. 27 PangoContext* GetPangoContext(); 28 29 // Returns the resolution (DPI) used by pango. A negative values means the 30 // resolution hasn't been set. 31 double GetPangoResolution(); 32 33 // Utility class to ensure that PangoFontDescription is freed. 34 class ScopedPangoFontDescription { 35 public: 36 explicit ScopedPangoFontDescription(PangoFontDescription* description) 37 : description_(description) { 38 DCHECK(description); 39 } 40 41 ~ScopedPangoFontDescription() { 42 pango_font_description_free(description_); 43 } 44 45 PangoFontDescription* get() { return description_; } 46 47 private: 48 PangoFontDescription* description_; 49 50 DISALLOW_COPY_AND_ASSIGN(ScopedPangoFontDescription); 51 }; 52 53 // Uses Pango to draw text onto |cr|. This is the public method for d 54 void UI_EXPORT DrawTextOntoCairoSurface(cairo_t* cr, 55 const base::string16& text, 56 const gfx::Font& font, 57 const gfx::Rect& bounds, 58 const gfx::Rect& clip, 59 SkColor text_color, 60 int flags); 61 62 // ---------------------------------------------------------------------------- 63 // All other methods in this file are only to be used within the ui/ directory. 64 // They are shared with internal skia interfaces. 65 // ---------------------------------------------------------------------------- 66 67 // Setup pango |layout|; set the |text|, the font description based on |font|, 68 // the |width| in PANGO_SCALE for RTL locale, the base |text_direction|, 69 // alignment, ellipsis, word wrapping, resolution, etc. 70 void SetupPangoLayout(PangoLayout* layout, 71 const base::string16& text, 72 const gfx::Font& font, 73 int width, 74 base::i18n::TextDirection text_direction, 75 int flags); 76 77 // Setup pango layout |layout| the same way as SetupPangoLayout(), except this 78 // sets the font description based on |font_description|. 79 void SetupPangoLayoutWithFontDescription( 80 PangoLayout* layout, 81 const base::string16& text, 82 const std::string& font_description, 83 int width, 84 base::i18n::TextDirection text_direction, 85 int flags); 86 87 // Draws the |layout| (pango tuple of font, actual text, etc) onto |cr| using 88 // |text_color| as the cairo pattern. 89 void DrawPangoLayout(cairo_t* cr, 90 PangoLayout* layout, 91 const Font& font, 92 const gfx::Rect& bounds, 93 const gfx::Rect& text_rect, 94 SkColor text_color, 95 base::i18n::TextDirection text_direction, 96 int flags); 97 98 // Draw an underline under the text using |cr|, which must already be 99 // initialized with the correct source. |extra_edge_width| is added to the 100 // outer edge of the line. 101 void DrawPangoTextUnderline(cairo_t* cr, 102 gfx::PlatformFontPango* platform_font, 103 double extra_edge_width, 104 const Rect& text_rect); 105 106 // Returns the size in pixels for the specified |pango_font|. 107 size_t GetPangoFontSizeInPixels(PangoFontDescription* pango_font); 108 109 // Retrieves the Pango metrics for a Pango font description. Caches the metrics 110 // and never frees them. The metrics objects are relatively small and very 111 // expensive to look up. 112 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc); 113 114 } // namespace gfx 115 116 #endif // UI_GFX_PANGO_UTIL_H_ 117