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

Side by Side Diff: gfx/platform_font_win.cc

Issue 3083022: Rework gfx::Font by moving platform-specific code into inner classes.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 4 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
« no previous file with comments | « gfx/platform_font_win.h ('k') | printing/printed_document.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "gfx/font.h" 5 #include "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>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/win_util.h" 14 #include "base/win_util.h"
15 #include "gfx/canvas_skia.h" 15 #include "gfx/canvas_skia.h"
16 #include "gfx/font.h"
16 17
17 namespace gfx { 18 namespace {
18
19 // static
20 Font::HFontRef* Font::base_font_ref_;
21
22 // static
23 Font::AdjustFontCallback Font::adjust_font_callback = NULL;
24 Font::GetMinimumFontSizeCallback Font::get_minimum_font_size_callback = NULL;
25 19
26 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the 20 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the
27 // font is bold. 21 // font is bold.
28 static const int kTextMetricWeightBold = 700; 22 const int kTextMetricWeightBold = 700;
29 23
30 // Returns either minimum font allowed for a current locale or 24 // Returns either minimum font allowed for a current locale or
31 // lf_height + size_delta value. 25 // lf_height + size_delta value.
32 static int AdjustFontSize(int lf_height, int size_delta) { 26 int AdjustFontSize(int lf_height, int size_delta) {
33 if (lf_height < 0) { 27 if (lf_height < 0) {
34 lf_height -= size_delta; 28 lf_height -= size_delta;
35 } else { 29 } else {
36 lf_height += size_delta; 30 lf_height += size_delta;
37 } 31 }
38 int min_font_size = 0; 32 int min_font_size = 0;
39 if (Font::get_minimum_font_size_callback) 33 if (gfx::PlatformFontWin::get_minimum_font_size_callback)
40 min_font_size = Font::get_minimum_font_size_callback(); 34 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 35 // Make sure lf_height is not smaller than allowed min font size for current
42 // locale. 36 // locale.
43 if (abs(lf_height) < min_font_size) { 37 if (abs(lf_height) < min_font_size) {
44 return lf_height < 0 ? -min_font_size : min_font_size; 38 return lf_height < 0 ? -min_font_size : min_font_size;
45 } else { 39 } else {
46 return lf_height; 40 return lf_height;
47 } 41 }
48 } 42 }
49 43
50 // 44 } // namespace
51 // Font
52 //
53 45
54 Font::Font() 46 namespace gfx {
55 : font_ref_(GetBaseFontRef()) { 47
48 // static
49 PlatformFontWin::HFontRef* PlatformFontWin::base_font_ref_;
50
51 // static
52 PlatformFontWin::AdjustFontCallback
53 PlatformFontWin::adjust_font_callback = NULL;
54 PlatformFontWin::GetMinimumFontSizeCallback
55 PlatformFontWin::get_minimum_font_size_callback = NULL;
56
57 ////////////////////////////////////////////////////////////////////////////////
58 // PlatformFontWin, public
59
60 PlatformFontWin::PlatformFontWin() : font_ref_(GetBaseFontRef()) {
56 } 61 }
57 62
58 int Font::height() const { 63 PlatformFontWin::PlatformFontWin(const Font& other) {
64 InitWithCopyOfHFONT(other.GetNativeFont());
65 }
66
67 PlatformFontWin::PlatformFontWin(NativeFont native_font) {
68 InitWithCopyOfHFONT(native_font);
69 }
70
71 PlatformFontWin::PlatformFontWin(const std::wstring& font_name,
72 int font_size) {
73 InitWithFontNameAndSize(font_name, font_size);
74 }
75
76 ////////////////////////////////////////////////////////////////////////////////
77 // PlatformFontWin, PlatformFont implementation:
78
79 Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
80 LOGFONT font_info;
81 GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
82 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta);
83 font_info.lfUnderline =
84 ((style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED);
85 font_info.lfItalic = ((style & gfx::Font::ITALIC) == gfx::Font::ITALIC);
86 font_info.lfWeight = (style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL;
87
88 HFONT hfont = CreateFontIndirect(&font_info);
89 return Font(new PlatformFontWin(CreateHFontRef(hfont)));
90 }
91
92 int PlatformFontWin::GetHeight() const {
59 return font_ref_->height(); 93 return font_ref_->height();
60 } 94 }
61 95
62 int Font::baseline() const { 96 int PlatformFontWin::GetBaseline() const {
63 return font_ref_->baseline(); 97 return font_ref_->baseline();
64 } 98 }
65 99
66 int Font::ave_char_width() const { 100 int PlatformFontWin::GetAverageCharacterWidth() const {
67 return font_ref_->ave_char_width(); 101 return font_ref_->ave_char_width();
68 } 102 }
69 103
70 int Font::GetExpectedTextWidth(int length) const { 104 int PlatformFontWin::GetStringWidth(const std::wstring& text) const {
71 return length * std::min(font_ref_->dlu_base_x(), ave_char_width()); 105 int width = 0, height = 0;
106 CanvasSkia::SizeStringInt(text, Font(const_cast<PlatformFontWin*>(this)),
107 &width, &height, gfx::Canvas::NO_ELLIPSIS);
108 return width;
72 } 109 }
73 110
74 int Font::style() const { 111 int PlatformFontWin::GetExpectedTextWidth(int length) const {
112 return length * std::min(font_ref_->dlu_base_x(), GetAverageCharacterWidth());
113 }
114
115 int PlatformFontWin::GetStyle() const {
75 return font_ref_->style(); 116 return font_ref_->style();
76 } 117 }
77 118
78 NativeFont Font::nativeFont() const { 119 const std::wstring& PlatformFontWin::GetFontName() const {
79 return hfont(); 120 return font_ref_->font_name();
80 } 121 }
81 122
82 // static 123 int PlatformFontWin::GetFontSize() const {
83 Font Font::CreateFont(HFONT font) {
84 DCHECK(font);
85 LOGFONT font_info; 124 LOGFONT font_info;
86 GetObject(font, sizeof(LOGFONT), &font_info); 125 GetObject(font_ref_->hfont(), sizeof(LOGFONT), &font_info);
87 return Font(CreateHFontRef(CreateFontIndirect(&font_info))); 126 long lf_height = font_info.lfHeight;
127 HDC hdc = GetDC(NULL);
128 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY);
129 int font_size = 0;
130 if (device_caps != 0) {
131 float font_size_float = -static_cast<float>(lf_height)*72/device_caps;
132 font_size = static_cast<int>(::ceil(font_size_float - 0.5));
133 }
134 ReleaseDC(NULL, hdc);
135 return font_size;
88 } 136 }
89 137
90 Font Font::CreateFont(const std::wstring& font_name, int font_size) { 138 NativeFont PlatformFontWin::GetNativeFont() const {
139 return font_ref_->hfont();
140 }
141
142 ////////////////////////////////////////////////////////////////////////////////
143 // Font, private:
144
145 void PlatformFontWin::InitWithCopyOfHFONT(HFONT hfont) {
146 DCHECK(hfont);
147 LOGFONT font_info;
148 GetObject(hfont, sizeof(LOGFONT), &font_info);
149 font_ref_ = CreateHFontRef(CreateFontIndirect(&font_info));
150 }
151
152 void PlatformFontWin::InitWithFontNameAndSize(const std::wstring& font_name,
153 int font_size) {
91 HDC hdc = GetDC(NULL); 154 HDC hdc = GetDC(NULL);
92 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); 155 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72);
93 ReleaseDC(NULL, hdc); 156 ReleaseDC(NULL, hdc);
94 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 157 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 font_name.c_str()); 158 font_name.c_str());
96 return Font::CreateFont(hf); 159 font_ref_ = CreateHFontRef(hf);
97 } 160 }
98 161
99 // static 162 // static
100 Font::HFontRef* Font::GetBaseFontRef() { 163 PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() {
101 if (base_font_ref_ == NULL) { 164 if (base_font_ref_ == NULL) {
102 NONCLIENTMETRICS metrics; 165 NONCLIENTMETRICS metrics;
103 win_util::GetNonClientMetrics(&metrics); 166 win_util::GetNonClientMetrics(&metrics);
104 167
105 if (adjust_font_callback) 168 if (adjust_font_callback)
106 adjust_font_callback(&metrics.lfMessageFont); 169 adjust_font_callback(&metrics.lfMessageFont);
107 metrics.lfMessageFont.lfHeight = 170 metrics.lfMessageFont.lfHeight =
108 AdjustFontSize(metrics.lfMessageFont.lfHeight, 0); 171 AdjustFontSize(metrics.lfMessageFont.lfHeight, 0);
109 HFONT font = CreateFontIndirect(&metrics.lfMessageFont); 172 HFONT font = CreateFontIndirect(&metrics.lfMessageFont);
110 DLOG_ASSERT(font); 173 DLOG_ASSERT(font);
111 base_font_ref_ = Font::CreateHFontRef(font); 174 base_font_ref_ = PlatformFontWin::CreateHFontRef(font);
112 // base_font_ref_ is global, up the ref count so it's never deleted. 175 // base_font_ref_ is global, up the ref count so it's never deleted.
113 base_font_ref_->AddRef(); 176 base_font_ref_->AddRef();
114 } 177 }
115 return base_font_ref_; 178 return base_font_ref_;
116 } 179 }
117 180
118 const std::wstring& Font::FontName() const { 181 PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
119 return font_ref_->font_name(); 182 TEXTMETRIC font_metrics;
183 HDC screen_dc = GetDC(NULL);
184 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font));
185 int last_map_mode = SetMapMode(screen_dc, MM_TEXT);
186 GetTextMetrics(screen_dc, &font_metrics);
187 // Yes, this is how Microsoft recommends calculating the dialog unit
188 // conversions.
189 SIZE ave_text_size;
190 GetTextExtentPoint32(screen_dc,
191 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
192 52, &ave_text_size);
193 const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2;
194 // To avoid the DC referencing font_handle_, select the previous font.
195 SelectObject(screen_dc, previous_font);
196 SetMapMode(screen_dc, last_map_mode);
197 ReleaseDC(NULL, screen_dc);
198
199 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight));
200 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent));
201 const int ave_char_width =
202 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth));
203 int style = 0;
204 if (font_metrics.tmItalic)
205 style |= Font::ITALIC;
206 if (font_metrics.tmUnderlined)
207 style |= Font::UNDERLINED;
208 if (font_metrics.tmWeight >= kTextMetricWeightBold)
209 style |= Font::BOLD;
210
211 return new HFontRef(font, height, baseline, ave_char_width, style,
212 dlu_base_x);
120 } 213 }
121 214
122 int Font::FontSize() { 215 PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
123 LOGFONT font_info;
124 GetObject(hfont(), sizeof(LOGFONT), &font_info);
125 long lf_height = font_info.lfHeight;
126 HDC hdc = GetDC(NULL);
127 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY);
128 int font_size = 0;
129 if (device_caps != 0) {
130 float font_size_float = -static_cast<float>(lf_height)*72/device_caps;
131 font_size = static_cast<int>(::ceil(font_size_float - 0.5));
132 }
133 ReleaseDC(NULL, hdc);
134 return font_size;
135 } 216 }
136 217
137 Font::HFontRef::HFontRef(HFONT hfont, 218 ////////////////////////////////////////////////////////////////////////////////
219 // PlatformFontWin::HFontRef:
220
221 PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
138 int height, 222 int height,
139 int baseline, 223 int baseline,
140 int ave_char_width, 224 int ave_char_width,
141 int style, 225 int style,
142 int dlu_base_x) 226 int dlu_base_x)
143 : hfont_(hfont), 227 : hfont_(hfont),
144 height_(height), 228 height_(height),
145 baseline_(baseline), 229 baseline_(baseline),
146 ave_char_width_(ave_char_width), 230 ave_char_width_(ave_char_width),
147 style_(style), 231 style_(style),
148 dlu_base_x_(dlu_base_x) { 232 dlu_base_x_(dlu_base_x) {
149 DLOG_ASSERT(hfont); 233 DLOG_ASSERT(hfont);
150 234
151 LOGFONT font_info; 235 LOGFONT font_info;
152 GetObject(hfont_, sizeof(LOGFONT), &font_info); 236 GetObject(hfont_, sizeof(LOGFONT), &font_info);
153 font_name_ = std::wstring(font_info.lfFaceName); 237 font_name_ = std::wstring(font_info.lfFaceName);
154 } 238 }
155 239
156 Font::HFontRef::~HFontRef() { 240 PlatformFontWin::HFontRef::~HFontRef() {
157 DeleteObject(hfont_); 241 DeleteObject(hfont_);
158 } 242 }
159 243
160 Font Font::DeriveFont(int size_delta, int style) const { 244 ////////////////////////////////////////////////////////////////////////////////
161 LOGFONT font_info; 245 // PlatformFont, public:
162 GetObject(hfont(), sizeof(LOGFONT), &font_info);
163 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta);
164 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED);
165 font_info.lfItalic = ((style & ITALIC) == ITALIC);
166 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL;
167 246
168 HFONT hfont = CreateFontIndirect(&font_info); 247 // static
169 return Font(CreateHFontRef(hfont)); 248 PlatformFont* PlatformFont::CreateDefault() {
249 return new PlatformFontWin;
170 } 250 }
171 251
172 int Font::GetStringWidth(const std::wstring& text) const { 252 // static
173 int width = 0, height = 0; 253 PlatformFont* PlatformFont::CreateFromFont(const Font& other) {
174 CanvasSkia::SizeStringInt(text, *this, &width, &height, 254 return new PlatformFontWin(other);
175 gfx::Canvas::NO_ELLIPSIS);
176 return width;
177 } 255 }
178 256
179 Font::HFontRef* Font::CreateHFontRef(HFONT font) { 257 // static
180 TEXTMETRIC font_metrics; 258 PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
181 HDC screen_dc = GetDC(NULL); 259 return new PlatformFontWin(native_font);
182 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); 260 }
183 int last_map_mode = SetMapMode(screen_dc, MM_TEXT);
184 GetTextMetrics(screen_dc, &font_metrics);
185 // Yes, this is how Microsoft recommends calculating the dialog unit
186 // conversions.
187 SIZE ave_text_size;
188 GetTextExtentPoint32(screen_dc,
189 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
190 52, &ave_text_size);
191 const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2;
192 // To avoid the DC referencing font_handle_, select the previous font.
193 SelectObject(screen_dc, previous_font);
194 SetMapMode(screen_dc, last_map_mode);
195 ReleaseDC(NULL, screen_dc);
196 261
197 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); 262 // static
198 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); 263 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::wstring& font_name,
199 const int ave_char_width = 264 int font_size) {
200 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); 265 return new PlatformFontWin(font_name, font_size);
201 int style = 0;
202 if (font_metrics.tmItalic) {
203 style |= Font::ITALIC;
204 }
205 if (font_metrics.tmUnderlined) {
206 style |= Font::UNDERLINED;
207 }
208 if (font_metrics.tmWeight >= kTextMetricWeightBold) {
209 style |= Font::BOLD;
210 }
211
212 return new HFontRef(font, height, baseline, ave_char_width, style,
213 dlu_base_x);
214 } 266 }
215 267
216 } // namespace gfx 268 } // namespace gfx
OLDNEW
« no previous file with comments | « gfx/platform_font_win.h ('k') | printing/printed_document.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698