Chromium Code Reviews| Index: ui/gfx/platform_font_linux.cc |
| diff --git a/ui/gfx/platform_font_linux.cc b/ui/gfx/platform_font_linux.cc |
| index 3c58ccb695b3c313a5985c550dc164cc7afb401c..3f381d29ca8fea28c977929e7381752e3896db68 100644 |
| --- a/ui/gfx/platform_font_linux.cc |
| +++ b/ui/gfx/platform_font_linux.cc |
| @@ -13,6 +13,7 @@ |
| #include "base/strings/string_split.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "build/build_config.h" |
| +#include "third_party/skia/include/core/SkFontStyle.h" |
| #include "third_party/skia/include/core/SkPaint.h" |
| #include "third_party/skia/include/core/SkString.h" |
| #include "third_party/skia/include/core/SkTypeface.h" |
| @@ -41,22 +42,21 @@ base::LazyInstance<scoped_refptr<PlatformFontLinux>>::Leaky g_default_font = |
| // Creates a SkTypeface for the passed-in Font::FontStyle and family. If a |
| // fallback typeface is used instead of the requested family, |family| will be |
| // updated to contain the fallback's family name. |
| -skia::RefPtr<SkTypeface> CreateSkTypeface(int style, std::string* family) { |
| +skia::RefPtr<SkTypeface> CreateSkTypeface(int style, |
| + int weight, |
| + std::string* family) { |
| DCHECK(family); |
| - int skia_style = SkTypeface::kNormal; |
| - if (Font::BOLD & style) |
| - skia_style |= SkTypeface::kBold; |
| - if (Font::ITALIC & style) |
| - skia_style |= SkTypeface::kItalic; |
| - |
| - skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(SkTypeface::CreateFromName( |
| - family->c_str(), static_cast<SkTypeface::Style>(skia_style))); |
| + SkFontStyle sk_style(weight, SkFontStyle::kNormal_Width, |
| + Font::ITALIC & style ? SkFontStyle::kItalic_Slant |
| + : SkFontStyle::kUpright_Slant); |
| + skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( |
| + SkTypeface::CreateFromNameAndStyle(family->c_str(), sk_style)); |
| if (!typeface) { |
| // A non-scalable font such as .pcf is specified. Fall back to a default |
| // scalable font. |
| - typeface = skia::AdoptRef(SkTypeface::CreateFromName( |
| - kFallbackFontFamilyName, static_cast<SkTypeface::Style>(skia_style))); |
| + typeface = skia::AdoptRef( |
| + SkTypeface::CreateFromNameAndStyle(kFallbackFontFamilyName, sk_style)); |
| CHECK(typeface) << "Could not find any font: " << family << ", " |
| << kFallbackFontFamilyName; |
| *family = kFallbackFontFamilyName; |
| @@ -78,6 +78,7 @@ PlatformFontLinux::PlatformFontLinux() { |
| std::string family = kFallbackFontFamilyName; |
| int size_pixels = 12; |
| int style = Font::NORMAL; |
| + Font::FontWeight weight = Font::WEIGHT_NORMAL; |
| FontRenderParams params; |
| #if defined(OS_CHROMEOS) |
| @@ -92,19 +93,21 @@ PlatformFontLinux::PlatformFontLinux() { |
| params = gfx::GetFontRenderParams(query, &family); |
| size_pixels = query.pixel_size; |
| style = query.style; |
| + weight = query.weight; |
| } |
| #else |
| // On Linux, LinuxFontDelegate is used to query the native toolkit (e.g. |
| // GTK+) for the default UI font. |
| const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); |
| if (delegate) { |
| - delegate->GetDefaultFontDescription( |
| - &family, &size_pixels, &style, ¶ms); |
| + delegate->GetDefaultFontDescription(&family, &size_pixels, &style, |
|
msw
2016/03/22 01:53:44
Should this also get the weight from the system UI
msw
2016/03/22 18:24:10
Ping! Please address this comment.
Mikus
2016/03/23 17:53:21
Done.
|
| + ¶ms); |
| } |
| #endif |
| - g_default_font.Get() = new PlatformFontLinux( |
| - CreateSkTypeface(style, &family), family, size_pixels, style, params); |
| + g_default_font.Get() = |
| + new PlatformFontLinux(CreateSkTypeface(style, weight, &family), family, |
| + size_pixels, style, weight, params); |
| } |
| InitFromPlatformFont(g_default_font.Get().get()); |
| @@ -117,7 +120,8 @@ PlatformFontLinux::PlatformFontLinux(const std::string& font_name, |
| query.pixel_size = font_size_pixels; |
| query.style = Font::NORMAL; |
| InitFromDetails(skia::RefPtr<SkTypeface>(), font_name, font_size_pixels, |
| - query.style, gfx::GetFontRenderParams(query, NULL)); |
| + query.style, query.weight, |
| + gfx::GetFontRenderParams(query, NULL)); |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -139,14 +143,17 @@ void PlatformFontLinux::SetDefaultFontDescription( |
| #endif |
| -Font PlatformFontLinux::DeriveFont(int size_delta, int style) const { |
| +Font PlatformFontLinux::DeriveFont(int size_delta, |
| + int style, |
| + gfx::Font::FontWeight weight) const { |
| const int new_size = font_size_pixels_ + size_delta; |
| DCHECK_GT(new_size, 0); |
| // If the style changed, we may need to load a new face. |
| std::string new_family = font_family_; |
| skia::RefPtr<SkTypeface> typeface = |
| - (style == style_) ? typeface_ : CreateSkTypeface(style, &new_family); |
| + (style == style_) ? typeface_ |
| + : CreateSkTypeface(style, weight, &new_family); |
| FontRenderParamsQuery query; |
| query.families.push_back(new_family); |
| @@ -154,6 +161,7 @@ Font PlatformFontLinux::DeriveFont(int size_delta, int style) const { |
| query.style = style; |
| return Font(new PlatformFontLinux(typeface, new_family, new_size, style, |
| + weight, |
| gfx::GetFontRenderParams(query, NULL))); |
| } |
| @@ -162,6 +170,10 @@ int PlatformFontLinux::GetHeight() { |
| return height_pixels_; |
| } |
| +gfx::Font::FontWeight PlatformFontLinux::GetWeight() { |
| + return weight_; |
| +} |
| + |
| int PlatformFontLinux::GetBaseline() { |
| ComputeMetricsIfNecessary(); |
| return ascent_pixels_; |
| @@ -218,8 +230,9 @@ PlatformFontLinux::PlatformFontLinux(const skia::RefPtr<SkTypeface>& typeface, |
| const std::string& family, |
| int size_pixels, |
| int style, |
| + gfx::Font::FontWeight weight, |
| const FontRenderParams& render_params) { |
| - InitFromDetails(typeface, family, size_pixels, style, render_params); |
| + InitFromDetails(typeface, family, size_pixels, style, weight, render_params); |
| } |
| PlatformFontLinux::~PlatformFontLinux() {} |
| @@ -229,19 +242,21 @@ void PlatformFontLinux::InitFromDetails( |
| const std::string& font_family, |
| int font_size_pixels, |
| int style, |
| + gfx::Font::FontWeight weight, |
| const FontRenderParams& render_params) { |
| DCHECK_GT(font_size_pixels, 0); |
| font_family_ = font_family; |
| - typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_); |
| + typeface_ = |
| + typeface ? typeface : CreateSkTypeface(style, weight, &font_family_); |
| font_size_pixels_ = font_size_pixels; |
| style_ = style; |
| + weight_ = weight; |
| #if defined(OS_CHROMEOS) |
| device_scale_factor_ = GetFontRenderParamsDeviceScaleFactor(); |
| #endif |
| font_render_params_ = render_params; |
| - |
| } |
| void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { |
| @@ -249,6 +264,7 @@ void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { |
| font_family_ = other->font_family_; |
| font_size_pixels_ = other->font_size_pixels_; |
| style_ = other->style_; |
| + weight_ = other->weight_; |
| #if defined(OS_CHROMEOS) |
| device_scale_factor_ = other->device_scale_factor_; |
| #endif |
| @@ -272,7 +288,8 @@ void PlatformFontLinux::ComputeMetricsIfNecessary() { |
| paint.setSubpixelText(false); |
| paint.setTextSize(font_size_pixels_); |
| paint.setTypeface(typeface_.get()); |
| - paint.setFakeBoldText((Font::BOLD & style_) && !typeface_->isBold()); |
| + paint.setFakeBoldText(weight_ >= gfx::Font::WEIGHT_BOLD && |
| + !typeface_->isBold()); |
| paint.setTextSkewX((Font::ITALIC & style_) && !typeface_->isItalic() ? |
| -SK_Scalar1/4 : 0); |
| SkPaint::FontMetrics metrics; |