• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PLATFORM_FONT_PANGO_H_
6 #define UI_GFX_PLATFORM_FONT_PANGO_H_
7 
8 #include <string>
9 
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "skia/ext/refptr.h"
13 #include "third_party/skia/include/core/SkRefCnt.h"
14 #include "ui/gfx/platform_font.h"
15 
16 class SkTypeface;
17 class SkPaint;
18 
19 namespace gfx {
20 
21 class GFX_EXPORT PlatformFontPango : public PlatformFont {
22  public:
23   PlatformFontPango();
24   explicit PlatformFontPango(NativeFont native_font);
25   PlatformFontPango(const std::string& font_name, int font_size);
26 
27   // Converts |gfx_font| to a new pango font. Free the returned font with
28   // pango_font_description_free().
29   static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font);
30 
31   // Resets and reloads the cached system font used by the default constructor.
32   // This function is useful when the system font has changed, for example, when
33   // the locale has changed.
34   static void ReloadDefaultFont();
35 
36 #if defined(OS_CHROMEOS)
37   // Sets the default font.
38   static void SetDefaultFontDescription(const std::string& font_description);
39 #endif
40 
41   // Position as an offset from the height of the drawn text, used to draw
42   // an underline. This is a negative number, so the underline would be
43   // drawn at y + height + underline_position.
44   double underline_position() const;
45   // The thickness to draw the underline.
46   double underline_thickness() const;
47 
48   // Overridden from PlatformFont:
49   virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
50   virtual int GetHeight() const OVERRIDE;
51   virtual int GetBaseline() const OVERRIDE;
52   virtual int GetCapHeight() const OVERRIDE;
53   virtual int GetAverageCharacterWidth() const OVERRIDE;
54   virtual int GetStringWidth(const base::string16& text) const OVERRIDE;
55   virtual int GetExpectedTextWidth(int length) const OVERRIDE;
56   virtual int GetStyle() const OVERRIDE;
57   virtual std::string GetFontName() const OVERRIDE;
58   virtual std::string GetActualFontNameForTesting() const OVERRIDE;
59   virtual int GetFontSize() const OVERRIDE;
60   virtual NativeFont GetNativeFont() const OVERRIDE;
61 
62  private:
63   // Create a new instance of this object with the specified properties. Called
64   // from DeriveFont.
65   PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
66                     const std::string& name,
67                     int size,
68                     int style);
69   virtual ~PlatformFontPango();
70 
71   // Returns a Pango font description (suitable for parsing by
72   // pango_font_description_from_string()) for the default UI font.
73   static std::string GetDefaultFont();
74 
75   // Initialize this object.
76   void InitWithNameAndSize(const std::string& font_name, int font_size);
77   void InitWithTypefaceNameSizeAndStyle(
78       const skia::RefPtr<SkTypeface>& typeface,
79       const std::string& font_family,
80       int font_size,
81       int style);
82   void InitFromPlatformFont(const PlatformFontPango* other);
83 
84   // Potentially slow call to get pango metrics (average width, underline info).
85   void InitPangoMetrics();
86 
87   // Setup a Skia context to use the current typeface.
88   void PaintSetup(SkPaint* paint) const;
89 
90   // Make |this| a copy of |other|.
91   void CopyFont(const Font& other);
92 
93   // The average width of a character, initialized and cached if needed.
94   double GetAverageWidth() const;
95 
96   skia::RefPtr<SkTypeface> typeface_;
97 
98   // Additional information about the face
99   // Skia actually expects a family name and not a font name.
100   std::string font_family_;
101   int font_size_pixels_;
102   int style_;
103 
104   // Cached metrics, generated at construction.
105   int height_pixels_;
106   int ascent_pixels_;
107 
108   // The pango metrics are much more expensive so we wait until we need them
109   // to compute them.
110   bool pango_metrics_inited_;
111   double average_width_pixels_;
112   double underline_position_pixels_;
113   double underline_thickness_pixels_;
114 
115   // The default font, used for the default constructor.
116   static Font* default_font_;
117 
118 #if defined(OS_CHROMEOS)
119   static std::string* default_font_description_;
120 #endif
121 
122   DISALLOW_COPY_AND_ASSIGN(PlatformFontPango);
123 };
124 
125 }  // namespace gfx
126 
127 #endif  // UI_GFX_PLATFORM_FONT_PANGO_H_
128