Commit 50b7e2fc authored by derat@chromium.org's avatar derat@chromium.org

Use fallback font in PlatformFontPango::DeriveFont().

Make DeriveFont() fall back to "sans" if it doesn't find an
SkTypeface matching the requested family, as
InitFromDetails() already did.

Also make InitFromDetails() pass through the requested
style.

BUG=394860

Review URL: https://codereview.chromium.org/399113005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284125 0039d316-1c4b-4281-b951-d872f2087c98
parent 744a494d
...@@ -31,6 +31,32 @@ namespace { ...@@ -31,6 +31,32 @@ namespace {
// IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp.
const char* kFallbackFontFamilyName = "sans"; const char* kFallbackFontFamilyName = "sans";
// 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) {
DCHECK(family);
int skia_style = SkTypeface::kNormal;
if (gfx::Font::BOLD & style)
skia_style |= SkTypeface::kBold;
if (gfx::Font::ITALIC & style)
skia_style |= SkTypeface::kItalic;
skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(SkTypeface::CreateFromName(
family->c_str(), static_cast<SkTypeface::Style>(skia_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)));
CHECK(typeface) << "Could not find any font: " << family << ", "
<< kFallbackFontFamilyName;
*family = kFallbackFontFamilyName;
}
return typeface;
}
} // namespace } // namespace
namespace gfx { namespace gfx {
...@@ -139,27 +165,20 @@ Font PlatformFontPango::DeriveFont(int size_delta, int style) const { ...@@ -139,27 +165,20 @@ Font PlatformFontPango::DeriveFont(int size_delta, int style) const {
DCHECK_GT(new_size, 0); DCHECK_GT(new_size, 0);
// If the style changed, we may need to load a new face. // If the style changed, we may need to load a new face.
skia::RefPtr<SkTypeface> typeface = typeface_; std::string new_family = font_family_;
if (style != style_) { skia::RefPtr<SkTypeface> typeface =
int skstyle = SkTypeface::kNormal; (style == style_) ? typeface_ : CreateSkTypeface(style, &new_family);
if (gfx::Font::BOLD & style)
skstyle |= SkTypeface::kBold;
if (gfx::Font::ITALIC & style)
skstyle |= SkTypeface::kItalic;
typeface = skia::AdoptRef(SkTypeface::CreateFromName(
font_family_.c_str(), static_cast<SkTypeface::Style>(skstyle)));
}
// If the size changed, get updated rendering settings. // If the size or family changed, get updated rendering settings.
FontRenderParams render_params = font_render_params_; FontRenderParams render_params = font_render_params_;
if (size_delta != 0) { if (size_delta != 0 || new_family != font_family_) {
const std::vector<std::string> family_list(1, font_family_); const std::vector<std::string> family_list(1, new_family);
render_params = GetCustomFontRenderParams( render_params = GetCustomFontRenderParams(
false, &family_list, &new_size, NULL, NULL); false, &family_list, &new_size, NULL, NULL);
} }
return Font(new PlatformFontPango(typeface, return Font(new PlatformFontPango(typeface,
font_family_, new_family,
new_size, new_size,
style, style,
render_params)); render_params));
...@@ -254,21 +273,8 @@ void PlatformFontPango::InitFromDetails( ...@@ -254,21 +273,8 @@ void PlatformFontPango::InitFromDetails(
const FontRenderParams& render_params) { const FontRenderParams& render_params) {
DCHECK_GT(font_size_pixels, 0); DCHECK_GT(font_size_pixels, 0);
typeface_ = typeface;
font_family_ = font_family; font_family_ = font_family;
if (!typeface_) { typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_);
typeface_ = skia::AdoptRef(
SkTypeface::CreateFromName(font_family.c_str(), SkTypeface::kNormal));
if (!typeface_) {
// A non-scalable font such as .pcf is specified. Fall back to a default
// scalable font.
typeface_ = skia::AdoptRef(SkTypeface::CreateFromName(
kFallbackFontFamilyName, SkTypeface::kNormal));
CHECK(typeface_) << "Could not find any font: " << font_family << ", "
<< kFallbackFontFamilyName;
font_family_ = kFallbackFontFamilyName;
}
}
font_size_pixels_ = font_size_pixels; font_size_pixels_ = font_size_pixels;
style_ = style; style_ = style;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment