• 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; }
getLeftPaddingAt(size_t)36     float getLeftPaddingAt(size_t) const override { return 0; }
getRightPaddingAt(size_t)37     float getRightPaddingAt(size_t) const override { return 0; }
38 
39 private:
40     float mWidth;
41 };
42 
43 // The run implemenataion for returning the same width for all characters.
44 class ConstantRun : public Run {
45 public:
ConstantRun(const Range & range,const std::string & lang,float width)46     ConstantRun(const Range& range, const std::string& lang, float width)
47             : Run(range), mPaint(nullptr /* font collection */), mWidth(width) {
48         mLocaleListId = LocaleListCache::getId(lang);
49     }
50 
isRtl()51     virtual bool isRtl() const override { return false; }
canHyphenate()52     virtual bool canHyphenate() const override { return true; }
getLocaleListId()53     virtual uint32_t getLocaleListId() const { return mLocaleListId; }
54 
getMetrics(const U16StringPiece &,float * advances,MinikinExtent *,LayoutPieces *)55     virtual void getMetrics(const U16StringPiece&, float* advances, MinikinExtent*,
56                             LayoutPieces*) const {
57         std::fill(advances, advances + mRange.getLength(), mWidth);
58     }
59 
getBounds(const U16StringPiece &,const Range &,const LayoutPieces &)60     virtual std::pair<float, MinikinRect> getBounds(const U16StringPiece& /* text */,
61                                                     const Range& /* range */,
62                                                     const LayoutPieces& /* pieces */) const {
63         return std::make_pair(mWidth, MinikinRect());
64     }
65 
getPaint()66     virtual const MinikinPaint* getPaint() const { return &mPaint; }
67 
measureHyphenPiece(const U16StringPiece &,const Range & range,StartHyphenEdit start,EndHyphenEdit end,float *,LayoutPieces *)68     virtual float measureHyphenPiece(const U16StringPiece&, const Range& range,
69                                      StartHyphenEdit start, EndHyphenEdit end, float*,
70                                      LayoutPieces*) const {
71         uint32_t extraCharForHyphen = 0;
72         if (isInsertion(start)) {
73             extraCharForHyphen++;
74         }
75         if (isInsertion(end)) {
76             extraCharForHyphen++;
77         }
78         return mWidth * (range.getLength() + extraCharForHyphen);
79     }
80 
81 private:
82     MinikinPaint mPaint;
83     uint32_t mLocaleListId;
84     float mWidth;
85 };
86 
87 struct LineBreakExpectation {
LineBreakExpectationLineBreakExpectation88     LineBreakExpectation(const std::string& lineContent, float width, StartHyphenEdit startEdit,
89                          EndHyphenEdit endEdit)
90             : mLineContent(lineContent), mWidth(width), mStartEdit(startEdit), mEndEdit(endEdit){};
91 
92     std::string mLineContent;
93     float mWidth;
94     StartHyphenEdit mStartEdit;
95     EndHyphenEdit mEndEdit;
96 };
97 
sameLineBreak(const std::vector<LineBreakExpectation> & expected,const LineBreakResult & actual)98 static bool sameLineBreak(const std::vector<LineBreakExpectation>& expected,
99                           const LineBreakResult& actual) {
100     if (expected.size() != actual.breakPoints.size()) {
101         return false;
102     }
103 
104     uint32_t breakOffset = 0;
105     for (uint32_t i = 0; i < expected.size(); ++i) {
106         std::vector<uint16_t> u16Str = utf8ToUtf16(expected[i].mLineContent);
107 
108         // The expected string contains auto inserted hyphen. Remove it for computing offset.
109         uint32_t lineLength = u16Str.size();
110         if (isInsertion(expected[i].mStartEdit)) {
111             if (u16Str[0] != '-') {
112                 return false;
113             }
114             --lineLength;
115         }
116         if (isInsertion(expected[i].mEndEdit)) {
117             if (u16Str.back() != '-') {
118                 return false;
119             }
120             --lineLength;
121         }
122         breakOffset += lineLength;
123 
124         if (breakOffset != static_cast<uint32_t>(actual.breakPoints[i])) {
125             return false;
126         }
127         if (expected[i].mWidth != actual.widths[i]) {
128             return false;
129         }
130         HyphenEdit edit = static_cast<HyphenEdit>(actual.flags[i] & 0xFF);
131         if (expected[i].mStartEdit != startHyphenEdit(edit)) {
132             return false;
133         }
134         if (expected[i].mEndEdit != endHyphenEdit(edit)) {
135             return false;
136         }
137     }
138     return true;
139 }
140 
141 // Make debug string.
toString(const std::vector<LineBreakExpectation> & lines)142 static std::string toString(const std::vector<LineBreakExpectation>& lines) {
143     std::string out;
144     for (uint32_t i = 0; i < lines.size(); ++i) {
145         const LineBreakExpectation& line = lines[i];
146 
147         char lineMsg[128] = {};
148         snprintf(lineMsg, sizeof(lineMsg),
149                  "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Text: \"%s\"\n", i, line.mWidth,
150                  line.mStartEdit, line.mEndEdit, line.mLineContent.c_str());
151         out += lineMsg;
152     }
153     return out;
154 }
155 
156 // Make debug string.
toString(const U16StringPiece & textBuf,const LineBreakResult & lines)157 static std::string toString(const U16StringPiece& textBuf, const LineBreakResult& lines) {
158     std::string out;
159     for (uint32_t i = 0; i < lines.breakPoints.size(); ++i) {
160         const Range textRange(i == 0 ? 0 : lines.breakPoints[i - 1], lines.breakPoints[i]);
161         const HyphenEdit edit = static_cast<HyphenEdit>(lines.flags[i] & 0xFF);
162 
163         const StartHyphenEdit startEdit = startHyphenEdit(edit);
164         const EndHyphenEdit endEdit = endHyphenEdit(edit);
165         std::string hyphenatedStr = utf16ToUtf8(textBuf.substr(textRange));
166 
167         if (isInsertion(startEdit)) {
168             hyphenatedStr.insert(0, "-");
169         }
170         if (isInsertion(endEdit)) {
171             hyphenatedStr.push_back('-');
172         }
173         char lineMsg[128] = {};
174         snprintf(lineMsg, sizeof(lineMsg),
175                  "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Text: \"%s\"\n", i, lines.widths[i],
176                  startEdit, endEdit, hyphenatedStr.c_str());
177         out += lineMsg;
178     }
179     return out;
180 }
181 
182 }  // namespace line_breaker_test_helper
183 }  // namespace minikin
184