Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: ui/gfx/platform_font_win.cc

Issue 10228009: Fix CJK font linking size on Windows XP. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 // CreateFontIndirect() doesn't return the largest size for the given height
91 // when decreasing the height. Iterate to find it.
92 if (GetHeight() > height) {
msw 2012/04/30 17:40:01 Should this heed the min locale height/size like A
Alexei Svitkine (slow) 2012/04/30 17:49:14 No, since DeriveFont() goes through AdjustFontSize
msw 2012/04/30 18:55:39 So an arg |height| < min will cause an infinite wh
Alexei Svitkine (slow) 2012/04/30 19:24:00 Good catch. Fixed and added test.
93 Font font = this->DeriveFont(-1, style);
msw 2012/04/30 17:40:01 Is there a name conflict or can you nix "this->"?
Alexei Svitkine (slow) 2012/04/30 17:49:14 Done.
94 while (font.GetHeight() > height) {
95 font = font.DeriveFont(-1, style);
96 }
97 return font;
98 }
99
100 LOGFONT font_info;
101 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
102 font_info.lfHeight = height;
103 SetLogFontStyle(style, &font_info);
104
105 HFONT hfont = CreateFontIndirect(&font_info);
106 return Font(new PlatformFontWin(CreateHFontRef(hfont)));
107 }
108
78 //////////////////////////////////////////////////////////////////////////////// 109 ////////////////////////////////////////////////////////////////////////////////
79 // PlatformFontWin, PlatformFont implementation: 110 // PlatformFontWin, PlatformFont implementation:
80 111
81 Font PlatformFontWin::DeriveFont(int size_delta, int style) const { 112 Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
82 LOGFONT font_info; 113 LOGFONT font_info;
83 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); 114 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
84 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); 115 const int requested_font_size = font_ref_->requested_font_size();
85 font_info.lfUnderline = 116 font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta);
86 ((style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED); 117 SetLogFontStyle(style, &font_info);
87 font_info.lfItalic = ((style & gfx::Font::ITALIC) == gfx::Font::ITALIC);
88 font_info.lfWeight = (style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL;
89 118
90 HFONT hfont = CreateFontIndirect(&font_info); 119 HFONT hfont = CreateFontIndirect(&font_info);
91 return Font(new PlatformFontWin(CreateHFontRef(hfont))); 120 return Font(new PlatformFontWin(CreateHFontRef(hfont)));
92 } 121 }
93 122
94 int PlatformFontWin::GetHeight() const { 123 int PlatformFontWin::GetHeight() const {
95 return font_ref_->height(); 124 return font_ref_->height();
96 } 125 }
97 126
98 int PlatformFontWin::GetBaseline() const { 127 int PlatformFontWin::GetBaseline() const {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) { 213 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
185 TEXTMETRIC font_metrics; 214 TEXTMETRIC font_metrics;
186 215
187 { 216 {
188 base::win::ScopedGetDC screen_dc(NULL); 217 base::win::ScopedGetDC screen_dc(NULL);
189 base::win::ScopedSelectObject font(screen_dc, font); 218 base::win::ScopedSelectObject font(screen_dc, font);
190 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); 219 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT);
191 GetTextMetrics(screen_dc, &font_metrics); 220 GetTextMetrics(screen_dc, &font_metrics);
192 } 221 }
193 222
194 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); 223 const int height = std::max<int>(1, font_metrics.tmHeight);
195 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); 224 const int baseline = std::max<int>(1, font_metrics.tmAscent);
196 const int ave_char_width = 225 const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth);
197 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); 226 const int font_size =
227 std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading);
198 int style = 0; 228 int style = 0;
199 if (font_metrics.tmItalic) 229 if (font_metrics.tmItalic)
200 style |= Font::ITALIC; 230 style |= Font::ITALIC;
201 if (font_metrics.tmUnderlined) 231 if (font_metrics.tmUnderlined)
202 style |= Font::UNDERLINED; 232 style |= Font::UNDERLINED;
203 if (font_metrics.tmWeight >= kTextMetricWeightBold) 233 if (font_metrics.tmWeight >= kTextMetricWeightBold)
204 style |= Font::BOLD; 234 style |= Font::BOLD;
205 235
206 return new HFontRef(font, height, baseline, ave_char_width, style); 236 return new HFontRef(font, font_size, height, baseline, ave_char_width, style);
207 } 237 }
208 238
209 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { 239 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
210 } 240 }
211 241
212 //////////////////////////////////////////////////////////////////////////////// 242 ////////////////////////////////////////////////////////////////////////////////
213 // PlatformFontWin::HFontRef: 243 // PlatformFontWin::HFontRef:
214 244
215 PlatformFontWin::HFontRef::HFontRef(HFONT hfont, 245 PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
246 int font_size,
216 int height, 247 int height,
217 int baseline, 248 int baseline,
218 int ave_char_width, 249 int ave_char_width,
219 int style) 250 int style)
220 : hfont_(hfont), 251 : hfont_(hfont),
252 font_size_(font_size),
221 height_(height), 253 height_(height),
222 baseline_(baseline), 254 baseline_(baseline),
223 ave_char_width_(ave_char_width), 255 ave_char_width_(ave_char_width),
224 style_(style), 256 style_(style),
225 dlu_base_x_(-1) { 257 dlu_base_x_(-1),
258 requested_font_size_(font_size) {
226 DLOG_ASSERT(hfont); 259 DLOG_ASSERT(hfont);
227 260
228 LOGFONT font_info; 261 LOGFONT font_info;
229 GetObject(hfont_, sizeof(LOGFONT), &font_info); 262 GetObject(hfont_, sizeof(LOGFONT), &font_info);
230 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); 263 font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName));
231 DCHECK_LT(font_info.lfHeight, 0); 264 if (font_info.lfHeight < 0)
232 font_size_ = -font_info.lfHeight; 265 requested_font_size_ = -font_info.lfHeight;
233 } 266 }
234 267
235 int PlatformFontWin::HFontRef::GetDluBaseX() { 268 int PlatformFontWin::HFontRef::GetDluBaseX() {
236 if (dlu_base_x_ != -1) 269 if (dlu_base_x_ != -1)
237 return dlu_base_x_; 270 return dlu_base_x_;
238 271
239 base::win::ScopedGetDC screen_dc(NULL); 272 base::win::ScopedGetDC screen_dc(NULL);
240 base::win::ScopedSelectObject font(screen_dc, hfont_); 273 base::win::ScopedSelectObject font(screen_dc, hfont_);
241 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT); 274 ui::ScopedSetMapMode mode(screen_dc, MM_TEXT);
242 275
(...skipping 26 matching lines...) Expand all
269 return new PlatformFontWin(native_font); 302 return new PlatformFontWin(native_font);
270 } 303 }
271 304
272 // static 305 // static
273 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 306 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
274 int font_size) { 307 int font_size) {
275 return new PlatformFontWin(font_name, font_size); 308 return new PlatformFontWin(font_name, font_size);
276 } 309 }
277 310
278 } // namespace gfx 311 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698