Home | History | Annotate | Download | only in unittest
      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:
     31     RectangleLineWidth(float width) : mWidth(width) {}
     32     virtual ~RectangleLineWidth() {}
     33 
     34     float getAt(size_t) const override { return mWidth; }
     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:
     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 
     54     virtual bool isRtl() const override { return false; }
     55     virtual bool canBreak() const override { return true; }
     56     virtual uint32_t getLocaleListId() const { return mLocaleListId; }
     57 
     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 
     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 
     70     virtual MinikinExtent getExtent(const U16StringPiece& /* text */, const Range& /* range */,
     71                                     const LayoutPieces& /* pieces */) const override {
     72         return {mAscent, mDescent};
     73     }
     74 
     75     virtual const MinikinPaint* getPaint() const { return &mPaint; }
     76 
     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 
     90     virtual void appendLayout(const U16StringPiece&, const Range&, const Range&,
     91                               const LayoutPieces&, const MinikinPaint&, uint32_t, StartHyphenEdit,
     92                               EndHyphenEdit, Layout*) const {}
     93 
     94 private:
     95     MinikinPaint mPaint;
     96     uint32_t mLocaleListId;
     97     float mWidth;
     98     float mAscent;
     99     float mDescent;
    100 };
    101 
    102 struct LineBreakExpectation {
    103     std::string mLineContent;
    104     float mWidth;
    105     StartHyphenEdit mStartEdit;
    106     EndHyphenEdit mEndEdit;
    107     float mAscent;
    108     float mDescent;
    109 };
    110 
    111 static bool sameLineBreak(const std::vector<LineBreakExpectation>& expected,
    112                           const LineBreakResult& actual) {
    113     if (expected.size() != actual.breakPoints.size()) {
    114         return false;
    115     }
    116 
    117     uint32_t breakOffset = 0;
    118     for (uint32_t i = 0; i < expected.size(); ++i) {
    119         std::vector<uint16_t> u16Str = utf8ToUtf16(expected[i].mLineContent);
    120 
    121         // The expected string contains auto inserted hyphen. Remove it for computing offset.
    122         uint32_t lineLength = u16Str.size();
    123         if (isInsertion(expected[i].mStartEdit)) {
    124             if (u16Str[0] != '-') {
    125                 return false;
    126             }
    127             --lineLength;
    128         }
    129         if (isInsertion(expected[i].mEndEdit)) {
    130             if (u16Str.back() != '-') {
    131                 return false;
    132             }
    133             --lineLength;
    134         }
    135         breakOffset += lineLength;
    136 
    137         if (breakOffset != static_cast<uint32_t>(actual.breakPoints[i])) {
    138             return false;
    139         }
    140         if (expected[i].mWidth != actual.widths[i]) {
    141             return false;
    142         }
    143         HyphenEdit edit = static_cast<HyphenEdit>(actual.flags[i] & 0xFF);
    144         if (expected[i].mStartEdit != startHyphenEdit(edit)) {
    145             return false;
    146         }
    147         if (expected[i].mEndEdit != endHyphenEdit(edit)) {
    148             return false;
    149         }
    150         if (expected[i].mAscent != actual.ascents[i]) {
    151             return false;
    152         }
    153         if (expected[i].mDescent != actual.descents[i]) {
    154             return false;
    155         }
    156     }
    157     return true;
    158 }
    159 
    160 // Make debug string.
    161 static std::string toString(const std::vector<LineBreakExpectation>& lines) {
    162     std::string out;
    163     for (uint32_t i = 0; i < lines.size(); ++i) {
    164         const LineBreakExpectation& line = lines[i];
    165 
    166         char lineMsg[128] = {};
    167         snprintf(lineMsg, sizeof(lineMsg),
    168                  "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Extent(%5.1f, %5.1f), Text: \"%s\"\n",
    169                  i, line.mWidth, line.mStartEdit, line.mEndEdit, line.mAscent, line.mDescent,
    170                  line.mLineContent.c_str());
    171         out += lineMsg;
    172     }
    173     return out;
    174 }
    175 
    176 // Make debug string.
    177 static std::string toString(const U16StringPiece& textBuf, const LineBreakResult& lines) {
    178     std::string out;
    179     for (uint32_t i = 0; i < lines.breakPoints.size(); ++i) {
    180         const Range textRange(i == 0 ? 0 : lines.breakPoints[i - 1], lines.breakPoints[i]);
    181         const HyphenEdit edit = static_cast<HyphenEdit>(lines.flags[i] & 0xFF);
    182 
    183         const StartHyphenEdit startEdit = startHyphenEdit(edit);
    184         const EndHyphenEdit endEdit = endHyphenEdit(edit);
    185         std::string hyphenatedStr = utf16ToUtf8(textBuf.substr(textRange));
    186 
    187         if (isInsertion(startEdit)) {
    188             hyphenatedStr.insert(0, "-");
    189         }
    190         if (isInsertion(endEdit)) {
    191             hyphenatedStr.push_back('-');
    192         }
    193         char lineMsg[128] = {};
    194         snprintf(lineMsg, sizeof(lineMsg),
    195                  "Line %2d, Width: %5.1f, Hyphen(%hhu, %hhu), Extent(%5.1f, %5.1f), Text: \"%s\"\n",
    196                  i, lines.widths[i], startEdit, endEdit, lines.ascents[i], lines.descents[i],
    197                  hyphenatedStr.c_str());
    198         out += lineMsg;
    199     }
    200     return out;
    201 }
    202 
    203 }  // namespace line_breaker_test_helper
    204 }  // namespace minikin
    205