3a494389c139a51babb39da06cc2a92b3b808008
[platform/framework/web/crosswalk.git] / src / ui / gfx / platform_font_pango.h
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/font_render_params.h"
15 #include "ui/gfx/platform_font.h"
16
17 class SkTypeface;
18 class SkPaint;
19
20 namespace gfx {
21
22 class GFX_EXPORT PlatformFontPango : public PlatformFont {
23  public:
24   PlatformFontPango();
25   explicit PlatformFontPango(NativeFont native_font);
26   PlatformFontPango(const std::string& font_name, int font_size_pixels);
27
28   // Converts |gfx_font| to a new pango font. Free the returned font with
29   // pango_font_description_free().
30   static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font);
31
32   // Resets and reloads the cached system font used by the default constructor.
33   // This function is useful when the system font has changed, for example, when
34   // the locale has changed.
35   static void ReloadDefaultFont();
36
37 #if defined(OS_CHROMEOS)
38   // Sets the default font. |font_description| is a Pango font description that
39   // will be passed to pango_font_description_from_string().
40   static void SetDefaultFontDescription(const std::string& font_description);
41 #endif
42
43   // Position as an offset from the height of the drawn text, used to draw
44   // an underline. This is a negative number, so the underline would be
45   // drawn at y + height + underline_position.
46   double underline_position() const;
47   // The thickness to draw the underline.
48   double underline_thickness() const;
49
50   // Overridden from PlatformFont:
51   virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
52   virtual int GetHeight() const OVERRIDE;
53   virtual int GetBaseline() const OVERRIDE;
54   virtual int GetCapHeight() 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 const FontRenderParams& GetFontRenderParams() const OVERRIDE;
61   virtual NativeFont GetNativeFont() const OVERRIDE;
62
63  private:
64   // Create a new instance of this object with the specified properties. Called
65   // from DeriveFont.
66   PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
67                     const std::string& name,
68                     int size_pixels,
69                     int style,
70                     const FontRenderParams& params);
71   virtual ~PlatformFontPango();
72
73   // Initializes this object based on the passed-in details. If |typeface| is
74   // empty, a new typeface will be loaded.
75   void InitFromDetails(
76       const skia::RefPtr<SkTypeface>& typeface,
77       const std::string& font_family,
78       int font_size_pixels,
79       int style,
80       const FontRenderParams& params);
81
82   // Initializes this object as a copy of another PlatformFontPango.
83   void InitFromPlatformFont(const PlatformFontPango* other);
84
85   // Potentially slow call to get pango metrics (average width, underline info).
86   void InitPangoMetrics();
87
88   // Setup a Skia context to use the current typeface.
89   void PaintSetup(SkPaint* paint) const;
90
91   // Make |this| a copy of |other|.
92   void CopyFont(const Font& other);
93
94   // The average width of a character, initialized and cached if needed.
95   double GetAverageWidth() const;
96
97   skia::RefPtr<SkTypeface> typeface_;
98
99   // Additional information about the face.
100   // Skia actually expects a family name and not a font name.
101   std::string font_family_;
102   int font_size_pixels_;
103   int style_;
104
105   // Information describing how the font should be rendered.
106   FontRenderParams font_render_params_;
107
108   // Cached metrics, generated at construction.
109   int ascent_pixels_;
110   int height_pixels_;
111   int cap_height_pixels_;
112
113   // The pango metrics are much more expensive so we wait until we need them
114   // to compute them.
115   bool pango_metrics_inited_;
116   double average_width_pixels_;
117   double underline_position_pixels_;
118   double underline_thickness_pixels_;
119
120   // The default font, used for the default constructor.
121   static Font* default_font_;
122
123 #if defined(OS_CHROMEOS)
124   // A Pango font description.
125   static std::string* default_font_description_;
126 #endif
127
128   DISALLOW_COPY_AND_ASSIGN(PlatformFontPango);
129 };
130
131 }  // namespace gfx
132
133 #endif  // UI_GFX_PLATFORM_FONT_PANGO_H_