• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include "utils/TypefaceUtils.h"
40 
41 using namespace android;
42 
43 namespace {
44 
45 constexpr char kRobotoVariable[] = "/system/fonts/Roboto-Regular.ttf";
46 constexpr char kJPFont[] = "/system/fonts/NotoSansCJK-Regular.ttc";
47 
48 // The underline position and thickness are cames from post table.
49 constexpr float ROBOTO_POSITION_EM = 150.0 / 2048.0;
50 constexpr float ROBOTO_THICKNESS_EM = 100.0 / 2048.0;
51 constexpr float NOTO_CJK_POSITION_EM = 125.0 / 1000.0;
52 constexpr float NOTO_CJK_THICKNESS_EM = 50.0 / 1000.0;
53 
unmap(const void * ptr,void * context)54 void 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.
buildFamily(const char * fileName)61 std::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));
70     sk_sp<SkFontMgr> fm = android::FreeTypeFontMgr();
71     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.
makeTypeface()82 Typeface* 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.
doLayout(const std::vector<uint16_t> text,Paint paint,Typeface * typeface)90 minikin::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 
processFunctor(const std::vector<uint16_t> & text,Paint * paint)95 DrawTextFunctor 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 
107     DrawTextFunctor f(layout, &canvas, *paint, 0, 0, layout.getAdvance());
108     MinikinUtils::forFontRun(layout, paint, f);
109     return f;
110 }
111 
TEST_WITH_FLAGS(UnderlineTest,Roboto,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (com::android::text::flags,fix_double_underline)))112 TEST_WITH_FLAGS(UnderlineTest, Roboto,
113                 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::text::flags,
114                                                     fix_double_underline))) {
115     float textSize = 100;
116     Paint paint;
117     paint.getSkFont().setSize(textSize);
118     paint.setUnderline(true);
119     // the text is "abc"
120     DrawTextFunctor functor = processFunctor({0x0061, 0x0062, 0x0063}, &paint);
121 
122     EXPECT_EQ(ROBOTO_POSITION_EM * textSize, functor.getUnderlinePosition());
123     EXPECT_EQ(ROBOTO_THICKNESS_EM * textSize, functor.getUnderlineThickness());
124 }
125 
TEST_WITH_FLAGS(UnderlineTest,NotoCJK,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (com::android::text::flags,fix_double_underline)))126 TEST_WITH_FLAGS(UnderlineTest, NotoCJK,
127                 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::text::flags,
128                                                     fix_double_underline))) {
129     float textSize = 100;
130     Paint paint;
131     paint.getSkFont().setSize(textSize);
132     paint.setUnderline(true);
133     // The text is 恂恄恆 in Japanese
134     DrawTextFunctor functor = processFunctor({0x3042, 0x3044, 0x3046}, &paint);
135 
136     EXPECT_EQ(NOTO_CJK_POSITION_EM * textSize, functor.getUnderlinePosition());
137     EXPECT_EQ(NOTO_CJK_THICKNESS_EM * textSize, functor.getUnderlineThickness());
138 }
139 
TEST_WITH_FLAGS(UnderlineTest,Mixture,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (com::android::text::flags,fix_double_underline)))140 TEST_WITH_FLAGS(UnderlineTest, Mixture,
141                 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::text::flags,
142                                                     fix_double_underline))) {
143     float textSize = 100;
144     Paint paint;
145     paint.getSkFont().setSize(textSize);
146     paint.setUnderline(true);
147     // The text is a恄c. The only middle of the character is Japanese.
148     DrawTextFunctor functor = processFunctor({0x0061, 0x3044, 0x0063}, &paint);
149 
150     // We use the bottom, thicker line as underline. Here, use Noto's one.
151     EXPECT_EQ(NOTO_CJK_POSITION_EM * textSize, functor.getUnderlinePosition());
152     EXPECT_EQ(NOTO_CJK_THICKNESS_EM * textSize, functor.getUnderlineThickness());
153 }
154 }  // namespace
155