diff options
author | derat <derat@chromium.org> | 2015-01-02 21:47:37 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-03 05:48:15 +0000 |
commit | f1e9026f116ac3b22d10fdd387a2f50b8919c22b (patch) | |
tree | 930f845b2ae82d478a369230b5a23295373f6d29 | |
parent | ebe974f000199a7020da9855e326b31daad59be3 (diff) | |
download | chromium_src-f1e9026f116ac3b22d10fdd387a2f50b8919c22b.zip chromium_src-f1e9026f116ac3b22d10fdd387a2f50b8919c22b.tar.gz chromium_src-f1e9026f116ac3b22d10fdd387a2f50b8919c22b.tar.bz2 |
linux: Make PlatformFontPango get average width from Skia.
As a first step toward converting PlatformFontPango to
PlatformFontSkia, make it get the average character width
from Skia rather than from Pango.
Also remove some expensive code that was lazily called to
compute the average by rendering a long string of text --
the code was using the minimum of this computed value and
the average provided by Pango, but the Pango-/Skia-provided
averages appear to always (?) be a few pixels smaller than
the computed values, make the computation an apparent no-op.
BUG=398885
Review URL: https://codereview.chromium.org/818303002
Cr-Commit-Position: refs/heads/master@{#309874}
-rw-r--r-- | ui/gfx/platform_font_pango.cc | 59 | ||||
-rw-r--r-- | ui/gfx/platform_font_pango.h | 16 |
2 files changed, 12 insertions, 63 deletions
diff --git a/ui/gfx/platform_font_pango.cc b/ui/gfx/platform_font_pango.cc index 96c601a..67c1b2d 100644 --- a/ui/gfx/platform_font_pango.cc +++ b/ui/gfx/platform_font_pango.cc @@ -179,8 +179,7 @@ int PlatformFontPango::GetCapHeight() const { } int PlatformFontPango::GetExpectedTextWidth(int length) const { - double char_width = const_cast<PlatformFontPango*>(this)->GetAverageWidth(); - return round(static_cast<float>(length) * char_width); + return round(static_cast<float>(length) * average_width_pixels_); } int PlatformFontPango::GetStyle() const { @@ -278,15 +277,19 @@ void PlatformFontPango::InitFromDetails( font_render_params_ = render_params; SkPaint paint; + paint.setAntiAlias(false); + paint.setSubpixelText(false); + paint.setTextSize(font_size_pixels_); + paint.setTypeface(typeface_.get()); + paint.setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); + paint.setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? + -SK_Scalar1/4 : 0); SkPaint::FontMetrics metrics; - PaintSetup(&paint); paint.getFontMetrics(&metrics); ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent); cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight); - - pango_metrics_inited_ = false; - average_width_pixels_ = 0.0f; + average_width_pixels_ = SkScalarToDouble(metrics.fAvgCharWidth); } void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) { @@ -294,52 +297,14 @@ void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) { font_family_ = other->font_family_; font_size_pixels_ = other->font_size_pixels_; style_ = other->style_; +#if defined(OS_CHROMEOS) + device_scale_factor_ = other->device_scale_factor_; +#endif font_render_params_ = other->font_render_params_; ascent_pixels_ = other->ascent_pixels_; height_pixels_ = other->height_pixels_; cap_height_pixels_ = other->cap_height_pixels_; - pango_metrics_inited_ = other->pango_metrics_inited_; average_width_pixels_ = other->average_width_pixels_; -#if defined(OS_CHROMEOS) - device_scale_factor_ = other->device_scale_factor_; -#endif -} - -void PlatformFontPango::PaintSetup(SkPaint* paint) const { - paint->setAntiAlias(false); - paint->setSubpixelText(false); - paint->setTextSize(font_size_pixels_); - paint->setTypeface(typeface_.get()); - paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); - paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? - -SK_Scalar1/4 : 0); -} - -void PlatformFontPango::InitPangoMetrics() { - if (!pango_metrics_inited_) { - pango_metrics_inited_ = true; - ScopedPangoFontDescription pango_desc(GetNativeFont()); - PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get()); - - // First get the Pango-based width (converting from Pango units to pixels). - const double pango_width_pixels = - pango_font_metrics_get_approximate_char_width(pango_metrics) / - PANGO_SCALE; - - // Yes, this is how Microsoft recommends calculating the dialog unit - // conversions. - const int text_width_pixels = GetStringWidth( - base::ASCIIToUTF16( - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), - FontList(Font(this))); - const double dialog_units_pixels = (text_width_pixels / 26 + 1) / 2; - average_width_pixels_ = std::min(pango_width_pixels, dialog_units_pixels); - } -} - -double PlatformFontPango::GetAverageWidth() const { - const_cast<PlatformFontPango*>(this)->InitPangoMetrics(); - return average_width_pixels_; } //////////////////////////////////////////////////////////////////////////////// diff --git a/ui/gfx/platform_font_pango.h b/ui/gfx/platform_font_pango.h index f9783d3..47f402e 100644 --- a/ui/gfx/platform_font_pango.h +++ b/ui/gfx/platform_font_pango.h @@ -75,18 +75,6 @@ class GFX_EXPORT PlatformFontPango : public PlatformFont { // Initializes this object as a copy of another PlatformFontPango. void InitFromPlatformFont(const PlatformFontPango* other); - // Potentially slow call to get pango metrics (average width). - void InitPangoMetrics(); - - // Setup a Skia context to use the current typeface. - void PaintSetup(SkPaint* paint) const; - - // Make |this| a copy of |other|. - void CopyFont(const Font& other); - - // The average width of a character, initialized and cached if needed. - double GetAverageWidth() const; - skia::RefPtr<SkTypeface> typeface_; // Additional information about the face. @@ -105,10 +93,6 @@ class GFX_EXPORT PlatformFontPango : public PlatformFont { int ascent_pixels_; int height_pixels_; int cap_height_pixels_; - - // The pango metrics are much more expensive so we wait until we need them - // to compute them. - bool pango_metrics_inited_; double average_width_pixels_; // The default font, used for the default constructor. |