OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_FONT_H_ | 5 #ifndef GFX_FONT_H_ |
6 #define GFX_FONT_H_ | 6 #define GFX_FONT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "build/build_config.h" | |
10 | |
11 #include <string> | 9 #include <string> |
12 | 10 |
13 #if defined(OS_WIN) | |
14 typedef struct HFONT__* HFONT; | |
15 #elif !defined(OS_MACOSX) | |
16 #include "third_party/skia/include/core/SkRefCnt.h" | |
17 class SkPaint; | |
18 class SkTypeface; | |
19 #endif | |
20 | |
21 #if defined(OS_WIN) | |
22 typedef struct HFONT__* NativeFont; | |
23 #elif defined(OS_MACOSX) | |
24 #ifdef __OBJC__ | |
25 @class NSFont; | |
26 #else | |
27 class NSFont; | |
28 #endif | |
29 typedef NSFont* NativeFont; | |
30 #else | |
31 typedef struct _PangoFontDescription PangoFontDescription; | |
32 class SkTypeface; | |
33 typedef SkTypeface* NativeFont; | |
34 #endif | |
35 | |
36 #include "base/basictypes.h" | |
37 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
38 #include "base/scoped_ptr.h" | 12 #include "gfx/native_widget_types.h" |
39 | 13 |
40 namespace gfx { | 14 namespace gfx { |
41 | 15 |
| 16 class PlatformFont; |
| 17 |
42 // Font provides a wrapper around an underlying font. Copy and assignment | 18 // Font provides a wrapper around an underlying font. Copy and assignment |
43 // operators are explicitly allowed, and cheap. | 19 // operators are explicitly allowed, and cheap. |
44 class Font { | 20 class Font { |
45 public: | 21 public: |
46 // The following constants indicate the font style. | 22 // The following constants indicate the font style. |
47 enum { | 23 enum FontStyle { |
48 NORMAL = 0, | 24 NORMAL = 0, |
49 BOLD = 1, | 25 BOLD = 1, |
50 ITALIC = 2, | 26 ITALIC = 2, |
51 UNDERLINED = 4, | 27 UNDERLINED = 4, |
52 }; | 28 }; |
53 | 29 |
54 // Creates a Font given font name (e.g. arial), font size (e.g. 12). | 30 // Creates a font with the default name and style. |
55 // Skia actually expects a family name and not a font name. | 31 Font(); |
56 static Font CreateFont(const std::wstring& font_name, int font_size); | |
57 | 32 |
58 ~Font() { } | 33 // Creates a font that is a clone of another font object. |
| 34 Font(const Font& other); |
| 35 gfx::Font& operator=(const Font& other); |
| 36 |
| 37 // Creates a font from the specified native font. |
| 38 explicit Font(NativeFont native_font); |
| 39 |
| 40 // Construct a Font object with the specified PlatformFont object. The Font |
| 41 // object takes ownership of the PlatformFont object. |
| 42 explicit Font(PlatformFont* platform_font); |
| 43 |
| 44 // Creates a font with the specified name and size. |
| 45 Font(const std::wstring& font_name, int font_size); |
| 46 |
| 47 ~Font(); |
59 | 48 |
60 // Returns a new Font derived from the existing font. | 49 // Returns a new Font derived from the existing font. |
61 // size_deta is the size to add to the current font. For example, a value | 50 // size_deta is the size to add to the current font. For example, a value |
62 // of 5 results in a font 5 units bigger than this font. | 51 // of 5 results in a font 5 units bigger than this font. |
63 Font DeriveFont(int size_delta) const { | 52 Font DeriveFont(int size_delta) const; |
64 return DeriveFont(size_delta, style()); | |
65 } | |
66 | 53 |
67 // Returns a new Font derived from the existing font. | 54 // Returns a new Font derived from the existing font. |
68 // size_delta is the size to add to the current font. See the single | 55 // size_delta is the size to add to the current font. See the single |
69 // argument version of this method for an example. | 56 // argument version of this method for an example. |
70 // The style parameter specifies the new style for the font, and is a | 57 // The style parameter specifies the new style for the font, and is a |
71 // bitmask of the values: BOLD, ITALIC and UNDERLINED. | 58 // bitmask of the values: BOLD, ITALIC and UNDERLINED. |
72 Font DeriveFont(int size_delta, int style) const; | 59 Font DeriveFont(int size_delta, int style) const; |
73 | 60 |
74 // Returns the number of vertical pixels needed to display characters from | 61 // Returns the number of vertical pixels needed to display characters from |
75 // the specified font. This may include some leading, i.e. height may be | 62 // the specified font. This may include some leading, i.e. height may be |
76 // greater than just ascent + descent. Specifically, the Windows and Mac | 63 // greater than just ascent + descent. Specifically, the Windows and Mac |
77 // implementations include leading and the Linux one does not. This may | 64 // implementations include leading and the Linux one does not. This may |
78 // need to be revisited in the future. | 65 // need to be revisited in the future. |
79 int height() const; | 66 int GetHeight() const; |
80 | 67 |
81 // Returns the baseline, or ascent, of the font. | 68 // Returns the baseline, or ascent, of the font. |
82 int baseline() const; | 69 int GetBaseline() const; |
83 | 70 |
84 // Returns the average character width for the font. | 71 // Returns the average character width for the font. |
85 int ave_char_width() const; | 72 int GetAverageCharacterWidth() const; |
86 | 73 |
87 // Returns the number of horizontal pixels needed to display the specified | 74 // Returns the number of horizontal pixels needed to display the specified |
88 // string. | 75 // string. |
89 int GetStringWidth(const std::wstring& text) const; | 76 int GetStringWidth(const std::wstring& text) const; |
90 | 77 |
91 // Returns the expected number of horizontal pixels needed to display | 78 // Returns the expected number of horizontal pixels needed to display the |
92 // the specified length of characters. | 79 // specified length of characters. Call GetStringWidth() to retrieve the |
93 // Call GetStringWidth() to retrieve the actual number. | 80 // actual number. |
94 int GetExpectedTextWidth(int length) const; | 81 int GetExpectedTextWidth(int length) const; |
95 | 82 |
96 // Returns the style of the font. | 83 // Returns the style of the font. |
97 int style() const; | 84 int GetStyle() const; |
98 | 85 |
99 // Font Name. | 86 // Returns the font name. |
100 // It is actually a font family name, because Skia expects a family name | 87 const std::wstring& GetFontName() const; |
101 // and not a font name. | |
102 const std::wstring& FontName() const; | |
103 | 88 |
104 // Font Size. | 89 // Returns the font size in pixels. |
105 int FontSize(); | 90 int GetFontSize() const; |
106 | 91 |
107 NativeFont nativeFont() const; | 92 // Returns the native font handle. |
| 93 // Lifetime lore: |
| 94 // Windows: This handle is owned by the Font object, and should not be |
| 95 // destroyed by the caller. |
| 96 // Mac: Caller must release this object. |
| 97 // Gtk: This handle is created on demand, and must be freed by calling |
| 98 // pango_font_description_free() when the caller is done using it. |
| 99 NativeFont GetNativeFont() const; |
108 | 100 |
109 // Creates a font with the default name and style. | 101 // Raw access to the underlying platform font implementation. Can be |
110 Font(); | 102 // static_cast to a known implementation type if needed. |
111 | 103 PlatformFont* platform_font() const { return platform_font_.get(); } |
112 #if defined(OS_WIN) | |
113 // Creates a Font from the specified HFONT. The supplied HFONT is effectively | |
114 // copied. | |
115 static Font CreateFont(HFONT hfont); | |
116 | |
117 // Returns the handle to the underlying HFONT. This is used by gfx::Canvas to | |
118 // draw text. | |
119 HFONT hfont() const { return font_ref_->hfont(); } | |
120 | |
121 // Dialog units to pixels conversion. | |
122 // See http://support.microsoft.com/kb/145994 for details. | |
123 int horizontal_dlus_to_pixels(int dlus) { | |
124 return dlus * font_ref_->dlu_base_x() / 4; | |
125 } | |
126 int vertical_dlus_to_pixels(int dlus) { | |
127 return dlus * font_ref_->height() / 8; | |
128 } | |
129 | |
130 // Callback that returns the minimum height that should be used for | |
131 // gfx::Fonts. Optional. If not specified, the minimum font size is 0. | |
132 typedef int (*GetMinimumFontSizeCallback)(); | |
133 static GetMinimumFontSizeCallback get_minimum_font_size_callback; | |
134 | |
135 // Callback that adjusts a LOGFONT to meet suitability requirements of the | |
136 // embedding application. Optional. If not specified, no adjustments are | |
137 // performed other than clamping to a minimum font height if | |
138 // |get_minimum_font_size_callback| is specified. | |
139 typedef void (*AdjustFontCallback)(LOGFONT* lf); | |
140 static AdjustFontCallback adjust_font_callback; | |
141 | |
142 #elif !defined(OS_MACOSX) | |
143 static Font CreateFont(PangoFontDescription* desc); | |
144 // We need a copy constructor and assignment operator to deal with | |
145 // the Skia reference counting. | |
146 Font(const Font& other); | |
147 Font& operator=(const Font& other); | |
148 // Setup a Skia context to use the current typeface | |
149 void PaintSetup(SkPaint* paint) const; | |
150 | |
151 // Converts |gfx_font| to a new pango font. Free the returned font with | |
152 // pango_font_description_free(). | |
153 static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font); | |
154 | |
155 // Position as an offset from the height of the drawn text, used to draw | |
156 // an underline. This is a negative number, so the underline would be | |
157 // drawn at y + height + underline_position; | |
158 double underline_position() const; | |
159 // The thickness to draw the underline. | |
160 double underline_thickness() const; | |
161 #endif | |
162 | 104 |
163 private: | 105 private: |
164 | 106 // Wrapped platform font implementation. |
165 #if defined(OS_WIN) | 107 scoped_refptr<PlatformFont> platform_font_; |
166 // Chrome text drawing bottoms out in the Windows GDI functions that take an | |
167 // HFONT (an opaque handle into Windows). To avoid lots of GDI object | |
168 // allocation and destruction, Font indirectly refers to the HFONT by way of | |
169 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT. | |
170 // | |
171 // HFontRef is reference counted. Upon deletion, it deletes the HFONT. | |
172 // By making HFontRef maintain the reference to the HFONT, multiple | |
173 // HFontRefs can share the same HFONT, and Font can provide value semantics. | |
174 class HFontRef : public base::RefCounted<HFontRef> { | |
175 public: | |
176 // This constructor takes control of the HFONT, and will delete it when | |
177 // the HFontRef is deleted. | |
178 HFontRef(HFONT hfont, | |
179 int height, | |
180 int baseline, | |
181 int ave_char_width, | |
182 int style, | |
183 int dlu_base_x); | |
184 | |
185 // Accessors | |
186 HFONT hfont() const { return hfont_; } | |
187 int height() const { return height_; } | |
188 int baseline() const { return baseline_; } | |
189 int ave_char_width() const { return ave_char_width_; } | |
190 int style() const { return style_; } | |
191 int dlu_base_x() const { return dlu_base_x_; } | |
192 const std::wstring& font_name() const { return font_name_; } | |
193 | |
194 private: | |
195 friend class base::RefCounted<HFontRef>; | |
196 | |
197 ~HFontRef(); | |
198 | |
199 const HFONT hfont_; | |
200 const int height_; | |
201 const int baseline_; | |
202 const int ave_char_width_; | |
203 const int style_; | |
204 // Constants used in converting dialog units to pixels. | |
205 const int dlu_base_x_; | |
206 std::wstring font_name_; | |
207 | |
208 DISALLOW_COPY_AND_ASSIGN(HFontRef); | |
209 }; | |
210 | |
211 // Returns the base font ref. This should ONLY be invoked on the | |
212 // UI thread. | |
213 static HFontRef* GetBaseFontRef(); | |
214 | |
215 // Creates and returns a new HFONTRef from the specified HFONT. | |
216 static HFontRef* CreateHFontRef(HFONT font); | |
217 | |
218 explicit Font(HFontRef* font_ref) : font_ref_(font_ref) { } | |
219 | |
220 // Reference to the base font all fonts are derived from. | |
221 static HFontRef* base_font_ref_; | |
222 | |
223 // Indirect reference to the HFontRef, which references the underlying HFONT. | |
224 scoped_refptr<HFontRef> font_ref_; | |
225 #elif !defined(OS_MACOSX) | |
226 explicit Font(SkTypeface* typeface, const std::wstring& name, | |
227 int size, int style); | |
228 // Calculate and cache the font metrics. | |
229 void calculateMetrics(); | |
230 // Make |this| a copy of |other|. | |
231 void CopyFont(const Font& other); | |
232 | |
233 // The default font, used for the default constructor. | |
234 static Font* default_font_; | |
235 | |
236 // Return the scale factor for fonts that account for DPI. | |
237 static float GetPangoScaleFactor(); | |
238 | |
239 // The average width of a character, initialized and cached if needed. | |
240 double avg_width() const; | |
241 | |
242 // Potentially slow call to get pango metrics (avg width, underline info). | |
243 void InitPangoMetrics(); | |
244 | |
245 // These two both point to the same SkTypeface. We use the SkAutoUnref to | |
246 // handle the reference counting, but without @typeface_ we would have to | |
247 // cast the SkRefCnt from @typeface_helper_ every time. | |
248 scoped_ptr<SkAutoUnref> typeface_helper_; | |
249 SkTypeface *typeface_; | |
250 | |
251 // Additional information about the face | |
252 // Skia actually expects a family name and not a font name. | |
253 std::wstring font_family_; | |
254 int font_size_; | |
255 int style_; | |
256 | |
257 // Cached metrics, generated at construction | |
258 int height_; | |
259 int ascent_; | |
260 | |
261 // The pango metrics are much more expensive so we wait until we need them | |
262 // to compute them. | |
263 bool pango_metrics_inited_; | |
264 double avg_width_; | |
265 double underline_position_; | |
266 double underline_thickness_; | |
267 #else // OS_MACOSX | |
268 explicit Font(const std::wstring& font_name, int font_size, int style); | |
269 | |
270 // Calculate and cache the font metrics. | |
271 void calculateMetrics(); | |
272 | |
273 std::wstring font_name_; | |
274 int font_size_; | |
275 int style_; | |
276 | |
277 // Cached metrics, generated at construction | |
278 int height_; | |
279 int ascent_; | |
280 int avg_width_; | |
281 #endif | |
282 | |
283 }; | 108 }; |
284 | 109 |
285 } // namespace gfx | 110 } // namespace gfx |
286 | 111 |
287 #endif // GFX_FONT_H_ | 112 #endif // GFX_FONT_H_ |
| 113 |
OLD | NEW |