summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/fonts/orientation_iterator_test.cc
blob: 52753c29b4e3e3f24df6e33e616eedc4daa526ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/fonts/orientation_iterator.h"

#include "testing/gtest/include/gtest/gtest.h"
#include <string>

namespace blink {

struct OrientationTestRun {
  std::string text;
  OrientationIterator::RenderOrientation code;
};

struct OrientationExpectedRun {
  unsigned limit;
  OrientationIterator::RenderOrientation render_orientation;

  OrientationExpectedRun(
      unsigned the_limit,
      OrientationIterator::RenderOrientation the_render_orientation)
      : limit(the_limit), render_orientation(the_render_orientation) {}
};

class OrientationIteratorTest : public testing::Test {
 protected:
  void CheckRuns(const Vector<OrientationTestRun>& runs) {
    String text(g_empty_string16_bit);
    Vector<OrientationExpectedRun> expect;
    for (auto& run : runs) {
      text.append(String::FromUTF8(run.text.c_str()));
      expect.push_back(OrientationExpectedRun(text.length(), run.code));
    }
    OrientationIterator orientation_iterator(text.Characters16(), text.length(),
                                             FontOrientation::kVerticalMixed);
    VerifyRuns(&orientation_iterator, expect);
  }

  void VerifyRuns(OrientationIterator* orientation_iterator,
                  const Vector<OrientationExpectedRun>& expect) {
    unsigned limit;
    OrientationIterator::RenderOrientation render_orientation;
    unsigned long run_count = 0;
    while (orientation_iterator->Consume(&limit, &render_orientation)) {
      ASSERT_LT(run_count, expect.size());
      ASSERT_EQ(expect[run_count].limit, limit);
      ASSERT_EQ(expect[run_count].render_orientation, render_orientation);
      ++run_count;
    }
    ASSERT_EQ(expect.size(), run_count);
  }
};

// TODO(esprehn): WTF::Vector should allow initialization from a literal.
#define CHECK_ORIENTATION(...)                                       \
  static const OrientationTestRun kRunsArray[] = __VA_ARGS__;        \
  Vector<OrientationTestRun> runs;                                   \
  runs.Append(kRunsArray, sizeof(kRunsArray) / sizeof(*kRunsArray)); \
  CheckRuns(runs);

TEST_F(OrientationIteratorTest, Empty) {
  String empty(g_empty_string16_bit);
  OrientationIterator orientation_iterator(empty.Characters16(), empty.length(),
                                           FontOrientation::kVerticalMixed);
  unsigned limit = 0;
  OrientationIterator::RenderOrientation orientation =
      OrientationIterator::kOrientationInvalid;
  DCHECK(!orientation_iterator.Consume(&limit, &orientation));
  ASSERT_EQ(limit, 0u);
  ASSERT_EQ(orientation, OrientationIterator::kOrientationInvalid);
}

TEST_F(OrientationIteratorTest, OneCharLatin) {
  CHECK_ORIENTATION({{"A", OrientationIterator::kOrientationRotateSideways}});
}

TEST_F(OrientationIteratorTest, OneAceOfSpades) {
  CHECK_ORIENTATION({{"πŸ‚‘", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, CombiningCircle) {
  CHECK_ORIENTATION({{"β—ŒΜβ—ŒΜ€β—ŒΜˆβ—ŒΜ‚β—ŒΜ„β—ŒΜŠ", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, OneEthiopicSyllable) {
  CHECK_ORIENTATION({{"αŒ€", OrientationIterator::kOrientationRotateSideways}});
}

TEST_F(OrientationIteratorTest, JapaneseLetterlikeEnd) {
  CHECK_ORIENTATION(
      {{"いろは", OrientationIterator::kOrientationKeep},
       {"ℐℒℐℒℐℒℐℒℐℒℐℒℐℒ", OrientationIterator::kOrientationRotateSideways}});
}

TEST_F(OrientationIteratorTest, LetterlikeJapaneseEnd) {
  CHECK_ORIENTATION({{"ℐ", OrientationIterator::kOrientationRotateSideways},
                     {"いろは", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, OneCharJapanese) {
  CHECK_ORIENTATION({{"い", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, Japanese) {
  CHECK_ORIENTATION(
      {{"いろはにほへと", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, IVS) {
  CHECK_ORIENTATION(
      {{"愉\xF3\xA0\x84\x81", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, MarkAtFirstCharRotated) {
  // Unicode General Category M should be combined with the previous base
  // character, but they have their own orientation if they appear at the
  // beginning of a run.
  // http://www.unicode.org/reports/tr50/#grapheme_clusters
  // https://drafts.csswg.org/css-writing-modes-3/#vertical-orientations
  // U+0300 COMBINING GRAVE ACCENT is Mn (Mark, Nonspacing) with Rotated.
  CHECK_ORIENTATION(
      {{"\xCC\x80", OrientationIterator::kOrientationRotateSideways}});
}

TEST_F(OrientationIteratorTest, MarkAtFirstCharUpright) {
  // U+20DD COMBINING ENCLOSING CIRCLE is Me (Mark, Enclosing) with Upright.
  CHECK_ORIENTATION({{"\xE2\x83\x9D", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, MarksAtFirstCharUpright) {
  // U+20DD COMBINING ENCLOSING CIRCLE is Me (Mark, Enclosing) with Upright.
  // U+0300 COMBINING GRAVE ACCENT is Mn (Mark, Nonspacing) with Rotated.
  CHECK_ORIENTATION(
      {{"\xE2\x83\x9D\xCC\x80", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, MarksAtFirstCharUprightThenBase) {
  // U+20DD COMBINING ENCLOSING CIRCLE is Me (Mark, Enclosing) with Upright.
  // U+0300 COMBINING GRAVE ACCENT is Mn (Mark, Nonspacing) with Rotated.
  CHECK_ORIENTATION(
      {{"\xE2\x83\x9D\xCC\x80", OrientationIterator::kOrientationKeep},
       {"ABC\xE2\x83\x9D", OrientationIterator::kOrientationRotateSideways}});
}

TEST_F(OrientationIteratorTest, JapaneseLatinMixedInside) {
  CHECK_ORIENTATION({{"いろはに", OrientationIterator::kOrientationKeep},
                     {"Abc", OrientationIterator::kOrientationRotateSideways},
                     {"ほへと", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, PunctuationJapanese) {
  CHECK_ORIENTATION({{".…‘", OrientationIterator::kOrientationRotateSideways},
                     {"ほへと", OrientationIterator::kOrientationKeep}});
}

TEST_F(OrientationIteratorTest, JapaneseLatinMixedOutside) {
  CHECK_ORIENTATION({{"Abc", OrientationIterator::kOrientationRotateSideways},
                     {"ほへと", OrientationIterator::kOrientationKeep},
                     {"Xyz", OrientationIterator::kOrientationRotateSideways}});
}

TEST_F(OrientationIteratorTest, JapaneseMahjonggMixed) {
  CHECK_ORIENTATION(
      {{"γ„γ‚γ―γ«πŸ€€γ»γΈγ¨", OrientationIterator::kOrientationKeep}});
}

}  // namespace blink