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 "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"
13
14class SkTypeface;
15class SkPaint;
16
17namespace gfx {
18
19class UI_EXPORT PlatformFontPango : public PlatformFont {
20 public:
21  PlatformFontPango();
22  explicit PlatformFontPango(NativeFont native_font);
23  PlatformFontPango(const std::string& font_name, int font_size);
24
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);
28
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();
33
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;
40
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;
52
53 private:
54  // Create a new instance of this object with the specified properties. Called
55  // from DeriveFont.
56  PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
57                    const std::string& name,
58                    int size,
59                    int style);
60  virtual ~PlatformFontPango();
61
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,
67      int size,
68      int style);
69  void InitFromPlatformFont(const PlatformFontPango* other);
70
71  // Potentially slow call to get pango metrics (average width, underline info).
72  void InitPangoMetrics();
73
74  // Setup a Skia context to use the current typeface.
75  void PaintSetup(SkPaint* paint) const;
76
77  // Make |this| a copy of |other|.
78  void CopyFont(const Font& other);
79
80  // The average width of a character, initialized and cached if needed.
81  double GetAverageWidth() const;
82
83  skia::RefPtr<SkTypeface> typeface_;
84
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_;
89  int style_;
90
91  // Cached metrics, generated at construction.
92  int height_pixels_;
93  int ascent_pixels_;
94
95  // The pango metrics are much more expensive so we wait until we need them
96  // to compute them.
97  bool pango_metrics_inited_;
98  double average_width_pixels_;
99  double underline_position_pixels_;
100  double underline_thickness_pixels_;
101
102  // The default font, used for the default constructor.
103  static Font* default_font_;
104
105  DISALLOW_COPY_AND_ASSIGN(PlatformFontPango);
106};
107
108}  // namespace gfx
109
110#endif  // UI_GFX_PLATFORM_FONT_PANGO_H_
111