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)127 Typeface* Typeface::createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>>&& families,
128 int weight, int italic) {
129 Typeface* result = new Typeface;
130 result->fFontCollection.reset(new minikin::FontCollection(families));
131
132 if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
133 int weightFromFont;
134 bool italicFromFont;
135
136 const minikin::FontStyle defaultStyle;
137 const minikin::MinikinFont* mf =
138 families.empty()
139 ? nullptr
140 : families[0]->getClosestMatch(defaultStyle).font->typeface().get();
141 if (mf != nullptr) {
142 SkTypeface* skTypeface = reinterpret_cast<const MinikinFontSkia*>(mf)->GetSkTypeface();
143 const SkFontStyle& style = skTypeface->fontStyle();
144 weightFromFont = style.weight();
145 italicFromFont = style.slant() != SkFontStyle::kUpright_Slant;
146 } else {
147 // We can't obtain any information from fonts. Just use default values.
148 weightFromFont = SkFontStyle::kNormal_Weight;
149 italicFromFont = false;
150 }
151
152 if (weight == RESOLVE_BY_FONT_TABLE) {
153 weight = weightFromFont;
154 }
155 if (italic == RESOLVE_BY_FONT_TABLE) {
156 italic = italicFromFont ? 1 : 0;
157 }
158 }
159
160 // Sanitize the invalid value passed from public API.
161 if (weight < 0) {
162 weight = SkFontStyle::kNormal_Weight;
163 }
164
165 result->fBaseWeight = weight;
166 result->fAPIStyle = computeAPIStyle(weight, italic);
167 result->fStyle = computeMinikinStyle(weight, italic);
168 return result;
169 }
170
setDefault(const Typeface * face)171 void Typeface::setDefault(const Typeface* face) {
172 gDefaultTypeface = face;
173 }
174
setRobotoTypefaceForTest()175 void Typeface::setRobotoTypefaceForTest() {
176 #ifndef _WIN32
177 const char* kRobotoFont = "/system/fonts/Roboto-Regular.ttf";
178
179 int fd = open(kRobotoFont, O_RDONLY);
180 LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", kRobotoFont);
181 struct stat st = {};
182 LOG_ALWAYS_FATAL_IF(fstat(fd, &st) == -1, "Failed to stat file %s", kRobotoFont);
183 void* data = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
184 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(data, st.st_size));
185 sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(std::move(fontData));
186 LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);
187
188 std::shared_ptr<minikin::MinikinFont> font =
189 std::make_shared<MinikinFontSkia>(std::move(typeface), 0, data, st.st_size, kRobotoFont,
190 0, std::vector<minikin::FontVariation>());
191 std::vector<std::shared_ptr<minikin::Font>> fonts;
192 fonts.push_back(minikin::Font::Builder(font).build());
193
194 std::shared_ptr<minikin::FontCollection> collection = std::make_shared<minikin::FontCollection>(
195 std::make_shared<minikin::FontFamily>(std::move(fonts)));
196
197 Typeface* hwTypeface = new Typeface();
198 hwTypeface->fFontCollection = collection;
199 hwTypeface->fAPIStyle = Typeface::kNormal;
200 hwTypeface->fBaseWeight = SkFontStyle::kNormal_Weight;
201 hwTypeface->fStyle = minikin::FontStyle();
202
203 Typeface::setDefault(hwTypeface);
204 #endif
205 }
206 } // namespace android
207