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"  // For tests.
29 #include "SkTypeface.h"
30 #include "utils/TypefaceUtils.h"
31 
32 #include <minikin/FontCollection.h>
33 #include <minikin/FontFamily.h>
34 #include <minikin/Layout.h>
35 #include <utils/Log.h>
36 #include <utils/MathUtils.h>
37 
38 namespace android {
39 
computeAPIStyle(int weight,bool italic)40 static Typeface::Style computeAPIStyle(int weight, bool italic) {
41     // This bold detection comes from SkTypeface.h
42     if (weight >= SkFontStyle::kSemiBold_Weight) {
43         return italic ? Typeface::kBoldItalic : Typeface::kBold;
44     } else {
45         return italic ? Typeface::kItalic : Typeface::kNormal;
46     }
47 }
48 
computeMinikinStyle(int weight,bool italic)49 static minikin::FontStyle computeMinikinStyle(int weight, bool italic) {
50     return minikin::FontStyle(uirenderer::MathUtils::clamp(weight, 1, 1000),
51                               static_cast<minikin::FontStyle::Slant>(italic));
52 }
53 
54 // Resolve the relative weight from the baseWeight and target style.
computeRelativeStyle(int baseWeight,Typeface::Style relativeStyle)55 static minikin::FontStyle computeRelativeStyle(int baseWeight, Typeface::Style relativeStyle) {
56     int weight = baseWeight;
57     if ((relativeStyle & Typeface::kBold) != 0) {
58         weight += 300;
59     }
60     bool italic = (relativeStyle & Typeface::kItalic) != 0;
61     return computeMinikinStyle(weight, italic);
62 }
63 
64 const Typeface* gDefaultTypeface = NULL;
65 
resolveDefault(const Typeface * src)66 const Typeface* Typeface::resolveDefault(const Typeface* src) {
67     LOG_ALWAYS_FATAL_IF(src == nullptr && gDefaultTypeface == nullptr);
68     return src == nullptr ? gDefaultTypeface : src;
69 }
70 
createRelative(Typeface * src,Typeface::Style style)71 Typeface* Typeface::createRelative(Typeface* src, Typeface::Style style) {
72     const Typeface* resolvedFace = Typeface::resolveDefault(src);
73     Typeface* result = new Typeface;
74     if (result != nullptr) {
75         result->fFontCollection = resolvedFace->fFontCollection;
76         result->fBaseWeight = resolvedFace->fBaseWeight;
77         result->fAPIStyle = style;
78         result->fStyle = computeRelativeStyle(result->fBaseWeight, style);
79     }
80     return result;
81 }
82 
createAbsolute(Typeface * base,int weight,bool italic)83 Typeface* Typeface::createAbsolute(Typeface* base, int weight, bool italic) {
84     const Typeface* resolvedFace = Typeface::resolveDefault(base);
85     Typeface* result = new Typeface();
86     if (result != nullptr) {
87         result->fFontCollection = resolvedFace->fFontCollection;
88         result->fBaseWeight = resolvedFace->fBaseWeight;
89         result->fAPIStyle = computeAPIStyle(weight, italic);
90         result->fStyle = computeMinikinStyle(weight, italic);
91     }
92     return result;
93 }
94 
createFromTypefaceWithVariation(Typeface * src,const std::vector<minikin::FontVariation> & variations)95 Typeface* Typeface::createFromTypefaceWithVariation(
96         Typeface* src, const std::vector<minikin::FontVariation>& variations) {
97     const Typeface* resolvedFace = Typeface::resolveDefault(src);
98     Typeface* result = new Typeface();
99     if (result != nullptr) {
100         result->fFontCollection =
101                 resolvedFace->fFontCollection->createCollectionWithVariation(variations);
102         if (result->fFontCollection == nullptr) {
103             // None of passed axes are supported by this collection.
104             // So we will reuse the same collection with incrementing reference count.
105             result->fFontCollection = resolvedFace->fFontCollection;
106         }
107         // Do not update styles.
108         // TODO: We may want to update base weight if the 'wght' is specified.
109         result->fBaseWeight = resolvedFace->fBaseWeight;
110         result->fAPIStyle = resolvedFace->fAPIStyle;
111         result->fStyle = resolvedFace->fStyle;
112     }
113     return result;
114 }
115 
createWithDifferentBaseWeight(Typeface * src,int weight)116 Typeface* Typeface::createWithDifferentBaseWeight(Typeface* src, int weight) {
117     const Typeface* resolvedFace = Typeface::resolveDefault(src);
118     Typeface* result = new Typeface;
119     if (result != nullptr) {
120         result->fFontCollection = resolvedFace->fFontCollection;
121         result->fBaseWeight = weight;
122         result->fAPIStyle = resolvedFace->fAPIStyle;
123         result->fStyle = computeRelativeStyle(weight, result->fAPIStyle);
124     }
125     return result;
126 }
127 
createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>> && families,int weight,int italic,const Typeface * fallback)128 Typeface* Typeface::createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>>&& families,
129                                        int weight, int italic, const Typeface* fallback) {
130     Typeface* result = new Typeface;
131     if (fallback == nullptr) {
132         result->fFontCollection = minikin::FontCollection::create(std::move(families));
133     } else {
134         result->fFontCollection =
135                 fallback->fFontCollection->createCollectionWithFamilies(std::move(families));
136     }
137 
138     if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
139         int weightFromFont;
140         bool italicFromFont;
141 
142         const minikin::FontStyle defaultStyle;
143         const minikin::MinikinFont* mf =
144                 families.empty() ? nullptr
145                                  : families[0]->getClosestMatch(defaultStyle).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<SkFontMgr> fm = android::FreeTypeFontMgr();
191     LOG_ALWAYS_FATAL_IF(fm == nullptr, "Could not load FreeType SkFontMgr");
192     sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(fontData));
193     LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);
194 
195     std::shared_ptr<minikin::MinikinFont> font =
196             std::make_shared<MinikinFontSkia>(std::move(typeface), 0, data, st.st_size, kRobotoFont,
197                                               0, std::vector<minikin::FontVariation>());
198     std::vector<std::shared_ptr<minikin::Font>> fonts;
199     fonts.push_back(minikin::Font::Builder(font).build());
200 
201     std::shared_ptr<minikin::FontCollection> collection =
202             minikin::FontCollection::create(minikin::FontFamily::create(std::move(fonts)));
203 
204     Typeface* hwTypeface = new Typeface();
205     hwTypeface->fFontCollection = collection;
206     hwTypeface->fAPIStyle = Typeface::kNormal;
207     hwTypeface->fBaseWeight = SkFontStyle::kNormal_Weight;
208     hwTypeface->fStyle = minikin::FontStyle();
209 
210     Typeface::setDefault(hwTypeface);
211 #endif
212 }
213 }  // namespace android
214