• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     return new Typeface(resolvedFace->getFontCollection(),
74                         computeRelativeStyle(resolvedFace->getBaseWeight(), style), style,
75                         resolvedFace->getBaseWeight(), resolvedFace->isVariationInstance());
76 }
77 
createAbsolute(Typeface * base,int weight,bool italic)78 Typeface* Typeface::createAbsolute(Typeface* base, int weight, bool italic) {
79     const Typeface* resolvedFace = Typeface::resolveDefault(base);
80     return new Typeface(resolvedFace->getFontCollection(), computeMinikinStyle(weight, italic),
81                         computeAPIStyle(weight, italic), resolvedFace->getBaseWeight(),
82                         resolvedFace->isVariationInstance());
83 }
84 
createFromTypefaceWithVariation(Typeface * src,const minikin::VariationSettings & variations)85 Typeface* Typeface::createFromTypefaceWithVariation(Typeface* src,
86                                                     const minikin::VariationSettings& variations) {
87     const Typeface* resolvedFace = Typeface::resolveDefault(src);
88     const std::shared_ptr<minikin::FontCollection>& fc =
89             resolvedFace->getFontCollection()->createCollectionWithVariation(variations);
90     return new Typeface(
91             // None of passed axes are supported by this collection.
92             // So we will reuse the same collection with incrementing reference count.
93             fc ? fc : resolvedFace->getFontCollection(),
94             // Do not update styles.
95             // TODO: We may want to update base weight if the 'wght' is specified.
96             resolvedFace->fStyle, resolvedFace->getAPIStyle(), resolvedFace->getBaseWeight(), true);
97 }
98 
createWithDifferentBaseWeight(Typeface * src,int weight)99 Typeface* Typeface::createWithDifferentBaseWeight(Typeface* src, int weight) {
100     const Typeface* resolvedFace = Typeface::resolveDefault(src);
101     return new Typeface(resolvedFace->getFontCollection(),
102                         computeRelativeStyle(weight, resolvedFace->getAPIStyle()),
103                         resolvedFace->getAPIStyle(), weight, resolvedFace->isVariationInstance());
104 }
105 
createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>> && families,int weight,int italic,const Typeface * fallback)106 Typeface* Typeface::createFromFamilies(std::vector<std::shared_ptr<minikin::FontFamily>>&& families,
107                                        int weight, int italic, const Typeface* fallback) {
108     const std::shared_ptr<minikin::FontCollection>& fc =
109             fallback ? fallback->getFontCollection()->createCollectionWithFamilies(
110                                std::move(families))
111                      : minikin::FontCollection::create(std::move(families));
112 
113     if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
114         int weightFromFont;
115         bool italicFromFont;
116 
117         const minikin::FontStyle defaultStyle;
118         const minikin::MinikinFont* mf =
119                 families.empty() ? nullptr
120                                  : families[0]->getClosestMatch(defaultStyle).typeface().get();
121         if (mf != nullptr) {
122             SkTypeface* skTypeface = reinterpret_cast<const MinikinFontSkia*>(mf)->GetSkTypeface();
123             const SkFontStyle& style = skTypeface->fontStyle();
124             weightFromFont = style.weight();
125             italicFromFont = style.slant() != SkFontStyle::kUpright_Slant;
126         } else {
127             // We can't obtain any information from fonts. Just use default values.
128             weightFromFont = SkFontStyle::kNormal_Weight;
129             italicFromFont = false;
130         }
131 
132         if (weight == RESOLVE_BY_FONT_TABLE) {
133             weight = weightFromFont;
134         }
135         if (italic == RESOLVE_BY_FONT_TABLE) {
136             italic = italicFromFont ? 1 : 0;
137         }
138     }
139 
140     // Sanitize the invalid value passed from public API.
141     if (weight < 0) {
142         weight = SkFontStyle::kNormal_Weight;
143     }
144 
145     return new Typeface(fc, computeMinikinStyle(weight, italic), computeAPIStyle(weight, italic),
146                         weight, false);
147 }
148 
setDefault(const Typeface * face)149 void Typeface::setDefault(const Typeface* face) {
150     gDefaultTypeface = face;
151 }
152 
setRobotoTypefaceForTest()153 void Typeface::setRobotoTypefaceForTest() {
154 #ifndef _WIN32
155     const char* kRobotoFont = "/system/fonts/Roboto-Regular.ttf";
156 
157     int fd = open(kRobotoFont, O_RDONLY);
158     LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", kRobotoFont);
159     struct stat st = {};
160     LOG_ALWAYS_FATAL_IF(fstat(fd, &st) == -1, "Failed to stat file %s", kRobotoFont);
161     void* data = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
162     std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(data, st.st_size));
163     sk_sp<SkFontMgr> fm = android::FreeTypeFontMgr();
164     LOG_ALWAYS_FATAL_IF(fm == nullptr, "Could not load FreeType SkFontMgr");
165     sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(fontData));
166     LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);
167 
168     std::shared_ptr<minikin::MinikinFont> font = std::make_shared<MinikinFontSkia>(
169             std::move(typeface), 0, data, st.st_size, kRobotoFont, 0, minikin::VariationSettings());
170     std::vector<std::shared_ptr<minikin::Font>> fonts;
171     fonts.push_back(minikin::Font::Builder(font).build());
172 
173     std::shared_ptr<minikin::FontCollection> collection =
174             minikin::FontCollection::create(minikin::FontFamily::create(std::move(fonts)));
175 
176     Typeface* hwTypeface = new Typeface(collection, minikin::FontStyle(), Typeface::kNormal,
177                                         SkFontStyle::kNormal_Weight, false);
178 
179     Typeface::setDefault(hwTypeface);
180 #endif
181 }
182 }  // namespace android
183