| OLD | NEW |
| 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/font.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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); | 163 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); |
| 164 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED); | 164 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED); |
| 165 font_info.lfItalic = ((style & ITALIC) == ITALIC); | 165 font_info.lfItalic = ((style & ITALIC) == ITALIC); |
| 166 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL; | 166 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL; |
| 167 | 167 |
| 168 HFONT hfont = CreateFontIndirect(&font_info); | 168 HFONT hfont = CreateFontIndirect(&font_info); |
| 169 return Font(CreateHFontRef(hfont)); | 169 return Font(CreateHFontRef(hfont)); |
| 170 } | 170 } |
| 171 | 171 |
| 172 int Font::GetStringWidth(const std::wstring& text) const { | 172 int Font::GetStringWidth(const std::wstring& text) const { |
| 173 int width = 0, height = 0; | 173 int width = 0; |
| 174 CanvasSkia::SizeStringInt(text, *this, &width, &height, | 174 HDC dc = GetDC(NULL); |
| 175 gfx::Canvas::NO_ELLIPSIS); | 175 HFONT previous_font = static_cast<HFONT>(SelectObject(dc, hfont())); |
| 176 SIZE size; |
| 177 if (GetTextExtentPoint32(dc, text.c_str(), static_cast<int>(text.size()), |
| 178 &size)) { |
| 179 width = size.cx; |
| 180 } else { |
| 181 width = 0; |
| 182 } |
| 183 SelectObject(dc, previous_font); |
| 184 ReleaseDC(NULL, dc); |
| 176 return width; | 185 return width; |
| 177 } | 186 } |
| 178 | 187 |
| 179 Font::HFontRef* Font::CreateHFontRef(HFONT font) { | 188 Font::HFontRef* Font::CreateHFontRef(HFONT font) { |
| 180 TEXTMETRIC font_metrics; | 189 TEXTMETRIC font_metrics; |
| 181 HDC screen_dc = GetDC(NULL); | 190 HDC screen_dc = GetDC(NULL); |
| 182 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); | 191 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); |
| 183 int last_map_mode = SetMapMode(screen_dc, MM_TEXT); | 192 int last_map_mode = SetMapMode(screen_dc, MM_TEXT); |
| 184 GetTextMetrics(screen_dc, &font_metrics); | 193 GetTextMetrics(screen_dc, &font_metrics); |
| 185 // Yes, this is how Microsoft recommends calculating the dialog unit | 194 // Yes, this is how Microsoft recommends calculating the dialog unit |
| (...skipping 21 matching lines...) Expand all Loading... |
| 207 } | 216 } |
| 208 if (font_metrics.tmWeight >= kTextMetricWeightBold) { | 217 if (font_metrics.tmWeight >= kTextMetricWeightBold) { |
| 209 style |= Font::BOLD; | 218 style |= Font::BOLD; |
| 210 } | 219 } |
| 211 | 220 |
| 212 return new HFontRef(font, height, baseline, ave_char_width, style, | 221 return new HFontRef(font, height, baseline, ave_char_width, style, |
| 213 dlu_base_x); | 222 dlu_base_x); |
| 214 } | 223 } |
| 215 | 224 |
| 216 } // namespace gfx | 225 } // namespace gfx |
| OLD | NEW |