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/gfx/gfx_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: ScopedPangoFontDescription(PangoFontDescription * description)36 explicit ScopedPangoFontDescription(PangoFontDescription* description) 37 : description_(description) { 38 DCHECK(description); 39 } 40 ~ScopedPangoFontDescription()41 ~ScopedPangoFontDescription() { 42 pango_font_description_free(description_); 43 } 44 get()45 PangoFontDescription* get() { return description_; } 46 47 private: 48 PangoFontDescription* description_; 49 50 DISALLOW_COPY_AND_ASSIGN(ScopedPangoFontDescription); 51 }; 52 53 // ---------------------------------------------------------------------------- 54 // All other methods in this file are only to be used within the ui/ directory. 55 // They are shared with internal skia interfaces. 56 // ---------------------------------------------------------------------------- 57 58 // Setup pango |layout|; set the |text|, the font description based on 59 // |font_description|, the |width| in PANGO_SCALE for RTL locale, the base 60 // |text_direction|, alignment, ellipsis, word wrapping, resolution, etc. 61 void SetupPangoLayoutWithFontDescription( 62 PangoLayout* layout, 63 const base::string16& text, 64 const std::string& font_description, 65 int width, 66 base::i18n::TextDirection text_direction, 67 int flags); 68 69 // Returns the size in pixels for the specified |pango_font|. 70 size_t GetPangoFontSizeInPixels(PangoFontDescription* pango_font); 71 72 // Retrieves the Pango metrics for a Pango font description. Caches the metrics 73 // and never frees them. The metrics objects are relatively small and very 74 // expensive to look up. 75 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc); 76 77 } // namespace gfx 78 79 #endif // UI_GFX_PANGO_UTIL_H_ 80