| 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/font.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/win_util.h" | |
| 15 | |
| 16 namespace gfx { | |
| 17 | |
| 18 // static | |
| 19 Font::HFontRef* Font::base_font_ref_; | |
| 20 | |
| 21 // static | |
| 22 Font::AdjustFontCallback Font::adjust_font_callback = NULL; | |
| 23 Font::GetMinimumFontSizeCallback Font::get_minimum_font_size_callback = NULL; | |
| 24 | |
| 25 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the | |
| 26 // font is bold. | |
| 27 static const int kTextMetricWeightBold = 700; | |
| 28 | |
| 29 // Returns either minimum font allowed for a current locale or | |
| 30 // lf_height + size_delta value. | |
| 31 static int AdjustFontSize(int lf_height, int size_delta) { | |
| 32 if (lf_height < 0) { | |
| 33 lf_height -= size_delta; | |
| 34 } else { | |
| 35 lf_height += size_delta; | |
| 36 } | |
| 37 int min_font_size = 0; | |
| 38 if (Font::get_minimum_font_size_callback) | |
| 39 min_font_size = Font::get_minimum_font_size_callback(); | |
| 40 // Make sure lf_height is not smaller than allowed min font size for current | |
| 41 // locale. | |
| 42 if (abs(lf_height) < min_font_size) { | |
| 43 return lf_height < 0 ? -min_font_size : min_font_size; | |
| 44 } else { | |
| 45 return lf_height; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 // | |
| 50 // Font | |
| 51 // | |
| 52 | |
| 53 Font::Font() | |
| 54 : font_ref_(GetBaseFontRef()) { | |
| 55 } | |
| 56 | |
| 57 int Font::height() const { | |
| 58 return font_ref_->height(); | |
| 59 } | |
| 60 | |
| 61 int Font::baseline() const { | |
| 62 return font_ref_->baseline(); | |
| 63 } | |
| 64 | |
| 65 int Font::ave_char_width() const { | |
| 66 return font_ref_->ave_char_width(); | |
| 67 } | |
| 68 | |
| 69 int Font::GetExpectedTextWidth(int length) const { | |
| 70 return length * std::min(font_ref_->dlu_base_x(), ave_char_width()); | |
| 71 } | |
| 72 | |
| 73 int Font::style() const { | |
| 74 return font_ref_->style(); | |
| 75 } | |
| 76 | |
| 77 NativeFont Font::nativeFont() const { | |
| 78 return hfont(); | |
| 79 } | |
| 80 | |
| 81 // static | |
| 82 Font Font::CreateFont(HFONT font) { | |
| 83 DCHECK(font); | |
| 84 LOGFONT font_info; | |
| 85 GetObject(font, sizeof(LOGFONT), &font_info); | |
| 86 return Font(CreateHFontRef(CreateFontIndirect(&font_info))); | |
| 87 } | |
| 88 | |
| 89 Font Font::CreateFont(const std::wstring& font_name, int font_size) { | |
| 90 HDC hdc = GetDC(NULL); | |
| 91 long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); | |
| 92 ReleaseDC(NULL, hdc); | |
| 93 HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 94 font_name.c_str()); | |
| 95 return Font::CreateFont(hf); | |
| 96 } | |
| 97 | |
| 98 // static | |
| 99 Font::HFontRef* Font::GetBaseFontRef() { | |
| 100 if (base_font_ref_ == NULL) { | |
| 101 NONCLIENTMETRICS metrics; | |
| 102 win_util::GetNonClientMetrics(&metrics); | |
| 103 | |
| 104 if (adjust_font_callback) | |
| 105 adjust_font_callback(&metrics.lfMessageFont); | |
| 106 metrics.lfMessageFont.lfHeight = | |
| 107 AdjustFontSize(metrics.lfMessageFont.lfHeight, 0); | |
| 108 HFONT font = CreateFontIndirect(&metrics.lfMessageFont); | |
| 109 DLOG_ASSERT(font); | |
| 110 base_font_ref_ = Font::CreateHFontRef(font); | |
| 111 // base_font_ref_ is global, up the ref count so it's never deleted. | |
| 112 base_font_ref_->AddRef(); | |
| 113 } | |
| 114 return base_font_ref_; | |
| 115 } | |
| 116 | |
| 117 const std::wstring& Font::FontName() const { | |
| 118 return font_ref_->font_name(); | |
| 119 } | |
| 120 | |
| 121 int Font::FontSize() { | |
| 122 LOGFONT font_info; | |
| 123 GetObject(hfont(), sizeof(LOGFONT), &font_info); | |
| 124 long lf_height = font_info.lfHeight; | |
| 125 HDC hdc = GetDC(NULL); | |
| 126 int device_caps = GetDeviceCaps(hdc, LOGPIXELSY); | |
| 127 int font_size = 0; | |
| 128 if (device_caps != 0) { | |
| 129 float font_size_float = -static_cast<float>(lf_height)*72/device_caps; | |
| 130 font_size = static_cast<int>(::ceil(font_size_float - 0.5)); | |
| 131 } | |
| 132 ReleaseDC(NULL, hdc); | |
| 133 return font_size; | |
| 134 } | |
| 135 | |
| 136 Font::HFontRef::HFontRef(HFONT hfont, | |
| 137 int height, | |
| 138 int baseline, | |
| 139 int ave_char_width, | |
| 140 int style, | |
| 141 int dlu_base_x) | |
| 142 : hfont_(hfont), | |
| 143 height_(height), | |
| 144 baseline_(baseline), | |
| 145 ave_char_width_(ave_char_width), | |
| 146 style_(style), | |
| 147 dlu_base_x_(dlu_base_x) { | |
| 148 DLOG_ASSERT(hfont); | |
| 149 | |
| 150 LOGFONT font_info; | |
| 151 GetObject(hfont_, sizeof(LOGFONT), &font_info); | |
| 152 font_name_ = std::wstring(font_info.lfFaceName); | |
| 153 } | |
| 154 | |
| 155 Font::HFontRef::~HFontRef() { | |
| 156 DeleteObject(hfont_); | |
| 157 } | |
| 158 | |
| 159 Font Font::DeriveFont(int size_delta, int style) const { | |
| 160 LOGFONT font_info; | |
| 161 GetObject(hfont(), sizeof(LOGFONT), &font_info); | |
| 162 font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); | |
| 163 font_info.lfUnderline = ((style & UNDERLINED) == UNDERLINED); | |
| 164 font_info.lfItalic = ((style & ITALIC) == ITALIC); | |
| 165 font_info.lfWeight = (style & BOLD) ? FW_BOLD : FW_NORMAL; | |
| 166 | |
| 167 HFONT hfont = CreateFontIndirect(&font_info); | |
| 168 return Font(CreateHFontRef(hfont)); | |
| 169 } | |
| 170 | |
| 171 int Font::GetStringWidth(const std::wstring& text) const { | |
| 172 int width = 0; | |
| 173 HDC dc = GetDC(NULL); | |
| 174 HFONT previous_font = static_cast<HFONT>(SelectObject(dc, hfont())); | |
| 175 SIZE size; | |
| 176 if (GetTextExtentPoint32(dc, text.c_str(), static_cast<int>(text.size()), | |
| 177 &size)) { | |
| 178 width = size.cx; | |
| 179 } else { | |
| 180 width = 0; | |
| 181 } | |
| 182 SelectObject(dc, previous_font); | |
| 183 ReleaseDC(NULL, dc); | |
| 184 return width; | |
| 185 } | |
| 186 | |
| 187 Font::HFontRef* Font::CreateHFontRef(HFONT font) { | |
| 188 TEXTMETRIC font_metrics; | |
| 189 HDC screen_dc = GetDC(NULL); | |
| 190 HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); | |
| 191 int last_map_mode = SetMapMode(screen_dc, MM_TEXT); | |
| 192 GetTextMetrics(screen_dc, &font_metrics); | |
| 193 // Yes, this is how Microsoft recommends calculating the dialog unit | |
| 194 // conversions. | |
| 195 SIZE ave_text_size; | |
| 196 GetTextExtentPoint32(screen_dc, | |
| 197 L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | |
| 198 52, &ave_text_size); | |
| 199 const int dlu_base_x = (ave_text_size.cx / 26 + 1) / 2; | |
| 200 // To avoid the DC referencing font_handle_, select the previous font. | |
| 201 SelectObject(screen_dc, previous_font); | |
| 202 SetMapMode(screen_dc, last_map_mode); | |
| 203 ReleaseDC(NULL, screen_dc); | |
| 204 | |
| 205 const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); | |
| 206 const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); | |
| 207 const int ave_char_width = | |
| 208 std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); | |
| 209 int style = 0; | |
| 210 if (font_metrics.tmItalic) { | |
| 211 style |= Font::ITALIC; | |
| 212 } | |
| 213 if (font_metrics.tmUnderlined) { | |
| 214 style |= Font::UNDERLINED; | |
| 215 } | |
| 216 if (font_metrics.tmWeight >= kTextMetricWeightBold) { | |
| 217 style |= Font::BOLD; | |
| 218 } | |
| 219 | |
| 220 return new HFontRef(font, height, baseline, ave_char_width, style, | |
| 221 dlu_base_x); | |
| 222 } | |
| 223 | |
| 224 } // namespace gfx | |
| OLD | NEW |