Commit 403e5606 authored by oshima's avatar oshima Committed by Commit bot

Fix alignment format in multiline mode

BUG=461182
TEST=covered by RenderTextTest.Multiline_HorizontalAlignemnt

Review URL: https://codereview.chromium.org/915383003

Cr-Commit-Position: refs/heads/master@{#317750}
parent a5b3c8ad
......@@ -1092,14 +1092,14 @@ HorizontalAlignment RenderText::GetCurrentHorizontalAlignment() {
}
Vector2d RenderText::GetAlignmentOffset(size_t line_number) {
// TODO(ckocagil): Enable |lines_| usage in other platforms.
#if defined(OS_WIN)
// TODO(ckocagil): Enable |lines_| usage on RenderTextMac.
#if !defined(OS_MACOSX)
DCHECK_LT(line_number, lines_.size());
#endif
Vector2d offset;
HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment();
if (horizontal_alignment != ALIGN_LEFT) {
#if defined(OS_WIN)
#if !defined(OS_MACOSX)
const int width = std::ceil(lines_[line_number].size.width()) +
(cursor_enabled_ ? 1 : 0);
#else
......
......@@ -162,6 +162,7 @@ class GFX_EXPORT RenderTextHarfBuzz : public RenderText {
private:
friend class RenderTextTest;
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_HorizontalAlignment);
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, Multiline_NormalWidth);
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_RunDirection);
FRIEND_TEST_ALL_PREFIXES(RenderTextTest, HarfBuzz_HorizontalPositions);
......
......@@ -9,6 +9,7 @@
#include "base/format_macros.h"
#include "base/i18n/break_iterator.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
......@@ -2143,6 +2144,57 @@ TEST_F(RenderTextTest, Multiline_NewlineCharacterReplacement) {
}
}
#if !defined(OS_MACOSX)
// Ensure horizontal alignment works in multiline mode.
TEST_F(RenderTextTest, Multiline_HorizontalAlignment) {
const struct {
const wchar_t* const text;
const gfx::HorizontalAlignment alignment;
} kTestStrings[] = {
{ L"abcdefghij\nhijkl", gfx::ALIGN_LEFT },
{ L"nhijkl\nabcdefghij", gfx::ALIGN_LEFT },
// hebrew, 2nd line shorter
{ L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
gfx::ALIGN_RIGHT },
// hebrew, 2nd line longer
{ L"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
gfx::ALIGN_RIGHT },
// arabic, 2nd line shorter
{ L"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
gfx::ALIGN_RIGHT },
// arabic, 2nd line longer
{ L"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
gfx::ALIGN_RIGHT },
};
const int kGlyphSize = 5;
RenderTextHarfBuzz render_text;
render_text.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
render_text.set_glyph_width_for_test(kGlyphSize);
render_text.SetDisplayRect(Rect(100, 1000));
render_text.SetMultiline(true);
Canvas canvas;
for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "] %ls", i,
kTestStrings[i].text));
render_text.SetText(WideToUTF16(kTestStrings[i].text));
render_text.Draw(&canvas);
ASSERT_LE(2u, render_text.lines().size());
if (kTestStrings[i].alignment == gfx::ALIGN_LEFT) {
EXPECT_EQ(0, render_text.GetAlignmentOffset(0).x());
EXPECT_EQ(0, render_text.GetAlignmentOffset(1).x());
} else {
std::vector<base::string16> lines;
base::SplitString(base::WideToUTF16(kTestStrings[i].text), '\n', &lines);
ASSERT_EQ(2u, lines.size());
int difference = (lines[0].length() - lines[1].length()) * kGlyphSize;
EXPECT_EQ(render_text.GetAlignmentOffset(0).x() + difference,
render_text.GetAlignmentOffset(1).x());
}
}
}
#endif
TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) {
const wchar_t* kTestStrings[] = {
L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment