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.
5 #ifndef UI_GFX_PLATFORM_FONT_PANGO_H_
6 #define UI_GFX_PLATFORM_FONT_PANGO_H_
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"
22 class GFX_EXPORT PlatformFontPango : public PlatformFont {
25 explicit PlatformFontPango(NativeFont native_font);
26 PlatformFontPango(const std::string& font_name, int font_size_pixels);
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);
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();
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);
43 // Overridden from PlatformFont:
44 virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
45 virtual int GetHeight() const OVERRIDE;
46 virtual int GetBaseline() const OVERRIDE;
47 virtual int GetCapHeight() const OVERRIDE;
48 virtual int GetExpectedTextWidth(int length) const OVERRIDE;
49 virtual int GetStyle() const OVERRIDE;
50 virtual std::string GetFontName() const OVERRIDE;
51 virtual std::string GetActualFontNameForTesting() const OVERRIDE;
52 virtual int GetFontSize() const OVERRIDE;
53 virtual const FontRenderParams& GetFontRenderParams() const OVERRIDE;
54 virtual NativeFont GetNativeFont() const OVERRIDE;
57 // Create a new instance of this object with the specified properties. Called
59 PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
60 const std::string& name,
63 const FontRenderParams& params);
64 virtual ~PlatformFontPango();
66 // Initializes this object based on the passed-in details. If |typeface| is
67 // empty, a new typeface will be loaded.
69 const skia::RefPtr<SkTypeface>& typeface,
70 const std::string& font_family,
73 const FontRenderParams& params);
75 // Initializes this object as a copy of another PlatformFontPango.
76 void InitFromPlatformFont(const PlatformFontPango* other);
78 // Potentially slow call to get pango metrics (average width).
79 void InitPangoMetrics();
81 // Setup a Skia context to use the current typeface.
82 void PaintSetup(SkPaint* paint) const;
84 // Make |this| a copy of |other|.
85 void CopyFont(const Font& other);
87 // The average width of a character, initialized and cached if needed.
88 double GetAverageWidth() const;
90 skia::RefPtr<SkTypeface> typeface_;
92 // Additional information about the face.
93 // Skia actually expects a family name and not a font name.
94 std::string font_family_;
95 int font_size_pixels_;
98 // Information describing how the font should be rendered.
99 FontRenderParams font_render_params_;
101 // Cached metrics, generated at construction.
104 int cap_height_pixels_;
106 // The pango metrics are much more expensive so we wait until we need them
108 bool pango_metrics_inited_;
109 double average_width_pixels_;
111 // The default font, used for the default constructor.
112 static Font* default_font_;
114 #if defined(OS_CHROMEOS)
115 // A Pango font description.
116 static std::string* default_font_description_;
119 DISALLOW_COPY_AND_ASSIGN(PlatformFontPango);
124 #endif // UI_GFX_PLATFORM_FONT_PANGO_H_