Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: ui/gfx/font_render_params_linux.cc

Issue 789583002: Updates subpixel positioning and hinting when DSF is changed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: linux fix Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/font_render_params.cc ('k') | ui/gfx/platform_font.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/font_render_params.h" 5 #include "ui/gfx/font_render_params.h"
6 6
7 #include <fontconfig/fontconfig.h> 7 #include <fontconfig/fontconfig.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/containers/mru_cache.h" 10 #include "base/containers/mru_cache.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 struct SynchronizedCache { 54 struct SynchronizedCache {
55 SynchronizedCache() : cache(kCacheSize) {} 55 SynchronizedCache() : cache(kCacheSize) {}
56 56
57 base::Lock lock; 57 base::Lock lock;
58 Cache cache; 58 Cache cache;
59 }; 59 };
60 60
61 base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache = 61 base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache =
62 LAZY_INSTANCE_INITIALIZER; 62 LAZY_INSTANCE_INITIALIZER;
63 63
64 bool IsBrowserTextSubpixelPositioningEnabled() { 64 bool IsBrowserTextSubpixelPositioningEnabled(
65 const FontRenderParamsQuery& query) {
65 #if defined(OS_CHROMEOS) 66 #if defined(OS_CHROMEOS)
66 return device_scale_factor_for_internal_display > 1.0f; 67 return query.device_scale_factor > 1.0f;
67 #else 68 #else
68 return false; 69 return false;
69 #endif 70 #endif
70 } 71 }
71 72
72 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting. 73 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting.
73 FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) { 74 FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) {
74 switch (hint_style) { 75 switch (hint_style) {
75 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT; 76 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT;
76 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM; 77 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 params_out->subpixel_rendering = ConvertFontconfigRgba(fc_rgba); 173 params_out->subpixel_rendering = ConvertFontconfigRgba(fc_rgba);
173 } 174 }
174 175
175 FcPatternDestroy(result_pattern); 176 FcPatternDestroy(result_pattern);
176 return true; 177 return true;
177 } 178 }
178 179
179 // Serialize |query| into a string and hash it to a value suitable for use as a 180 // Serialize |query| into a string and hash it to a value suitable for use as a
180 // cache key. 181 // cache key.
181 uint32 HashFontRenderParamsQuery(const FontRenderParamsQuery& query) { 182 uint32 HashFontRenderParamsQuery(const FontRenderParamsQuery& query) {
182 return base::Hash(base::StringPrintf("%d|%d|%d|%d|%s", 183 return base::Hash(base::StringPrintf(
183 query.for_web_contents, query.pixel_size, query.point_size, query.style, 184 "%d|%d|%d|%d|%s|%f", query.for_web_contents, query.pixel_size,
184 JoinString(query.families, ',').c_str())); 185 query.point_size, query.style, JoinString(query.families, ',').c_str(),
186 query.device_scale_factor));
185 } 187 }
186 188
187 } // namespace 189 } // namespace
188 190
189 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, 191 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
190 std::string* family_out) { 192 std::string* family_out) {
191 const uint32 hash = HashFontRenderParamsQuery(query); 193 FontRenderParamsQuery actual_query(query);
194 #if defined(OS_CHROMEOS)
195 if (actual_query.device_scale_factor == 0)
196 actual_query.device_scale_factor = device_scale_factor_for_internal_display;
197 #endif
198 const uint32 hash = HashFontRenderParamsQuery(actual_query);
192 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); 199 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
193 200
194 { 201 {
195 // Try to find a cached result so Fontconfig doesn't need to be queried. 202 // Try to find a cached result so Fontconfig doesn't need to be queried.
196 base::AutoLock lock(synchronized_cache->lock); 203 base::AutoLock lock(synchronized_cache->lock);
197 Cache::const_iterator it = synchronized_cache->cache.Get(hash); 204 Cache::const_iterator it = synchronized_cache->cache.Get(hash);
198 if (it != synchronized_cache->cache.end()) { 205 if (it != synchronized_cache->cache.end()) {
199 DVLOG(1) << "Returning cached params for " << hash; 206 DVLOG(1) << "Returning cached params for " << hash;
200 const QueryResult& result = it->second; 207 const QueryResult& result = it->second;
201 if (family_out) 208 if (family_out)
202 *family_out = result.family; 209 *family_out = result.family;
203 return result.params; 210 return result.params;
204 } 211 }
205 } 212 }
206 213
207 DVLOG(1) << "Computing params for " << hash; 214 DVLOG(1) << "Computing params for " << hash;
208 if (family_out) 215 if (family_out)
209 family_out->clear(); 216 family_out->clear();
210 217
211 // Start with the delegate's settings, but let Fontconfig have the final say. 218 // Start with the delegate's settings, but let Fontconfig have the final say.
212 FontRenderParams params; 219 FontRenderParams params;
213 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); 220 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance();
214 if (delegate) 221 if (delegate)
215 params = delegate->GetDefaultFontRenderParams(); 222 params = delegate->GetDefaultFontRenderParams();
216 QueryFontconfig(query, &params, family_out); 223 QueryFontconfig(actual_query, &params, family_out);
217 if (!params.antialiasing) { 224 if (!params.antialiasing) {
218 // Cairo forces full hinting when antialiasing is disabled, since anything 225 // Cairo forces full hinting when antialiasing is disabled, since anything
219 // less than that looks awful; do the same here. Requesting subpixel 226 // less than that looks awful; do the same here. Requesting subpixel
220 // rendering or positioning doesn't make sense either. 227 // rendering or positioning doesn't make sense either.
221 params.hinting = FontRenderParams::HINTING_FULL; 228 params.hinting = FontRenderParams::HINTING_FULL;
222 params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE; 229 params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
223 params.subpixel_positioning = false; 230 params.subpixel_positioning = false;
224 } else { 231 } else {
225 // Fontconfig doesn't support configuring subpixel positioning; check a 232 // Fontconfig doesn't support configuring subpixel positioning; check a
226 // flag. 233 // flag.
227 params.subpixel_positioning = 234 params.subpixel_positioning =
228 query.for_web_contents ? 235 actual_query.for_web_contents
229 CommandLine::ForCurrentProcess()->HasSwitch( 236 ? CommandLine::ForCurrentProcess()->HasSwitch(
230 switches::kEnableWebkitTextSubpixelPositioning) : 237 switches::kEnableWebkitTextSubpixelPositioning)
231 IsBrowserTextSubpixelPositioningEnabled(); 238 : IsBrowserTextSubpixelPositioningEnabled(actual_query);
232 239
233 // To enable subpixel positioning, we need to disable hinting. 240 // To enable subpixel positioning, we need to disable hinting.
234 if (params.subpixel_positioning) 241 if (params.subpixel_positioning)
235 params.hinting = FontRenderParams::HINTING_NONE; 242 params.hinting = FontRenderParams::HINTING_NONE;
236 } 243 }
237 244
238 // Use the first family from the list if Fontconfig didn't suggest a family. 245 // Use the first family from the list if Fontconfig didn't suggest a family.
239 if (family_out && family_out->empty() && !query.families.empty()) 246 if (family_out && family_out->empty() && !actual_query.families.empty())
240 *family_out = query.families[0]; 247 *family_out = actual_query.families[0];
241 248
242 { 249 {
243 // Store the result. It's fine if this overwrites a result that was cached 250 // Store the result. It's fine if this overwrites a result that was cached
244 // by a different thread in the meantime; the values should be identical. 251 // by a different thread in the meantime; the values should be identical.
245 base::AutoLock lock(synchronized_cache->lock); 252 base::AutoLock lock(synchronized_cache->lock);
246 synchronized_cache->cache.Put(hash, 253 synchronized_cache->cache.Put(hash,
247 QueryResult(params, family_out ? *family_out : std::string())); 254 QueryResult(params, family_out ? *family_out : std::string()));
248 } 255 }
249 256
250 return params; 257 return params;
251 } 258 }
252 259
253 void ClearFontRenderParamsCacheForTest() { 260 void ClearFontRenderParamsCacheForTest() {
254 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); 261 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer();
255 base::AutoLock lock(synchronized_cache->lock); 262 base::AutoLock lock(synchronized_cache->lock);
256 synchronized_cache->cache.Clear(); 263 synchronized_cache->cache.Clear();
257 } 264 }
258 265
259 #if defined(OS_CHROMEOS) 266 #if defined(OS_CHROMEOS)
267 float GetFontRenderParamsDeviceScaleFactor() {
268 return device_scale_factor_for_internal_display;
269 }
270
260 void SetFontRenderParamsDeviceScaleFactor(float device_scale_factor) { 271 void SetFontRenderParamsDeviceScaleFactor(float device_scale_factor) {
261 device_scale_factor_for_internal_display = device_scale_factor; 272 device_scale_factor_for_internal_display = device_scale_factor;
262 } 273 }
263 #endif 274 #endif
264 275
265 } // namespace gfx 276 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/font_render_params.cc ('k') | ui/gfx/platform_font.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698