• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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; }
getLocaleListId()56     virtual uint32_t getLocaleListId() const { return mLocaleListId; }
57 
getMetrics(const U16StringPiece &,std::vector<float> * advances,LayoutPieces *,LayoutPieces *)58     virtual void getMetrics(const U16StringPiece&, std::vector<float>* advances, LayoutPieces*,
59                             LayoutPieces*) const {
60         std::fill(advances->begin() + mRange.getStart(), advances->begin() + mRange.getEnd(),
61                   mWidth);
62     }
63 
getBounds(const U16StringPiece &,const Range &,const LayoutPieces &)64     virtual std::pair<float, MinikinRect> getBounds(const U16StringPiece& /* text */,
65                                                     const Range& /* range */,
66                                                     const LayoutPieces& /* pieces */) const {
67         return std::make_pair(mWidth, MinikinRect());
68     }
69 
getExtent(const U16StringPiece &,const Range &,const LayoutPieces &)70     virtual MinikinExtent getExtent(const U16StringPiece& /* text */, const Range& /* range */,
71                                     const LayoutPieces& /* pieces */) const override {
72         return {mAscent, mDescent};
73     }
74 
getPaint()75     virtual const MinikinPaint* getPaint() const { return &mPaint; }
76 
measureHyphenPiece(const U16StringPiece &,const Range & range,StartHyphenEdit start,EndHyphenEdit end,LayoutPieces *)77     virtual float measureHyphenPiece(const U16StringPiece&, const Range& range,
78                                      StartHyphenEdit start, EndHyphenEdit end,
79                                      LayoutPieces*) const {
80         uint32_t extraCharForHyphen = 0;
81         if (isInsertion(start)) {
82             extraCharForHyphen++;
83         }
84         if (isInsertion(end)) {
85             extraCharForHyphen++;
86         }
87         return mWidth * (range.getLength() + extraCharForHyphen);
88     }
89 
appendLayout(const U16StringPiece &,const Range &,const Range &,const LayoutPieces &,const MinikinPaint &,uint32_t,StartHyphenEdit,EndHyphenEdit,Layout *)90     virtual void appendLayout(const U16StringPiece&, const Range&, const Range&,
91                               const LayoutPieces&, const MinikinPaint&, uint32_t, StartHyphenEdit,
92                               EndHyphenEdit, Layout*) const {}
93 
measureText(const U16StringPiece &)94     virtual float measureText(const U16StringPiece&) const { return 0; }
95 
lineBreakStyle()96     virtual LineBreakStyle lineBreakStyle() const override { return LineBreakStyle::None; }
97 
lineBreakWordStyle()98     virtual LineBreakWordStyle lineBreakWordStyle() const override {
99         return LineBreakWordStyle::None;
100     }
101 
102 private:
103     MinikinPaint mPaint;
104     uint32_t mLocaleListId;
105     float mWidth;
106     float mAscent;
107     float mDescent;
108 };
109 
110 struct LineBreakExpectation {
111     std::string mLineContent;
112     float mWidth;
113     StartHyphenEdit mStartEdit;
114     EndHyphenEdit mEndEdit;
115     float mAscent;
116     float mDescent;
117 };
118 
sameLineBreak(const std::vector<LineBreakExpectation> & expected,const LineBreakResult & actual)119 static bool sameLineBreak(const std::vector<LineBreakExpectation>& expected,
120                           const LineBreakResult& actual) {
121     if (expected.size() != actual.breakPoints.size()) {
122         return false;
123     }
124 
125     uint32_t breakOffset = 0;
126     for (uint32_t i = 0; i < expected.size(); ++i) {
127         std::vector<uint16_t> u16Str = utf8ToUtf16(expected[i].mLineContent);
128 
129         // The expected string contains auto inserted hyphen. Remove it for computing offset.
130         uint32_t lineLength = u16Str.size();
131         if (isInsertion(expected[i].mStartEdit)) {
132             if (u16Str[0] != '-') {
133                 return false;
134             }
135             --lineLength;
136         }
137         if (isInsertion(expected[i].mEndEdit)) {
138             if (u16Str.back() != '-') {
139                 return false;
140             }
141             --lineLength;
142         }
143         breakOffset += lineLength;
144 
145         if (breakOffset != static_cast<uint32_t>(actual.breakPoints[i])) {
146             return false;
147         }
148         if (expected[i].mWidth != actual.widths[i]) {
149             return false;
150         }
151         HyphenEdit edit = static_cast<HyphenEdit>(actual.flags[i] & 0xFF);
152         if (expected[i].mStartEdit != startHyphenEdit(edit)) {
153             return false;
154         }
155         if (expected[i].mEndEdit != endHyphenEdit(edit)) {
156             return false;
157         }
158         if (expected[i].mAscent != actual.ascents[i]) {
159             return false;
160         }
161         if (expected[i].mDescent != actual.descents[i]) {
162             return false;
163         }
164     }
165     return true;
166 }
167 
168 // Make debug string.
toString(const std::vector<LineBreakExpectation> & lines)169 static std::string toString(const std::vector<LineBreakExpectation>& lines) {
170     std::string out;
171     for (uint32_t i = 0; i < lines.size(); ++i) {
172         const LineBreakExpectation& line = lines[i];
173 
174         char lineMsg[128] = {};
175         snprintf(lineMsg, sizeof(lineMsg),
176                  "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Extent(%5.1f, %5.1f), Text: \"%s\"\n",
177                  i, line.mWidth, line.mStartEdit, line.mEndEdit, line.mAscent, line.mDescent,
178                  line.mLineContent.c_str());
179         out += lineMsg;
180     }
181     return out;
182 }
183 
184 // Make debug string.
toString(const U16StringPiece & textBuf,const LineBreakResult & lines)185 static std::string toString(const U16StringPiece& textBuf, const LineBreakResult& lines) {
186     std::string out;
187     for (uint32_t i = 0; i < lines.breakPoints.size(); ++i) {
188         const Range textRange(i == 0 ? 0 : lines.breakPoints[i - 1], lines.breakPoints[i]);
189         const HyphenEdit edit = static_cast<HyphenEdit>(lines.flags[i] & 0xFF);
190 
191         const StartHyphenEdit startEdit = startHyphenEdit(edit);
192         const EndHyphenEdit endEdit = endHyphenEdit(edit);
193         std::string hyphenatedStr = utf16ToUtf8(textBuf.substr(textRange));
194 
195         if (isInsertion(startEdit)) {
196             hyphenatedStr.insert(0, "-");
197         }
198         if (isInsertion(endEdit)) {
199             hyphenatedStr.push_back('-');
200         }
201         char lineMsg[128] = {};
202         snprintf(lineMsg, sizeof(lineMsg),
203                  "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Extent(%5.1f, %5.1f), Text: \"%s\"\n",
204                  i, lines.widths[i], startEdit, endEdit, lines.ascents[i], lines.descents[i],
205                  hyphenatedStr.c_str());
206         out += lineMsg;
207     }
208     return out;
209 }
210 
211 }  // namespace line_breaker_test_helper
212 }  // namespace minikin
213