OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 UI_GFX_PLATFORM_FONT_WIN_H_ | |
6 #define UI_GFX_PLATFORM_FONT_WIN_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "ui/gfx/gfx_export.h" | |
13 #include "ui/gfx/platform_font.h" | |
14 | |
15 namespace gfx { | |
16 | |
17 class GFX_EXPORT PlatformFontWin : public PlatformFont { | |
18 public: | |
19 PlatformFontWin(); | |
20 explicit PlatformFontWin(NativeFont native_font); | |
21 PlatformFontWin(const std::string& font_name, int font_size); | |
22 | |
23 // Dialog units to pixels conversion. | |
24 // See http://support.microsoft.com/kb/145994 for details. | |
25 int horizontal_dlus_to_pixels(int dlus) const { | |
26 return dlus * font_ref_->GetDluBaseX() / 4; | |
27 } | |
28 int vertical_dlus_to_pixels(int dlus) const { | |
29 return dlus * font_ref_->height() / 8; | |
30 } | |
31 | |
32 // Callback that returns the minimum height that should be used for | |
33 // gfx::Fonts. Optional. If not specified, the minimum font size is 0. | |
34 typedef int (*GetMinimumFontSizeCallback)(); | |
35 static GetMinimumFontSizeCallback get_minimum_font_size_callback; | |
36 | |
37 // Callback that adjusts a LOGFONT to meet suitability requirements of the | |
38 // embedding application. Optional. If not specified, no adjustments are | |
39 // performed other than clamping to a minimum font height if | |
40 // |get_minimum_font_size_callback| is specified. | |
41 typedef void (*AdjustFontCallback)(LOGFONT* lf); | |
42 static AdjustFontCallback adjust_font_callback; | |
43 | |
44 // Returns the font name for the system locale. Some fonts, particularly | |
45 // East Asian fonts, have different names per locale. If the localized font | |
46 // name could not be retrieved, returns GetFontName(). | |
47 std::string GetLocalizedFontName() const; | |
48 | |
49 // Returns a derived Font with the specified |style| and with height at most | |
50 // |height|. If the height and style of the receiver already match, it is | |
51 // returned. Otherwise, the returned Font will have the largest size such that | |
52 // its height is less than or equal to |height| (since there may not exist a | |
53 // size that matches the exact |height| specified). | |
54 Font DeriveFontWithHeight(int height, int style); | |
55 | |
56 // Overridden from PlatformFont: | |
57 virtual Font DeriveFont(int size_delta, int style) const override; | |
58 virtual int GetHeight() const override; | |
59 virtual int GetBaseline() const override; | |
60 virtual int GetCapHeight() const override; | |
61 virtual int GetExpectedTextWidth(int length) const override; | |
62 virtual int GetStyle() const override; | |
63 virtual std::string GetFontName() const override; | |
64 virtual std::string GetActualFontNameForTesting() const override; | |
65 virtual int GetFontSize() const override; | |
66 virtual const FontRenderParams& GetFontRenderParams() const override; | |
67 virtual NativeFont GetNativeFont() const override; | |
68 | |
69 private: | |
70 virtual ~PlatformFontWin() {} | |
71 | |
72 // Chrome text drawing bottoms out in the Windows GDI functions that take an | |
73 // HFONT (an opaque handle into Windows). To avoid lots of GDI object | |
74 // allocation and destruction, Font indirectly refers to the HFONT by way of | |
75 // an HFontRef. That is, every Font has an HFontRef, which has an HFONT. | |
76 // | |
77 // HFontRef is reference counted. Upon deletion, it deletes the HFONT. | |
78 // By making HFontRef maintain the reference to the HFONT, multiple | |
79 // HFontRefs can share the same HFONT, and Font can provide value semantics. | |
80 class GFX_EXPORT HFontRef : public base::RefCounted<HFontRef> { | |
81 public: | |
82 // This constructor takes control of the HFONT, and will delete it when | |
83 // the HFontRef is deleted. | |
84 HFontRef(HFONT hfont, | |
85 int font_size, | |
86 int height, | |
87 int baseline, | |
88 int cap_height, | |
89 int ave_char_width, | |
90 int style); | |
91 | |
92 // Accessors | |
93 HFONT hfont() const { return hfont_; } | |
94 int height() const { return height_; } | |
95 int baseline() const { return baseline_; } | |
96 int cap_height() const { return cap_height_; } | |
97 int ave_char_width() const { return ave_char_width_; } | |
98 int style() const { return style_; } | |
99 const std::string& font_name() const { return font_name_; } | |
100 int font_size() const { return font_size_; } | |
101 int requested_font_size() const { return requested_font_size_; } | |
102 | |
103 // Returns the average character width in dialog units. | |
104 int GetDluBaseX(); | |
105 | |
106 private: | |
107 friend class base::RefCounted<HFontRef>; | |
108 | |
109 ~HFontRef(); | |
110 | |
111 const HFONT hfont_; | |
112 const int font_size_; | |
113 const int height_; | |
114 const int baseline_; | |
115 const int cap_height_; | |
116 const int ave_char_width_; | |
117 const int style_; | |
118 // Average character width in dialog units. This is queried lazily from the | |
119 // system, with an initial value of -1 meaning it hasn't yet been queried. | |
120 int dlu_base_x_; | |
121 std::string font_name_; | |
122 | |
123 // If the requested font size is not possible for the font, |font_size_| | |
124 // will be different than |requested_font_size_|. This is stored separately | |
125 // so that code that increases the font size in a loop will not cause the | |
126 // loop to get stuck on the same size. | |
127 int requested_font_size_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(HFontRef); | |
130 }; | |
131 | |
132 // Initializes this object with a copy of the specified HFONT. | |
133 void InitWithCopyOfHFONT(HFONT hfont); | |
134 | |
135 // Initializes this object with the specified font name and size. | |
136 void InitWithFontNameAndSize(const std::string& font_name, | |
137 int font_size); | |
138 | |
139 // Returns the base font ref. This should ONLY be invoked on the | |
140 // UI thread. | |
141 static HFontRef* GetBaseFontRef(); | |
142 | |
143 // Creates and returns a new HFONTRef from the specified HFONT. | |
144 static HFontRef* CreateHFontRef(HFONT font); | |
145 | |
146 // Creates and returns a new HFONTRef from the specified HFONT. Uses provided | |
147 // |font_metrics| instead of calculating new one. | |
148 static HFontRef* CreateHFontRef(HFONT font, const TEXTMETRIC& font_metrics); | |
149 | |
150 // Returns a largest derived Font whose height does not exceed the height of | |
151 // |base_font|. | |
152 static Font DeriveWithCorrectedSize(HFONT base_font); | |
153 | |
154 // Creates a new PlatformFontWin with the specified HFontRef. Used when | |
155 // constructing a Font from a HFONT we don't want to copy. | |
156 explicit PlatformFontWin(HFontRef* hfont_ref); | |
157 | |
158 // Reference to the base font all fonts are derived from. | |
159 static HFontRef* base_font_ref_; | |
160 | |
161 // Indirect reference to the HFontRef, which references the underlying HFONT. | |
162 scoped_refptr<HFontRef> font_ref_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(PlatformFontWin); | |
165 }; | |
166 | |
167 } // namespace gfx | |
168 | |
169 #endif // UI_GFX_PLATFORM_FONT_WIN_H_ | |
OLD | NEW |