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/platform_font_linux.h" | 5 #include "ui/gfx/platform_font_linux.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 const char* kFallbackFontFamilyName = "sans"; | 34 const char* kFallbackFontFamilyName = "sans"; |
35 #endif | 35 #endif |
36 | 36 |
37 // The default font, used for the default constructor. | 37 // The default font, used for the default constructor. |
38 base::LazyInstance<scoped_refptr<PlatformFontLinux>>::Leaky g_default_font = | 38 base::LazyInstance<scoped_refptr<PlatformFontLinux>>::Leaky g_default_font = |
39 LAZY_INSTANCE_INITIALIZER; | 39 LAZY_INSTANCE_INITIALIZER; |
40 | 40 |
41 // Creates a SkTypeface for the passed-in Font::FontStyle and family. If a | 41 // Creates a SkTypeface for the passed-in Font::FontStyle and family. If a |
42 // fallback typeface is used instead of the requested family, |family| will be | 42 // fallback typeface is used instead of the requested family, |family| will be |
43 // updated to contain the fallback's family name. | 43 // updated to contain the fallback's family name. |
44 skia::RefPtr<SkTypeface> CreateSkTypeface(int style, std::string* family) { | 44 sk_sp<SkTypeface> CreateSkTypeface(int style, std::string* family) { |
45 DCHECK(family); | 45 DCHECK(family); |
46 | 46 |
47 int skia_style = SkTypeface::kNormal; | 47 int skia_style = SkTypeface::kNormal; |
48 if (Font::BOLD & style) | 48 if (Font::BOLD & style) |
49 skia_style |= SkTypeface::kBold; | 49 skia_style |= SkTypeface::kBold; |
50 if (Font::ITALIC & style) | 50 if (Font::ITALIC & style) |
51 skia_style |= SkTypeface::kItalic; | 51 skia_style |= SkTypeface::kItalic; |
52 | 52 |
53 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(SkTypeface::CreateFromName( | 53 sk_sp<SkTypeface> typeface(SkTypeface::CreateFromName( |
54 family->c_str(), static_cast<SkTypeface::Style>(skia_style))); | 54 family->c_str(), static_cast<SkTypeface::Style>(skia_style))); |
55 if (!typeface) { | 55 if (!typeface) { |
56 // A non-scalable font such as .pcf is specified. Fall back to a default | 56 // A non-scalable font such as .pcf is specified. Fall back to a default |
57 // scalable font. | 57 // scalable font. |
58 typeface = skia::AdoptRef(SkTypeface::CreateFromName( | 58 typeface = sk_sp<SkTypeface>(SkTypeface::CreateFromName( |
59 kFallbackFontFamilyName, static_cast<SkTypeface::Style>(skia_style))); | 59 kFallbackFontFamilyName, static_cast<SkTypeface::Style>(skia_style))); |
60 CHECK(typeface) << "Could not find any font: " << family << ", " | 60 CHECK(typeface) << "Could not find any font: " << family << ", " |
61 << kFallbackFontFamilyName; | 61 << kFallbackFontFamilyName; |
62 *family = kFallbackFontFamilyName; | 62 *family = kFallbackFontFamilyName; |
63 } | 63 } |
64 return typeface; | 64 return typeface; |
65 } | 65 } |
66 | 66 |
67 } // namespace | 67 } // namespace |
68 | 68 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 | 109 |
110 InitFromPlatformFont(g_default_font.Get().get()); | 110 InitFromPlatformFont(g_default_font.Get().get()); |
111 } | 111 } |
112 | 112 |
113 PlatformFontLinux::PlatformFontLinux(const std::string& font_name, | 113 PlatformFontLinux::PlatformFontLinux(const std::string& font_name, |
114 int font_size_pixels) { | 114 int font_size_pixels) { |
115 FontRenderParamsQuery query; | 115 FontRenderParamsQuery query; |
116 query.families.push_back(font_name); | 116 query.families.push_back(font_name); |
117 query.pixel_size = font_size_pixels; | 117 query.pixel_size = font_size_pixels; |
118 query.style = Font::NORMAL; | 118 query.style = Font::NORMAL; |
119 InitFromDetails(skia::RefPtr<SkTypeface>(), font_name, font_size_pixels, | 119 InitFromDetails(nullptr, font_name, font_size_pixels, |
120 query.style, gfx::GetFontRenderParams(query, NULL)); | 120 query.style, gfx::GetFontRenderParams(query, NULL)); |
121 } | 121 } |
122 | 122 |
123 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
124 // PlatformFontLinux, PlatformFont implementation: | 124 // PlatformFontLinux, PlatformFont implementation: |
125 | 125 |
126 // static | 126 // static |
127 void PlatformFontLinux::ReloadDefaultFont() { | 127 void PlatformFontLinux::ReloadDefaultFont() { |
128 // Reset the scoped_refptr. | 128 // Reset the scoped_refptr. |
129 g_default_font.Get() = nullptr; | 129 g_default_font.Get() = nullptr; |
130 } | 130 } |
131 | 131 |
132 #if defined(OS_CHROMEOS) | 132 #if defined(OS_CHROMEOS) |
133 // static | 133 // static |
134 void PlatformFontLinux::SetDefaultFontDescription( | 134 void PlatformFontLinux::SetDefaultFontDescription( |
135 const std::string& font_description) { | 135 const std::string& font_description) { |
136 delete default_font_description_; | 136 delete default_font_description_; |
137 default_font_description_ = new std::string(font_description); | 137 default_font_description_ = new std::string(font_description); |
138 } | 138 } |
139 | 139 |
140 #endif | 140 #endif |
141 | 141 |
142 Font PlatformFontLinux::DeriveFont(int size_delta, int style) const { | 142 Font PlatformFontLinux::DeriveFont(int size_delta, int style) const { |
143 const int new_size = font_size_pixels_ + size_delta; | 143 const int new_size = font_size_pixels_ + size_delta; |
144 DCHECK_GT(new_size, 0); | 144 DCHECK_GT(new_size, 0); |
145 | 145 |
146 // If the style changed, we may need to load a new face. | 146 // If the style changed, we may need to load a new face. |
147 std::string new_family = font_family_; | 147 std::string new_family = font_family_; |
148 skia::RefPtr<SkTypeface> typeface = | 148 sk_sp<SkTypeface> typeface = |
149 (style == style_) ? typeface_ : CreateSkTypeface(style, &new_family); | 149 (style == style_) ? typeface_ : CreateSkTypeface(style, &new_family); |
150 | 150 |
151 FontRenderParamsQuery query; | 151 FontRenderParamsQuery query; |
152 query.families.push_back(new_family); | 152 query.families.push_back(new_family); |
153 query.pixel_size = new_size; | 153 query.pixel_size = new_size; |
154 query.style = style; | 154 query.style = style; |
155 | 155 |
156 return Font(new PlatformFontLinux(typeface, new_family, new_size, style, | 156 return Font(new PlatformFontLinux(typeface, new_family, new_size, style, |
f(malita)
2016/05/04 15:49:57
std::move(typeface)
tomhudson
2016/05/04 16:51:54
Done.
| |
157 gfx::GetFontRenderParams(query, NULL))); | 157 gfx::GetFontRenderParams(query, NULL))); |
158 } | 158 } |
159 | 159 |
160 int PlatformFontLinux::GetHeight() { | 160 int PlatformFontLinux::GetHeight() { |
161 ComputeMetricsIfNecessary(); | 161 ComputeMetricsIfNecessary(); |
162 return height_pixels_; | 162 return height_pixels_; |
163 } | 163 } |
164 | 164 |
165 int PlatformFontLinux::GetBaseline() { | 165 int PlatformFontLinux::GetBaseline() { |
166 ComputeMetricsIfNecessary(); | 166 ComputeMetricsIfNecessary(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 query.device_scale_factor = current_scale_factor; | 205 query.device_scale_factor = current_scale_factor; |
206 font_render_params_ = gfx::GetFontRenderParams(query, nullptr); | 206 font_render_params_ = gfx::GetFontRenderParams(query, nullptr); |
207 device_scale_factor_ = current_scale_factor; | 207 device_scale_factor_ = current_scale_factor; |
208 } | 208 } |
209 return font_render_params_; | 209 return font_render_params_; |
210 } | 210 } |
211 | 211 |
212 //////////////////////////////////////////////////////////////////////////////// | 212 //////////////////////////////////////////////////////////////////////////////// |
213 // PlatformFontLinux, private: | 213 // PlatformFontLinux, private: |
214 | 214 |
215 PlatformFontLinux::PlatformFontLinux(const skia::RefPtr<SkTypeface>& typeface, | 215 PlatformFontLinux::PlatformFontLinux(sk_sp<SkTypeface> typeface, |
216 const std::string& family, | 216 const std::string& family, |
217 int size_pixels, | 217 int size_pixels, |
218 int style, | 218 int style, |
219 const FontRenderParams& render_params) { | 219 const FontRenderParams& render_params) { |
220 InitFromDetails(typeface, family, size_pixels, style, render_params); | 220 InitFromDetails(typeface, family, size_pixels, style, render_params); |
f(malita)
2016/05/04 15:49:57
std::move(typeface)
tomhudson
2016/05/04 16:51:54
Done.
| |
221 } | 221 } |
222 | 222 |
223 PlatformFontLinux::~PlatformFontLinux() {} | 223 PlatformFontLinux::~PlatformFontLinux() {} |
224 | 224 |
225 void PlatformFontLinux::InitFromDetails( | 225 void PlatformFontLinux::InitFromDetails( |
226 const skia::RefPtr<SkTypeface>& typeface, | 226 sk_sp<SkTypeface> typeface, |
227 const std::string& font_family, | 227 const std::string& font_family, |
228 int font_size_pixels, | 228 int font_size_pixels, |
229 int style, | 229 int style, |
230 const FontRenderParams& render_params) { | 230 const FontRenderParams& render_params) { |
231 DCHECK_GT(font_size_pixels, 0); | 231 DCHECK_GT(font_size_pixels, 0); |
232 | 232 |
233 font_family_ = font_family; | 233 font_family_ = font_family; |
234 typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_); | 234 typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_); |
f(malita)
2016/05/04 15:49:57
std::move(typeface)
tomhudson
2016/05/04 16:51:54
Done.
| |
235 | 235 |
236 font_size_pixels_ = font_size_pixels; | 236 font_size_pixels_ = font_size_pixels; |
237 style_ = style; | 237 style_ = style; |
238 device_scale_factor_ = GetFontRenderParamsDeviceScaleFactor(); | 238 device_scale_factor_ = GetFontRenderParamsDeviceScaleFactor(); |
239 font_render_params_ = render_params; | 239 font_render_params_ = render_params; |
240 | 240 |
241 } | 241 } |
242 | 242 |
243 void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { | 243 void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { |
244 typeface_ = other->typeface_; | 244 typeface_ = other->typeface_; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 return new PlatformFontLinux; | 286 return new PlatformFontLinux; |
287 } | 287 } |
288 | 288 |
289 // static | 289 // static |
290 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 290 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
291 int font_size) { | 291 int font_size) { |
292 return new PlatformFontLinux(font_name, font_size); | 292 return new PlatformFontLinux(font_name, font_size); |
293 } | 293 } |
294 | 294 |
295 } // namespace gfx | 295 } // namespace gfx |
OLD | NEW |