Chromium Code Reviews| Index: ui/gfx/platform_font_win.cc |
| =================================================================== |
| --- ui/gfx/platform_font_win.cc (revision 134552) |
| +++ ui/gfx/platform_font_win.cc (working copy) |
| @@ -27,6 +27,14 @@ |
| // font is bold. |
| const int kTextMetricWeightBold = 700; |
| +// Returns the minimum font size, using the minimum size callback, if set. |
| +int GetMinimumFontSize() { |
| + int min_font_size = 0; |
| + if (gfx::PlatformFontWin::get_minimum_font_size_callback) |
| + min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback(); |
| + return min_font_size; |
| +} |
| + |
| // Returns either minimum font allowed for a current locale or |
| // lf_height + size_delta value. |
| int AdjustFontSize(int lf_height, int size_delta) { |
| @@ -35,9 +43,7 @@ |
| } else { |
| lf_height += size_delta; |
| } |
| - int min_font_size = 0; |
| - if (gfx::PlatformFontWin::get_minimum_font_size_callback) |
| - min_font_size = gfx::PlatformFontWin::get_minimum_font_size_callback(); |
| + const int min_font_size = GetMinimumFontSize(); |
| // Make sure lf_height is not smaller than allowed min font size for current |
| // locale. |
| if (abs(lf_height) < min_font_size) { |
| @@ -47,6 +53,13 @@ |
| } |
| } |
| +// Sets style properties on |font_info| based on |font_style|. |
| +void SetLogFontStyle(int font_style, LOGFONT* font_info) { |
| + font_info->lfUnderline = (font_style & gfx::Font::UNDERLINED) != 0; |
| + font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0; |
| + font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; |
| +} |
| + |
| } // namespace |
| namespace gfx { |
| @@ -75,17 +88,40 @@ |
| InitWithFontNameAndSize(font_name, font_size); |
| } |
| +Font PlatformFontWin::DeriveFontWithHeight(int height, int style) { |
| + DCHECK_GE(height, 0); |
| + if (GetHeight() == height && GetStyle() == style) |
| + return Font(this); |
| + |
| + // CreateFontIndirect() doesn't return the largest size for the given height |
| + // when decreasing the height. Iterate to find it. |
| + if (GetHeight() > height) { |
| + const int min_font_size = GetMinimumFontSize(); |
| + Font font = DeriveFont(-1, style); |
| + while (font.GetHeight() > height && font.GetFontSize() != min_font_size) { |
|
msw
2012/04/30 19:30:23
optionally: font.GetFontSize() >= min_font_size ?
|
| + font = font.DeriveFont(-1, style); |
| + } |
| + return font; |
| + } |
| + |
| + LOGFONT font_info; |
| + GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
| + font_info.lfHeight = height; |
| + SetLogFontStyle(style, &font_info); |
| + |
| + HFONT hfont = CreateFontIndirect(&font_info); |
| + return Font(new PlatformFontWin(CreateHFontRef(hfont))); |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // PlatformFontWin, PlatformFont implementation: |
| Font PlatformFontWin::DeriveFont(int size_delta, int style) const { |
| LOGFONT font_info; |
| GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
| - font_info.lfHeight = AdjustFontSize(font_info.lfHeight, size_delta); |
| - font_info.lfUnderline = |
| - ((style & gfx::Font::UNDERLINED) == gfx::Font::UNDERLINED); |
| - font_info.lfItalic = ((style & gfx::Font::ITALIC) == gfx::Font::ITALIC); |
| - font_info.lfWeight = (style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL; |
| + const int requested_font_size = font_ref_->requested_font_size(); |
| + font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta); |
| + SetLogFontStyle(style, &font_info); |
| HFONT hfont = CreateFontIndirect(&font_info); |
| return Font(new PlatformFontWin(CreateHFontRef(hfont))); |
| @@ -191,10 +227,11 @@ |
| GetTextMetrics(screen_dc, &font_metrics); |
| } |
| - const int height = std::max(1, static_cast<int>(font_metrics.tmHeight)); |
| - const int baseline = std::max(1, static_cast<int>(font_metrics.tmAscent)); |
| - const int ave_char_width = |
| - std::max(1, static_cast<int>(font_metrics.tmAveCharWidth)); |
| + const int height = std::max<int>(1, font_metrics.tmHeight); |
| + const int baseline = std::max<int>(1, font_metrics.tmAscent); |
| + const int ave_char_width = std::max<int>(1, font_metrics.tmAveCharWidth); |
| + const int font_size = |
| + std::max<int>(1, font_metrics.tmHeight - font_metrics.tmInternalLeading); |
| int style = 0; |
| if (font_metrics.tmItalic) |
| style |= Font::ITALIC; |
| @@ -203,7 +240,7 @@ |
| if (font_metrics.tmWeight >= kTextMetricWeightBold) |
| style |= Font::BOLD; |
| - return new HFontRef(font, height, baseline, ave_char_width, style); |
| + return new HFontRef(font, font_size, height, baseline, ave_char_width, style); |
| } |
| PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { |
| @@ -213,23 +250,26 @@ |
| // PlatformFontWin::HFontRef: |
| PlatformFontWin::HFontRef::HFontRef(HFONT hfont, |
| + int font_size, |
| int height, |
| int baseline, |
| int ave_char_width, |
| int style) |
| : hfont_(hfont), |
| + font_size_(font_size), |
| height_(height), |
| baseline_(baseline), |
| ave_char_width_(ave_char_width), |
| style_(style), |
| - dlu_base_x_(-1) { |
| + dlu_base_x_(-1), |
| + requested_font_size_(font_size) { |
| DLOG_ASSERT(hfont); |
| LOGFONT font_info; |
| GetObject(hfont_, sizeof(LOGFONT), &font_info); |
| font_name_ = UTF16ToUTF8(string16(font_info.lfFaceName)); |
| - DCHECK_LT(font_info.lfHeight, 0); |
| - font_size_ = -font_info.lfHeight; |
| + if (font_info.lfHeight < 0) |
| + requested_font_size_ = -font_info.lfHeight; |
| } |
| int PlatformFontWin::HFontRef::GetDluBaseX() { |