| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 "app/gfx/chrome_font.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include <math.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 | |
| 12 #include "app/l10n_util_win.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/win_util.h" | |
| 15 #include "grit/generated_resources.h" | |
| 16 #include "grit/locale_settings.h" | |
| 17 | |
| 18 namespace gfx { | |
| 19 | |
| 20 /*static*/ | |
| 21 Font::HFontRef* Font::base_font_ref_; | |
| 22 | |
| 23 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the | |
| 24 // font is bold. | |
| 25 static const int kTextMetricWeightBold = 700; | |
| 26 | |
| 27 // | |
| 28 // Font | |
| 29 // | |
| 30 | |
| 31 Font::Font() | |
| 32 : font_ref_(GetBaseFontRef()) { | |
| 33 } | |
| 34 | |
| 35 int Font::height() const { | |
| 36 return font_ref_->height(); | |
| 37 } | |
| 38 | |
| 39 int Font::baseline() const { | |
| 40 return font_ref_->baseline(); | |
| 41 } | |
| 42 | |
| 43 int Font::ave_char_width() const { | |
| 44 return font_ref_->ave_char_width(); | |
| 45 } | |
| 46 | |
| 47 int Font::GetExpectedTextWidth(int length) const { | |
| 48 return length * std::min(font_ref_->dlu_base_x(), ave_char_width()); | |
| 49 } | |
| 50 | |
| 51 int Font::style() const { | |
| 52 return font_ref_->style(); | |
| 53 } | |
| 54 | |
| 55 NativeFont Font::nativeFont() const { | |
| 56 return hfont(); | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 Font Font::CreateFont(HFONT font) { | |
| 61 DCHECK(font); | |
| 62 LOGFONT font_info; | |
| 63 GetObject(font, sizeof(LOGFONT), &font_info); | |
| 64 return Font(CreateHFontRef(CreateFontIndirect(&font_info))); | |
| 65 } | |
| 66 | |
| 67 Font Font::CreateFont(const std::wstring& font_name, int font_size) { | |
| 68 HDC hdc = GetDC(NULL); | |
| 69 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); | |
| 70 ReleaseDC(NULL, hdc); | |
| 71 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 72 font_name.c_str()); | |
| 73 return Font::CreateFont(hf); | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 Font::HFontRef* Font::GetBaseFontRef() { | |
| 78 if (base_font_ref_ == NULL) { | |
| 79 NONCLIENTMETRICS metrics; | |
| 80 win_util::GetNonClientMetrics(&metrics); | |
| 81 | |
| 82 l10n_util::AdjustUIFont(&metrics.lfMessageFont); | |
| 83 | |
| 84 // See comment in Font::DeriveFont() about font size. | |
| 85 // TODO(jungshik): Add a per-locale resource entry for the minimum | |
| 86 // font size and actually enforce the lower-bound. 5 is way too small | |
| 87 // for CJK, Thai, and Indian locales. | |
| 88 DCHECK_GE(abs(metrics.lfMessageFont.lfHeight), 5); | |
| 89 HFONT font = CreateFontIndirect(&metrics.lfMessageFont); | |
| 90 DLOG_ASSERT(font); | |
| 91 base_font_ref_ = Font::CreateHFontRef(font); | |
| 92 // base_font_ref_ is global, up the ref count so it's never deleted. | |
| 93 base_font_ref_->AddRef(); | |
| 94 } | |
| 95 return base_font_ref_; | |
| 96 } | |
| 97 | |
| 98 std::wstring Font::FontName() { | |
| 99 LOGFONT font_info; | |
| 100 GetObject(hfont(), sizeof(LOGFONT), &font_info); | |
| 101 return (std::wstring(font_info.lfFaceName)); | |
| 102 } | |
| 103 | |
| 104 int Font::FontSize() { | |
| 105 LOGFONT font_info; | |
| 106 GetObject(hfont(), sizeof(LOGFONT), &font_info); | |
| 107 long lf_height = font_info.lfHeight; | |
| 108 HDC hdc = GetDC(NULL); | |
| 109 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY); | |
| 110 int font_size = 0; | |
| 111 if (device_caps != 0) { | |
| 112 float font_size_float = -static_cast<float>(lf_height)*72/device_caps; | |
| 113 font_size = static_cast<int>(::ceil(font_size_float - 0.5)); | |
| 114 } | |
| 115 ReleaseDC(NULL, hdc); | |
| 116 return font_size; | |
| 117 } | |
| 118 | |
| 119 Font::HFontRef::HFontRef(HFONT hfont, | |
| 120 int height, | |
| 121 int baseline, | |
| 122 int ave_char_width, | |
| 123 int style, | |
| 124 int dlu_base_x) | |
| 125 : hfont_(hfont), | |
| 126 height_(height), | |
| 127 baseline_(baseline), | |
| 128 ave_char_width_(ave_char_width), | |
| 129 style_(style), | |
| 130 dlu_base_x_(dlu_base_x) { | |
| 131 DLOG_ASSERT(hfont); | |
| 132 } | |
| 133 | |
| 134 Font::HFontRef::~HFontRef() { | |
| 135 DeleteObject(hfont_); | |
| 136 } | |
| 137 | |
| 138 | |
| 139 Font Font::DeriveFont(int size_delta, int style) const { | |
| 140 LOGFONT font_info; | |
| 141 GetObject(hfont(), sizeof(LOGFONT), &font_info); | |
| 142 // LOGFONT returns two types of font heights, negative is measured slightly | |
| 143 // differently (character height, vs cell height). | |
| 144 if (font_info.lfHeight < 0) { | |
| 145 font_info.lfHeight -= size_delta; | |
| 146 } else { | |
| 147 font_info.lfHeight += size_delta; | |
| 148 } | |
| 149 // Even with "Small Fonts", the smallest readable font size is 5. It is easy | |
| 150 // to create a non-drawing font and forget about the fact that text should be | |
| 151 // drawn in the UI. This test ensures that the font will be readable. | |
| 152 DCHECK_GE(abs(font_info.lfHeight), 5); | |
| 153 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED); | |
| 154 font_info.lfItalic = ((style & ITALIC) == ITALIC); | |
| 155 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL; | |
| 156 | |
| 157 HFONT hfont = CreateFontIndirect(&font_info); | |
| 158 return Font(CreateHFontRef(hfont)); | |
| 159 } | |
| 160 | |
| 161 int Font::GetStringWidth(const std::wstring& text) const { | |
| 162 int width = 0; | |
| 163 HDC dc = GetDC(NULL); | |
| 164 HFONT previous_font = static_cast<HFONT>(SelectObject(dc, hfont())); | |
| 165 SIZE size; | |
| 166 if (GetTextExtentPoint32(dc, text.c_str(), static_cast<int>(text.size()), | |
| 167 &size)) { | |
| 168 width = size.cx; | |
| 169 } else { | |
| 170 width = 0; | |
| 171 } | |
| 172 SelectObject(dc, previous_font); | |
| 173 ReleaseDC(NULL, dc); | |
| 174 return width; | |
| 175 } | |
| 176 | |
| 177 Font::HFontRef* Font::CreateHFontRef(HFONT font) { | |
| 178 TEXTMETRIC font_metrics; | |
| 179 HDC screen_dc = GetDC(NULL); | |
| 180 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); | |
| 181 int last_map_mode = SetMapMode(screen_dc, MM_TEXT); | |
| 182 GetTextMetrics(screen_dc, &font_metrics); | |
| 183 // Yes, this is how Microsoft recommends calculating the dialog unit | |
| 184 // conversions. | |
| 185 SIZE ave_text_size; | |
| 186 GetTextExtentPoint32(screen_dc, | |
| 187 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | |
| 188 52, &ave_text_size); | |
| 189 const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2; | |
| 190 // To avoid the DC referencing font_handle_, select the previous font. | |
| 191 SelectObject(screen_dc, previous_font); | |
| 192 SetMapMode(screen_dc, last_map_mode); | |
| 193 ReleaseDC(NULL, screen_dc); | |
| 194 | |
| 195 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); | |
| 196 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); | |
| 197 const int ave_char_width = | |
| 198 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); | |
| 199 int style = 0; | |
| 200 if (font_metrics.tmItalic) { | |
| 201 style |= Font::ITALIC; | |
| 202 } | |
| 203 if (font_metrics.tmUnderlined) { | |
| 204 style |= Font::UNDERLINED; | |
| 205 } | |
| 206 if (font_metrics.tmWeight >= kTextMetricWeightBold) { | |
| 207 style |= Font::BOLD; | |
| 208 } | |
| 209 | |
| 210 return new HFontRef(font, height, baseline, ave_char_width, style, | |
| 211 dlu_base_x); | |
| 212 } | |
| 213 | |
| 214 } // namespace gfx | |
| OLD | NEW |