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