summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-04 14:50:00 +0000
committerasvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-04 14:50:00 +0000
commitf9905327d7243185ccde9f06a8a1f3e19c184b9e (patch)
tree35a3a7af39b4ada6da84d5f023799e91374b6d2b
parent1272b1e6790224afe68736fd7528caf0a9a921d9 (diff)
downloadchromium_src-f9905327d7243185ccde9f06a8a1f3e19c184b9e.zip
chromium_src-f9905327d7243185ccde9f06a8a1f3e19c184b9e.tar.gz
chromium_src-f9905327d7243185ccde9f06a8a1f3e19c184b9e.tar.bz2
Re-land: Use a common font baseline in RenderText classes.
This ensures the text in different runs gets aligned along a common bottom baseline and that the correct font's baseline gets used in case of font fallback. Original CL: http://codereview.chromium.org/9968040/ BUG=105550 TEST=none TBR=msw Review URL: https://chromiumcodereview.appspot.com/9982001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130618 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--ui/gfx/render_text.cc6
-rw-r--r--ui/gfx/render_text.h7
-rw-r--r--ui/gfx/render_text_linux.cc5
-rw-r--r--ui/gfx/render_text_unittest.cc12
-rw-r--r--ui/gfx/render_text_win.cc8
-rw-r--r--ui/gfx/render_text_win.h4
6 files changed, 27 insertions, 15 deletions
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index aad57f4..7af7690 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -732,16 +732,14 @@ Point RenderText::GetAlignmentOffset() {
return Point();
}
-Point RenderText::GetOriginForSkiaDrawing() {
+Point RenderText::GetOriginForDrawing() {
Point origin(GetTextOrigin());
- // TODO(msw): Establish a vertical baseline for strings of mixed font heights.
+ // TODO(asvitkine): Use GetStringSize().height() here instead.
const Font& font = GetFont();
int height = font.GetHeight();
DCHECK_LE(height, display_rect().height());
// Center the text vertically in the display area.
origin.Offset(0, (display_rect().height() - height) / 2);
- // Offset to account for Skia expecting y to be the baseline.
- origin.Offset(0, font.GetBaseline());
return origin;
}
diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h
index 9573494..1e32489 100644
--- a/ui/gfx/render_text.h
+++ b/ui/gfx/render_text.h
@@ -332,8 +332,9 @@ class UI_EXPORT RenderText {
// Returns display offset based on current text alignment.
Point GetAlignmentOffset();
- // Returns the origin point for drawing text via Skia.
- Point GetOriginForSkiaDrawing();
+ // Returns the origin point for drawing text. Does not account for font
+ // baseline, as needed by Skia.
+ Point GetOriginForDrawing();
// Applies fade effects to |renderer|.
void ApplyFadeEffects(internal::SkiaTextRenderer* renderer);
@@ -354,7 +355,7 @@ class UI_EXPORT RenderText {
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, PasswordCensorship);
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, GraphemePositions);
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, EdgeSelectionModels);
- FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForSkiaDrawing);
+ FRIEND_TEST_ALL_PREFIXES(RenderTextTest, OriginForDrawing);
// Set the cursor to |position|, with the caret trailing the previous
// grapheme, or if there is no previous grapheme, leading the cursor position.
diff --git a/ui/gfx/render_text_linux.cc b/ui/gfx/render_text_linux.cc
index f5c3a9b..e411cd9 100644
--- a/ui/gfx/render_text_linux.cc
+++ b/ui/gfx/render_text_linux.cc
@@ -359,7 +359,10 @@ void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) {
void RenderTextLinux::DrawVisualText(Canvas* canvas) {
DCHECK(layout_);
- Point offset(GetOriginForSkiaDrawing());
+ Point offset(GetOriginForDrawing());
+ // Skia will draw glyphs with respect to the baseline.
+ offset.Offset(0, PANGO_PIXELS(pango_layout_get_baseline(layout_)));
+
SkScalar x = SkIntToScalar(offset.x());
SkScalar y = SkIntToScalar(offset.y());
diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc
index 88ca60f..5769fe1 100644
--- a/ui/gfx/render_text_unittest.cc
+++ b/ui/gfx/render_text_unittest.cc
@@ -993,27 +993,27 @@ TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
}
-TEST_F(RenderTextTest, OriginForSkiaDrawing) {
+TEST_F(RenderTextTest, OriginForDrawing) {
scoped_ptr<RenderText> render_text(RenderText::CreateRenderText());
render_text->SetText(ASCIIToUTF16("abcdefg"));
render_text->SetFontList(FontList("Arial, 13px"));
// Set display area's height equals to font height.
- int font_height = render_text->GetFont().GetHeight();
+ const int font_height = render_text->GetStringSize().height();
Rect display_rect(0, 0, 100, font_height);
render_text->SetDisplayRect(display_rect);
- Point origin = render_text->GetOriginForSkiaDrawing();
+ Point origin = render_text->GetOriginForDrawing();
EXPECT_EQ(origin.x(), 0);
- EXPECT_EQ(origin.y(), render_text->GetFont().GetBaseline());
+ EXPECT_EQ(origin.y(), 0);
// Set display area's height greater than font height.
display_rect = Rect(0, 0, 100, font_height + 2);
render_text->SetDisplayRect(display_rect);
- origin = render_text->GetOriginForSkiaDrawing();
+ origin = render_text->GetOriginForDrawing();
EXPECT_EQ(origin.x(), 0);
- EXPECT_EQ(origin.y(), render_text->GetFont().GetBaseline() + 1);
+ EXPECT_EQ(origin.y(), 1);
}
TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
diff --git a/ui/gfx/render_text_win.cc b/ui/gfx/render_text_win.cc
index 58c4d13..cee9738 100644
--- a/ui/gfx/render_text_win.cc
+++ b/ui/gfx/render_text_win.cc
@@ -179,6 +179,7 @@ std::map<std::string, std::vector<Font> > RenderTextWin::cached_linked_fonts_;
RenderTextWin::RenderTextWin()
: RenderText(),
+ common_baseline_(0),
needs_layout_(false) {
memset(&script_control_, 0, sizeof(script_control_));
memset(&script_state_, 0, sizeof(script_state_));
@@ -464,7 +465,10 @@ void RenderTextWin::EnsureLayout() {
void RenderTextWin::DrawVisualText(Canvas* canvas) {
DCHECK(!needs_layout_);
- Point offset(GetOriginForSkiaDrawing());
+ Point offset(GetOriginForDrawing());
+ // Skia will draw glyphs with respect to the baseline.
+ offset.Offset(0, common_baseline_);
+
SkScalar x = SkIntToScalar(offset.x());
SkScalar y = SkIntToScalar(offset.y());
@@ -518,6 +522,7 @@ void RenderTextWin::ItemizeLogicalText() {
STLDeleteContainerPointers(runs_.begin(), runs_.end());
runs_.clear();
string_size_ = Size(0, GetFont().GetHeight());
+ common_baseline_ = 0;
if (text().empty())
return;
@@ -684,6 +689,7 @@ void RenderTextWin::LayoutVisualText() {
DCHECK(SUCCEEDED(hr));
string_size_.set_height(std::max(string_size_.height(),
run->font.GetHeight()));
+ common_baseline_ = std::max(common_baseline_, run->font.GetBaseline());
if (run->glyph_count > 0) {
run->advance_widths.reset(new int[run->glyph_count]);
diff --git a/ui/gfx/render_text_win.h b/ui/gfx/render_text_win.h
index d85e9ef..d79c877 100644
--- a/ui/gfx/render_text_win.h
+++ b/ui/gfx/render_text_win.h
@@ -121,6 +121,10 @@ class RenderTextWin : public RenderText {
std::vector<internal::TextRun*> runs_;
Size string_size_;
+ // A common vertical baseline for all the text runs. This is computed as the
+ // largest baseline over all the runs' fonts.
+ int common_baseline_;
+
scoped_array<int> visual_to_logical_;
scoped_array<int> logical_to_visual_;