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

Side by Side Diff: gfx/platform_font_win.cc

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

Powered by Google App Engine
This is Rietveld 408576698