1 /*
2 * Copyright (C) 2017 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 "minikin/MeasuredText.h"
18
19 #include <gtest/gtest.h>
20
21 #include "minikin/LineBreaker.h"
22
23 #include "FontTestUtils.h"
24 #include "UnicodeUtils.h"
25
26 namespace minikin {
27
28 constexpr float CHAR_WIDTH = 10.0; // Mock implementation always returns 10.0 for advance.
29
TEST(MeasuredTextTest,RunTests)30 TEST(MeasuredTextTest, RunTests) {
31 constexpr uint32_t CHAR_COUNT = 6;
32 constexpr float REPLACEMENT_WIDTH = 20.0f;
33 auto font = buildFontCollection("Ascii.ttf");
34
35 MeasuredTextBuilder builder;
36
37 MinikinPaint paint1(font);
38 paint1.size = 10.0f; // make 1em = 10px
39 builder.addStyleRun(0, 2, std::move(paint1), false /* is RTL */);
40 builder.addReplacementRun(2, 4, REPLACEMENT_WIDTH, 0 /* locale list id */);
41 MinikinPaint paint2(font);
42 paint2.size = 10.0f; // make 1em = 10px
43 builder.addStyleRun(4, 6, std::move(paint2), false /* is RTL */);
44
45 std::vector<uint16_t> text(CHAR_COUNT, 'a');
46
47 std::unique_ptr<MeasuredText> measuredText =
48 builder.build(text, true /* compute hyphenation */, false /* compute full layout */);
49
50 ASSERT_TRUE(measuredText);
51
52 // ReplacementRun assigns all width to the first character and leave zeros others.
53 std::vector<float> expectedWidths = {CHAR_WIDTH, CHAR_WIDTH, REPLACEMENT_WIDTH,
54 0, CHAR_WIDTH, CHAR_WIDTH};
55
56 EXPECT_EQ(expectedWidths, measuredText->widths);
57 }
58
59 } // namespace minikin
60