1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "minikin/Hyphenator.h"
20 #include "minikin/LineBreaker.h"
21
22 #include "LocaleListCache.h"
23 #include "MinikinInternal.h"
24 #include "UnicodeUtils.h"
25
26 namespace minikin {
27 namespace line_breaker_test_helper {
28
29 class RectangleLineWidth : public LineWidth {
30 public:
RectangleLineWidth(float width)31 RectangleLineWidth(float width) : mWidth(width) {}
~RectangleLineWidth()32 virtual ~RectangleLineWidth() {}
33
getAt(size_t)34 float getAt(size_t) const override { return mWidth; }
getMin()35 float getMin() const override { return mWidth; }
36
37 private:
38 float mWidth;
39 };
40
41 // The run implemenataion for returning the same width for all characters.
42 class ConstantRun : public Run {
43 public:
ConstantRun(const Range & range,const std::string & lang,float width,float ascent,float descent)44 ConstantRun(const Range& range, const std::string& lang, float width, float ascent,
45 float descent)
46 : Run(range),
47 mPaint(nullptr /* font collection */),
48 mWidth(width),
49 mAscent(ascent),
50 mDescent(descent) {
51 mLocaleListId = LocaleListCache::getId(lang);
52 }
53
isRtl()54 virtual bool isRtl() const override { return false; }
canBreak()55 virtual bool canBreak() const override { return true; }
canHyphenate()56 virtual bool canHyphenate() const override { return true; }
getLocaleListId()57 virtual uint32_t getLocaleListId() const { return mLocaleListId; }
58
getMetrics(const U16StringPiece &,std::vector<float> * advances,std::vector<uint8_t> *,LayoutPieces *,bool,LayoutPieces *)59 virtual void getMetrics(const U16StringPiece&, std::vector<float>* advances,
60 std::vector<uint8_t>* /*flags*/, LayoutPieces*,
61 bool /*boundsCalculation*/, LayoutPieces*) const {
62 std::fill(advances->begin() + mRange.getStart(), advances->begin() + mRange.getEnd(),
63 mWidth);
64 }
65
getBounds(const U16StringPiece &,const Range &,const LayoutPieces &)66 virtual std::pair<float, MinikinRect> getBounds(const U16StringPiece& /* text */,
67 const Range& /* range */,
68 const LayoutPieces& /* pieces */) const {
69 return std::make_pair(mWidth, MinikinRect());
70 }
71
getExtent(const U16StringPiece &,const Range &,const LayoutPieces &)72 virtual MinikinExtent getExtent(const U16StringPiece& /* text */, const Range& /* range */,
73 const LayoutPieces& /* pieces */) const override {
74 return {mAscent, mDescent};
75 }
76
getLineMetrics(const U16StringPiece & text,const Range & range,const LayoutPieces & pieces)77 virtual LineMetrics getLineMetrics(const U16StringPiece& text, const Range& range,
78 const LayoutPieces& pieces) const {
79 auto [adv, rect] = getBounds(text, range, pieces);
80 return LineMetrics(getExtent(text, range, pieces), rect, adv);
81 }
82
getPaint()83 virtual const MinikinPaint* getPaint() const { return &mPaint; }
84
measureHyphenPiece(const U16StringPiece &,const Range & range,StartHyphenEdit start,EndHyphenEdit end,LayoutPieces *)85 virtual float measureHyphenPiece(const U16StringPiece&, const Range& range,
86 StartHyphenEdit start, EndHyphenEdit end,
87 LayoutPieces*) const {
88 uint32_t extraCharForHyphen = 0;
89 if (isInsertion(start)) {
90 extraCharForHyphen++;
91 }
92 if (isInsertion(end)) {
93 extraCharForHyphen++;
94 }
95 return mWidth * (range.getLength() + extraCharForHyphen);
96 }
97
appendLayout(const U16StringPiece &,const Range &,const Range &,const LayoutPieces &,const MinikinPaint &,uint32_t,StartHyphenEdit,EndHyphenEdit,Layout *)98 virtual void appendLayout(const U16StringPiece&, const Range&, const Range&,
99 const LayoutPieces&, const MinikinPaint&, uint32_t, StartHyphenEdit,
100 EndHyphenEdit, Layout*) const {}
101
measureText(const U16StringPiece &)102 virtual float measureText(const U16StringPiece&) const { return 0; }
103
lineBreakStyle()104 virtual LineBreakStyle lineBreakStyle() const override { return LineBreakStyle::None; }
105
lineBreakWordStyle()106 virtual LineBreakWordStyle lineBreakWordStyle() const override {
107 return LineBreakWordStyle::None;
108 }
109
110 private:
111 MinikinPaint mPaint;
112 uint32_t mLocaleListId;
113 float mWidth;
114 float mAscent;
115 float mDescent;
116 };
117
118 struct LineBreakExpectation {
119 std::string mLineContent;
120 float mWidth;
121 StartHyphenEdit mStartEdit;
122 EndHyphenEdit mEndEdit;
123 float mAscent;
124 float mDescent;
125 };
126
sameLineBreak(const std::vector<LineBreakExpectation> & expected,const LineBreakResult & actual)127 static bool sameLineBreak(const std::vector<LineBreakExpectation>& expected,
128 const LineBreakResult& actual) {
129 if (expected.size() != actual.breakPoints.size()) {
130 return false;
131 }
132
133 uint32_t breakOffset = 0;
134 for (uint32_t i = 0; i < expected.size(); ++i) {
135 std::vector<uint16_t> u16Str = utf8ToUtf16(expected[i].mLineContent);
136
137 // The expected string contains auto inserted hyphen. Remove it for computing offset.
138 uint32_t lineLength = u16Str.size();
139 if (isInsertion(expected[i].mStartEdit)) {
140 if (u16Str[0] != '-') {
141 return false;
142 }
143 --lineLength;
144 }
145 if (isInsertion(expected[i].mEndEdit)) {
146 if (u16Str.back() != '-') {
147 return false;
148 }
149 --lineLength;
150 }
151 breakOffset += lineLength;
152
153 if (breakOffset != static_cast<uint32_t>(actual.breakPoints[i])) {
154 return false;
155 }
156 if (expected[i].mWidth != actual.widths[i]) {
157 return false;
158 }
159 HyphenEdit edit = static_cast<HyphenEdit>(actual.flags[i] & 0xFF);
160 if (expected[i].mStartEdit != startHyphenEdit(edit)) {
161 return false;
162 }
163 if (expected[i].mEndEdit != endHyphenEdit(edit)) {
164 return false;
165 }
166 if (expected[i].mAscent != actual.ascents[i]) {
167 return false;
168 }
169 if (expected[i].mDescent != actual.descents[i]) {
170 return false;
171 }
172 }
173 return true;
174 }
175
176 // Make debug string.
toString(const std::vector<LineBreakExpectation> & lines)177 static std::string toString(const std::vector<LineBreakExpectation>& lines) {
178 std::string out;
179 for (uint32_t i = 0; i < lines.size(); ++i) {
180 const LineBreakExpectation& line = lines[i];
181
182 char lineMsg[128] = {};
183 snprintf(lineMsg, sizeof(lineMsg),
184 "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Extent(%5.1f, %5.1f), Text: \"%s\"\n",
185 i, line.mWidth, line.mStartEdit, line.mEndEdit, line.mAscent, line.mDescent,
186 line.mLineContent.c_str());
187 out += lineMsg;
188 }
189 return out;
190 }
191
192 // Make debug string.
toString(const U16StringPiece & textBuf,const LineBreakResult & lines)193 static std::string toString(const U16StringPiece& textBuf, const LineBreakResult& lines) {
194 std::string out;
195 for (uint32_t i = 0; i < lines.breakPoints.size(); ++i) {
196 const Range textRange(i == 0 ? 0 : lines.breakPoints[i - 1], lines.breakPoints[i]);
197 const HyphenEdit edit = static_cast<HyphenEdit>(lines.flags[i] & 0xFF);
198
199 const StartHyphenEdit startEdit = startHyphenEdit(edit);
200 const EndHyphenEdit endEdit = endHyphenEdit(edit);
201 std::string hyphenatedStr = utf16ToUtf8(textBuf.substr(textRange));
202
203 if (isInsertion(startEdit)) {
204 hyphenatedStr.insert(0, "-");
205 }
206 if (isInsertion(endEdit)) {
207 hyphenatedStr.push_back('-');
208 }
209 char lineMsg[256] = {};
210 snprintf(lineMsg, sizeof(lineMsg),
211 "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Extent(%5.1f, %5.1f), Bounds(%f, %f, "
212 "%f, %f), Text: \"%s\"\n",
213 i, lines.widths[i], startEdit, endEdit, lines.ascents[i], lines.descents[i],
214 lines.bounds[i].mLeft, lines.bounds[i].mTop, lines.bounds[i].mRight,
215 lines.bounds[i].mBottom, hyphenatedStr.c_str());
216 out += lineMsg;
217 }
218 return out;
219 }
220
221 } // namespace line_breaker_test_helper
222 } // namespace minikin
223