OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/gfx/platform_font_win.h" | 5 #include "ui/gfx/platform_font_win.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> |
(...skipping 29 matching lines...) Expand all Loading... | |
40 min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback(); | 40 min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback(); |
41 // Make sure lf_height is not smaller than allowed min font size for current | 41 // Make sure lf_height is not smaller than allowed min font size for current |
42 // locale. | 42 // locale. |
43 if (abs(lf_height) < min_font_size) { | 43 if (abs(lf_height) < min_font_size) { |
44 return lf_height < 0 ? -min_font_size : min_font_size; | 44 return lf_height < 0 ? -min_font_size : min_font_size; |
45 } else { | 45 } else { |
46 return lf_height; | 46 return lf_height; |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 // Sets style properties on |font_info| based on |font_style|. | |
51 void SetLogFontStyle(int font_style, LOGFONT* font_info) { | |
52 font_info->lfUnderline = | |
53 ((font_style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED); | |
msw
2012/04/26 21:04:42
Just set the bool to the bit-wise & value (kinda l
Alexei Svitkine (slow)
2012/04/26 21:45:56
Done. I used != 0 since I don't know if passing va
| |
54 font_info->lfItalic = ((font_style & gfx::Font::ITALIC) == gfx::Font::ITALIC); | |
55 font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; | |
56 } | |
57 | |
50 } // namespace | 58 } // namespace |
51 | 59 |
52 namespace gfx { | 60 namespace gfx { |
53 | 61 |
54 // static | 62 // static |
55 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; | 63 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; |
56 | 64 |
57 // static | 65 // static |
58 PlatformFontWin::AdjustFontCallback | 66 PlatformFontWin::AdjustFontCallback |
59 PlatformFontWin::adjust_font_callback = NULL; | 67 PlatformFontWin::adjust_font_callback = NULL; |
60 PlatformFontWin::GetMinimumFontSizeCallback | 68 PlatformFontWin::GetMinimumFontSizeCallback |
61 PlatformFontWin::get_minimum_font_size_callback = NULL; | 69 PlatformFontWin::get_minimum_font_size_callback = NULL; |
62 | 70 |
63 //////////////////////////////////////////////////////////////////////////////// | 71 //////////////////////////////////////////////////////////////////////////////// |
64 // PlatformFontWin, public | 72 // PlatformFontWin, public |
65 | 73 |
66 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { | 74 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { |
67 } | 75 } |
68 | 76 |
69 PlatformFontWin::PlatformFontWin(NativeFont native_font) { | 77 PlatformFontWin::PlatformFontWin(NativeFont native_font) { |
70 InitWithCopyOfHFONT(native_font); | 78 InitWithCopyOfHFONT(native_font); |
71 } | 79 } |
72 | 80 |
73 PlatformFontWin::PlatformFontWin(const std::string& font_name, | 81 PlatformFontWin::PlatformFontWin(const std::string& font_name, |
74 int font_size) { | 82 int font_size) { |
75 InitWithFontNameAndSize(font_name, font_size); | 83 InitWithFontNameAndSize(font_name, font_size); |
76 } | 84 } |
77 | 85 |
86 Font PlatformFontWin::DeriveFontWithHeight(int height, int style) { | |
87 DCHECK_GE(height, 0); | |
88 if (GetHeight() == height && GetStyle() == style) | |
89 return Font(this); | |
90 | |
91 LOGFONT font_info; | |
92 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); | |
93 font_info.lfHeight = height; | |
94 SetLogFontStyle(style, &font_info); | |
95 | |
96 HFONT hfont = CreateFontIndirect(&font_info); | |
97 return Font(new PlatformFontWin(CreateHFontRef(hfont))); | |
98 } | |
99 | |
78 //////////////////////////////////////////////////////////////////////////////// | 100 //////////////////////////////////////////////////////////////////////////////// |
79 // PlatformFontWin, PlatformFont implementation: | 101 // PlatformFontWin, PlatformFont implementation: |
80 | 102 |
81 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { | 103 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { |
82 LOGFONT font_info; | 104 LOGFONT font_info; |
83 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); | 105 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
84 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); | 106 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); |
85 font_info.lfUnderline = | 107 SetLogFontStyle(style, &font_info); |
86 ((style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED); | |
87 font_info.lfItalic = ((style & gfx::Font::ITALIC) == gfx::Font::ITALIC); | |
88 font_info.lfWeight = (style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; | |
89 | 108 |
90 HFONT hfont = CreateFontIndirect(&font_info); | 109 HFONT hfont = CreateFontIndirect(&font_info); |
91 return Font(new PlatformFontWin(CreateHFontRef(hfont))); | 110 return Font(new PlatformFontWin(CreateHFontRef(hfont))); |
92 } | 111 } |
93 | 112 |
94 int PlatformFontWin::GetHeight() const { | 113 int PlatformFontWin::GetHeight() const { |
95 return font_ref_->height(); | 114 return font_ref_->height(); |
96 } | 115 } |
97 | 116 |
98 int PlatformFontWin::GetBaseline() const { | 117 int PlatformFontWin::GetBaseline() const { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { | 203 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { |
185 TEXTMETRIC font_metrics; | 204 TEXTMETRIC font_metrics; |
186 | 205 |
187 { | 206 { |
188 base::win::ScopedGetDC screen_dc(NULL); | 207 base::win::ScopedGetDC screen_dc(NULL); |
189 base::win::ScopedSelectObject font(screen_dc, font); | 208 base::win::ScopedSelectObject font(screen_dc, font); |
190 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); | 209 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
191 GetTextMetrics(screen_dc, &font_metrics); | 210 GetTextMetrics(screen_dc, &font_metrics); |
192 } | 211 } |
193 | 212 |
194 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); | 213 const int height = std::max<int>(1, font_metrics.tmHeight); |
msw
2012/04/26 21:04:42
nice :)
| |
195 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); | 214 const int baseline = std::max<int>(1, font_metrics.tmAscent); |
196 const int ave_char_width = | 215 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); |
197 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); | 216 const int font_size = |
217 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); | |
198 int style = 0; | 218 int style = 0; |
199 if (font_metrics.tmItalic) | 219 if (font_metrics.tmItalic) |
200 style |= Font::ITALIC; | 220 style |= Font::ITALIC; |
201 if (font_metrics.tmUnderlined) | 221 if (font_metrics.tmUnderlined) |
202 style |= Font::UNDERLINED; | 222 style |= Font::UNDERLINED; |
203 if (font_metrics.tmWeight >= kTextMetricWeightBold) | 223 if (font_metrics.tmWeight >= kTextMetricWeightBold) |
204 style |= Font::BOLD; | 224 style |= Font::BOLD; |
205 | 225 |
206 return new HFontRef(font, height, baseline, ave_char_width, style); | 226 return new HFontRef(font, font_size, height, baseline, ave_char_width, style); |
msw
2012/04/26 21:04:42
Seems like HFontRef's ctor should just take (HFONT
Alexei Svitkine (slow)
2012/04/26 21:45:56
I agree, but let's leave it for another CL.
msw
2012/04/26 22:14:13
SGTM.
| |
207 } | 227 } |
208 | 228 |
209 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { | 229 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { |
210 } | 230 } |
211 | 231 |
212 //////////////////////////////////////////////////////////////////////////////// | 232 //////////////////////////////////////////////////////////////////////////////// |
213 // PlatformFontWin::HFontRef: | 233 // PlatformFontWin::HFontRef: |
214 | 234 |
215 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, | 235 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, |
236 int font_size, | |
216 int height, | 237 int height, |
217 int baseline, | 238 int baseline, |
218 int ave_char_width, | 239 int ave_char_width, |
219 int style) | 240 int style) |
220 : hfont_(hfont), | 241 : hfont_(hfont), |
242 font_size_(font_size), | |
221 height_(height), | 243 height_(height), |
222 baseline_(baseline), | 244 baseline_(baseline), |
223 ave_char_width_(ave_char_width), | 245 ave_char_width_(ave_char_width), |
224 style_(style), | 246 style_(style), |
225 dlu_base_x_(-1) { | 247 dlu_base_x_(-1) { |
226 DLOG_ASSERT(hfont); | 248 DLOG_ASSERT(hfont); |
227 | 249 |
228 LOGFONT font_info; | 250 LOGFONT font_info; |
229 GetObject(hfont_, sizeof(LOGFONT), &font_info); | 251 GetObject(hfont_, sizeof(LOGFONT), &font_info); |
230 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); | 252 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); |
231 DCHECK_LT(font_info.lfHeight, 0); | |
232 font_size_ = -font_info.lfHeight; | |
233 } | 253 } |
234 | 254 |
235 int PlatformFontWin::HFontRef::GetDluBaseX() { | 255 int PlatformFontWin::HFontRef::GetDluBaseX() { |
236 if (dlu_base_x_ != -1) | 256 if (dlu_base_x_ != -1) |
237 return dlu_base_x_; | 257 return dlu_base_x_; |
238 | 258 |
239 base::win::ScopedGetDC screen_dc(NULL); | 259 base::win::ScopedGetDC screen_dc(NULL); |
240 base::win::ScopedSelectObject font(screen_dc, hfont_); | 260 base::win::ScopedSelectObject font(screen_dc, hfont_); |
241 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); | 261 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
242 | 262 |
(...skipping 26 matching lines...) Expand all Loading... | |
269 return new PlatformFontWin(native_font); | 289 return new PlatformFontWin(native_font); |
270 } | 290 } |
271 | 291 |
272 // static | 292 // static |
273 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 293 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
274 int font_size) { | 294 int font_size) { |
275 return new PlatformFontWin(font_name, font_size); | 295 return new PlatformFontWin(font_name, font_size); |
276 } | 296 } |
277 | 297 |
278 } // namespace gfx | 298 } // namespace gfx |
OLD | NEW |