1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/platform_font_pango.h"
#include <pango/pango.h>
#include <algorithm>
#include <string>
#include "base/logging.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.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"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/linux_font_delegate.h"
#include "ui/gfx/pango_util.h"
#include "ui/gfx/text_utils.h"
namespace {
// The font family name which is used when a user's application font for
// GNOME/KDE is a non-scalable one. The name should be listed in the
// IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp.
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 gfx {
// static
Font* PlatformFontPango::default_font_ = NULL;
#if defined(OS_CHROMEOS)
// static
std::string* PlatformFontPango::default_font_description_ = NULL;
#endif
////////////////////////////////////////////////////////////////////////////////
// PlatformFontPango, public:
PlatformFontPango::PlatformFontPango() {
if (!default_font_) {
scoped_ptr<ScopedPangoFontDescription> description;
#if defined(OS_CHROMEOS)
CHECK(default_font_description_);
description.reset(
new ScopedPangoFontDescription(*default_font_description_));
#else
const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
if (delegate)
description = delegate->GetDefaultPangoFontDescription();
#endif
if (!description || !description->get())
description.reset(new ScopedPangoFontDescription("sans 10"));
default_font_ = new Font(description->get());
}
InitFromPlatformFont(
static_cast<PlatformFontPango*>(default_font_->platform_font()));
}
PlatformFontPango::PlatformFontPango(NativeFont native_font) {
FontRenderParamsQuery query(false);
base::SplitString(pango_font_description_get_family(native_font), ',',
&query.families);
const int pango_size =
pango_font_description_get_size(native_font) / PANGO_SCALE;
if (pango_font_description_get_size_is_absolute(native_font))
query.pixel_size = pango_size;
else
query.point_size = pango_size;
query.style = gfx::Font::NORMAL;
// TODO(davemoore): Support weights other than bold?
if (pango_font_description_get_weight(native_font) == PANGO_WEIGHT_BOLD)
query.style |= gfx::Font::BOLD;
// TODO(davemoore): What about PANGO_STYLE_OBLIQUE?
if (pango_font_description_get_style(native_font) == PANGO_STYLE_ITALIC)
query.style |= gfx::Font::ITALIC;
std::string font_family;
const FontRenderParams params = gfx::GetFontRenderParams(query, &font_family);
InitFromDetails(skia::RefPtr<SkTypeface>(), font_family,
gfx::GetPangoFontSizeInPixels(native_font),
query.style, params);
}
PlatformFontPango::PlatformFontPango(const std::string& font_name,
int font_size_pixels) {
FontRenderParamsQuery query(false);
query.families.push_back(font_name);
query.pixel_size = font_size_pixels;
query.style = gfx::Font::NORMAL;
InitFromDetails(skia::RefPtr<SkTypeface>(), font_name, font_size_pixels,
query.style, gfx::GetFontRenderParams(query, NULL));
}
////////////////////////////////////////////////////////////////////////////////
// PlatformFontPango, PlatformFont implementation:
// static
void PlatformFontPango::ReloadDefaultFont() {
delete default_font_;
default_font_ = NULL;
}
#if defined(OS_CHROMEOS)
// static
void PlatformFontPango::SetDefaultFontDescription(
const std::string& font_description) {
delete default_font_description_;
default_font_description_ = new std::string(font_description);
}
#endif
Font PlatformFontPango::DeriveFont(int size_delta, int style) 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);
FontRenderParamsQuery query(false);
query.families.push_back(new_family);
query.pixel_size = new_size;
query.style = style;
return Font(new PlatformFontPango(typeface, new_family, new_size, style,
gfx::GetFontRenderParams(query, NULL)));
}
int PlatformFontPango::GetHeight() const {
return height_pixels_;
}
int PlatformFontPango::GetBaseline() const {
return ascent_pixels_;
}
int PlatformFontPango::GetCapHeight() const {
return cap_height_pixels_;
}
int PlatformFontPango::GetExpectedTextWidth(int length) const {
return round(static_cast<float>(length) * average_width_pixels_);
}
int PlatformFontPango::GetStyle() const {
return style_;
}
std::string PlatformFontPango::GetFontName() const {
return font_family_;
}
std::string PlatformFontPango::GetActualFontNameForTesting() const {
SkString family_name;
typeface_->getFamilyName(&family_name);
return family_name.c_str();
}
int PlatformFontPango::GetFontSize() const {
return font_size_pixels_;
}
const FontRenderParams& PlatformFontPango::GetFontRenderParams() {
#if defined(OS_CHROMEOS)
float current_scale_factor = gfx::GetFontRenderParamsDeviceScaleFactor();
if (current_scale_factor != device_scale_factor_) {
FontRenderParamsQuery query(false);
query.families.push_back(font_family_);
query.pixel_size = font_size_pixels_;
query.style = style_;
query.device_scale_factor = current_scale_factor;
font_render_params_ = gfx::GetFontRenderParams(query, nullptr);
device_scale_factor_ = current_scale_factor;
}
#endif
return font_render_params_;
}
NativeFont PlatformFontPango::GetNativeFont() const {
PangoFontDescription* pfd = pango_font_description_new();
pango_font_description_set_family(pfd, GetFontName().c_str());
// Set the absolute size to avoid overflowing UI elements.
// pango_font_description_set_absolute_size() takes a size in Pango units.
// There are PANGO_SCALE Pango units in one device unit. Screen output
// devices use pixels as their device units.
pango_font_description_set_absolute_size(
pfd, font_size_pixels_ * PANGO_SCALE);
switch (GetStyle()) {
case gfx::Font::NORMAL:
// Nothing to do, should already be PANGO_STYLE_NORMAL.
break;
case gfx::Font::BOLD:
pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD);
break;
case gfx::Font::ITALIC:
pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC);
break;
case gfx::Font::UNDERLINE:
// TODO(deanm): How to do underline? Where do we use it? Probably have
// to paint it ourselves, see pango_font_metrics_get_underline_position.
break;
}
return pfd;
}
////////////////////////////////////////////////////////////////////////////////
// PlatformFontPango, private:
PlatformFontPango::PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
const std::string& name,
int size_pixels,
int style,
const FontRenderParams& render_params) {
InitFromDetails(typeface, name, size_pixels, style, render_params);
}
PlatformFontPango::~PlatformFontPango() {}
void PlatformFontPango::InitFromDetails(
const skia::RefPtr<SkTypeface>& typeface,
const std::string& font_family,
int font_size_pixels,
int style,
const FontRenderParams& render_params) {
DCHECK_GT(font_size_pixels, 0);
font_family_ = font_family;
typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_);
font_size_pixels_ = font_size_pixels;
style_ = style;
#if defined(OS_CHROMEOS)
device_scale_factor_ = gfx::GetFontRenderParamsDeviceScaleFactor();
#endif
font_render_params_ = render_params;
SkPaint paint;
paint.setAntiAlias(false);
paint.setSubpixelText(false);
paint.setTextSize(font_size_pixels_);
paint.setTypeface(typeface_.get());
paint.setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold());
paint.setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ?
-SK_Scalar1/4 : 0);
SkPaint::FontMetrics metrics;
paint.getFontMetrics(&metrics);
ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent);
height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent);
cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight);
average_width_pixels_ = SkScalarToDouble(metrics.fAvgCharWidth);
}
void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) {
typeface_ = other->typeface_;
font_family_ = other->font_family_;
font_size_pixels_ = other->font_size_pixels_;
style_ = other->style_;
#if defined(OS_CHROMEOS)
device_scale_factor_ = other->device_scale_factor_;
#endif
font_render_params_ = other->font_render_params_;
ascent_pixels_ = other->ascent_pixels_;
height_pixels_ = other->height_pixels_;
cap_height_pixels_ = other->cap_height_pixels_;
average_width_pixels_ = other->average_width_pixels_;
}
////////////////////////////////////////////////////////////////////////////////
// PlatformFont, public:
// static
PlatformFont* PlatformFont::CreateDefault() {
return new PlatformFontPango;
}
// static
PlatformFont* PlatformFont::CreateFromNativeFont(NativeFont native_font) {
return new PlatformFontPango(native_font);
}
// static
PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
int font_size) {
return new PlatformFontPango(font_name, font_size);
}
} // namespace gfx
|