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..0f47013777dc1e358fdec98bffb4cc5750ea4470 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,24 @@ 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(bool italic, |
+ Font::Weight 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))); |
+ const int font_weight = (weight == Font::Weight::INVALID) |
+ ? static_cast<int>(Font::Weight::NORMAL) |
+ : static_cast<int>(weight); |
+ SkFontStyle sk_style( |
+ font_weight, SkFontStyle::kNormal_Width, |
+ italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant); |
+ skia::RefPtr<SkTypeface> typeface = |
+ skia::AdoptRef(SkTypeface::CreateFromName(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::CreateFromName(kFallbackFontFamilyName, sk_style)); |
CHECK(typeface) << "Could not find any font: " << family << ", " |
<< kFallbackFontFamilyName; |
*family = kFallbackFontFamilyName; |
@@ -77,7 +80,8 @@ PlatformFontLinux::PlatformFontLinux() { |
if (!g_default_font.Get()) { |
std::string family = kFallbackFontFamilyName; |
int size_pixels = 12; |
- int style = Font::NORMAL; |
+ bool italic = false; |
+ Font::Weight weight = Font::Weight::NORMAL; |
FontRenderParams params; |
#if defined(OS_CHROMEOS) |
@@ -92,19 +96,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, &italic, |
+ &weight, ¶ms); |
} |
#endif |
- g_default_font.Get() = new PlatformFontLinux( |
- CreateSkTypeface(style, &family), family, size_pixels, style, params); |
+ g_default_font.Get() = |
+ new PlatformFontLinux(CreateSkTypeface(italic, weight, &family), family, |
+ size_pixels, italic, weight, params); |
} |
InitFromPlatformFont(g_default_font.Get().get()); |
@@ -115,9 +121,8 @@ PlatformFontLinux::PlatformFontLinux(const std::string& font_name, |
FontRenderParamsQuery query; |
query.families.push_back(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)); |
+ false, query.weight, gfx::GetFontRenderParams(query, NULL)); |
} |
//////////////////////////////////////////////////////////////////////////////// |
@@ -139,14 +144,19 @@ void PlatformFontLinux::SetDefaultFontDescription( |
#endif |
-Font PlatformFontLinux::DeriveFont(int size_delta, int style) const { |
+Font PlatformFontLinux::DeriveFont(int size_delta, |
+ int style, |
+ gfx::Font::Weight weight) const { |
const int new_size = font_size_pixels_ + size_delta; |
DCHECK_GT(new_size, 0); |
+ const int current_style = italic_ ? gfx::Font::ITALIC : gfx::Font::NORMAL; |
+ |
// 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 == current_style) ? typeface_ |
+ : CreateSkTypeface(style, weight, &new_family); |
FontRenderParamsQuery query; |
query.families.push_back(new_family); |
@@ -154,6 +164,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 +173,10 @@ int PlatformFontLinux::GetHeight() { |
return height_pixels_; |
} |
+gfx::Font::Weight PlatformFontLinux::GetWeight() const { |
+ return weight_; |
+} |
+ |
int PlatformFontLinux::GetBaseline() { |
ComputeMetricsIfNecessary(); |
return ascent_pixels_; |
@@ -178,7 +193,7 @@ int PlatformFontLinux::GetExpectedTextWidth(int length) { |
} |
int PlatformFontLinux::GetStyle() const { |
- return style_; |
+ return italic_ ? gfx::Font::ITALIC : gfx::Font::NORMAL; |
} |
const std::string& PlatformFontLinux::GetFontName() const { |
@@ -217,9 +232,10 @@ const FontRenderParams& PlatformFontLinux::GetFontRenderParams() { |
PlatformFontLinux::PlatformFontLinux(const skia::RefPtr<SkTypeface>& typeface, |
const std::string& family, |
int size_pixels, |
- int style, |
+ bool italic, |
+ gfx::Font::Weight weight, |
const FontRenderParams& render_params) { |
- InitFromDetails(typeface, family, size_pixels, style, render_params); |
+ InitFromDetails(typeface, family, size_pixels, italic, weight, render_params); |
} |
PlatformFontLinux::~PlatformFontLinux() {} |
@@ -228,27 +244,30 @@ void PlatformFontLinux::InitFromDetails( |
const skia::RefPtr<SkTypeface>& typeface, |
const std::string& font_family, |
int font_size_pixels, |
- int style, |
+ bool italic, |
+ gfx::Font::Weight 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(italic, weight, &font_family_); |
font_size_pixels_ = font_size_pixels; |
- style_ = style; |
+ italic_ = italic; |
+ weight_ = weight; |
#if defined(OS_CHROMEOS) |
device_scale_factor_ = GetFontRenderParamsDeviceScaleFactor(); |
#endif |
font_render_params_ = render_params; |
- |
} |
void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { |
typeface_ = other->typeface_; |
font_family_ = other->font_family_; |
font_size_pixels_ = other->font_size_pixels_; |
- style_ = other->style_; |
+ italic_ = other->italic_; |
+ weight_ = other->weight_; |
#if defined(OS_CHROMEOS) |
device_scale_factor_ = other->device_scale_factor_; |
#endif |
@@ -272,9 +291,9 @@ void PlatformFontLinux::ComputeMetricsIfNecessary() { |
paint.setSubpixelText(false); |
paint.setTextSize(font_size_pixels_); |
paint.setTypeface(typeface_.get()); |
- paint.setFakeBoldText((Font::BOLD & style_) && !typeface_->isBold()); |
- paint.setTextSkewX((Font::ITALIC & style_) && !typeface_->isItalic() ? |
- -SK_Scalar1/4 : 0); |
+ paint.setFakeBoldText(weight_ >= gfx::Font::Weight::BOLD && |
+ !typeface_->isBold()); |
+ paint.setTextSkewX(italic_ && !typeface_->isItalic() ? -SK_Scalar1 / 4 : 0); |
SkPaint::FontMetrics metrics; |
paint.getFontMetrics(&metrics); |
ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); |