mac: Add the flag "-gline-tables-only" to reduce dSYM size. (attempt #2)
[chromium-blink-merge.git] / ui / gfx / font_render_params_linux.cc
blobf09f7033bbe200f94badbf1f56492465d37e7f5a
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/font_render_params.h"
7 #include <fontconfig/fontconfig.h>
9 #include "base/command_line.h"
10 #include "base/containers/mru_cache.h"
11 #include "base/hash.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/synchronization/lock.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/font.h"
21 #include "ui/gfx/linux_font_delegate.h"
22 #include "ui/gfx/screen.h"
23 #include "ui/gfx/switches.h"
25 namespace gfx {
27 namespace {
29 #if defined(OS_CHROMEOS)
30 // A device scale factor for an internal display (if any)
31 // that is used to determine if subpixel positioning should be used.
32 float device_scale_factor_for_internal_display = 1.0f;
33 #endif
35 // Number of recent GetFontRenderParams() results to cache.
36 const size_t kCacheSize = 256;
38 // Cached result from a call to GetFontRenderParams().
39 struct QueryResult {
40 QueryResult(const FontRenderParams& params, const std::string& family)
41 : params(params),
42 family(family) {
44 ~QueryResult() {}
46 FontRenderParams params;
47 std::string family;
50 // Keyed by hashes of FontRenderParamQuery structs from
51 // HashFontRenderParamsQuery().
52 typedef base::MRUCache<uint32, QueryResult> Cache;
54 // A cache and the lock that must be held while accessing it.
55 // GetFontRenderParams() is called by both the UI thread and the sandbox IPC
56 // thread.
57 struct SynchronizedCache {
58 SynchronizedCache() : cache(kCacheSize) {}
60 base::Lock lock;
61 Cache cache;
64 base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache =
65 LAZY_INSTANCE_INITIALIZER;
67 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting.
68 FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) {
69 switch (hint_style) {
70 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT;
71 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM;
72 case FC_HINT_FULL: return FontRenderParams::HINTING_FULL;
73 default: return FontRenderParams::HINTING_NONE;
77 // Converts Fontconfig FC_RGBA to FontRenderParams::SubpixelRendering.
78 FontRenderParams::SubpixelRendering ConvertFontconfigRgba(int rgba) {
79 switch (rgba) {
80 case FC_RGBA_RGB: return FontRenderParams::SUBPIXEL_RENDERING_RGB;
81 case FC_RGBA_BGR: return FontRenderParams::SUBPIXEL_RENDERING_BGR;
82 case FC_RGBA_VRGB: return FontRenderParams::SUBPIXEL_RENDERING_VRGB;
83 case FC_RGBA_VBGR: return FontRenderParams::SUBPIXEL_RENDERING_VBGR;
84 default: return FontRenderParams::SUBPIXEL_RENDERING_NONE;
88 // Queries Fontconfig for rendering settings and updates |params_out| and
89 // |family_out| (if non-NULL). Returns false on failure.
90 bool QueryFontconfig(const FontRenderParamsQuery& query,
91 FontRenderParams* params_out,
92 std::string* family_out) {
93 struct FcPatternDeleter {
94 void operator()(FcPattern* ptr) const { FcPatternDestroy(ptr); }
96 typedef scoped_ptr<FcPattern, FcPatternDeleter> ScopedFcPattern;
98 ScopedFcPattern query_pattern(FcPatternCreate());
99 CHECK(query_pattern);
101 FcPatternAddBool(query_pattern.get(), FC_SCALABLE, FcTrue);
103 for (std::vector<std::string>::const_iterator it = query.families.begin();
104 it != query.families.end(); ++it) {
105 FcPatternAddString(query_pattern.get(),
106 FC_FAMILY, reinterpret_cast<const FcChar8*>(it->c_str()));
108 if (query.pixel_size > 0)
109 FcPatternAddDouble(query_pattern.get(), FC_PIXEL_SIZE, query.pixel_size);
110 if (query.point_size > 0)
111 FcPatternAddInteger(query_pattern.get(), FC_SIZE, query.point_size);
112 if (query.style >= 0) {
113 FcPatternAddInteger(query_pattern.get(), FC_SLANT,
114 (query.style & Font::ITALIC) ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
115 FcPatternAddInteger(query_pattern.get(), FC_WEIGHT,
116 (query.style & Font::BOLD) ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL);
119 FcConfigSubstitute(NULL, query_pattern.get(), FcMatchPattern);
120 FcDefaultSubstitute(query_pattern.get());
122 ScopedFcPattern result_pattern;
123 if (query.is_empty()) {
124 // If the query was empty, call FcConfigSubstituteWithPat() to get a
125 // non-family- or size-specific configuration so it can be used as the
126 // default.
127 result_pattern.reset(FcPatternDuplicate(query_pattern.get()));
128 if (!result_pattern)
129 return false;
130 FcPatternDel(result_pattern.get(), FC_FAMILY);
131 FcPatternDel(result_pattern.get(), FC_PIXEL_SIZE);
132 FcPatternDel(result_pattern.get(), FC_SIZE);
133 FcConfigSubstituteWithPat(NULL, result_pattern.get(), query_pattern.get(),
134 FcMatchFont);
135 } else {
136 FcResult result;
137 result_pattern.reset(FcFontMatch(NULL, query_pattern.get(), &result));
138 if (!result_pattern)
139 return false;
141 DCHECK(result_pattern);
143 if (family_out) {
144 FcChar8* family = NULL;
145 FcPatternGetString(result_pattern.get(), FC_FAMILY, 0, &family);
146 if (family)
147 family_out->assign(reinterpret_cast<const char*>(family));
150 if (params_out) {
151 FcBool fc_antialias = 0;
152 if (FcPatternGetBool(result_pattern.get(), FC_ANTIALIAS, 0,
153 &fc_antialias) == FcResultMatch) {
154 params_out->antialiasing = fc_antialias;
157 FcBool fc_autohint = 0;
158 if (FcPatternGetBool(result_pattern.get(), FC_AUTOHINT, 0, &fc_autohint) ==
159 FcResultMatch) {
160 params_out->autohinter = fc_autohint;
163 FcBool fc_bitmap = 0;
164 if (FcPatternGetBool(result_pattern.get(), FC_EMBEDDED_BITMAP, 0,
165 &fc_bitmap) ==
166 FcResultMatch) {
167 params_out->use_bitmaps = fc_bitmap;
170 FcBool fc_hinting = 0;
171 if (FcPatternGetBool(result_pattern.get(), FC_HINTING, 0, &fc_hinting) ==
172 FcResultMatch) {
173 int fc_hint_style = FC_HINT_NONE;
174 if (fc_hinting) {
175 FcPatternGetInteger(
176 result_pattern.get(), FC_HINT_STYLE, 0, &fc_hint_style);
178 params_out->hinting = ConvertFontconfigHintStyle(fc_hint_style);
181 int fc_rgba = FC_RGBA_NONE;
182 if (FcPatternGetInteger(result_pattern.get(), FC_RGBA, 0, &fc_rgba) ==
183 FcResultMatch)
184 params_out->subpixel_rendering = ConvertFontconfigRgba(fc_rgba);
187 return true;
190 // Serialize |query| into a string and hash it to a value suitable for use as a
191 // cache key.
192 uint32 HashFontRenderParamsQuery(const FontRenderParamsQuery& query) {
193 return base::Hash(base::StringPrintf(
194 "%d|%d|%d|%s|%f", query.pixel_size, query.point_size, query.style,
195 JoinString(query.families, ',').c_str(), query.device_scale_factor));
198 } // namespace
200 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
201 std::string* family_out) {
202 FontRenderParamsQuery actual_query(query);
203 if (actual_query.device_scale_factor == 0) {
204 #if defined(OS_CHROMEOS)
205 actual_query.device_scale_factor = device_scale_factor_for_internal_display;
206 #else
207 // Linux does not support per-display DPI, so we use a slightly simpler
208 // code path than on Chrome OS to figure out the device scale factor.
209 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
210 if (screen) {
211 gfx::Display display = screen->GetPrimaryDisplay();
212 actual_query.device_scale_factor = display.device_scale_factor();
214 #endif
216 const uint32 hash = HashFontRenderParamsQuery(actual_query);
217 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
220 // Try to find a cached result so Fontconfig doesn't need to be queried.
221 base::AutoLock lock(synchronized_cache->lock);
222 Cache::const_iterator it = synchronized_cache->cache.Get(hash);
223 if (it != synchronized_cache->cache.end()) {
224 DVLOG(1) << "Returning cached params for " << hash;
225 const QueryResult& result = it->second;
226 if (family_out)
227 *family_out = result.family;
228 return result.params;
232 DVLOG(1) << "Computing params for " << hash;
233 if (family_out)
234 family_out->clear();
236 // Start with the delegate's settings, but let Fontconfig have the final say.
237 FontRenderParams params;
238 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance();
239 if (delegate)
240 params = delegate->GetDefaultFontRenderParams();
241 QueryFontconfig(actual_query, &params, family_out);
242 if (!params.antialiasing) {
243 // Cairo forces full hinting when antialiasing is disabled, since anything
244 // less than that looks awful; do the same here. Requesting subpixel
245 // rendering or positioning doesn't make sense either.
246 params.hinting = FontRenderParams::HINTING_FULL;
247 params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
248 params.subpixel_positioning = false;
249 } else {
250 params.subpixel_positioning = actual_query.device_scale_factor > 1.0f;
252 // To enable subpixel positioning, we need to disable hinting.
253 if (params.subpixel_positioning)
254 params.hinting = FontRenderParams::HINTING_NONE;
257 // Use the first family from the list if Fontconfig didn't suggest a family.
258 if (family_out && family_out->empty() && !actual_query.families.empty())
259 *family_out = actual_query.families[0];
262 // Store the result. It's fine if this overwrites a result that was cached
263 // by a different thread in the meantime; the values should be identical.
264 base::AutoLock lock(synchronized_cache->lock);
265 synchronized_cache->cache.Put(hash,
266 QueryResult(params, family_out ? *family_out : std::string()));
269 return params;
272 void ClearFontRenderParamsCacheForTest() {
273 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
274 base::AutoLock lock(synchronized_cache->lock);
275 synchronized_cache->cache.Clear();
278 #if defined(OS_CHROMEOS)
279 float GetFontRenderParamsDeviceScaleFactor() {
280 return device_scale_factor_for_internal_display;
283 void SetFontRenderParamsDeviceScaleFactor(float device_scale_factor) {
284 device_scale_factor_for_internal_display = device_scale_factor;
286 #endif
288 } // namespace gfx