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 = (font_style & gfx::Font::UNDERLINED) != 0; |
| 53 font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0; |
| 54 font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; |
| 55 } |
| 56 |
50 } // namespace | 57 } // namespace |
51 | 58 |
52 namespace gfx { | 59 namespace gfx { |
53 | 60 |
54 // static | 61 // static |
55 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; | 62 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_; |
56 | 63 |
57 // static | 64 // static |
58 PlatformFontWin::AdjustFontCallback | 65 PlatformFontWin::AdjustFontCallback |
59 PlatformFontWin::adjust_font_callback = NULL; | 66 PlatformFontWin::adjust_font_callback = NULL; |
60 PlatformFontWin::GetMinimumFontSizeCallback | 67 PlatformFontWin::GetMinimumFontSizeCallback |
61 PlatformFontWin::get_minimum_font_size_callback = NULL; | 68 PlatformFontWin::get_minimum_font_size_callback = NULL; |
62 | 69 |
63 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
64 // PlatformFontWin, public | 71 // PlatformFontWin, public |
65 | 72 |
66 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { | 73 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) { |
67 } | 74 } |
68 | 75 |
69 PlatformFontWin::PlatformFontWin(NativeFont native_font) { | 76 PlatformFontWin::PlatformFontWin(NativeFont native_font) { |
70 InitWithCopyOfHFONT(native_font); | 77 InitWithCopyOfHFONT(native_font); |
71 } | 78 } |
72 | 79 |
73 PlatformFontWin::PlatformFontWin(const std::string& font_name, | 80 PlatformFontWin::PlatformFontWin(const std::string& font_name, |
74 int font_size) { | 81 int font_size) { |
75 InitWithFontNameAndSize(font_name, font_size); | 82 InitWithFontNameAndSize(font_name, font_size); |
76 } | 83 } |
77 | 84 |
| 85 Font PlatformFontWin::DeriveFontWithHeight(int height, int style) { |
| 86 DCHECK_GE(height, 0); |
| 87 if (GetHeight() == height && GetStyle() == style) |
| 88 return Font(this); |
| 89 |
| 90 LOGFONT font_info; |
| 91 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
| 92 font_info.lfHeight = height; |
| 93 SetLogFontStyle(style, &font_info); |
| 94 |
| 95 HFONT hfont = CreateFontIndirect(&font_info); |
| 96 return Font(new PlatformFontWin(CreateHFontRef(hfont))); |
| 97 } |
| 98 |
78 //////////////////////////////////////////////////////////////////////////////// | 99 //////////////////////////////////////////////////////////////////////////////// |
79 // PlatformFontWin, PlatformFont implementation: | 100 // PlatformFontWin, PlatformFont implementation: |
80 | 101 |
81 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { | 102 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { |
82 LOGFONT font_info; | 103 LOGFONT font_info; |
83 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); | 104 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
84 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); | 105 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); |
85 font_info.lfUnderline = | 106 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 | 107 |
90 HFONT hfont = CreateFontIndirect(&font_info); | 108 HFONT hfont = CreateFontIndirect(&font_info); |
91 return Font(new PlatformFontWin(CreateHFontRef(hfont))); | 109 return Font(new PlatformFontWin(CreateHFontRef(hfont))); |
92 } | 110 } |
93 | 111 |
94 int PlatformFontWin::GetHeight() const { | 112 int PlatformFontWin::GetHeight() const { |
95 return font_ref_->height(); | 113 return font_ref_->height(); |
96 } | 114 } |
97 | 115 |
98 int PlatformFontWin::GetBaseline() const { | 116 int PlatformFontWin::GetBaseline() const { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { | 202 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { |
185 TEXTMETRIC font_metrics; | 203 TEXTMETRIC font_metrics; |
186 | 204 |
187 { | 205 { |
188 base::win::ScopedGetDC screen_dc(NULL); | 206 base::win::ScopedGetDC screen_dc(NULL); |
189 base::win::ScopedSelectObject font(screen_dc, font); | 207 base::win::ScopedSelectObject font(screen_dc, font); |
190 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); | 208 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
191 GetTextMetrics(screen_dc, &font_metrics); | 209 GetTextMetrics(screen_dc, &font_metrics); |
192 } | 210 } |
193 | 211 |
194 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); | 212 const int height = std::max<int>(1, font_metrics.tmHeight); |
195 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); | 213 const int baseline = std::max<int>(1, font_metrics.tmAscent); |
196 const int ave_char_width = | 214 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); |
197 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); | 215 const int font_size = |
| 216 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); |
198 int style = 0; | 217 int style = 0; |
199 if (font_metrics.tmItalic) | 218 if (font_metrics.tmItalic) |
200 style |= Font::ITALIC; | 219 style |= Font::ITALIC; |
201 if (font_metrics.tmUnderlined) | 220 if (font_metrics.tmUnderlined) |
202 style |= Font::UNDERLINED; | 221 style |= Font::UNDERLINED; |
203 if (font_metrics.tmWeight >= kTextMetricWeightBold) | 222 if (font_metrics.tmWeight >= kTextMetricWeightBold) |
204 style |= Font::BOLD; | 223 style |= Font::BOLD; |
205 | 224 |
206 return new HFontRef(font, height, baseline, ave_char_width, style); | 225 return new HFontRef(font, font_size, height, baseline, ave_char_width, style); |
207 } | 226 } |
208 | 227 |
209 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { | 228 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { |
210 } | 229 } |
211 | 230 |
212 //////////////////////////////////////////////////////////////////////////////// | 231 //////////////////////////////////////////////////////////////////////////////// |
213 // PlatformFontWin::HFontRef: | 232 // PlatformFontWin::HFontRef: |
214 | 233 |
215 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, | 234 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, |
| 235 int font_size, |
216 int height, | 236 int height, |
217 int baseline, | 237 int baseline, |
218 int ave_char_width, | 238 int ave_char_width, |
219 int style) | 239 int style) |
220 : hfont_(hfont), | 240 : hfont_(hfont), |
| 241 font_size_(font_size), |
221 height_(height), | 242 height_(height), |
222 baseline_(baseline), | 243 baseline_(baseline), |
223 ave_char_width_(ave_char_width), | 244 ave_char_width_(ave_char_width), |
224 style_(style), | 245 style_(style), |
225 dlu_base_x_(-1) { | 246 dlu_base_x_(-1) { |
226 DLOG_ASSERT(hfont); | 247 DLOG_ASSERT(hfont); |
227 | 248 |
228 LOGFONT font_info; | 249 LOGFONT font_info; |
229 GetObject(hfont_, sizeof(LOGFONT), &font_info); | 250 GetObject(hfont_, sizeof(LOGFONT), &font_info); |
230 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); | 251 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); |
231 DCHECK_LT(font_info.lfHeight, 0); | |
232 font_size_ = -font_info.lfHeight; | |
233 } | 252 } |
234 | 253 |
235 int PlatformFontWin::HFontRef::GetDluBaseX() { | 254 int PlatformFontWin::HFontRef::GetDluBaseX() { |
236 if (dlu_base_x_ != -1) | 255 if (dlu_base_x_ != -1) |
237 return dlu_base_x_; | 256 return dlu_base_x_; |
238 | 257 |
239 base::win::ScopedGetDC screen_dc(NULL); | 258 base::win::ScopedGetDC screen_dc(NULL); |
240 base::win::ScopedSelectObject font(screen_dc, hfont_); | 259 base::win::ScopedSelectObject font(screen_dc, hfont_); |
241 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); | 260 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
242 | 261 |
(...skipping 26 matching lines...) Expand all Loading... |
269 return new PlatformFontWin(native_font); | 288 return new PlatformFontWin(native_font); |
270 } | 289 } |
271 | 290 |
272 // static | 291 // static |
273 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 292 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
274 int font_size) { | 293 int font_size) { |
275 return new PlatformFontWin(font_name, font_size); | 294 return new PlatformFontWin(font_name, font_size); |
276 } | 295 } |
277 | 296 |
278 } // namespace gfx | 297 } // namespace gfx |
OLD | NEW |