Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Unified Diff: ui/gfx/platform_font_win.cc

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/platform_font_win.cc
diff --git a/ui/gfx/platform_font_win.cc b/ui/gfx/platform_font_win.cc
index e48bca270efaf4942a72650cb3469ab0c942682d..b00535a488624ec34d4fb9b1c9927b99a2af8b54 100644
--- a/ui/gfx/platform_font_win.cc
+++ b/ui/gfx/platform_font_win.cc
@@ -67,7 +67,10 @@ int AdjustFontSize(int lf_height, int size_delta) {
void SetLogFontStyle(int font_style, LOGFONT* font_info) {
font_info->lfUnderline = (font_style & gfx::Font::UNDERLINE) != 0;
font_info->lfItalic = (font_style & gfx::Font::ITALIC) != 0;
- font_info->lfWeight = (font_style & gfx::Font::BOLD) ? FW_BOLD : FW_NORMAL;
+}
+
+gfx::Font::FontWeight ToGfxFontWeight(int weight) {
+ return static_cast<gfx::Font::FontWeight>(weight);
}
// Returns the family name for the |IDWriteFont| interface passed in.
@@ -237,9 +240,8 @@ HRESULT GetMatchingDirectWriteFont(LOGFONT* font_info,
return hr;
}
- DWRITE_FONT_WEIGHT weight = (font_style & SkTypeface::kBold)
- ? DWRITE_FONT_WEIGHT_BOLD
- : DWRITE_FONT_WEIGHT_NORMAL;
+ DWRITE_FONT_WEIGHT weight =
+ static_cast<DWRITE_FONT_WEIGHT>(font_info->lfWeight);
DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_NORMAL;
DWRITE_FONT_STYLE italic = (font_style & SkTypeface::kItalic)
? DWRITE_FONT_STYLE_ITALIC
@@ -309,13 +311,16 @@ PlatformFontWin::PlatformFontWin(const std::string& font_name,
InitWithFontNameAndSize(font_name, font_size);
}
-Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
+Font PlatformFontWin::DeriveFontWithHeight(int height,
+ int style,
+ gfx::Font::FontWeight weight) {
DCHECK_GE(height, 0);
// Create a font with a height near that of the target height.
LOGFONT font_info;
GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
font_info.lfHeight = height;
+ font_info.lfWeight = weight;
SetLogFontStyle(style, &font_info);
HFONT hfont = CreateFontIndirect(&font_info);
@@ -331,7 +336,7 @@ Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
while ((font.GetHeight() > height) &&
(font.GetFontSize() >= min_font_size)) {
ran_shrink_loop = true;
- Font derived_font = font.Derive(-1, style);
+ Font derived_font = font.Derive(-1, style, font.GetWeight());
// Break the loop if the derived font is too small or hasn't shrunk at all.
if ((derived_font.GetFontSize() < min_font_size) ||
((derived_font.GetFontSize() == font.GetFontSize()) &&
@@ -342,7 +347,7 @@ Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
while ((!ran_shrink_loop && font.GetHeight() <= height) ||
(font.GetFontSize() < min_font_size)) {
- Font derived_font = font.Derive(1, style);
+ Font derived_font = font.Derive(1, style, font.GetWeight());
// Break the loop if the derived font is too large or hasn't grown at all.
if (((derived_font.GetHeight() > height) &&
(font.GetFontSize() >= min_font_size)) ||
@@ -357,11 +362,14 @@ Font PlatformFontWin::DeriveFontWithHeight(int height, int style) {
////////////////////////////////////////////////////////////////////////////////
// PlatformFontWin, PlatformFont implementation:
-Font PlatformFontWin::DeriveFont(int size_delta, int style) const {
+Font PlatformFontWin::DeriveFont(int size_delta,
+ int style,
+ gfx::Font::FontWeight weight) const {
LOGFONT font_info;
GetObject(GetNativeFont(), sizeof(LOGFONT), &font_info);
const int requested_font_size = font_ref_->requested_font_size();
font_info.lfHeight = AdjustFontSize(-requested_font_size, size_delta);
+ font_info.lfWeight = weight;
SetLogFontStyle(style, &font_info);
HFONT hfont = CreateFontIndirect(&font_info);
@@ -372,6 +380,10 @@ int PlatformFontWin::GetHeight() {
return font_ref_->height();
}
+gfx::Font::FontWeight PlatformFontWin::GetWeight() {
+ return font_ref_->weight();
+}
+
int PlatformFontWin::GetBaseline() {
return font_ref_->baseline();
}
@@ -530,11 +542,10 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromGDI(
style |= Font::ITALIC;
if (font_metrics.tmUnderlined)
style |= Font::UNDERLINE;
- if (font_metrics.tmWeight >= kTextMetricWeightBold)
- style |= Font::BOLD;
return new HFontRef(font, font_size, height, baseline, cap_height,
- ave_char_width, style);
+ ave_char_width, ToGfxFontWeight(font_metrics.tmWeight),
+ style);
}
// static
@@ -556,10 +567,8 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia(
}
int skia_style = SkTypeface::kNormal;
- if (font_info.lfWeight >= FW_SEMIBOLD &&
- font_info.lfWeight <= FW_ULTRABOLD) {
+ if (font_info.lfWeight >= FW_SEMIBOLD && font_info.lfWeight <= FW_ULTRABOLD)
skia_style |= SkTypeface::kBold;
msw 2016/03/22 01:53:44 This is ignored by GetMatchingDirectWriteFont now;
Mikus 2016/03/22 14:19:51 Done.
- }
if (font_info.lfItalic)
skia_style |= SkTypeface::kItalic;
@@ -583,10 +592,13 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia(
DWRITE_FONT_METRICS dwrite_font_metrics = {0};
dwrite_font->GetMetrics(&dwrite_font_metrics);
- skia::RefPtr<SkTypeface> skia_face = skia::AdoptRef(
- SkTypeface::CreateFromName(
- base::SysWideToUTF8(font_info.lfFaceName).c_str(),
- static_cast<SkTypeface::Style>(skia_style)));
+ SkFontStyle skia_font_style(font_info.lfWeight, SkFontStyle::kNormal_Width,
+ font_info.lfItalic ? SkFontStyle::kItalic_Slant
+ : SkFontStyle::kUpright_Slant);
+
+ skia::RefPtr<SkTypeface> skia_face =
+ skia::AdoptRef(SkTypeface::CreateFromNameAndStyle(
+ base::SysWideToUTF8(font_info.lfFaceName).c_str(), skia_font_style));
gfx::FontRenderParams font_params =
gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), nullptr);
@@ -627,15 +639,14 @@ PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia(
style |= Font::ITALIC;
if (font_info.lfUnderline)
style |= Font::UNDERLINE;
- if (font_info.lfWeight >= kTextMetricWeightBold)
- style |= Font::BOLD;
// DirectWrite may have substituted the GDI font name with a fallback
// font. Ensure that it is updated here.
DeleteObject(gdi_font);
gdi_font = ::CreateFontIndirect(&font_info);
return new HFontRef(gdi_font, -font_info.lfHeight, height, baseline,
- cap_height, ave_char_width, style);
+ cap_height, ave_char_width,
+ ToGfxFontWeight(font_metrics.tmWeight), style);
}
PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
@@ -653,6 +664,7 @@ PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
int baseline,
int cap_height,
int ave_char_width,
+ gfx::Font::FontWeight weight,
int style)
: hfont_(hfont),
font_size_(font_size),
@@ -660,6 +672,7 @@ PlatformFontWin::HFontRef::HFontRef(HFONT hfont,
baseline_(baseline),
cap_height_(cap_height),
ave_char_width_(ave_char_width),
+ weight_(weight),
style_(style),
dlu_base_x_(-1),
requested_font_size_(font_size) {

Powered by Google App Engine
This is Rietveld 408576698