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