blob: ecb06d8ca4dbc1e3acf4229bdd740e4c0f769727 [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"
Kevin Lubick04931012023-11-15 19:17:43 +000039#include "utils/TypefaceUtils.h"
Seigo Nonakacd348c62023-09-12 16:05:44 +090040
41using namespace android;
42
43namespace {
44
45constexpr char kRobotoVariable[] = "/system/fonts/Roboto-Regular.ttf";
46constexpr char kJPFont[] = "/system/fonts/NotoSansCJK-Regular.ttc";
47
48// The underline position and thickness are cames from post table.
49constexpr float ROBOTO_POSITION_EM = 150.0 / 2048.0;
50constexpr float ROBOTO_THICKNESS_EM = 100.0 / 2048.0;
51constexpr float NOTO_CJK_POSITION_EM = 125.0 / 1000.0;
52constexpr float NOTO_CJK_THICKNESS_EM = 50.0 / 1000.0;
53
54void unmap(const void* ptr, void* context) {
55 void* p = const_cast<void*>(ptr);
56 size_t len = reinterpret_cast<size_t>(context);
57 munmap(p, len);
58}
59
60// Create a font family from a single font file.
61std::shared_ptr<minikin::FontFamily> buildFamily(const char* fileName) {
62 int fd = open(fileName, O_RDONLY);
63 LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", fileName);
64 struct stat st = {};
65 LOG_ALWAYS_FATAL_IF(fstat(fd, &st) == -1, "Failed to stat file %s", fileName);
66 void* data = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
67 sk_sp<SkData> skData =
68 SkData::MakeWithProc(data, st.st_size, unmap, reinterpret_cast<void*>(st.st_size));
69 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(skData));
Kevin Lubick04931012023-11-15 19:17:43 +000070 sk_sp<SkFontMgr> fm = android::FreeTypeFontMgr();
Seigo Nonakacd348c62023-09-12 16:05:44 +090071 sk_sp<SkTypeface> typeface(fm->makeFromStream(std::move(fontData)));
72 LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", fileName);
73 std::shared_ptr<minikin::MinikinFont> font =
74 std::make_shared<MinikinFontSkia>(std::move(typeface), 0, data, st.st_size, fileName, 0,
75 std::vector<minikin::FontVariation>());
76 std::vector<std::shared_ptr<minikin::Font>> fonts;
77 fonts.push_back(minikin::Font::Builder(font).build());
78 return minikin::FontFamily::create(std::move(fonts));
79}
80
81// Create a typeface from roboto and NotoCJK.
82Typeface* makeTypeface() {
83 return Typeface::createFromFamilies(
84 std::vector<std::shared_ptr<minikin::FontFamily>>(
85 {buildFamily(kRobotoVariable), buildFamily(kJPFont)}),
86 RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE, nullptr /* fallback */);
87}
88
89// Execute a text layout.
90minikin::Layout doLayout(const std::vector<uint16_t> text, Paint paint, Typeface* typeface) {
91 return MinikinUtils::doLayout(&paint, minikin::Bidi::LTR, typeface, text.data(), text.size(),
92 0 /* start */, text.size(), 0, text.size(), nullptr);
93}
94
95DrawTextFunctor processFunctor(const std::vector<uint16_t>& text, Paint* paint) {
96 // Create canvas
97 SkImageInfo info = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType);
98 sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info);
99 SkBitmap skBitmap;
100 bitmap->getSkBitmap(&skBitmap);
101 SkiaCanvas canvas(skBitmap);
102
103 // Create minikin::Layout
104 std::unique_ptr<Typeface> typeface(makeTypeface());
105 minikin::Layout layout = doLayout(text, *paint, typeface.get());
106
Tyler Freeman73566bb2024-02-07 22:12:56 +0000107 DrawTextFunctor f(layout, &canvas, *paint, 0, 0, layout.getAdvance());
Seigo Nonakacd348c62023-09-12 16:05:44 +0900108 MinikinUtils::forFontRun(layout, paint, f);
109 return f;
110}
111
Seigo Nonakab60dc9a2024-09-05 11:45:17 +0900112TEST(UnderlineTest, Roboto) {
Seigo Nonakacd348c62023-09-12 16:05:44 +0900113 float textSize = 100;
114 Paint paint;
115 paint.getSkFont().setSize(textSize);
116 paint.setUnderline(true);
117 // the text is "abc"
118 DrawTextFunctor functor = processFunctor({0x0061, 0x0062, 0x0063}, &paint);
119
120 EXPECT_EQ(ROBOTO_POSITION_EM * textSize, functor.getUnderlinePosition());
121 EXPECT_EQ(ROBOTO_THICKNESS_EM * textSize, functor.getUnderlineThickness());
122}
123
Seigo Nonakab60dc9a2024-09-05 11:45:17 +0900124TEST(UnderlineTest, NotoCJK) {
Seigo Nonakacd348c62023-09-12 16:05:44 +0900125 float textSize = 100;
126 Paint paint;
127 paint.getSkFont().setSize(textSize);
128 paint.setUnderline(true);
129 // The text is 恂恄恆 in Japanese
130 DrawTextFunctor functor = processFunctor({0x3042, 0x3044, 0x3046}, &paint);
131
132 EXPECT_EQ(NOTO_CJK_POSITION_EM * textSize, functor.getUnderlinePosition());
133 EXPECT_EQ(NOTO_CJK_THICKNESS_EM * textSize, functor.getUnderlineThickness());
134}
135
Seigo Nonakab60dc9a2024-09-05 11:45:17 +0900136TEST(UnderlineTest, Mixture) {
Seigo Nonakacd348c62023-09-12 16:05:44 +0900137 float textSize = 100;
138 Paint paint;
139 paint.getSkFont().setSize(textSize);
140 paint.setUnderline(true);
141 // The text is a恄c. The only middle of the character is Japanese.
142 DrawTextFunctor functor = processFunctor({0x0061, 0x3044, 0x0063}, &paint);
143
144 // We use the bottom, thicker line as underline. Here, use Noto's one.
145 EXPECT_EQ(NOTO_CJK_POSITION_EM * textSize, functor.getUnderlinePosition());
146 EXPECT_EQ(NOTO_CJK_THICKNESS_EM * textSize, functor.getUnderlineThickness());
147}
148} // namespace