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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 std::string* PlatformFontLinux::default_font_description_ = NULL; | 73 std::string* PlatformFontLinux::default_font_description_ = NULL; |
74 #endif | 74 #endif |
75 | 75 |
76 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
77 // PlatformFontLinux, public: | 77 // PlatformFontLinux, public: |
78 | 78 |
79 PlatformFontLinux::PlatformFontLinux() { | 79 PlatformFontLinux::PlatformFontLinux() { |
80 if (!g_default_font.Get()) { | 80 if (!g_default_font.Get()) { |
81 std::string family = kFallbackFontFamilyName; | 81 std::string family = kFallbackFontFamilyName; |
82 int size_pixels = 12; | 82 int size_pixels = 12; |
83 bool italic = false; | 83 int style = Font::NORMAL; |
84 Font::Weight weight = Font::Weight::NORMAL; | 84 Font::Weight weight = Font::Weight::NORMAL; |
85 FontRenderParams params; | 85 FontRenderParams params; |
86 | 86 |
87 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
88 // On Chrome OS, a FontList font description string is stored as a | 88 // On Chrome OS, a FontList font description string is stored as a |
89 // translatable resource and passed in via SetDefaultFontDescription(). | 89 // translatable resource and passed in via SetDefaultFontDescription(). |
90 if (default_font_description_) { | 90 if (default_font_description_) { |
91 FontRenderParamsQuery query; | 91 FontRenderParamsQuery query; |
92 CHECK(FontList::ParseDescription(*default_font_description_, | 92 CHECK(FontList::ParseDescription(*default_font_description_, |
93 &query.families, &query.style, | 93 &query.families, &query.style, |
94 &query.pixel_size, &query.weight)) | 94 &query.pixel_size, &query.weight)) |
95 << "Failed to parse font description " << *default_font_description_; | 95 << "Failed to parse font description " << *default_font_description_; |
96 params = gfx::GetFontRenderParams(query, &family); | 96 params = gfx::GetFontRenderParams(query, &family); |
97 size_pixels = query.pixel_size; | 97 size_pixels = query.pixel_size; |
98 italic = query.style & gfx::Font::ITALIC; | 98 style = query.style; |
99 weight = query.weight; | 99 weight = query.weight; |
100 } | 100 } |
101 #else | 101 #else |
102 // On Linux, LinuxFontDelegate is used to query the native toolkit (e.g. | 102 // On Linux, LinuxFontDelegate is used to query the native toolkit (e.g. |
103 // GTK+) for the default UI font. | 103 // GTK+) for the default UI font. |
104 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); | 104 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); |
105 if (delegate) { | 105 if (delegate) { |
106 delegate->GetDefaultFontDescription(&family, &size_pixels, &italic, | 106 delegate->GetDefaultFontDescription(&family, &size_pixels, &style, |
107 &weight, ¶ms); | 107 &weight, ¶ms); |
108 } | 108 } |
109 #endif | 109 #endif |
110 | 110 |
111 g_default_font.Get() = | 111 g_default_font.Get() = new PlatformFontLinux( |
112 new PlatformFontLinux(CreateSkTypeface(italic, weight, &family), family, | 112 CreateSkTypeface(style & Font::ITALIC, weight, &family), family, |
113 size_pixels, italic, weight, params); | 113 size_pixels, style, weight, params); |
114 } | 114 } |
115 | 115 |
116 InitFromPlatformFont(g_default_font.Get().get()); | 116 InitFromPlatformFont(g_default_font.Get().get()); |
117 } | 117 } |
118 | 118 |
119 PlatformFontLinux::PlatformFontLinux(const std::string& font_name, | 119 PlatformFontLinux::PlatformFontLinux(const std::string& font_name, |
120 int font_size_pixels) { | 120 int font_size_pixels) { |
121 FontRenderParamsQuery query; | 121 FontRenderParamsQuery query; |
122 query.families.push_back(font_name); | 122 query.families.push_back(font_name); |
123 query.pixel_size = font_size_pixels; | 123 query.pixel_size = font_size_pixels; |
124 query.weight = Font::Weight::NORMAL; | 124 query.weight = Font::Weight::NORMAL; |
125 InitFromDetails(nullptr, font_name, font_size_pixels, | 125 InitFromDetails(nullptr, font_name, font_size_pixels, |
126 false, query.weight, gfx::GetFontRenderParams(query, NULL)); | 126 Font::NORMAL, query.weight, |
| 127 gfx::GetFontRenderParams(query, NULL)); |
127 } | 128 } |
128 | 129 |
129 //////////////////////////////////////////////////////////////////////////////// | 130 //////////////////////////////////////////////////////////////////////////////// |
130 // PlatformFontLinux, PlatformFont implementation: | 131 // PlatformFontLinux, PlatformFont implementation: |
131 | 132 |
132 // static | 133 // static |
133 void PlatformFontLinux::ReloadDefaultFont() { | 134 void PlatformFontLinux::ReloadDefaultFont() { |
134 // Reset the scoped_refptr. | 135 // Reset the scoped_refptr. |
135 g_default_font.Get() = nullptr; | 136 g_default_font.Get() = nullptr; |
136 } | 137 } |
137 | 138 |
138 #if defined(OS_CHROMEOS) | 139 #if defined(OS_CHROMEOS) |
139 // static | 140 // static |
140 void PlatformFontLinux::SetDefaultFontDescription( | 141 void PlatformFontLinux::SetDefaultFontDescription( |
141 const std::string& font_description) { | 142 const std::string& font_description) { |
142 delete default_font_description_; | 143 delete default_font_description_; |
143 default_font_description_ = new std::string(font_description); | 144 default_font_description_ = new std::string(font_description); |
144 } | 145 } |
145 | 146 |
146 #endif | 147 #endif |
147 | 148 |
148 Font PlatformFontLinux::DeriveFont(int size_delta, | 149 Font PlatformFontLinux::DeriveFont(int size_delta, |
149 int style, | 150 int style, |
150 Font::Weight weight) const { | 151 Font::Weight weight) const { |
151 const int new_size = font_size_pixels_ + size_delta; | 152 const int new_size = font_size_pixels_ + size_delta; |
152 DCHECK_GT(new_size, 0); | 153 DCHECK_GT(new_size, 0); |
153 | 154 |
154 const int current_style = italic_ ? Font::ITALIC : Font::NORMAL; | |
155 | |
156 // If the style changed, we may need to load a new face. | 155 // If the style changed, we may need to load a new face. |
157 std::string new_family = font_family_; | 156 std::string new_family = font_family_; |
158 sk_sp<SkTypeface> typeface = | 157 sk_sp<SkTypeface> typeface = |
159 (weight == weight_ && style == current_style) | 158 (weight == weight_ && style == style_) |
160 ? typeface_ | 159 ? typeface_ |
161 : CreateSkTypeface(style, weight, &new_family); | 160 : CreateSkTypeface(style, weight, &new_family); |
162 | 161 |
163 FontRenderParamsQuery query; | 162 FontRenderParamsQuery query; |
164 query.families.push_back(new_family); | 163 query.families.push_back(new_family); |
165 query.pixel_size = new_size; | 164 query.pixel_size = new_size; |
166 // TODO(mboc): Support UNDERLINE on Linux, if possible. | 165 query.style = style; |
167 query.style = style & Font::ITALIC; | |
168 | 166 |
169 return Font(new PlatformFontLinux(std::move(typeface), new_family, new_size, | 167 return Font(new PlatformFontLinux(std::move(typeface), new_family, new_size, |
170 style & Font::ITALIC, weight, gfx::GetFontRenderParams(query, NULL))); | 168 style, weight, gfx::GetFontRenderParams(query, NULL))); |
171 } | 169 } |
172 | 170 |
173 int PlatformFontLinux::GetHeight() { | 171 int PlatformFontLinux::GetHeight() { |
174 ComputeMetricsIfNecessary(); | 172 ComputeMetricsIfNecessary(); |
175 return height_pixels_; | 173 return height_pixels_; |
176 } | 174 } |
177 | 175 |
178 Font::Weight PlatformFontLinux::GetWeight() const { | 176 Font::Weight PlatformFontLinux::GetWeight() const { |
179 return weight_; | 177 return weight_; |
180 } | 178 } |
181 | 179 |
182 int PlatformFontLinux::GetBaseline() { | 180 int PlatformFontLinux::GetBaseline() { |
183 ComputeMetricsIfNecessary(); | 181 ComputeMetricsIfNecessary(); |
184 return ascent_pixels_; | 182 return ascent_pixels_; |
185 } | 183 } |
186 | 184 |
187 int PlatformFontLinux::GetCapHeight() { | 185 int PlatformFontLinux::GetCapHeight() { |
188 ComputeMetricsIfNecessary(); | 186 ComputeMetricsIfNecessary(); |
189 return cap_height_pixels_; | 187 return cap_height_pixels_; |
190 } | 188 } |
191 | 189 |
192 int PlatformFontLinux::GetExpectedTextWidth(int length) { | 190 int PlatformFontLinux::GetExpectedTextWidth(int length) { |
193 ComputeMetricsIfNecessary(); | 191 ComputeMetricsIfNecessary(); |
194 return round(static_cast<float>(length) * average_width_pixels_); | 192 return round(static_cast<float>(length) * average_width_pixels_); |
195 } | 193 } |
196 | 194 |
197 int PlatformFontLinux::GetStyle() const { | 195 int PlatformFontLinux::GetStyle() const { |
198 return italic_ ? Font::ITALIC : Font::NORMAL; | 196 return style_; |
199 } | 197 } |
200 | 198 |
201 const std::string& PlatformFontLinux::GetFontName() const { | 199 const std::string& PlatformFontLinux::GetFontName() const { |
202 return font_family_; | 200 return font_family_; |
203 } | 201 } |
204 | 202 |
205 std::string PlatformFontLinux::GetActualFontNameForTesting() const { | 203 std::string PlatformFontLinux::GetActualFontNameForTesting() const { |
206 SkString family_name; | 204 SkString family_name; |
207 typeface_->getFamilyName(&family_name); | 205 typeface_->getFamilyName(&family_name); |
208 return family_name.c_str(); | 206 return family_name.c_str(); |
209 } | 207 } |
210 | 208 |
211 int PlatformFontLinux::GetFontSize() const { | 209 int PlatformFontLinux::GetFontSize() const { |
212 return font_size_pixels_; | 210 return font_size_pixels_; |
213 } | 211 } |
214 | 212 |
215 const FontRenderParams& PlatformFontLinux::GetFontRenderParams() { | 213 const FontRenderParams& PlatformFontLinux::GetFontRenderParams() { |
216 float current_scale_factor = GetFontRenderParamsDeviceScaleFactor(); | 214 float current_scale_factor = GetFontRenderParamsDeviceScaleFactor(); |
217 if (current_scale_factor != device_scale_factor_) { | 215 if (current_scale_factor != device_scale_factor_) { |
218 FontRenderParamsQuery query; | 216 FontRenderParamsQuery query; |
219 query.families.push_back(font_family_); | 217 query.families.push_back(font_family_); |
220 query.pixel_size = font_size_pixels_; | 218 query.pixel_size = font_size_pixels_; |
221 query.style = italic_ ? Font::ITALIC : Font::NORMAL; | 219 query.style = style_; |
222 query.weight = weight_; | 220 query.weight = weight_; |
223 query.device_scale_factor = current_scale_factor; | 221 query.device_scale_factor = current_scale_factor; |
224 font_render_params_ = gfx::GetFontRenderParams(query, nullptr); | 222 font_render_params_ = gfx::GetFontRenderParams(query, nullptr); |
225 device_scale_factor_ = current_scale_factor; | 223 device_scale_factor_ = current_scale_factor; |
226 } | 224 } |
227 return font_render_params_; | 225 return font_render_params_; |
228 } | 226 } |
229 | 227 |
230 //////////////////////////////////////////////////////////////////////////////// | 228 //////////////////////////////////////////////////////////////////////////////// |
231 // PlatformFontLinux, private: | 229 // PlatformFontLinux, private: |
232 | 230 |
233 PlatformFontLinux::PlatformFontLinux(sk_sp<SkTypeface> typeface, | 231 PlatformFontLinux::PlatformFontLinux(sk_sp<SkTypeface> typeface, |
234 const std::string& family, | 232 const std::string& family, |
235 int size_pixels, | 233 int size_pixels, |
236 bool italic, | 234 int style, |
237 Font::Weight weight, | 235 Font::Weight weight, |
238 const FontRenderParams& render_params) { | 236 const FontRenderParams& render_params) { |
239 InitFromDetails(std::move(typeface), family, size_pixels, italic, weight, | 237 InitFromDetails(std::move(typeface), family, size_pixels, style, weight, |
240 render_params); | 238 render_params); |
241 } | 239 } |
242 | 240 |
243 PlatformFontLinux::~PlatformFontLinux() {} | 241 PlatformFontLinux::~PlatformFontLinux() {} |
244 | 242 |
245 void PlatformFontLinux::InitFromDetails( | 243 void PlatformFontLinux::InitFromDetails( |
246 sk_sp<SkTypeface> typeface, | 244 sk_sp<SkTypeface> typeface, |
247 const std::string& font_family, | 245 const std::string& font_family, |
248 int font_size_pixels, | 246 int font_size_pixels, |
249 bool italic, | 247 int style, |
250 Font::Weight weight, | 248 Font::Weight weight, |
251 const FontRenderParams& render_params) { | 249 const FontRenderParams& render_params) { |
252 DCHECK_GT(font_size_pixels, 0); | 250 DCHECK_GT(font_size_pixels, 0); |
253 | 251 |
254 font_family_ = font_family; | 252 font_family_ = font_family; |
255 typeface_ = typeface ? std::move(typeface) : | 253 typeface_ = typeface ? std::move(typeface) : |
256 CreateSkTypeface(italic, weight, &font_family_); | 254 CreateSkTypeface(style & Font::ITALIC, weight, &font_family_); |
257 | 255 |
258 font_size_pixels_ = font_size_pixels; | 256 font_size_pixels_ = font_size_pixels; |
259 italic_ = italic; | 257 style_ = style; |
260 weight_ = weight; | 258 weight_ = weight; |
261 device_scale_factor_ = GetFontRenderParamsDeviceScaleFactor(); | 259 device_scale_factor_ = GetFontRenderParamsDeviceScaleFactor(); |
262 font_render_params_ = render_params; | 260 font_render_params_ = render_params; |
263 } | 261 } |
264 | 262 |
265 void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { | 263 void PlatformFontLinux::InitFromPlatformFont(const PlatformFontLinux* other) { |
266 typeface_ = other->typeface_; | 264 typeface_ = other->typeface_; |
267 font_family_ = other->font_family_; | 265 font_family_ = other->font_family_; |
268 font_size_pixels_ = other->font_size_pixels_; | 266 font_size_pixels_ = other->font_size_pixels_; |
269 italic_ = other->italic_; | 267 style_ = other->style_; |
270 weight_ = other->weight_; | 268 weight_ = other->weight_; |
271 device_scale_factor_ = other->device_scale_factor_; | 269 device_scale_factor_ = other->device_scale_factor_; |
272 font_render_params_ = other->font_render_params_; | 270 font_render_params_ = other->font_render_params_; |
273 | 271 |
274 if (!other->metrics_need_computation_) { | 272 if (!other->metrics_need_computation_) { |
275 metrics_need_computation_ = false; | 273 metrics_need_computation_ = false; |
276 ascent_pixels_ = other->ascent_pixels_; | 274 ascent_pixels_ = other->ascent_pixels_; |
277 height_pixels_ = other->height_pixels_; | 275 height_pixels_ = other->height_pixels_; |
278 cap_height_pixels_ = other->cap_height_pixels_; | 276 cap_height_pixels_ = other->cap_height_pixels_; |
279 average_width_pixels_ = other->average_width_pixels_; | 277 average_width_pixels_ = other->average_width_pixels_; |
280 } | 278 } |
281 } | 279 } |
282 | 280 |
283 void PlatformFontLinux::ComputeMetricsIfNecessary() { | 281 void PlatformFontLinux::ComputeMetricsIfNecessary() { |
284 if (metrics_need_computation_) { | 282 if (metrics_need_computation_) { |
285 metrics_need_computation_ = false; | 283 metrics_need_computation_ = false; |
286 | 284 |
287 SkPaint paint; | 285 SkPaint paint; |
288 paint.setAntiAlias(false); | 286 paint.setAntiAlias(false); |
289 paint.setSubpixelText(false); | 287 paint.setSubpixelText(false); |
290 paint.setTextSize(font_size_pixels_); | 288 paint.setTextSize(font_size_pixels_); |
291 paint.setTypeface(typeface_.get()); | 289 paint.setTypeface(typeface_.get()); |
292 paint.setFakeBoldText(weight_ >= Font::Weight::BOLD && | 290 paint.setFakeBoldText(weight_ >= Font::Weight::BOLD && |
293 !typeface_->isBold()); | 291 !typeface_->isBold()); |
294 paint.setTextSkewX(italic_ && !typeface_->isItalic() ? -SK_Scalar1 / 4 : 0); | 292 paint.setTextSkewX((Font::ITALIC & style_) && !typeface_->isItalic() ? |
| 293 -SK_Scalar1/4 : 0); |
295 SkPaint::FontMetrics metrics; | 294 SkPaint::FontMetrics metrics; |
296 paint.getFontMetrics(&metrics); | 295 paint.getFontMetrics(&metrics); |
297 ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); | 296 ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); |
298 height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent); | 297 height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent); |
299 cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight); | 298 cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight); |
300 average_width_pixels_ = SkScalarToDouble(metrics.fAvgCharWidth); | 299 average_width_pixels_ = SkScalarToDouble(metrics.fAvgCharWidth); |
301 } | 300 } |
302 } | 301 } |
303 | 302 |
304 //////////////////////////////////////////////////////////////////////////////// | 303 //////////////////////////////////////////////////////////////////////////////// |
305 // PlatformFont, public: | 304 // PlatformFont, public: |
306 | 305 |
307 // static | 306 // static |
308 PlatformFont* PlatformFont::CreateDefault() { | 307 PlatformFont* PlatformFont::CreateDefault() { |
309 return new PlatformFontLinux; | 308 return new PlatformFontLinux; |
310 } | 309 } |
311 | 310 |
312 // static | 311 // static |
313 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 312 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
314 int font_size) { | 313 int font_size) { |
315 return new PlatformFontLinux(font_name, font_size); | 314 return new PlatformFontLinux(font_name, font_size); |
316 } | 315 } |
317 | 316 |
318 } // namespace gfx | 317 } // namespace gfx |
OLD | NEW |