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_
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "skia/ext/refptr.h"
11 #include "third_party/skia/include/core/SkRefCnt.h"
12 #include "ui/gfx/platform_font.h"
19 class UI_EXPORT PlatformFontPango
: public PlatformFont
{
22 explicit PlatformFontPango(NativeFont native_font
);
23 PlatformFontPango(const std::string
& font_name
, int font_size
);
25 // Converts |gfx_font| to a new pango font. Free the returned font with
26 // pango_font_description_free().
27 static PangoFontDescription
* PangoFontFromGfxFont(const gfx::Font
& gfx_font
);
29 // Resets and reloads the cached system font used by the default constructor.
30 // This function is useful when the system font has changed, for example, when
31 // the locale has changed.
32 static void ReloadDefaultFont();
34 // Position as an offset from the height of the drawn text, used to draw
35 // an underline. This is a negative number, so the underline would be
36 // drawn at y + height + underline_position.
37 double underline_position() const;
38 // The thickness to draw the underline.
39 double underline_thickness() const;
41 // Overridden from PlatformFont:
42 virtual Font
DeriveFont(int size_delta
, int style
) const OVERRIDE
;
43 virtual int GetHeight() const OVERRIDE
;
44 virtual int GetBaseline() const OVERRIDE
;
45 virtual int GetAverageCharacterWidth() const OVERRIDE
;
46 virtual int GetStringWidth(const base::string16
& text
) const OVERRIDE
;
47 virtual int GetExpectedTextWidth(int length
) const OVERRIDE
;
48 virtual int GetStyle() const OVERRIDE
;
49 virtual std::string
GetFontName() const OVERRIDE
;
50 virtual int GetFontSize() const OVERRIDE
;
51 virtual NativeFont
GetNativeFont() const OVERRIDE
;
54 // Create a new instance of this object with the specified properties. Called
56 PlatformFontPango(const skia::RefPtr
<SkTypeface
>& typeface
,
57 const std::string
& name
,
60 virtual ~PlatformFontPango();
62 // Initialize this object.
63 void InitWithNameAndSize(const std::string
& font_name
, int font_size
);
64 void InitWithTypefaceNameSizeAndStyle(
65 const skia::RefPtr
<SkTypeface
>& typeface
,
66 const std::string
& name
,
69 void InitFromPlatformFont(const PlatformFontPango
* other
);
71 // Potentially slow call to get pango metrics (average width, underline info).
72 void InitPangoMetrics();
74 // Setup a Skia context to use the current typeface.
75 void PaintSetup(SkPaint
* paint
) const;
77 // Make |this| a copy of |other|.
78 void CopyFont(const Font
& other
);
80 // The average width of a character, initialized and cached if needed.
81 double GetAverageWidth() const;
83 skia::RefPtr
<SkTypeface
> typeface_
;
85 // Additional information about the face
86 // Skia actually expects a family name and not a font name.
87 std::string font_family_
;
88 int font_size_pixels_
;
91 // Cached metrics, generated at construction.
95 // The pango metrics are much more expensive so we wait until we need them
97 bool pango_metrics_inited_
;
98 double average_width_pixels_
;
99 double underline_position_pixels_
;
100 double underline_thickness_pixels_
;
102 // The default font, used for the default constructor.
103 static Font
* default_font_
;
105 DISALLOW_COPY_AND_ASSIGN(PlatformFontPango
);
110 #endif // UI_GFX_PLATFORM_FONT_PANGO_H_