blob: 6a1998760ccbd3b32b637fe0dd8a3bbc5bfd496e [file] [log] [blame]
[email protected]7c613752012-01-24 21:08:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]267c03d2011-02-02 23:03:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c8703bb2011-10-19 14:35:155#ifndef UI_GFX_PLATFORM_FONT_PANGO_H_
6#define UI_GFX_PLATFORM_FONT_PANGO_H_
[email protected]267c03d2011-02-02 23:03:077
[email protected]c963883562013-09-18 08:15:228#include <string>
9
[email protected]c92434032011-11-15 23:42:5010#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]3699ae52012-12-05 01:00:5412#include "skia/ext/refptr.h"
[email protected]267c03d2011-02-02 23:03:0713#include "third_party/skia/include/core/SkRefCnt.h"
[email protected]08397d52011-02-05 01:53:3814#include "ui/gfx/platform_font.h"
[email protected]267c03d2011-02-02 23:03:0715
16class SkTypeface;
17class SkPaint;
18
19namespace gfx {
20
[email protected]4ffa7892013-09-27 16:56:0621class GFX_EXPORT PlatformFontPango : public PlatformFont {
[email protected]267c03d2011-02-02 23:03:0722 public:
[email protected]b6bb36e22011-09-21 17:52:5423 PlatformFontPango();
[email protected]b6bb36e22011-09-21 17:52:5424 explicit PlatformFontPango(NativeFont native_font);
[email protected]610ae5f2011-10-27 23:55:3725 PlatformFontPango(const std::string& font_name, int font_size);
[email protected]267c03d2011-02-02 23:03:0726
27 // Converts |gfx_font| to a new pango font. Free the returned font with
28 // pango_font_description_free().
29 static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font);
30
[email protected]28e466c2011-04-06 05:01:2031 // Resets and reloads the cached system font used by the default constructor.
32 // This function is useful when the system font has changed, for example, when
33 // the locale has changed.
34 static void ReloadDefaultFont();
35
[email protected]c963883562013-09-18 08:15:2236#if defined(OS_CHROMEOS)
37 // Sets the default font.
38 static void SetDefaultFontDescription(const std::string& font_description);
39#endif
40
[email protected]267c03d2011-02-02 23:03:0741 // Position as an offset from the height of the drawn text, used to draw
42 // an underline. This is a negative number, so the underline would be
[email protected]354ee472012-08-30 21:08:1543 // drawn at y + height + underline_position.
[email protected]267c03d2011-02-02 23:03:0744 double underline_position() const;
45 // The thickness to draw the underline.
46 double underline_thickness() const;
47
48 // Overridden from PlatformFont:
[email protected]c92434032011-11-15 23:42:5049 virtual Font DeriveFont(int size_delta, int style) const OVERRIDE;
50 virtual int GetHeight() const OVERRIDE;
51 virtual int GetBaseline() const OVERRIDE;
[email protected]c831ea9c2013-10-01 10:52:2452 virtual int GetCapHeight() const OVERRIDE;
[email protected]c92434032011-11-15 23:42:5053 virtual int GetExpectedTextWidth(int length) const OVERRIDE;
54 virtual int GetStyle() const OVERRIDE;
55 virtual std::string GetFontName() const OVERRIDE;
[email protected]2fb26572013-12-11 08:43:0656 virtual std::string GetActualFontNameForTesting() const OVERRIDE;
[email protected]c92434032011-11-15 23:42:5057 virtual int GetFontSize() const OVERRIDE;
58 virtual NativeFont GetNativeFont() const OVERRIDE;
[email protected]267c03d2011-02-02 23:03:0759
60 private:
61 // Create a new instance of this object with the specified properties. Called
62 // from DeriveFont.
[email protected]3699ae52012-12-05 01:00:5463 PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
[email protected]610ae5f2011-10-27 23:55:3764 const std::string& name,
65 int size,
66 int style);
[email protected]b6bb36e22011-09-21 17:52:5467 virtual ~PlatformFontPango();
[email protected]267c03d2011-02-02 23:03:0768
[email protected]c963883562013-09-18 08:15:2269 // Returns a Pango font description (suitable for parsing by
70 // pango_font_description_from_string()) for the default UI font.
71 static std::string GetDefaultFont();
72
[email protected]d90c0cd2014-07-08 02:31:3473 // Initializes this object based on the passed-in details. If |typeface| is
74 // empty, a new typeface will be loaded.
75 void InitFromDetails(
[email protected]3699ae52012-12-05 01:00:5476 const skia::RefPtr<SkTypeface>& typeface,
[email protected]2fb26572013-12-11 08:43:0677 const std::string& font_family,
78 int font_size,
[email protected]3699ae52012-12-05 01:00:5479 int style);
[email protected]d90c0cd2014-07-08 02:31:3480
81 // Initializes this object as a copy of another PlatformFontPango.
[email protected]b6bb36e22011-09-21 17:52:5482 void InitFromPlatformFont(const PlatformFontPango* other);
[email protected]267c03d2011-02-02 23:03:0783
84 // Potentially slow call to get pango metrics (average width, underline info).
85 void InitPangoMetrics();
86
[email protected]354ee472012-08-30 21:08:1587 // Setup a Skia context to use the current typeface.
[email protected]267c03d2011-02-02 23:03:0788 void PaintSetup(SkPaint* paint) const;
89
90 // Make |this| a copy of |other|.
91 void CopyFont(const Font& other);
92
93 // The average width of a character, initialized and cached if needed.
94 double GetAverageWidth() const;
95
[email protected]3699ae52012-12-05 01:00:5496 skia::RefPtr<SkTypeface> typeface_;
[email protected]267c03d2011-02-02 23:03:0797
98 // Additional information about the face
99 // Skia actually expects a family name and not a font name.
[email protected]610ae5f2011-10-27 23:55:37100 std::string font_family_;
[email protected]267c03d2011-02-02 23:03:07101 int font_size_pixels_;
102 int style_;
103
[email protected]354ee472012-08-30 21:08:15104 // Cached metrics, generated at construction.
[email protected]267c03d2011-02-02 23:03:07105 int ascent_pixels_;
[email protected]d90c0cd2014-07-08 02:31:34106 int height_pixels_;
[email protected]0fbd8de2013-12-24 15:18:55107 int cap_height_pixels_;
[email protected]267c03d2011-02-02 23:03:07108
109 // The pango metrics are much more expensive so we wait until we need them
110 // to compute them.
111 bool pango_metrics_inited_;
112 double average_width_pixels_;
113 double underline_position_pixels_;
114 double underline_thickness_pixels_;
115
116 // The default font, used for the default constructor.
117 static Font* default_font_;
[email protected]354ee472012-08-30 21:08:15118
[email protected]c963883562013-09-18 08:15:22119#if defined(OS_CHROMEOS)
120 static std::string* default_font_description_;
121#endif
122
[email protected]354ee472012-08-30 21:08:15123 DISALLOW_COPY_AND_ASSIGN(PlatformFontPango);
[email protected]267c03d2011-02-02 23:03:07124};
125
126} // namespace gfx
127
[email protected]c8703bb2011-10-19 14:35:15128#endif // UI_GFX_PLATFORM_FONT_PANGO_H_