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