| OLD | NEW | 
|---|
|  | (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/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 #include "gfx/canvas_skia.h" |  | 
| 16 |  | 
| 17 namespace gfx { |  | 
| 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 |  | 
| 26 // If the tmWeight field of a TEXTMETRIC structure has a value >= this, the |  | 
| 27 // font is bold. |  | 
| 28 static const int kTextMetricWeightBold = 700; |  | 
| 29 |  | 
| 30 // Returns either minimum font allowed for a current locale or |  | 
| 31 // lf_height + size_delta value. |  | 
| 32 static int AdjustFontSize(int lf_height, int size_delta) { |  | 
| 33   if (lf_height < 0) { |  | 
| 34     lf_height -= size_delta; |  | 
| 35   } else { |  | 
| 36     lf_height += size_delta; |  | 
| 37   } |  | 
| 38   int min_font_size = 0; |  | 
| 39   if (Font::get_minimum_font_size_callback) |  | 
| 40     min_font_size = Font::get_minimum_font_size_callback(); |  | 
| 41   // Make sure lf_height is not smaller than allowed min font size for current |  | 
| 42   // locale. |  | 
| 43   if (abs(lf_height) < min_font_size) { |  | 
| 44     return lf_height < 0 ? -min_font_size : min_font_size; |  | 
| 45   } else { |  | 
| 46     return lf_height; |  | 
| 47   } |  | 
| 48 } |  | 
| 49 |  | 
| 50 // |  | 
| 51 // Font |  | 
| 52 // |  | 
| 53 |  | 
| 54 Font::Font() |  | 
| 55     : font_ref_(GetBaseFontRef()) { |  | 
| 56 } |  | 
| 57 |  | 
| 58 int Font::height() const { |  | 
| 59   return font_ref_->height(); |  | 
| 60 } |  | 
| 61 |  | 
| 62 int Font::baseline() const { |  | 
| 63   return font_ref_->baseline(); |  | 
| 64 } |  | 
| 65 |  | 
| 66 int Font::ave_char_width() const { |  | 
| 67   return font_ref_->ave_char_width(); |  | 
| 68 } |  | 
| 69 |  | 
| 70 int Font::GetExpectedTextWidth(int length) const { |  | 
| 71   return length * std::min(font_ref_->dlu_base_x(), ave_char_width()); |  | 
| 72 } |  | 
| 73 |  | 
| 74 int Font::style() const { |  | 
| 75   return font_ref_->style(); |  | 
| 76 } |  | 
| 77 |  | 
| 78 NativeFont Font::nativeFont() const { |  | 
| 79   return hfont(); |  | 
| 80 } |  | 
| 81 |  | 
| 82 // static |  | 
| 83 Font Font::CreateFont(HFONT font) { |  | 
| 84   DCHECK(font); |  | 
| 85   LOGFONT font_info; |  | 
| 86   GetObject(font, sizeof(LOGFONT), &font_info); |  | 
| 87   return Font(CreateHFontRef(CreateFontIndirect(&font_info))); |  | 
| 88 } |  | 
| 89 |  | 
| 90 Font Font::CreateFont(const std::wstring& font_name, int font_size) { |  | 
| 91   HDC hdc = GetDC(NULL); |  | 
| 92   long lf_height = -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72); |  | 
| 93   ReleaseDC(NULL, hdc); |  | 
| 94   HFONT hf = ::CreateFont(lf_height, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |  | 
| 95                     font_name.c_str()); |  | 
| 96   return Font::CreateFont(hf); |  | 
| 97 } |  | 
| 98 |  | 
| 99 // static |  | 
| 100 Font::HFontRef* Font::GetBaseFontRef() { |  | 
| 101   if (base_font_ref_ == NULL) { |  | 
| 102     NONCLIENTMETRICS metrics; |  | 
| 103     win_util::GetNonClientMetrics(&metrics); |  | 
| 104 |  | 
| 105     if (adjust_font_callback) |  | 
| 106       adjust_font_callback(&metrics.lfMessageFont); |  | 
| 107     metrics.lfMessageFont.lfHeight = |  | 
| 108         AdjustFontSize(metrics.lfMessageFont.lfHeight, 0); |  | 
| 109     HFONT font = CreateFontIndirect(&metrics.lfMessageFont); |  | 
| 110     DLOG_ASSERT(font); |  | 
| 111     base_font_ref_ = Font::CreateHFontRef(font); |  | 
| 112     // base_font_ref_ is global, up the ref count so it's never deleted. |  | 
| 113     base_font_ref_->AddRef(); |  | 
| 114   } |  | 
| 115   return base_font_ref_; |  | 
| 116 } |  | 
| 117 |  | 
| 118 const std::wstring& Font::FontName() const { |  | 
| 119   return font_ref_->font_name(); |  | 
| 120 } |  | 
| 121 |  | 
| 122 int Font::FontSize() { |  | 
| 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 } |  | 
| 136 |  | 
| 137 Font::HFontRef::HFontRef(HFONT hfont, |  | 
| 138          int height, |  | 
| 139          int baseline, |  | 
| 140          int ave_char_width, |  | 
| 141          int style, |  | 
| 142          int dlu_base_x) |  | 
| 143     : hfont_(hfont), |  | 
| 144       height_(height), |  | 
| 145       baseline_(baseline), |  | 
| 146       ave_char_width_(ave_char_width), |  | 
| 147       style_(style), |  | 
| 148       dlu_base_x_(dlu_base_x) { |  | 
| 149   DLOG_ASSERT(hfont); |  | 
| 150 |  | 
| 151   LOGFONT font_info; |  | 
| 152   GetObject(hfont_, sizeof(LOGFONT), &font_info); |  | 
| 153   font_name_ = std::wstring(font_info.lfFaceName); |  | 
| 154 } |  | 
| 155 |  | 
| 156 Font::HFontRef::~HFontRef() { |  | 
| 157   DeleteObject(hfont_); |  | 
| 158 } |  | 
| 159 |  | 
| 160 Font Font::DeriveFont(int size_delta, int style) const { |  | 
| 161   LOGFONT font_info; |  | 
| 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 |  | 
| 168   HFONT hfont = CreateFontIndirect(&font_info); |  | 
| 169   return Font(CreateHFontRef(hfont)); |  | 
| 170 } |  | 
| 171 |  | 
| 172 int Font::GetStringWidth(const std::wstring& text) const { |  | 
| 173   int width = 0, height = 0; |  | 
| 174   CanvasSkia::SizeStringInt(text, *this, &width, &height, |  | 
| 175                             gfx::Canvas::NO_ELLIPSIS); |  | 
| 176   return width; |  | 
| 177 } |  | 
| 178 |  | 
| 179 Font::HFontRef* Font::CreateHFontRef(HFONT font) { |  | 
| 180   TEXTMETRIC font_metrics; |  | 
| 181   HDC screen_dc = GetDC(NULL); |  | 
| 182   HFONT previous_font = static_cast<HFONT>(SelectObject(screen_dc, font)); |  | 
| 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 |  | 
| 197   const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); |  | 
| 198   const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); |  | 
| 199   const int ave_char_width = |  | 
| 200       std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); |  | 
| 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 } |  | 
| 215 |  | 
| 216 }  // namespace gfx |  | 
| OLD | NEW | 
|---|