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_pango.h" | 5 #include "ui/gfx/platform_font_pango.h" |
6 | 6 |
7 #include <fontconfig/fontconfig.h> | 7 #include <fontconfig/fontconfig.h> |
8 #include <pango/pango.h> | 8 #include <pango/pango.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 style_)); | 167 style_)); |
168 } | 168 } |
169 | 169 |
170 // If the style has changed we may need to load a new face | 170 // If the style has changed we may need to load a new face |
171 int skstyle = SkTypeface::kNormal; | 171 int skstyle = SkTypeface::kNormal; |
172 if (gfx::Font::BOLD & style) | 172 if (gfx::Font::BOLD & style) |
173 skstyle |= SkTypeface::kBold; | 173 skstyle |= SkTypeface::kBold; |
174 if (gfx::Font::ITALIC & style) | 174 if (gfx::Font::ITALIC & style) |
175 skstyle |= SkTypeface::kItalic; | 175 skstyle |= SkTypeface::kItalic; |
176 | 176 |
177 SkTypeface* typeface = SkTypeface::CreateFromName( | 177 skia::RefPtr<SkTypeface> typeface = SkTypeface::CreateFromName( |
178 font_family_.c_str(), | 178 font_family_.c_str(), |
179 static_cast<SkTypeface::Style>(skstyle)); | 179 static_cast<SkTypeface::Style>(skstyle)); |
180 SkAutoUnref tf_helper(typeface); | |
181 | 180 |
182 return Font(new PlatformFontPango(typeface, | 181 return Font(new PlatformFontPango(typeface, |
183 font_family_, | 182 font_family_, |
184 font_size_pixels_ + size_delta, | 183 font_size_pixels_ + size_delta, |
185 style)); | 184 style)); |
186 } | 185 } |
187 | 186 |
188 int PlatformFontPango::GetHeight() const { | 187 int PlatformFontPango::GetHeight() const { |
189 return height_pixels_; | 188 return height_pixels_; |
190 } | 189 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 // to paint it ourselves, see pango_font_metrics_get_underline_position. | 244 // to paint it ourselves, see pango_font_metrics_get_underline_position. |
246 break; | 245 break; |
247 } | 246 } |
248 | 247 |
249 return pfd; | 248 return pfd; |
250 } | 249 } |
251 | 250 |
252 //////////////////////////////////////////////////////////////////////////////// | 251 //////////////////////////////////////////////////////////////////////////////// |
253 // PlatformFontPango, private: | 252 // PlatformFontPango, private: |
254 | 253 |
255 PlatformFontPango::PlatformFontPango(SkTypeface* typeface, | 254 PlatformFontPango::PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface, |
256 const std::string& name, | 255 const std::string& name, |
257 int size, | 256 int size, |
258 int style) { | 257 int style) { |
259 InitWithTypefaceNameSizeAndStyle(typeface, name, size, style); | 258 InitWithTypefaceNameSizeAndStyle(typeface, name, size, style); |
260 } | 259 } |
261 | 260 |
262 PlatformFontPango::~PlatformFontPango() {} | 261 PlatformFontPango::~PlatformFontPango() {} |
263 | 262 |
264 void PlatformFontPango::InitWithNameAndSize(const std::string& font_name, | 263 void PlatformFontPango::InitWithNameAndSize(const std::string& font_name, |
265 int font_size) { | 264 int font_size) { |
266 DCHECK_GT(font_size, 0); | 265 DCHECK_GT(font_size, 0); |
267 std::string fallback; | 266 std::string fallback; |
268 | 267 |
269 SkTypeface* typeface = SkTypeface::CreateFromName( | 268 skia::RefPtr<SkTypeface> typeface = SkTypeface::CreateFromName( |
270 font_name.c_str(), SkTypeface::kNormal); | 269 font_name.c_str(), SkTypeface::kNormal); |
271 if (!typeface) { | 270 if (!typeface) { |
272 // A non-scalable font such as .pcf is specified. Falls back to a default | 271 // A non-scalable font such as .pcf is specified. Falls back to a default |
273 // scalable font. | 272 // scalable font. |
274 typeface = SkTypeface::CreateFromName( | 273 typeface = SkTypeface::CreateFromName( |
275 kFallbackFontFamilyName, SkTypeface::kNormal); | 274 kFallbackFontFamilyName, SkTypeface::kNormal); |
276 CHECK(typeface) << "Could not find any font: " | 275 CHECK(typeface) << "Could not find any font: " |
277 << font_name | 276 << font_name |
278 << ", " << kFallbackFontFamilyName; | 277 << ", " << kFallbackFontFamilyName; |
279 fallback = kFallbackFontFamilyName; | 278 fallback = kFallbackFontFamilyName; |
280 } | 279 } |
281 SkAutoUnref typeface_helper(typeface); | |
282 | 280 |
283 InitWithTypefaceNameSizeAndStyle(typeface, | 281 InitWithTypefaceNameSizeAndStyle(typeface, |
284 fallback.empty() ? font_name : fallback, | 282 fallback.empty() ? font_name : fallback, |
285 font_size, | 283 font_size, |
286 gfx::Font::NORMAL); | 284 gfx::Font::NORMAL); |
287 } | 285 } |
288 | 286 |
289 void PlatformFontPango::InitWithTypefaceNameSizeAndStyle( | 287 void PlatformFontPango::InitWithTypefaceNameSizeAndStyle( |
290 SkTypeface* typeface, | 288 const skia::RefPtr<SkTypeface>& typeface, |
291 const std::string& font_family, | 289 const std::string& font_family, |
292 int font_size, | 290 int font_size, |
293 int style) { | 291 int style) { |
294 typeface_helper_.reset(new SkAutoUnref(typeface)); | |
295 typeface_ = typeface; | 292 typeface_ = typeface; |
296 typeface_->ref(); | |
297 font_family_ = font_family; | 293 font_family_ = font_family; |
298 font_size_pixels_ = font_size; | 294 font_size_pixels_ = font_size; |
299 style_ = style; | 295 style_ = style; |
300 pango_metrics_inited_ = false; | 296 pango_metrics_inited_ = false; |
301 average_width_pixels_ = 0.0f; | 297 average_width_pixels_ = 0.0f; |
302 underline_position_pixels_ = 0.0f; | 298 underline_position_pixels_ = 0.0f; |
303 underline_thickness_pixels_ = 0.0f; | 299 underline_thickness_pixels_ = 0.0f; |
304 | 300 |
305 SkPaint paint; | 301 SkPaint paint; |
306 SkPaint::FontMetrics metrics; | 302 SkPaint::FontMetrics metrics; |
307 PaintSetup(&paint); | 303 PaintSetup(&paint); |
308 paint.getFontMetrics(&metrics); | 304 paint.getFontMetrics(&metrics); |
309 | 305 |
310 ascent_pixels_ = SkScalarCeil(-metrics.fAscent); | 306 ascent_pixels_ = SkScalarCeil(-metrics.fAscent); |
311 height_pixels_ = ascent_pixels_ + SkScalarCeil(metrics.fDescent); | 307 height_pixels_ = ascent_pixels_ + SkScalarCeil(metrics.fDescent); |
312 } | 308 } |
313 | 309 |
314 void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) { | 310 void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) { |
315 typeface_helper_.reset(new SkAutoUnref(other->typeface_)); | |
316 typeface_ = other->typeface_; | 311 typeface_ = other->typeface_; |
317 typeface_->ref(); | |
318 font_family_ = other->font_family_; | 312 font_family_ = other->font_family_; |
319 font_size_pixels_ = other->font_size_pixels_; | 313 font_size_pixels_ = other->font_size_pixels_; |
320 style_ = other->style_; | 314 style_ = other->style_; |
321 height_pixels_ = other->height_pixels_; | 315 height_pixels_ = other->height_pixels_; |
322 ascent_pixels_ = other->ascent_pixels_; | 316 ascent_pixels_ = other->ascent_pixels_; |
323 pango_metrics_inited_ = other->pango_metrics_inited_; | 317 pango_metrics_inited_ = other->pango_metrics_inited_; |
324 average_width_pixels_ = other->average_width_pixels_; | 318 average_width_pixels_ = other->average_width_pixels_; |
325 underline_position_pixels_ = other->underline_position_pixels_; | 319 underline_position_pixels_ = other->underline_position_pixels_; |
326 underline_thickness_pixels_ = other->underline_thickness_pixels_; | 320 underline_thickness_pixels_ = other->underline_thickness_pixels_; |
327 } | 321 } |
328 | 322 |
329 void PlatformFontPango::PaintSetup(SkPaint* paint) const { | 323 void PlatformFontPango::PaintSetup(SkPaint* paint) const { |
330 paint->setAntiAlias(false); | 324 paint->setAntiAlias(false); |
331 paint->setSubpixelText(false); | 325 paint->setSubpixelText(false); |
332 paint->setTextSize(font_size_pixels_); | 326 paint->setTextSize(font_size_pixels_); |
333 paint->setTypeface(typeface_); | 327 paint->setTypeface(typeface_.get()); |
334 paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); | 328 paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); |
335 paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? | 329 paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? |
336 -SK_Scalar1/4 : 0); | 330 -SK_Scalar1/4 : 0); |
337 } | 331 } |
338 | 332 |
339 void PlatformFontPango::InitPangoMetrics() { | 333 void PlatformFontPango::InitPangoMetrics() { |
340 if (!pango_metrics_inited_) { | 334 if (!pango_metrics_inited_) { |
341 pango_metrics_inited_ = true; | 335 pango_metrics_inited_ = true; |
342 ScopedPangoFontDescription pango_desc(GetNativeFont()); | 336 ScopedPangoFontDescription pango_desc(GetNativeFont()); |
343 PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get()); | 337 PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 return new PlatformFontPango(native_font); | 382 return new PlatformFontPango(native_font); |
389 } | 383 } |
390 | 384 |
391 // static | 385 // static |
392 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 386 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
393 int font_size) { | 387 int font_size) { |
394 return new PlatformFontPango(font_name, font_size); | 388 return new PlatformFontPango(font_name, font_size); |
395 } | 389 } |
396 | 390 |
397 } // namespace gfx | 391 } // namespace gfx |
OLD | NEW |