1 /*
2  * Copyright (C) 2013 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 "Typeface.h"
18 
19 #include <fcntl.h>  // For tests.
20 #include <pthread.h>
21 #ifndef _WIN32
22 #include <sys/mman.h>  // For tests.
23 #endif
24 #include <sys/stat.h>  // For tests.
25 
26 #include "MinikinSkia.h"
27 #include "SkPaint.h"
28 #include "SkStream.h"  // Fot tests.
29 #include "SkTypeface.h"
30 
31 #include <minikin/FontCollection.h>
32 #include <minikin/FontFamily.h>
33 #include <minikin/Layout.h>
34 #include <utils/Log.h>
35 #include <utils/MathUtils.h>
36 
37 namespace android {
38 
computeAPIStyle(int weight,bool italic)39 static Typeface::Style computeAPIStyle(int weight, bool italic) {
40     // This bold detection comes from SkTypeface.h
41     if (weight >= SkFontStyle::kSemiBold_Weight) {
42         return italic ? Typeface::kBoldItalic : Typeface::kBold;
43     } else {
44         return italic ? Typeface::kItalic : Typeface::kNormal;
45     }
46 }
47 
computeMinikinStyle(int weight,bool italic)48 static minikin::FontStyle computeMinikinStyle(int weight, bool italic) {
49     return minikin::FontStyle(uirenderer::MathUtils::clamp(weight, 1, 1000),
50                               static_cast<minikin::FontStyle::Slant>(italic));
51 }
52 
53 // Resolve the relative weight from the baseWeight and target style.
computeRelativeStyle(int baseWeight,Typeface::Style relativeStyle)54 static minikin::FontStyle computeRelativeStyle(int baseWeight, Typeface::Style relativeStyle) {
55     int weight = baseWeight;
56     if ((relativeStyle & Typeface::kBold) != 0) {
57         weight += 300;
58     }
59     bool italic = (relativeStyle & Typeface::kItalic) != 0;
60     return computeMinikinStyle(weight, italic);
61 }
62 
63 const Typeface* gDefaultTypeface = NULL;
64 
resolveDefault(const Typeface * src)65 const Typeface* Typeface::resolveDefault(const Typeface* src) {
66     LOG_ALWAYS_FATAL_IF(src == nullptr && gDefaultTypeface == nullptr);
67     return src == nullptr ? gDefaultTypeface : src;
68 }
69 
createRelative(Typeface * src,Typeface::Style style)70 Typeface* Typeface::createRelative(Typeface* src, Typeface::Style style) {
71     const Typeface* resolvedFace = Typeface::resolveDefault(src);
72     Typeface* result = new Typeface;
73     if (result != nullptr) {
74         result->fFontCollection = resolvedFace->fFontCollection;
75         result->fBaseWeight = resolvedFace->fBaseWeight;
76         result->fAPIStyle = style;
77         result->fStyle = computeRelativeStyle(result->fBaseWeight, style);
78     }
79     return result;
80 }
81 
createAbsolute(Typeface * base,int weight,bool italic)82 Typeface* Typeface::createAbsolute(Typeface* base, int weight, bool italic) {
83     const Typeface* resolvedFace = Typeface::resolveDefault(base);
84     Typeface* result = new Typeface();
85     if (result != nullptr) {
86         result->fFontCollection = resolvedFace->fFontCollection;
87         result->fBaseWeight = resolvedFace->fBaseWeight;
88         result->fAPIStyle = computeAPIStyle(weight, italic);
89         result->fStyle = computeMinikinStyle(weight, italic);
90     }
91     return result;
92 }
93 
createFromTypefaceWithVariation(Typeface * src,const std::vector<minikin::FontVariation> & variations)94 Typeface* Typeface::createFromTypefaceWithVariation(
95         Typeface* src, const std::vector<minikin::FontVariation>& variations) {
96     const Typeface* resolvedFace = Typeface::resolveDefault(src);
97     Typeface* result = new Typeface();
98     if (result != nullptr) {
99         result->fFontCollection =
100                 resolvedFace->fFontCollection->createCollectionWithVariation(variations);
101         if (result->fFontCollection == nullptr) {
102             // None of passed axes are supported by this collection.
103             // So we will reuse the same collection with incrementing reference count.
104             result->fFontCollection = resolvedFace->fFontCollection;
105         }
106         // Do not update styles.
107         // TODO: We may want to update base weight if the 'wght' is specified.
108         result->fBaseWeight = resolvedFace->fBaseWeight;
109         result->fAPIStyle = resolvedFace->fAPIStyle;
110         result->fStyle = resolvedFace->fStyle;
111     }
112     return result;
113 }
114 
createWithDifferentBaseWeight(Typeface * src,int weight)115 Typeface* Typeface::createWithDifferentBaseWeight(Typeface* src, int weight) {
116     const Typeface* resolvedFace = Typeface::resolveDefault(src);
117     Typeface* result = new Typeface;
118     if (result != nullptr) {
119         result->fFontCollection = resolvedFace->fFontCollection;
120         result->fBaseWeight = weight;
121         result->fAPIStyle = resolvedFace->fAPIStyle;
122         result->fStyle = computeRelativeStyle(weight, result->fAPIStyle);
123     }
124     return result;
125 }
126 
createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>> && families,int weight,int italic,const Typeface * fallback)127 Typeface* Typeface::createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>>&& families,
128                                        int weight, int italic, const Typeface* fallback) {
129     Typeface* result = new Typeface;
130     if (fallback == nullptr) {
131         result->fFontCollection = minikin::FontCollection::create(std::move(families));
132     } else {
133         result->fFontCollection =
134                 fallback->fFontCollection->createCollectionWithFamilies(std::move(families));
135     }
136 
137     if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
138         int weightFromFont;
139         bool italicFromFont;
140 
141         const minikin::FontStyle defaultStyle;
142         const minikin::MinikinFont* mf =
143                 families.empty()
144                         ? nullptr
145                         : families[0]->getClosestMatch(defaultStyle).font->typeface().get();
146         if (mf != nullptr) {
147             SkTypeface* skTypeface = reinterpret_cast<const MinikinFontSkia*>(mf)->GetSkTypeface();
148             const SkFontStyle& style = skTypeface->fontStyle();
149             weightFromFont = style.weight();
150             italicFromFont = style.slant() != SkFontStyle::kUpright_Slant;
151         } else {
152             // We can't obtain any information from fonts. Just use default values.
153             weightFromFont = SkFontStyle::kNormal_Weight;
154             italicFromFont = false;
155         }
156 
157         if (weight == RESOLVE_BY_FONT_TABLE) {
158             weight = weightFromFont;
159         }
160         if (italic == RESOLVE_BY_FONT_TABLE) {
161             italic = italicFromFont ? 1 : 0;
162         }
163     }
164 
165     // Sanitize the invalid value passed from public API.
166     if (weight < 0) {
167         weight = SkFontStyle::kNormal_Weight;
168     }
169 
170     result->fBaseWeight = weight;
171     result->fAPIStyle = computeAPIStyle(weight, italic);
172     result->fStyle = computeMinikinStyle(weight, italic);
173     return result;
174 }
175 
setDefault(const Typeface * face)176 void Typeface::setDefault(const Typeface* face) {
177     gDefaultTypeface = face;
178 }
179 
setRobotoTypefaceForTest()180 void Typeface::setRobotoTypefaceForTest() {
181 #ifndef _WIN32
182     const char* kRobotoFont = "/system/fonts/Roboto-Regular.ttf";
183 
184     int fd = open(kRobotoFont, O_RDONLY);
185     LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", kRobotoFont);
186     struct stat st = {};
187     LOG_ALWAYS_FATAL_IF(fstat(fd, &st) == -1, "Failed to stat file %s", kRobotoFont);
188     void* data = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
189     std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(data, st.st_size));
190     sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(std::move(fontData));
191     LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);
192 
193     std::shared_ptr<minikin::MinikinFont> font =
194             std::make_shared<MinikinFontSkia>(std::move(typeface), 0, data, st.st_size, kRobotoFont,
195                                               0, std::vector<minikin::FontVariation>());
196     std::vector<std::shared_ptr<minikin::Font>> fonts;
197     fonts.push_back(minikin::Font::Builder(font).build());
198 
199     std::shared_ptr<minikin::FontCollection> collection =
200             minikin::FontCollection::create(minikin::FontFamily::create(std::move(fonts)));
201 
202     Typeface* hwTypeface = new Typeface();
203     hwTypeface->fFontCollection = collection;
204     hwTypeface->fAPIStyle = Typeface::kNormal;
205     hwTypeface->fBaseWeight = SkFontStyle::kNormal_Weight;
206     hwTypeface->fStyle = minikin::FontStyle();
207 
208     Typeface::setDefault(hwTypeface);
209 #endif
210 }
211 }  // namespace android
212