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 #include "app/gfx/chrome_font.h" | 5 #include "app/gfx/chrome_font.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <math.h> | 8 #include <math.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "app/l10n_util_win.h" | 12 #include "app/l10n_util_win.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/win_util.h" | 14 #include "base/win_util.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
16 #include "grit/locale_settings.h" | 16 #include "grit/locale_settings.h" |
17 | 17 |
| 18 namespace gfx { |
| 19 |
18 /*static*/ | 20 /*static*/ |
19 ChromeFont::HFontRef* ChromeFont::base_font_ref_; | 21 Font::HFontRef* Font::base_font_ref_; |
20 | 22 |
21 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the | 23 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the |
22 // font is bold. | 24 // font is bold. |
23 static const int kTextMetricWeightBold = 700; | 25 static const int kTextMetricWeightBold = 700; |
24 | 26 |
25 // | 27 // |
26 // ChromeFont | 28 // Font |
27 // | 29 // |
28 | 30 |
29 ChromeFont::ChromeFont() | 31 Font::Font() |
30 : font_ref_(GetBaseFontRef()) { | 32 : font_ref_(GetBaseFontRef()) { |
31 } | 33 } |
32 | 34 |
33 int ChromeFont::height() const { | 35 int Font::height() const { |
34 return font_ref_->height(); | 36 return font_ref_->height(); |
35 } | 37 } |
36 | 38 |
37 int ChromeFont::baseline() const { | 39 int Font::baseline() const { |
38 return font_ref_->baseline(); | 40 return font_ref_->baseline(); |
39 } | 41 } |
40 | 42 |
41 int ChromeFont::ave_char_width() const { | 43 int Font::ave_char_width() const { |
42 return font_ref_->ave_char_width(); | 44 return font_ref_->ave_char_width(); |
43 } | 45 } |
44 | 46 |
45 int ChromeFont::GetExpectedTextWidth(int length) const { | 47 int Font::GetExpectedTextWidth(int length) const { |
46 return length * std::min(font_ref_->dlu_base_x(), ave_char_width()); | 48 return length * std::min(font_ref_->dlu_base_x(), ave_char_width()); |
47 } | 49 } |
48 | 50 |
49 int ChromeFont::style() const { | 51 int Font::style() const { |
50 return font_ref_->style(); | 52 return font_ref_->style(); |
51 } | 53 } |
52 | 54 |
53 NativeFont ChromeFont::nativeFont() const { | 55 NativeFont Font::nativeFont() const { |
54 return hfont(); | 56 return hfont(); |
55 } | 57 } |
56 | 58 |
57 // static | 59 // static |
58 ChromeFont ChromeFont::CreateFont(HFONT font) { | 60 Font Font::CreateFont(HFONT font) { |
59 DCHECK(font); | 61 DCHECK(font); |
60 LOGFONT font_info; | 62 LOGFONT font_info; |
61 GetObject(font, sizeof(LOGFONT), &font_info); | 63 GetObject(font, sizeof(LOGFONT), &font_info); |
62 return ChromeFont(CreateHFontRef(CreateFontIndirect(&font_info))); | 64 return Font(CreateHFontRef(CreateFontIndirect(&font_info))); |
63 } | 65 } |
64 | 66 |
65 ChromeFont ChromeFont::CreateFont(const std::wstring& font_name, | 67 Font Font::CreateFont(const std::wstring& font_name, int font_size) { |
66 int font_size) { | |
67 HDC hdc = GetDC(NULL); | 68 HDC hdc = GetDC(NULL); |
68 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); | 69 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); |
69 ReleaseDC(NULL, hdc); | 70 ReleaseDC(NULL, hdc); |
70 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 71 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
71 font_name.c_str()); | 72 font_name.c_str()); |
72 return ChromeFont::CreateFont(hf); | 73 return Font::CreateFont(hf); |
73 } | 74 } |
74 | 75 |
75 // static | 76 // static |
76 ChromeFont::HFontRef* ChromeFont::GetBaseFontRef() { | 77 Font::HFontRef* Font::GetBaseFontRef() { |
77 if (base_font_ref_ == NULL) { | 78 if (base_font_ref_ == NULL) { |
78 NONCLIENTMETRICS metrics; | 79 NONCLIENTMETRICS metrics; |
79 win_util::GetNonClientMetrics(&metrics); | 80 win_util::GetNonClientMetrics(&metrics); |
80 | 81 |
81 l10n_util::AdjustUIFont(&metrics.lfMessageFont); | 82 l10n_util::AdjustUIFont(&metrics.lfMessageFont); |
82 | 83 |
83 // See comment in ChromeFont::DeriveFont() about font size. | 84 // See comment in Font::DeriveFont() about font size. |
84 // TODO(jungshik): Add a per-locale resource entry for the minimum | 85 // TODO(jungshik): Add a per-locale resource entry for the minimum |
85 // font size and actually enforce the lower-bound. 5 is way too small | 86 // font size and actually enforce the lower-bound. 5 is way too small |
86 // for CJK, Thai, and Indian locales. | 87 // for CJK, Thai, and Indian locales. |
87 DCHECK_GE(abs(metrics.lfMessageFont.lfHeight), 5); | 88 DCHECK_GE(abs(metrics.lfMessageFont.lfHeight), 5); |
88 HFONT font = CreateFontIndirect(&metrics.lfMessageFont); | 89 HFONT font = CreateFontIndirect(&metrics.lfMessageFont); |
89 DLOG_ASSERT(font); | 90 DLOG_ASSERT(font); |
90 base_font_ref_ = ChromeFont::CreateHFontRef(font); | 91 base_font_ref_ = Font::CreateHFontRef(font); |
91 // base_font_ref_ is global, up the ref count so it's never deleted. | 92 // base_font_ref_ is global, up the ref count so it's never deleted. |
92 base_font_ref_->AddRef(); | 93 base_font_ref_->AddRef(); |
93 } | 94 } |
94 return base_font_ref_; | 95 return base_font_ref_; |
95 } | 96 } |
96 | 97 |
97 std::wstring ChromeFont::FontName() { | 98 std::wstring Font::FontName() { |
98 LOGFONT font_info; | 99 LOGFONT font_info; |
99 GetObject(hfont(), sizeof(LOGFONT), &font_info); | 100 GetObject(hfont(), sizeof(LOGFONT), &font_info); |
100 return (std::wstring(font_info.lfFaceName)); | 101 return (std::wstring(font_info.lfFaceName)); |
101 } | 102 } |
102 | 103 |
103 int ChromeFont::FontSize() { | 104 int Font::FontSize() { |
104 LOGFONT font_info; | 105 LOGFONT font_info; |
105 GetObject(hfont(), sizeof(LOGFONT), &font_info); | 106 GetObject(hfont(), sizeof(LOGFONT), &font_info); |
106 long lf_height = font_info.lfHeight; | 107 long lf_height = font_info.lfHeight; |
107 HDC hdc = GetDC(NULL); | 108 HDC hdc = GetDC(NULL); |
108 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY); | 109 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY); |
109 int font_size = 0; | 110 int font_size = 0; |
110 if (device_caps != 0) { | 111 if (device_caps != 0) { |
111 float font_size_float = -static_cast<float>(lf_height)*72/device_caps; | 112 float font_size_float = -static_cast<float>(lf_height)*72/device_caps; |
112 font_size = static_cast<int>(::ceil(font_size_float - 0.5)); | 113 font_size = static_cast<int>(::ceil(font_size_float - 0.5)); |
113 } | 114 } |
114 ReleaseDC(NULL, hdc); | 115 ReleaseDC(NULL, hdc); |
115 return font_size; | 116 return font_size; |
116 } | 117 } |
117 | 118 |
118 ChromeFont::HFontRef::HFontRef(HFONT hfont, | 119 Font::HFontRef::HFontRef(HFONT hfont, |
119 int height, | 120 int height, |
120 int baseline, | 121 int baseline, |
121 int ave_char_width, | 122 int ave_char_width, |
122 int style, | 123 int style, |
123 int dlu_base_x) | 124 int dlu_base_x) |
124 : hfont_(hfont), | 125 : hfont_(hfont), |
125 height_(height), | 126 height_(height), |
126 baseline_(baseline), | 127 baseline_(baseline), |
127 ave_char_width_(ave_char_width), | 128 ave_char_width_(ave_char_width), |
128 style_(style), | 129 style_(style), |
129 dlu_base_x_(dlu_base_x) { | 130 dlu_base_x_(dlu_base_x) { |
130 DLOG_ASSERT(hfont); | 131 DLOG_ASSERT(hfont); |
131 } | 132 } |
132 | 133 |
133 ChromeFont::HFontRef::~HFontRef() { | 134 Font::HFontRef::~HFontRef() { |
134 DeleteObject(hfont_); | 135 DeleteObject(hfont_); |
135 } | 136 } |
136 | 137 |
137 | 138 |
138 ChromeFont ChromeFont::DeriveFont(int size_delta, int style) const { | 139 Font Font::DeriveFont(int size_delta, int style) const { |
139 LOGFONT font_info; | 140 LOGFONT font_info; |
140 GetObject(hfont(), sizeof(LOGFONT), &font_info); | 141 GetObject(hfont(), sizeof(LOGFONT), &font_info); |
141 // LOGFONT returns two types of font heights, negative is measured slightly | 142 // LOGFONT returns two types of font heights, negative is measured slightly |
142 // differently (character height, vs cell height). | 143 // differently (character height, vs cell height). |
143 if (font_info.lfHeight < 0) { | 144 if (font_info.lfHeight < 0) { |
144 font_info.lfHeight -= size_delta; | 145 font_info.lfHeight -= size_delta; |
145 } else { | 146 } else { |
146 font_info.lfHeight += size_delta; | 147 font_info.lfHeight += size_delta; |
147 } | 148 } |
148 // Even with "Small Fonts", the smallest readable font size is 5. It is easy | 149 // Even with "Small Fonts", the smallest readable font size is 5. It is easy |
149 // to create a non-drawing font and forget about the fact that text should be | 150 // to create a non-drawing font and forget about the fact that text should be |
150 // drawn in the UI. This test ensures that the font will be readable. | 151 // drawn in the UI. This test ensures that the font will be readable. |
151 DCHECK_GE(abs(font_info.lfHeight), 5); | 152 DCHECK_GE(abs(font_info.lfHeight), 5); |
152 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED); | 153 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED); |
153 font_info.lfItalic = ((style & ITALIC) == ITALIC); | 154 font_info.lfItalic = ((style & ITALIC) == ITALIC); |
154 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL; | 155 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL; |
155 | 156 |
156 HFONT hfont = CreateFontIndirect(&font_info); | 157 HFONT hfont = CreateFontIndirect(&font_info); |
157 return ChromeFont(CreateHFontRef(hfont)); | 158 return Font(CreateHFontRef(hfont)); |
158 } | 159 } |
159 | 160 |
160 int ChromeFont::GetStringWidth(const std::wstring& text) const { | 161 int Font::GetStringWidth(const std::wstring& text) const { |
161 int width = 0; | 162 int width = 0; |
162 HDC dc = GetDC(NULL); | 163 HDC dc = GetDC(NULL); |
163 HFONT previous_font = static_cast<HFONT>(SelectObject(dc, hfont())); | 164 HFONT previous_font = static_cast<HFONT>(SelectObject(dc, hfont())); |
164 SIZE size; | 165 SIZE size; |
165 if (GetTextExtentPoint32(dc, text.c_str(), static_cast<int>(text.size()), | 166 if (GetTextExtentPoint32(dc, text.c_str(), static_cast<int>(text.size()), |
166 &size)) { | 167 &size)) { |
167 width = size.cx; | 168 width = size.cx; |
168 } else { | 169 } else { |
169 width = 0; | 170 width = 0; |
170 } | 171 } |
171 SelectObject(dc, previous_font); | 172 SelectObject(dc, previous_font); |
172 ReleaseDC(NULL, dc); | 173 ReleaseDC(NULL, dc); |
173 return width; | 174 return width; |
174 } | 175 } |
175 | 176 |
176 ChromeFont::HFontRef* ChromeFont::CreateHFontRef(HFONT font) { | 177 Font::HFontRef* Font::CreateHFontRef(HFONT font) { |
177 TEXTMETRIC font_metrics; | 178 TEXTMETRIC font_metrics; |
178 HDC screen_dc = GetDC(NULL); | 179 HDC screen_dc = GetDC(NULL); |
179 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); | 180 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); |
180 int last_map_mode = SetMapMode(screen_dc, MM_TEXT); | 181 int last_map_mode = SetMapMode(screen_dc, MM_TEXT); |
181 GetTextMetrics(screen_dc, &font_metrics); | 182 GetTextMetrics(screen_dc, &font_metrics); |
182 // Yes, this is how Microsoft recommends calculating the dialog unit | 183 // Yes, this is how Microsoft recommends calculating the dialog unit |
183 // conversions. | 184 // conversions. |
184 SIZE ave_text_size; | 185 SIZE ave_text_size; |
185 GetTextExtentPoint32(screen_dc, | 186 GetTextExtentPoint32(screen_dc, |
186 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | 187 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", |
187 52, &ave_text_size); | 188 52, &ave_text_size); |
188 const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2; | 189 const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2; |
189 // To avoid the DC referencing font_handle_, select the previous font. | 190 // To avoid the DC referencing font_handle_, select the previous font. |
190 SelectObject(screen_dc, previous_font); | 191 SelectObject(screen_dc, previous_font); |
191 SetMapMode(screen_dc, last_map_mode); | 192 SetMapMode(screen_dc, last_map_mode); |
192 ReleaseDC(NULL, screen_dc); | 193 ReleaseDC(NULL, screen_dc); |
193 | 194 |
194 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); | 195 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); |
195 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); | 196 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); |
196 const int ave_char_width = | 197 const int ave_char_width = |
197 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); | 198 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); |
198 int style = 0; | 199 int style = 0; |
199 if (font_metrics.tmItalic) { | 200 if (font_metrics.tmItalic) { |
200 style |= ChromeFont::ITALIC; | 201 style |= Font::ITALIC; |
201 } | 202 } |
202 if (font_metrics.tmUnderlined) { | 203 if (font_metrics.tmUnderlined) { |
203 style |= ChromeFont::UNDERLINED; | 204 style |= Font::UNDERLINED; |
204 } | 205 } |
205 if (font_metrics.tmWeight >= kTextMetricWeightBold) { | 206 if (font_metrics.tmWeight >= kTextMetricWeightBold) { |
206 style |= ChromeFont::BOLD; | 207 style |= Font::BOLD; |
207 } | 208 } |
208 | 209 |
209 return new HFontRef(font, height, baseline, ave_char_width, style, | 210 return new HFontRef(font, height, baseline, ave_char_width, style, |
210 dlu_base_x); | 211 dlu_base_x); |
211 } | 212 } |
| 213 |
| 214 } // namespace gfx |
OLD | NEW |