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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/font_render_params.h"
#include <fontconfig/fontconfig.h>
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "base/command_line.h"
#include "base/containers/lru_cache.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_render_params_linux.h"
#include "ui/gfx/linux/fontconfig_util.h"
#include "ui/gfx/switches.h"
#if BUILDFLAG(IS_LINUX)
#include "ui/linux/linux_ui.h"
#endif
namespace gfx {
namespace {
int FontWeightToFCWeight(Font::Weight weight) {
const int weight_number = static_cast<int>(weight);
if (weight_number <= (static_cast<int>(Font::Weight::THIN) +
static_cast<int>(Font::Weight::EXTRA_LIGHT)) /
2)
return FC_WEIGHT_THIN;
else if (weight_number <= (static_cast<int>(Font::Weight::EXTRA_LIGHT) +
static_cast<int>(Font::Weight::LIGHT)) /
2)
return FC_WEIGHT_ULTRALIGHT;
else if (weight_number <= (static_cast<int>(Font::Weight::LIGHT) +
static_cast<int>(Font::Weight::NORMAL)) /
2)
return FC_WEIGHT_LIGHT;
else if (weight_number <= (static_cast<int>(Font::Weight::NORMAL) +
static_cast<int>(Font::Weight::MEDIUM)) /
2)
return FC_WEIGHT_NORMAL;
else if (weight_number <= (static_cast<int>(Font::Weight::MEDIUM) +
static_cast<int>(Font::Weight::SEMIBOLD)) /
2)
return FC_WEIGHT_MEDIUM;
else if (weight_number <= (static_cast<int>(Font::Weight::SEMIBOLD) +
static_cast<int>(Font::Weight::BOLD)) /
2)
return FC_WEIGHT_DEMIBOLD;
else if (weight_number <= (static_cast<int>(Font::Weight::BOLD) +
static_cast<int>(Font::Weight::EXTRA_BOLD)) /
2)
return FC_WEIGHT_BOLD;
else if (weight_number <= (static_cast<int>(Font::Weight::EXTRA_BOLD) +
static_cast<int>(Font::Weight::BLACK)) /
2)
return FC_WEIGHT_ULTRABOLD;
else
return FC_WEIGHT_BLACK;
}
// A device scale factor used to determine if subpixel positioning
// should be used.
float device_scale_factor_ = 1.0f;
// If true, disables the subpixel rendering even if it is enabled in config.
bool force_disable_subpixel_font_rendering = false;
// Number of recent GetFontRenderParams() results to cache.
constexpr size_t kCacheSize = 256;
// Cached result from a call to GetFontRenderParams().
struct QueryResult {
QueryResult(const FontRenderParams& params, const std::string& family)
: params(params), family(family) {}
~QueryResult() {}
FontRenderParams params;
std::string family;
};
// Keyed by hashes of FontRenderParamQuery structs from
// HashFontRenderParamsQuery().
typedef base::HashingLRUCache<std::string, QueryResult> Cache;
// A cache and the lock that must be held while accessing it.
// GetFontRenderParams() is called by both the UI thread and the sandbox IPC
// thread.
struct SynchronizedCache {
SynchronizedCache() : cache(kCacheSize) {}
base::Lock lock;
Cache cache;
};
base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache =
LAZY_INSTANCE_INITIALIZER;
// Serialize |query| into a string value suitable for use as a cache key.
std::string GetFontRenderParamsQueryKey(const FontRenderParamsQuery& query) {
return base::StringPrintf(
"%d|%d|%d|%d|%s|%f", query.pixel_size, query.point_size, query.style,
static_cast<int>(query.weight),
base::JoinString(query.families, ",").c_str(), query.device_scale_factor);
}
} // namespace
bool QueryFontconfig(const FontRenderParamsQuery& query,
FontRenderParams* params_out,
std::string* family_out) {
TRACE_EVENT0("fonts", "gfx::QueryFontconfig");
ScopedFcPattern query_pattern(FcPatternCreate());
CHECK(query_pattern);
FcPatternAddBool(query_pattern.get(), FC_SCALABLE, FcTrue);
for (auto it = query.families.begin(); it != query.families.end(); ++it) {
FcPatternAddString(query_pattern.get(), FC_FAMILY,
reinterpret_cast<const FcChar8*>(it->c_str()));
}
if (query.pixel_size > 0)
FcPatternAddDouble(query_pattern.get(), FC_PIXEL_SIZE, query.pixel_size);
if (query.point_size > 0)
FcPatternAddInteger(query_pattern.get(), FC_SIZE, query.point_size);
if (query.style >= 0) {
FcPatternAddInteger(
query_pattern.get(), FC_SLANT,
(query.style & Font::ITALIC) ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
}
if (query.weight != Font::Weight::INVALID) {
FcPatternAddInteger(query_pattern.get(), FC_WEIGHT,
FontWeightToFCWeight(query.weight));
}
FcConfig* config = GetGlobalFontConfig();
FcConfigSubstitute(config, query_pattern.get(), FcMatchPattern);
FcDefaultSubstitute(query_pattern.get());
ScopedFcPattern result_pattern;
if (query.is_empty()) {
// If the query was empty, call FcConfigSubstituteWithPat() to get a
// non-family- or size-specific configuration so it can be used as the
// default.
result_pattern.reset(FcPatternDuplicate(query_pattern.get()));
if (!result_pattern)
return false;
FcPatternDel(result_pattern.get(), FC_FAMILY);
FcPatternDel(result_pattern.get(), FC_PIXEL_SIZE);
FcPatternDel(result_pattern.get(), FC_SIZE);
FcConfigSubstituteWithPat(config, result_pattern.get(), query_pattern.get(),
FcMatchFont);
} else {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("fonts"), "FcFontMatch");
FcResult result;
result_pattern.reset(FcFontMatch(config, query_pattern.get(), &result));
if (!result_pattern)
return false;
}
DCHECK(result_pattern);
if (family_out) {
FcChar8* family = NULL;
FcPatternGetString(result_pattern.get(), FC_FAMILY, 0, &family);
if (family)
family_out->assign(reinterpret_cast<const char*>(family));
}
if (params_out)
GetFontRenderParamsFromFcPattern(result_pattern.get(), params_out);
return true;
}
void SetForceDisableSubpixelFontRendering(bool disable) {
force_disable_subpixel_font_rendering = disable;
}
bool GetFontRenderParamsSubpixelRenderingEnabledForTesting() {
return force_disable_subpixel_font_rendering;
}
FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
std::string* family_out) {
TRACE_EVENT0("fonts", "gfx::GetFontRenderParams");
FontRenderParamsQuery actual_query(query);
if (actual_query.device_scale_factor == 0)
actual_query.device_scale_factor = device_scale_factor_;
std::string query_key = GetFontRenderParamsQueryKey(actual_query);
SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
{
// Try to find a cached result so Fontconfig doesn't need to be queried.
base::AutoLock lock(synchronized_cache->lock);
Cache::const_iterator it = synchronized_cache->cache.Get(query_key);
if (it != synchronized_cache->cache.end()) {
DVLOG(1) << "Returning cached params for " << query_key;
const QueryResult& result = it->second;
if (family_out)
*family_out = result.family;
return result.params;
}
}
DVLOG(1) << "Computing params for " << query_key;
if (family_out)
family_out->clear();
// Start with the delegate's settings, but let Fontconfig have the final say.
FontRenderParams params;
#if BUILDFLAG(IS_LINUX)
if (auto* linux_ui = ui::LinuxUi::instance()) {
params = linux_ui->GetDefaultFontRenderParams();
}
#endif
QueryFontconfig(actual_query, ¶ms, family_out);
if (force_disable_subpixel_font_rendering) {
params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
}
if (!params.antialiasing) {
// Cairo forces full hinting when antialiasing is disabled, since anything
// less than that looks awful; do the same here. Requesting subpixel
// rendering or positioning doesn't make sense either.
params.hinting = FontRenderParams::HINTING_FULL;
params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
params.subpixel_positioning = false;
} else if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableFontSubpixelPositioning)) {
#if BUILDFLAG(IS_CHROMEOS)
// We want to enable subpixel positioning for fractional dsf.
params.subpixel_positioning =
std::abs(std::round(actual_query.device_scale_factor) -
actual_query.device_scale_factor) >
std::numeric_limits<float>::epsilon();
#else
params.subpixel_positioning = actual_query.device_scale_factor > 1.0f;
#endif // BUILDFLAG(IS_CHROMEOS)
// To enable subpixel positioning, we need to disable hinting.
if (params.subpixel_positioning)
params.hinting = FontRenderParams::HINTING_NONE;
}
// Use the first family from the list if Fontconfig didn't suggest a family.
if (family_out && family_out->empty() && !actual_query.families.empty())
*family_out = actual_query.families[0];
{
// Store the result. It's fine if this overwrites a result that was cached
// by a different thread in the meantime; the values should be identical.
base::AutoLock lock(synchronized_cache->lock);
synchronized_cache->cache.Put(
query_key,
QueryResult(params, family_out ? *family_out : std::string()));
}
return params;
}
void ClearFontRenderParamsCacheForTest() {
SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
base::AutoLock lock(synchronized_cache->lock);
synchronized_cache->cache.Clear();
}
float GetFontRenderParamsDeviceScaleFactor() {
return device_scale_factor_;
}
void SetFontRenderParamsDeviceScaleFactor(float device_scale_factor) {
device_scale_factor_ = device_scale_factor;
}
} // namespace gfx
|