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
17class SkTypeface;
18class SkPaint;
19
20namespace gfx {
21
22class 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  // 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;
55
56 private:
57  // Create a new instance of this object with the specified properties. Called
58  // from DeriveFont.
59  PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
60                    const std::string& name,
61                    int size_pixels,
62                    int style,
63                    const FontRenderParams& params);
64  virtual ~PlatformFontPango();
65
66  // Initializes this object based on the passed-in details. If |typeface| is
67  // empty, a new typeface will be loaded.
68  void InitFromDetails(
69      const skia::RefPtr<SkTypeface>& typeface,
70      const std::string& font_family,
71      int font_size_pixels,
72      int style,
73      const FontRenderParams& params);
74
75  // Initializes this object as a copy of another PlatformFontPango.
76  void InitFromPlatformFont(const PlatformFontPango* other);
77
78  // Potentially slow call to get pango metrics (average width).
79  void InitPangoMetrics();
80
81  // Setup a Skia context to use the current typeface.
82  void PaintSetup(SkPaint* paint) const;
83
84  // Make |this| a copy of |other|.
85  void CopyFont(const Font& other);
86
87  // The average width of a character, initialized and cached if needed.
88  double GetAverageWidth() const;
89
90  skia::RefPtr<SkTypeface> typeface_;
91
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_;
96  int style_;
97
98  // Information describing how the font should be rendered.
99  FontRenderParams font_render_params_;
100
101  // Cached metrics, generated at construction.
102  int ascent_pixels_;
103  int height_pixels_;
104  int cap_height_pixels_;
105
106  // The pango metrics are much more expensive so we wait until we need them
107  // to compute them.
108  bool pango_metrics_inited_;
109  double average_width_pixels_;
110
111  // The default font, used for the default constructor.
112  static Font* default_font_;
113
114#if defined(OS_CHROMEOS)
115  // A Pango font description.
116  static std::string* default_font_description_;
117#endif
118
119  DISALLOW_COPY_AND_ASSIGN(PlatformFontPango);
120};
121
122}  // namespace gfx
123
124#endif  // UI_GFX_PLATFORM_FONT_PANGO_H_
125