Index: ui/gfx/platform_font_win.cc |
diff --git a/ui/gfx/platform_font_win.cc b/ui/gfx/platform_font_win.cc |
index 5e9d2537e5a188d88bf8b1bd186c36b50376a954..6fc15a24eba73586a6f7d05468147b358999ea56 100644 |
--- a/ui/gfx/platform_font_win.cc |
+++ b/ui/gfx/platform_font_win.cc |
@@ -211,33 +211,38 @@ PlatformFontWin::PlatformFontWin(const std::string& font_name, |
Font PlatformFontWin::DeriveFontWithHeight(int height, int style) { |
DCHECK_GE(height, 0); |
- if (GetHeight() == height && GetStyle() == style) |
- return Font(this); |
+ |
+ // Create a font with a height near that of the target height. |
+ HFONT hfont = CreateFontWithHeight(height, style); |
+ Font font(new PlatformFontWin(CreateHFontRef(hfont))); |
+ |
+ // Respect the minimum font size constraint. |
+ const int min_font_size = GetMinimumFontSize(); |
// 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); |
- int font_height = font.GetHeight(); |
- int font_size = font.GetFontSize(); |
- while (font_height > height && font_size != min_font_size) { |
- font = font.Derive(-1, style); |
- if (font_height == font.GetHeight() && font_size == font.GetFontSize()) |
- break; |
- font_height = font.GetHeight(); |
- font_size = font.GetFontSize(); |
+ if (font.GetHeight() > height) { |
+ while ((font.GetHeight() > height) && |
+ (font.GetFontSize() >= min_font_size)) { |
+ Font derived_font = font.Derive(-1, style); |
+ if ((derived_font.GetFontSize() < min_font_size) || |
+ (derived_font.GetFontSize() == font.GetFontSize())) |
+ break; |
+ DCHECK_LT(derived_font.GetFontSize(), font.GetFontSize()); |
+ font = derived_font; |
} |
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 DeriveWithCorrectedSize(hfont); |
+ while (font.GetHeight() <= height) { |
+ Font derived_font = font.Derive(1, style); |
+ if ((derived_font.GetHeight() > height) && |
+ (font.GetFontSize() >= min_font_size)) |
+ break; |
+ DCHECK_GT(derived_font.GetFontSize(), font.GetFontSize()); |
+ font = derived_font; |
+ } |
+ return font; |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -423,37 +428,6 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromGDI( |
ave_char_width, style); |
} |
-Font PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font) { |
- base::win::ScopedGetDC screen_dc(NULL); |
- gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
- |
- base::win::ScopedGDIObject<HFONT> best_font(base_font); |
- TEXTMETRIC best_font_metrics; |
- GetTextMetricsForFont(screen_dc, best_font, &best_font_metrics); |
- |
- LOGFONT font_info; |
- GetObject(base_font, sizeof(LOGFONT), &font_info); |
- |
- // Set |lfHeight| to negative value to indicate it's the size, not the height. |
- font_info.lfHeight = |
- -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading); |
- |
- do { |
- // Increment font size. Prefer font with greater size if its height isn't |
- // greater than height of base font. |
- font_info.lfHeight = AdjustFontSize(font_info.lfHeight, 1); |
- base::win::ScopedGDIObject<HFONT> font(CreateFontIndirect(&font_info)); |
- TEXTMETRIC font_metrics; |
- GetTextMetricsForFont(screen_dc, font, &font_metrics); |
- if (font_metrics.tmHeight > best_font_metrics.tmHeight) |
- break; |
- best_font.Set(font.release()); |
- best_font_metrics = font_metrics; |
- } while (true); |
- |
- return Font(new PlatformFontWin(CreateHFontRef(best_font.release()))); |
-} |
- |
// static |
PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia( |
HFONT gdi_font, |
@@ -518,8 +492,7 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia( |
// The calculations below are similar to those in the CreateHFontRef |
// function. The height, baseline and cap height are rounded up to ensure |
// that they match up closely with GDI. |
- const int height = std::ceil( |
- skia_metrics.fDescent - skia_metrics.fAscent + skia_metrics.fLeading); |
+ const int height = std::ceil(skia_metrics.fDescent - skia_metrics.fAscent); |
const int baseline = std::max<int>(1, std::ceil(-skia_metrics.fAscent)); |
const int cap_height = std::ceil(paint.getTextSize() * |
static_cast<double>(dwrite_font_metrics.capHeight) / |
@@ -548,6 +521,48 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia( |
PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) { |
} |
+HFONT PlatformFontWin::CreateFontWithHeight(int height, int style) { |
msw
2015/01/13 23:54:59
I don't think that this simplifies the code as you
ananta
2015/01/14 00:17:23
Reverted this part of the change. This was an expe
|
+ LOGFONT font_info; |
+ GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info); |
+ font_info.lfHeight = height; |
+ SetLogFontStyle(style, &font_info); |
+ |
+ base::win::ScopedGetDC screen_dc(NULL); |
+ gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT); |
+ |
+ base::win::ScopedGDIObject<HFONT> best_font(CreateFontIndirect(&font_info)); |
+ |
+ TEXTMETRIC best_font_metrics = {0}; |
+ GetTextMetricsForFont(screen_dc, best_font, &best_font_metrics); |
+ |
+ // Set |lfHeight| to negative value to indicate it's the size, not the height. |
+ font_info.lfHeight = |
+ -(best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading); |
+ |
+ // Respect the minimum font size constraint. |
+ const int min_font_size = GetMinimumFontSize(); |
+ |
+ do { |
+ // Increment font size. Prefer font with greater size if its height isn't |
+ // greater than height of base font. Exception being the case where we have |
+ // a minimum font size being enforced. |
+ font_info.lfHeight = AdjustFontSize(font_info.lfHeight, 1); |
+ base::win::ScopedGDIObject<HFONT> font(CreateFontIndirect(&font_info)); |
+ TEXTMETRIC font_metrics = {0}; |
+ GetTextMetricsForFont(screen_dc, font, &font_metrics); |
+ if ((font_metrics.tmHeight > best_font_metrics.tmHeight) && |
+ ((best_font_metrics.tmHeight - best_font_metrics.tmInternalLeading) >= |
+ min_font_size)) { |
+ break; |
+ } |
+ best_font.Set(font.release()); |
+ best_font_metrics = font_metrics; |
+ } while (true); |
+ |
+ return best_font.release(); |
+} |
+ |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// PlatformFontWin::HFontRef: |