| OLD | NEW |
| 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" |
| 11 #include "base/hash.h" | 11 #include "base/hash.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "ui/gfx/display.h" |
| 19 #include "ui/gfx/font.h" | 20 #include "ui/gfx/font.h" |
| 20 #include "ui/gfx/linux_font_delegate.h" | 21 #include "ui/gfx/linux_font_delegate.h" |
| 22 #include "ui/gfx/screen.h" |
| 21 #include "ui/gfx/switches.h" | 23 #include "ui/gfx/switches.h" |
| 22 | 24 |
| 23 namespace gfx { | 25 namespace gfx { |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 28 // A device scale factor for an internal display (if any) | 30 // A device scale factor for an internal display (if any) |
| 29 // that is used to determine if subpixel positioning should be used. | 31 // that is used to determine if subpixel positioning should be used. |
| 30 float device_scale_factor_for_internal_display = 1.0f; | 32 float device_scale_factor_for_internal_display = 1.0f; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 55 struct SynchronizedCache { | 57 struct SynchronizedCache { |
| 56 SynchronizedCache() : cache(kCacheSize) {} | 58 SynchronizedCache() : cache(kCacheSize) {} |
| 57 | 59 |
| 58 base::Lock lock; | 60 base::Lock lock; |
| 59 Cache cache; | 61 Cache cache; |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache = | 64 base::LazyInstance<SynchronizedCache>::Leaky g_synchronized_cache = |
| 63 LAZY_INSTANCE_INITIALIZER; | 65 LAZY_INSTANCE_INITIALIZER; |
| 64 | 66 |
| 65 bool IsBrowserTextSubpixelPositioningEnabled( | |
| 66 const FontRenderParamsQuery& query) { | |
| 67 #if defined(OS_CHROMEOS) | |
| 68 return query.device_scale_factor > 1.0f; | |
| 69 #else | |
| 70 return false; | |
| 71 #endif | |
| 72 } | |
| 73 | |
| 74 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting. | 67 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting. |
| 75 FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) { | 68 FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) { |
| 76 switch (hint_style) { | 69 switch (hint_style) { |
| 77 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT; | 70 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT; |
| 78 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM; | 71 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM; |
| 79 case FC_HINT_FULL: return FontRenderParams::HINTING_FULL; | 72 case FC_HINT_FULL: return FontRenderParams::HINTING_FULL; |
| 80 default: return FontRenderParams::HINTING_NONE; | 73 default: return FontRenderParams::HINTING_NONE; |
| 81 } | 74 } |
| 82 } | 75 } |
| 83 | 76 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 "%d|%d|%d|%d|%s|%f", query.for_web_contents, query.pixel_size, | 194 "%d|%d|%d|%d|%s|%f", query.for_web_contents, query.pixel_size, |
| 202 query.point_size, query.style, JoinString(query.families, ',').c_str(), | 195 query.point_size, query.style, JoinString(query.families, ',').c_str(), |
| 203 query.device_scale_factor)); | 196 query.device_scale_factor)); |
| 204 } | 197 } |
| 205 | 198 |
| 206 } // namespace | 199 } // namespace |
| 207 | 200 |
| 208 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, | 201 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, |
| 209 std::string* family_out) { | 202 std::string* family_out) { |
| 210 FontRenderParamsQuery actual_query(query); | 203 FontRenderParamsQuery actual_query(query); |
| 204 if (actual_query.device_scale_factor == 0) { |
| 211 #if defined(OS_CHROMEOS) | 205 #if defined(OS_CHROMEOS) |
| 212 if (actual_query.device_scale_factor == 0) | |
| 213 actual_query.device_scale_factor = device_scale_factor_for_internal_display; | 206 actual_query.device_scale_factor = device_scale_factor_for_internal_display; |
| 207 #else |
| 208 // Linux does not support per-display DPI, so we use a slightly simpler |
| 209 // code path than on Chrome OS to figure out the device scale factor. |
| 210 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE); |
| 211 if (screen) { |
| 212 gfx::Display display = screen->GetPrimaryDisplay(); |
| 213 actual_query.device_scale_factor = display.device_scale_factor(); |
| 214 } |
| 214 #endif | 215 #endif |
| 216 } |
| 215 const uint32 hash = HashFontRenderParamsQuery(actual_query); | 217 const uint32 hash = HashFontRenderParamsQuery(actual_query); |
| 216 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); | 218 SynchronizedCache* synchronized_cache = g_synchronized_cache.Pointer(); |
| 217 | 219 |
| 218 { | 220 { |
| 219 // Try to find a cached result so Fontconfig doesn't need to be queried. | 221 // Try to find a cached result so Fontconfig doesn't need to be queried. |
| 220 base::AutoLock lock(synchronized_cache->lock); | 222 base::AutoLock lock(synchronized_cache->lock); |
| 221 Cache::const_iterator it = synchronized_cache->cache.Get(hash); | 223 Cache::const_iterator it = synchronized_cache->cache.Get(hash); |
| 222 if (it != synchronized_cache->cache.end()) { | 224 if (it != synchronized_cache->cache.end()) { |
| 223 DVLOG(1) << "Returning cached params for " << hash; | 225 DVLOG(1) << "Returning cached params for " << hash; |
| 224 const QueryResult& result = it->second; | 226 const QueryResult& result = it->second; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 245 params.hinting = FontRenderParams::HINTING_FULL; | 247 params.hinting = FontRenderParams::HINTING_FULL; |
| 246 params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE; | 248 params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE; |
| 247 params.subpixel_positioning = false; | 249 params.subpixel_positioning = false; |
| 248 } else { | 250 } else { |
| 249 // Fontconfig doesn't support configuring subpixel positioning; check a | 251 // Fontconfig doesn't support configuring subpixel positioning; check a |
| 250 // flag. | 252 // flag. |
| 251 params.subpixel_positioning = | 253 params.subpixel_positioning = |
| 252 actual_query.for_web_contents | 254 actual_query.for_web_contents |
| 253 ? base::CommandLine::ForCurrentProcess()->HasSwitch( | 255 ? base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 254 switches::kEnableWebkitTextSubpixelPositioning) | 256 switches::kEnableWebkitTextSubpixelPositioning) |
| 255 : IsBrowserTextSubpixelPositioningEnabled(actual_query); | 257 : actual_query.device_scale_factor > 1.0f; |
| 256 | 258 |
| 257 // To enable subpixel positioning, we need to disable hinting. | 259 // To enable subpixel positioning, we need to disable hinting. |
| 258 if (params.subpixel_positioning) | 260 if (params.subpixel_positioning) |
| 259 params.hinting = FontRenderParams::HINTING_NONE; | 261 params.hinting = FontRenderParams::HINTING_NONE; |
| 260 } | 262 } |
| 261 | 263 |
| 262 // Use the first family from the list if Fontconfig didn't suggest a family. | 264 // Use the first family from the list if Fontconfig didn't suggest a family. |
| 263 if (family_out && family_out->empty() && !actual_query.families.empty()) | 265 if (family_out && family_out->empty() && !actual_query.families.empty()) |
| 264 *family_out = actual_query.families[0]; | 266 *family_out = actual_query.families[0]; |
| 265 | 267 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 284 float GetFontRenderParamsDeviceScaleFactor() { | 286 float GetFontRenderParamsDeviceScaleFactor() { |
| 285 return device_scale_factor_for_internal_display; | 287 return device_scale_factor_for_internal_display; |
| 286 } | 288 } |
| 287 | 289 |
| 288 void SetFontRenderParamsDeviceScaleFactor(float device_scale_factor) { | 290 void SetFontRenderParamsDeviceScaleFactor(float device_scale_factor) { |
| 289 device_scale_factor_for_internal_display = device_scale_factor; | 291 device_scale_factor_for_internal_display = device_scale_factor; |
| 290 } | 292 } |
| 291 #endif | 293 #endif |
| 292 | 294 |
| 293 } // namespace gfx | 295 } // namespace gfx |
| OLD | NEW |