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 APP_GFX_CHROME_FONT_H_ | 5 #ifndef APP_GFX_FONT_H_ |
6 #define APP_GFX_CHROME_FONT_H_ | 6 #define APP_GFX_FONT_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
13 typedef struct HFONT__* HFONT; | 13 typedef struct HFONT__* HFONT; |
14 #elif defined(OS_LINUX) | 14 #elif defined(OS_LINUX) |
15 #include "third_party/skia/include/core/SkRefCnt.h" | 15 #include "third_party/skia/include/core/SkRefCnt.h" |
16 class SkPaint; | 16 class SkPaint; |
(...skipping 13 matching lines...) Expand all Loading... |
30 class SkTypeface; | 30 class SkTypeface; |
31 typedef SkTypeface* NativeFont; | 31 typedef SkTypeface* NativeFont; |
32 #else // null port. | 32 #else // null port. |
33 #error No known OS defined | 33 #error No known OS defined |
34 #endif | 34 #endif |
35 | 35 |
36 #include "base/basictypes.h" | 36 #include "base/basictypes.h" |
37 #include "base/ref_counted.h" | 37 #include "base/ref_counted.h" |
38 #include "base/scoped_ptr.h" | 38 #include "base/scoped_ptr.h" |
39 | 39 |
40 // ChromeFont provides a wrapper around an underlying font. Copy and assignment | 40 namespace gfx { |
| 41 |
| 42 // Font provides a wrapper around an underlying font. Copy and assignment |
41 // operators are explicitly allowed, and cheap. | 43 // operators are explicitly allowed, and cheap. |
42 class ChromeFont { | 44 class Font { |
43 public: | 45 public: |
44 // The following constants indicate the font style. | 46 // The following constants indicate the font style. |
45 enum { | 47 enum { |
46 NORMAL = 0, | 48 NORMAL = 0, |
47 BOLD = 1, | 49 BOLD = 1, |
48 ITALIC = 2, | 50 ITALIC = 2, |
49 UNDERLINED = 4, | 51 UNDERLINED = 4, |
50 }; | 52 }; |
51 | 53 |
52 // Creates a ChromeFont given font name (e.g. arial), font size (e.g. 12). | 54 // Creates a Font given font name (e.g. arial), font size (e.g. 12). |
53 // Skia actually expects a family name and not a font name. | 55 // Skia actually expects a family name and not a font name. |
54 static ChromeFont CreateFont(const std::wstring& font_name, int font_size); | 56 static Font CreateFont(const std::wstring& font_name, int font_size); |
55 | 57 |
56 ~ChromeFont() { } | 58 ~Font() { } |
57 | 59 |
58 // Returns a new Font derived from the existing font. | 60 // Returns a new Font derived from the existing font. |
59 // size_deta is the size to add to the current font. For example, a value | 61 // size_deta is the size to add to the current font. For example, a value |
60 // of 5 results in a font 5 units bigger than this font. | 62 // of 5 results in a font 5 units bigger than this font. |
61 ChromeFont DeriveFont(int size_delta) const { | 63 Font DeriveFont(int size_delta) const { |
62 return DeriveFont(size_delta, style()); | 64 return DeriveFont(size_delta, style()); |
63 } | 65 } |
64 | 66 |
65 // Returns a new Font derived from the existing font. | 67 // Returns a new Font derived from the existing font. |
66 // size_deta is the size to add to the current font. See the single | 68 // size_deta is the size to add to the current font. See the single |
67 // argument version of this method for an example. | 69 // argument version of this method for an example. |
68 // The style parameter specifies the new style for the font, and is a | 70 // The style parameter specifies the new style for the font, and is a |
69 // bitmask of the values: BOLD, ITALIC and UNDERLINED. | 71 // bitmask of the values: BOLD, ITALIC and UNDERLINED. |
70 ChromeFont DeriveFont(int size_delta, int style) const; | 72 Font DeriveFont(int size_delta, int style) const; |
71 | 73 |
72 // Returns the number of vertical pixels needed to display characters from | 74 // Returns the number of vertical pixels needed to display characters from |
73 // the specified font. | 75 // the specified font. |
74 int height() const; | 76 int height() const; |
75 | 77 |
76 // Returns the baseline, or ascent, of the font. | 78 // Returns the baseline, or ascent, of the font. |
77 int baseline() const; | 79 int baseline() const; |
78 | 80 |
79 // Returns the average character width for the font. | 81 // Returns the average character width for the font. |
80 int ave_char_width() const; | 82 int ave_char_width() const; |
(...skipping 14 matching lines...) Expand all Loading... |
95 // It is actually a font family name, because Skia expects a family name | 97 // It is actually a font family name, because Skia expects a family name |
96 // and not a font name. | 98 // and not a font name. |
97 std::wstring FontName(); | 99 std::wstring FontName(); |
98 | 100 |
99 // Font Size. | 101 // Font Size. |
100 int FontSize(); | 102 int FontSize(); |
101 | 103 |
102 NativeFont nativeFont() const; | 104 NativeFont nativeFont() const; |
103 | 105 |
104 // Creates a font with the default name and style. | 106 // Creates a font with the default name and style. |
105 ChromeFont(); | 107 Font(); |
106 | 108 |
107 #if defined(OS_WIN) | 109 #if defined(OS_WIN) |
108 // Creates a ChromeFont from the specified HFONT. The supplied HFONT is | 110 // Creates a Font from the specified HFONT. The supplied HFONT is effectively |
109 // effectively copied. | 111 // copied. |
110 static ChromeFont CreateFont(HFONT hfont); | 112 static Font CreateFont(HFONT hfont); |
111 | 113 |
112 // Returns the handle to the underlying HFONT. This is used by ChromeCanvas to | 114 // Returns the handle to the underlying HFONT. This is used by ChromeCanvas to |
113 // draw text. | 115 // draw text. |
114 HFONT hfont() const { return font_ref_->hfont(); } | 116 HFONT hfont() const { return font_ref_->hfont(); } |
115 | 117 |
116 // Dialog units to pixels conversion. | 118 // Dialog units to pixels conversion. |
117 // See http://support.microsoft.com/kb/145994 for details. | 119 // See http://support.microsoft.com/kb/145994 for details. |
118 int horizontal_dlus_to_pixels(int dlus) { | 120 int horizontal_dlus_to_pixels(int dlus) { |
119 return dlus * font_ref_->dlu_base_x() / 4; | 121 return dlus * font_ref_->dlu_base_x() / 4; |
120 } | 122 } |
121 int vertical_dlus_to_pixels(int dlus) { | 123 int vertical_dlus_to_pixels(int dlus) { |
122 return dlus * font_ref_->height() / 8; | 124 return dlus * font_ref_->height() / 8; |
123 } | 125 } |
124 #elif defined(OS_LINUX) | 126 #elif defined(OS_LINUX) |
125 // We need a copy constructor and assignment operator to deal with | 127 // We need a copy constructor and assignment operator to deal with |
126 // the Skia reference counting. | 128 // the Skia reference counting. |
127 ChromeFont(const ChromeFont& other); | 129 Font(const Font& other); |
128 ChromeFont& operator=(const ChromeFont& other); | 130 Font& operator=(const Font& other); |
129 // Setup a Skia context to use the current typeface | 131 // Setup a Skia context to use the current typeface |
130 void PaintSetup(SkPaint* paint) const; | 132 void PaintSetup(SkPaint* paint) const; |
131 #endif | 133 #endif |
132 | 134 |
133 private: | 135 private: |
134 | 136 |
135 #if defined(OS_WIN) | 137 #if defined(OS_WIN) |
136 // Chrome text drawing bottoms out in the Windows GDI functions that take an | 138 // Chrome text drawing bottoms out in the Windows GDI functions that take an |
137 // HFONT (an opaque handle into Windows). To avoid lots of GDI object | 139 // HFONT (an opaque handle into Windows). To avoid lots of GDI object |
138 // allocation and destruction, ChromeFont indirectly refers to the HFONT | 140 // allocation and destruction, Font indirectly refers to the HFONT by way of |
139 // by way of an HFontRef. That is, every ChromeFont has an HFontRef, which | 141 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT. |
140 // has an HFONT. | |
141 // | 142 // |
142 // HFontRef is reference counted. Upon deletion, it deletes the HFONT. | 143 // HFontRef is reference counted. Upon deletion, it deletes the HFONT. |
143 // By making HFontRef maintain the reference to the HFONT, multiple | 144 // By making HFontRef maintain the reference to the HFONT, multiple |
144 // HFontRefs can share the same HFONT, and ChromeFont can provide value | 145 // HFontRefs can share the same HFONT, and Font can provide value semantics. |
145 // semantics. | |
146 class HFontRef : public base::RefCounted<HFontRef> { | 146 class HFontRef : public base::RefCounted<HFontRef> { |
147 public: | 147 public: |
148 // This constructor takes control of the HFONT, and will delete it when | 148 // This constructor takes control of the HFONT, and will delete it when |
149 // the HFontRef is deleted. | 149 // the HFontRef is deleted. |
150 HFontRef(HFONT hfont, | 150 HFontRef(HFONT hfont, |
151 int height, | 151 int height, |
152 int baseline, | 152 int baseline, |
153 int ave_char_width, | 153 int ave_char_width, |
154 int style, | 154 int style, |
155 int dlu_base_x); | 155 int dlu_base_x); |
(...skipping 19 matching lines...) Expand all Loading... |
175 DISALLOW_COPY_AND_ASSIGN(HFontRef); | 175 DISALLOW_COPY_AND_ASSIGN(HFontRef); |
176 }; | 176 }; |
177 | 177 |
178 // Returns the base font ref. This should ONLY be invoked on the | 178 // Returns the base font ref. This should ONLY be invoked on the |
179 // UI thread. | 179 // UI thread. |
180 static HFontRef* GetBaseFontRef(); | 180 static HFontRef* GetBaseFontRef(); |
181 | 181 |
182 // Creates and returns a new HFONTRef from the specified HFONT. | 182 // Creates and returns a new HFONTRef from the specified HFONT. |
183 static HFontRef* CreateHFontRef(HFONT font); | 183 static HFontRef* CreateHFontRef(HFONT font); |
184 | 184 |
185 explicit ChromeFont(HFontRef* font_ref) : font_ref_(font_ref) { } | 185 explicit Font(HFontRef* font_ref) : font_ref_(font_ref) { } |
186 | 186 |
187 // Reference to the base font all fonts are derived from. | 187 // Reference to the base font all fonts are derived from. |
188 static HFontRef* base_font_ref_; | 188 static HFontRef* base_font_ref_; |
189 | 189 |
190 // Indirect reference to the HFontRef, which references the underlying HFONT. | 190 // Indirect reference to the HFontRef, which references the underlying HFONT. |
191 scoped_refptr<HFontRef> font_ref_; | 191 scoped_refptr<HFontRef> font_ref_; |
192 #elif defined(OS_LINUX) | 192 #elif defined(OS_LINUX) |
193 explicit ChromeFont(SkTypeface* typeface, const std::wstring& name, | 193 explicit Font(SkTypeface* typeface, const std::wstring& name, |
194 int size, int style); | 194 int size, int style); |
195 // Calculate and cache the font metrics. | 195 // Calculate and cache the font metrics. |
196 void calculateMetrics(); | 196 void calculateMetrics(); |
197 // Make |this| a copy of |other|. | 197 // Make |this| a copy of |other|. |
198 void CopyChromeFont(const ChromeFont& other); | 198 void CopyFont(const Font& other); |
199 | 199 |
200 // The default font, used for the default constructor. | 200 // The default font, used for the default constructor. |
201 static ChromeFont* default_font_; | 201 static Font* default_font_; |
202 | 202 |
203 // These two both point to the same SkTypeface. We use the SkAutoUnref to | 203 // These two both point to the same SkTypeface. We use the SkAutoUnref to |
204 // handle the reference counting, but without @typeface_ we would have to | 204 // handle the reference counting, but without @typeface_ we would have to |
205 // cast the SkRefCnt from @typeface_helper_ every time. | 205 // cast the SkRefCnt from @typeface_helper_ every time. |
206 scoped_ptr<SkAutoUnref> typeface_helper_; | 206 scoped_ptr<SkAutoUnref> typeface_helper_; |
207 SkTypeface *typeface_; | 207 SkTypeface *typeface_; |
208 | 208 |
209 // Additional information about the face | 209 // Additional information about the face |
210 // Skia actually expects a family name and not a font name. | 210 // Skia actually expects a family name and not a font name. |
211 std::wstring font_family_; | 211 std::wstring font_family_; |
212 int font_size_; | 212 int font_size_; |
213 int style_; | 213 int style_; |
214 | 214 |
215 // Cached metrics, generated at construction | 215 // Cached metrics, generated at construction |
216 int height_; | 216 int height_; |
217 int ascent_; | 217 int ascent_; |
218 int avg_width_; | 218 int avg_width_; |
219 #elif defined(OS_MACOSX) | 219 #elif defined(OS_MACOSX) |
220 explicit ChromeFont(const std::wstring& font_name, int font_size, int style); | 220 explicit Font(const std::wstring& font_name, int font_size, int style); |
221 | 221 |
222 // Calculate and cache the font metrics. | 222 // Calculate and cache the font metrics. |
223 void calculateMetrics(); | 223 void calculateMetrics(); |
224 | 224 |
225 std::wstring font_name_; | 225 std::wstring font_name_; |
226 int font_size_; | 226 int font_size_; |
227 int style_; | 227 int style_; |
228 | 228 |
229 // Cached metrics, generated at construction | 229 // Cached metrics, generated at construction |
230 int height_; | 230 int height_; |
231 int ascent_; | 231 int ascent_; |
232 int avg_width_; | 232 int avg_width_; |
233 #endif | 233 #endif |
234 | 234 |
235 }; | 235 }; |
236 | 236 |
237 #endif // APP_GFX_CHROME_FONT_H_ | 237 } // namespace gfx |
| 238 |
| 239 #endif // APP_GFX_FONT_H_ |
OLD | NEW |