diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-05 21:10:53 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-05 21:10:53 +0000 |
commit | 024653baac4eb55e914f663d1592b37f65eb1fb8 (patch) | |
tree | a82652a43668a1a4a9bb1b4c4d83becfa968c1d9 /ui/gfx/canvas_skia.cc | |
parent | 50fa34746fa1a3f1ab9d794dd1a55b8b1398fbe1 (diff) | |
download | chromium_src-024653baac4eb55e914f663d1592b37f65eb1fb8.zip chromium_src-024653baac4eb55e914f663d1592b37f65eb1fb8.tar.gz chromium_src-024653baac4eb55e914f663d1592b37f65eb1fb8.tar.bz2 |
Use the RenderText's computed text height instead of Font::GetHeight() in canvas_skia.cc.
This fixes the problem of incorrect alignment in the case of font fallback choosing a font with a different height.
Also, use the computed RenderText height to center the text in RenderText::GetOriginForDrawing(), so that the
two calculations (canvas_skia.cc and render_text.cc) use the same consistent value.
BUG=105550
TEST=Inspect views_examples text style example.
Review URL: https://chromiumcodereview.appspot.com/9923013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131011 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/canvas_skia.cc')
-rw-r--r-- | ui/gfx/canvas_skia.cc | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc index 3ce54d4..b790076 100644 --- a/ui/gfx/canvas_skia.cc +++ b/ui/gfx/canvas_skia.cc @@ -56,14 +56,12 @@ bool PixelShouldGetHalo(const SkBitmap& bitmap, } // Apply vertical alignment per |flags|. Returns y-coordinate delta. -int VAlignText(const gfx::Font& font, - int line_count, +int VAlignText(int text_height, int flags, int available_height) { if (flags & gfx::Canvas::TEXT_VALIGN_TOP) return 0; - const int text_height = line_count * font.GetHeight(); if (flags & gfx::Canvas::TEXT_VALIGN_BOTTOM) return available_height - text_height; @@ -194,8 +192,9 @@ void Canvas::SizeStringInt(const string16& text, for (size_t i = 0; i < strings.size(); ++i) { StripAcceleratorChars(flags, &strings[i]); render_text->SetText(strings[i]); - w = std::max(w, render_text->GetStringSize().width()); - h += font.GetHeight(); + const Size string_size = render_text->GetStringSize(); + w = std::max(w, string_size.width()); + h += string_size.height(); } *width = w; *height = h; @@ -205,15 +204,17 @@ void Canvas::SizeStringInt(const string16& text, const size_t kMaxRenderTextLength = 5000; if (text.length() >= kMaxRenderTextLength) { *width = text.length() * font.GetAverageCharacterWidth(); + *height = font.GetHeight(); } else { scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); gfx::Rect rect(*width, *height); string16 adjusted_text = text; StripAcceleratorChars(flags, &adjusted_text); UpdateRenderText(rect, adjusted_text, font, flags, 0, render_text.get()); - *width = render_text->GetStringSize().width(); + const Size string_size = render_text->GetStringSize(); + *width = string_size.width(); + *height = string_size.height(); } - *height = font.GetHeight(); } } @@ -260,13 +261,21 @@ void Canvas::DrawStringInt(const string16& text, ui::ElideRectangleText(adjusted_text, font, w, h, wrap_behavior, &strings); - rect.Offset(0, VAlignText(font, strings.size(), flags, h)); for (size_t i = 0; i < strings.size(); i++) { ui::Range range = StripAcceleratorChars(flags, &strings[i]); UpdateRenderText(rect, strings[i], font, flags, color, render_text.get()); + + // Apply vertical alignment over the block of text using the height of the + // first line. This may not be correct if different lines in the text have + // different heights, but avoids needing to do two passes. + const int line_height = render_text->GetStringSize().height(); + if (i == 0) + rect.Offset(0, VAlignText(strings.size() * line_height, flags, h)); + rect.set_height(line_height); + ApplyUnderlineStyle(range, render_text.get()); render_text->Draw(this); - rect.Offset(0, font.GetHeight()); + rect.Offset(0, line_height); } } else { ui::Range range = StripAcceleratorChars(flags, &adjusted_text); @@ -287,9 +296,14 @@ void Canvas::DrawStringInt(const string16& text, if (elide_text) ElideTextAndAdjustRange(font, w, &adjusted_text, &range); - rect.Offset(0, VAlignText(font, 1, flags, h)); UpdateRenderText(rect, adjusted_text, font, flags, color, render_text.get()); + + const int line_height = render_text->GetStringSize().height(); + rect.Offset(0, VAlignText(line_height, flags, h)); + rect.set_height(line_height); + render_text->SetDisplayRect(rect); + ApplyUnderlineStyle(range, render_text.get()); render_text->Draw(this); } @@ -399,9 +413,13 @@ void Canvas::DrawFadeTruncatingString( } gfx::Rect rect = display_rect; - rect.Offset(0, VAlignText(font, 1, flags, display_rect.height())); UpdateRenderText(rect, clipped_text, font, flags, color, render_text.get()); + const int line_height = render_text->GetStringSize().height(); + rect.Offset(0, VAlignText(line_height, flags, display_rect.height())); + rect.set_height(line_height); + render_text->SetDisplayRect(rect); + canvas_->save(SkCanvas::kClip_SaveFlag); ClipRect(display_rect); render_text->Draw(this); |