blob: db2be20936fb2ab358a754e6d2fde9fe359287ee [file] [log] [blame]
Seigo Nonakacd348c62023-09-12 16:05:44 +09001/*
2 * Copyright (C) 2023 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 <fcntl.h>
18#include <flag_macros.h>
19#include <gtest/gtest.h>
20#include <sys/mman.h>
21#include <sys/stat.h>
22#include <utils/Log.h>
23
24#include "SkAlphaType.h"
25#include "SkBitmap.h"
26#include "SkData.h"
27#include "SkFontMgr.h"
28#include "SkImageInfo.h"
29#include "SkRefCnt.h"
30#include "SkStream.h"
31#include "SkTypeface.h"
32#include "SkiaCanvas.h"
33#include "hwui/Bitmap.h"
34#include "hwui/DrawTextFunctor.h"
35#include "hwui/MinikinSkia.h"
36#include "hwui/MinikinUtils.h"
37#include "hwui/Paint.h"
38#include "hwui/Typeface.h"
39
40using namespace android;
41
42namespace {
43
44constexpr char kRobotoVariable[] = "/system/fonts/Roboto-Regular.ttf";
45constexpr char kJPFont[] = "/system/fonts/NotoSansCJK-Regular.ttc";
46
47// The underline position and thickness are cames from post table.
48constexpr float ROBOTO_POSITION_EM = 150.0 / 2048.0;
49constexpr float ROBOTO_THICKNESS_EM = 100.0 / 2048.0;
50constexpr float NOTO_CJK_POSITION_EM = 125.0 / 1000.0;
51constexpr float NOTO_CJK_THICKNESS_EM = 50.0 / 1000.0;
52
53void unmap(const void* ptr, void* context) {
54 void* p = const_cast<void*>(ptr);
55 size_t len = reinterpret_cast<size_t>(context);
56 munmap(p, len);
57}
58
59// Create a font family from a single font file.
60std::shared_ptr<minikin::FontFamily> buildFamily(const char* fileName) {
61 int fd = open(fileName, O_RDONLY);
62 LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", fileName);
63 struct stat st = {};
64 LOG_ALWAYS_FATAL_IF(fstat(fd, &st) == -1, "Failed to stat file %s", fileName);
65 void* data = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
66 sk_sp<SkData> skData =
67 SkData::MakeWithProc(data, st.st_size, unmap, reinterpret_cast<void*>(st.st_size));
68 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(skData));
69 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
70 sk_sp<SkTypeface> typeface(fm->makeFromStream(std::move(fontData)));
71 LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", fileName);
72 std::shared_ptr<minikin::MinikinFont> font =
73 std::make_shared<MinikinFontSkia>(std::move(typeface), 0, data, st.st_size, fileName, 0,
74 std::vector<minikin::FontVariation>());
75 std::vector<std::shared_ptr<minikin::Font>> fonts;
76 fonts.push_back(minikin::Font::Builder(font).build());
77 return minikin::FontFamily::create(std::move(fonts));
78}
79
80// Create a typeface from roboto and NotoCJK.
81Typeface* makeTypeface() {
82 return Typeface::createFromFamilies(
83 std::vector<std::shared_ptr<minikin::FontFamily>>(
84 {buildFamily(kRobotoVariable), buildFamily(kJPFont)}),
85 RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE, nullptr /* fallback */);
86}
87
88// Execute a text layout.
89minikin::Layout doLayout(const std::vector<uint16_t> text, Paint paint, Typeface* typeface) {
90 return MinikinUtils::doLayout(&paint, minikin::Bidi::LTR, typeface, text.data(), text.size(),
91 0 /* start */, text.size(), 0, text.size(), nullptr);
92}
93
94DrawTextFunctor processFunctor(const std::vector<uint16_t>& text, Paint* paint) {
95 // Create canvas
96 SkImageInfo info = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType);
97 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
98 SkBitmap skBitmap;
99 bitmap->getSkBitmap(&skBitmap);
100 SkiaCanvas canvas(skBitmap);
101
102 // Create minikin::Layout
103 std::unique_ptr<Typeface> typeface(makeTypeface());
104 minikin::Layout layout = doLayout(text, *paint, typeface.get());
105
106 DrawTextFunctor f(layout, &canvas, *paint, 0, 0, layout.getAdvance());
107 MinikinUtils::forFontRun(layout, paint, f);
108 return f;
109}
110
111TEST_WITH_FLAGS(UnderlineTest, Roboto,
112 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::text::flags,
113 fix_double_underline))) {
114 float textSize = 100;
115 Paint paint;
116 paint.getSkFont().setSize(textSize);
117 paint.setUnderline(true);
118 // the text is "abc"
119 DrawTextFunctor functor = processFunctor({0x0061, 0x0062, 0x0063}, &paint);
120
121 EXPECT_EQ(ROBOTO_POSITION_EM * textSize, functor.getUnderlinePosition());
122 EXPECT_EQ(ROBOTO_THICKNESS_EM * textSize, functor.getUnderlineThickness());
123}
124
125TEST_WITH_FLAGS(UnderlineTest, NotoCJK,
126 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::text::flags,
127 fix_double_underline))) {
128 float textSize = 100;
129 Paint paint;
130 paint.getSkFont().setSize(textSize);
131 paint.setUnderline(true);
132 // The text is 恂恄恆 in Japanese
133 DrawTextFunctor functor = processFunctor({0x3042, 0x3044, 0x3046}, &paint);
134
135 EXPECT_EQ(NOTO_CJK_POSITION_EM * textSize, functor.getUnderlinePosition());
136 EXPECT_EQ(NOTO_CJK_THICKNESS_EM * textSize, functor.getUnderlineThickness());
137}
138
139TEST_WITH_FLAGS(UnderlineTest, Mixture,
140 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::text::flags,
141 fix_double_underline))) {
142 float textSize = 100;
143 Paint paint;
144 paint.getSkFont().setSize(textSize);
145 paint.setUnderline(true);
146 // The text is a恄c. The only middle of the character is Japanese.
147 DrawTextFunctor functor = processFunctor({0x0061, 0x3044, 0x0063}, &paint);
148
149 // We use the bottom, thicker line as underline. Here, use Noto's one.
150 EXPECT_EQ(NOTO_CJK_POSITION_EM * textSize, functor.getUnderlinePosition());
151 EXPECT_EQ(NOTO_CJK_THICKNESS_EM * textSize, functor.getUnderlineThickness());
152}
153} // namespace