OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GFX_PLATFORM_FONT_GTK_ | 5 #ifndef GFX_PLATFORM_FONT_GTK_ |
6 #define GFX_PLATFORM_FONT_GTK_ | 6 #define GFX_PLATFORM_FONT_GTK_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "gfx/platform_font.h" | 10 #include "gfx/platform_font.h" |
11 #include "third_party/skia/include/core/SkRefCnt.h" | 11 #include "third_party/skia/include/core/SkRefCnt.h" |
12 | 12 |
13 class SkTypeface; | 13 class SkTypeface; |
14 class SkPaint; | 14 class SkPaint; |
15 | 15 |
16 namespace gfx { | 16 namespace gfx { |
17 | 17 |
18 class PlatformFontGtk : public PlatformFont { | 18 class PlatformFontGtk : public PlatformFont { |
19 public: | 19 public: |
20 PlatformFontGtk(); | 20 PlatformFontGtk(); |
21 explicit PlatformFontGtk(const Font& other); | 21 explicit PlatformFontGtk(const Font& other); |
22 explicit PlatformFontGtk(NativeFont native_font); | 22 explicit PlatformFontGtk(NativeFont native_font); |
23 PlatformFontGtk(const string16& font_name, | 23 PlatformFontGtk(const std::wstring& font_name, |
24 int font_size); | 24 int font_size); |
25 | 25 |
26 // Converts |gfx_font| to a new pango font. Free the returned font with | 26 // Converts |gfx_font| to a new pango font. Free the returned font with |
27 // pango_font_description_free(). | 27 // pango_font_description_free(). |
28 static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font); | 28 static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font); |
29 | 29 |
30 // Return the scale factor for fonts that account for DPI. | 30 // Return the scale factor for fonts that account for DPI. |
31 static float GetPangoScaleFactor(); | 31 static float GetPangoScaleFactor(); |
32 | 32 |
33 // Position as an offset from the height of the drawn text, used to draw | 33 // Position as an offset from the height of the drawn text, used to draw |
34 // an underline. This is a negative number, so the underline would be | 34 // an underline. This is a negative number, so the underline would be |
35 // drawn at y + height + underline_position; | 35 // drawn at y + height + underline_position; |
36 double underline_position() const; | 36 double underline_position() const; |
37 // The thickness to draw the underline. | 37 // The thickness to draw the underline. |
38 double underline_thickness() const; | 38 double underline_thickness() const; |
39 | 39 |
40 // Overridden from PlatformFont: | 40 // Overridden from PlatformFont: |
41 virtual Font DeriveFont(int size_delta, int style) const; | 41 virtual Font DeriveFont(int size_delta, int style) const; |
42 virtual int GetHeight() const; | 42 virtual int GetHeight() const; |
43 virtual int GetBaseline() const; | 43 virtual int GetBaseline() const; |
44 virtual int GetAverageCharacterWidth() const; | 44 virtual int GetAverageCharacterWidth() const; |
45 virtual int GetStringWidth(const string16& text) const; | 45 virtual int GetStringWidth(const string16& text) const; |
46 virtual int GetExpectedTextWidth(int length) const; | 46 virtual int GetExpectedTextWidth(int length) const; |
47 virtual int GetStyle() const; | 47 virtual int GetStyle() const; |
48 virtual string16 GetFontName() const; | 48 virtual const std::wstring& GetFontName() const; |
49 virtual int GetFontSize() const; | 49 virtual int GetFontSize() const; |
50 virtual NativeFont GetNativeFont() const; | 50 virtual NativeFont GetNativeFont() const; |
51 | 51 |
52 private: | 52 private: |
53 // Create a new instance of this object with the specified properties. Called | 53 // Create a new instance of this object with the specified properties. Called |
54 // from DeriveFont. | 54 // from DeriveFont. |
55 PlatformFontGtk(SkTypeface* typeface, | 55 PlatformFontGtk(SkTypeface* typeface, |
56 const string16& name, | 56 const std::wstring& name, |
57 int size, | 57 int size, |
58 int style); | 58 int style); |
59 virtual ~PlatformFontGtk(); | 59 virtual ~PlatformFontGtk(); |
60 | 60 |
61 // Initialize this object. | 61 // Initialize this object. |
62 void InitWithNameAndSize(const string16& font_name, int font_size); | 62 void InitWithNameAndSize(const std::wstring& font_name, int font_size); |
63 void InitWithTypefaceNameSizeAndStyle(SkTypeface* typeface, | 63 void InitWithTypefaceNameSizeAndStyle(SkTypeface* typeface, |
64 const string16& name, | 64 const std::wstring& name, |
65 int size, | 65 int size, |
66 int style); | 66 int style); |
67 void InitFromPlatformFont(const PlatformFontGtk* other); | 67 void InitFromPlatformFont(const PlatformFontGtk* other); |
68 | 68 |
69 // Potentially slow call to get pango metrics (average width, underline info). | 69 // Potentially slow call to get pango metrics (average width, underline info). |
70 void InitPangoMetrics(); | 70 void InitPangoMetrics(); |
71 | 71 |
72 // Setup a Skia context to use the current typeface | 72 // Setup a Skia context to use the current typeface |
73 void PaintSetup(SkPaint* paint) const; | 73 void PaintSetup(SkPaint* paint) const; |
74 | 74 |
75 // Make |this| a copy of |other|. | 75 // Make |this| a copy of |other|. |
76 void CopyFont(const Font& other); | 76 void CopyFont(const Font& other); |
77 | 77 |
78 // The average width of a character, initialized and cached if needed. | 78 // The average width of a character, initialized and cached if needed. |
79 double GetAverageWidth() const; | 79 double GetAverageWidth() const; |
80 | 80 |
81 // These two both point to the same SkTypeface. We use the SkAutoUnref to | 81 // These two both point to the same SkTypeface. We use the SkAutoUnref to |
82 // handle the reference counting, but without @typeface_ we would have to | 82 // handle the reference counting, but without @typeface_ we would have to |
83 // cast the SkRefCnt from @typeface_helper_ every time. | 83 // cast the SkRefCnt from @typeface_helper_ every time. |
84 scoped_ptr<SkAutoUnref> typeface_helper_; | 84 scoped_ptr<SkAutoUnref> typeface_helper_; |
85 SkTypeface *typeface_; | 85 SkTypeface *typeface_; |
86 | 86 |
87 // Additional information about the face | 87 // Additional information about the face |
88 // Skia actually expects a family name and not a font name. | 88 // Skia actually expects a family name and not a font name. |
89 string16 font_family_; | 89 std::wstring font_family_; |
90 int font_size_; | 90 int font_size_; |
91 int style_; | 91 int style_; |
92 | 92 |
93 // Cached metrics, generated at construction | 93 // Cached metrics, generated at construction |
94 int height_; | 94 int height_; |
95 int ascent_; | 95 int ascent_; |
96 | 96 |
97 // The pango metrics are much more expensive so we wait until we need them | 97 // The pango metrics are much more expensive so we wait until we need them |
98 // to compute them. | 98 // to compute them. |
99 bool pango_metrics_inited_; | 99 bool pango_metrics_inited_; |
100 double average_width_; | 100 double average_width_; |
101 double underline_position_; | 101 double underline_position_; |
102 double underline_thickness_; | 102 double underline_thickness_; |
103 | 103 |
104 // The default font, used for the default constructor. | 104 // The default font, used for the default constructor. |
105 static Font* default_font_; | 105 static Font* default_font_; |
106 }; | 106 }; |
107 | 107 |
108 } // namespace gfx | 108 } // namespace gfx |
109 | 109 |
110 #endif // GFX_PLATFORM_FONT_GTK_ | 110 #endif // GFX_PLATFORM_FONT_GTK_ |
111 | 111 |
OLD | NEW |