2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This is the implementation of the Typeface object. Historically, it has
19 * just been SkTypeface, but we are migrating to Minikin. For the time
20 * being, that choice is hidden under the USE_MINIKIN compile-time flag.
26 #include <fcntl.h> // For tests.
27 #include <sys/stat.h> // For tests.
28 #include <sys/mman.h> // For tests.
30 #include "MinikinSkia.h"
31 #include "SkTypeface.h"
33 #include "SkStream.h" // Fot tests.
35 #include <minikin/FontCollection.h>
36 #include <minikin/FontFamily.h>
37 #include <minikin/Layout.h>
38 #include <utils/Log.h>
39 #include <utils/MathUtils.h>
43 static SkTypeface::Style computeSkiaStyle(int weight, bool italic) {
44 // This bold detection comes from SkTypeface.h
45 if (weight >= SkFontStyle::kSemiBold_Weight) {
46 return italic ? SkTypeface::kBoldItalic : SkTypeface::kBold;
48 return italic ? SkTypeface::kItalic : SkTypeface::kNormal;
52 static minikin::FontStyle computeMinikinStyle(int weight, bool italic) {
53 // TODO: Better to use raw base weight value for font selection instead of dividing by 100.
54 const int minikinWeight = uirenderer::MathUtils::clamp((weight + 50) / 100, 1, 10);
55 return minikin::FontStyle(minikinWeight, italic);
58 // Resolve the relative weight from the baseWeight and target style.
59 static minikin::FontStyle computeRelativeStyle(int baseWeight, SkTypeface::Style relativeStyle) {
60 int weight = baseWeight;
61 if ((relativeStyle & SkTypeface::kBold) != 0) {
64 bool italic = (relativeStyle & SkTypeface::kItalic) != 0;
65 return computeMinikinStyle(weight, italic);
68 Typeface* gDefaultTypeface = NULL;
70 Typeface* Typeface::resolveDefault(Typeface* src) {
71 LOG_ALWAYS_FATAL_IF(src == nullptr && gDefaultTypeface == nullptr);
72 return src == nullptr ? gDefaultTypeface : src;
75 Typeface* Typeface::createRelative(Typeface* src, SkTypeface::Style style) {
76 Typeface* resolvedFace = Typeface::resolveDefault(src);
77 Typeface* result = new Typeface;
78 if (result != nullptr) {
79 result->fFontCollection = resolvedFace->fFontCollection;
80 result->fBaseWeight = resolvedFace->fBaseWeight;
81 result->fSkiaStyle = style;
82 result->fStyle = computeRelativeStyle(result->fBaseWeight, style);
87 Typeface* Typeface::createAbsolute(Typeface* base, int weight, bool italic) {
88 Typeface* resolvedFace = Typeface::resolveDefault(base);
89 Typeface* result = new Typeface();
90 if (result != nullptr) {
91 result->fFontCollection = resolvedFace->fFontCollection;
92 result->fBaseWeight = resolvedFace->fBaseWeight;
93 result->fSkiaStyle = computeSkiaStyle(weight, italic);
94 result->fStyle = computeMinikinStyle(weight, italic);
99 Typeface* Typeface::createFromTypefaceWithVariation(Typeface* src,
100 const std::vector<minikin::FontVariation>& variations) {
101 Typeface* resolvedFace = Typeface::resolveDefault(src);
102 Typeface* result = new Typeface();
103 if (result != nullptr) {
104 result->fFontCollection =
105 resolvedFace->fFontCollection->createCollectionWithVariation(variations);
106 if (result->fFontCollection == nullptr) {
107 // None of passed axes are supported by this collection.
108 // So we will reuse the same collection with incrementing reference count.
109 result->fFontCollection = resolvedFace->fFontCollection;
111 // Do not update styles.
112 // TODO: We may want to update base weight if the 'wght' is specified.
113 result->fBaseWeight = resolvedFace->fBaseWeight;
114 result->fSkiaStyle = resolvedFace->fSkiaStyle;
115 result->fStyle = resolvedFace->fStyle;
120 Typeface* Typeface::createWithDifferentBaseWeight(Typeface* src, int weight) {
121 Typeface* resolvedFace = Typeface::resolveDefault(src);
122 Typeface* result = new Typeface;
123 if (result != nullptr) {
124 result->fFontCollection = resolvedFace->fFontCollection;
125 result->fBaseWeight = weight;
126 result->fSkiaStyle = resolvedFace->fSkiaStyle;
127 result->fStyle = computeRelativeStyle(weight, result->fSkiaStyle);
132 Typeface* Typeface::createFromFamilies(
133 std::vector<std::shared_ptr<minikin::FontFamily>>&& families,
134 int weight, int italic) {
135 Typeface* result = new Typeface;
136 result->fFontCollection.reset(new minikin::FontCollection(families));
138 if (weight == RESOLVE_BY_FONT_TABLE || italic == RESOLVE_BY_FONT_TABLE) {
142 const minikin::FontStyle defaultStyle;
143 const minikin::MinikinFont* mf =
144 families.empty() ? nullptr : families[0]->getClosestMatch(defaultStyle).font;
146 SkTypeface* skTypeface = reinterpret_cast<const MinikinFontSkia*>(mf)->GetSkTypeface();
147 const SkFontStyle& style = skTypeface->fontStyle();
148 weightFromFont = style.weight();
149 italicFromFont = style.slant() != SkFontStyle::kUpright_Slant;
151 // We can't obtain any information from fonts. Just use default values.
152 weightFromFont = SkFontStyle::kNormal_Weight;
153 italicFromFont = false;
156 if (weight == RESOLVE_BY_FONT_TABLE) {
157 weight = weightFromFont;
159 if (italic == RESOLVE_BY_FONT_TABLE) {
160 italic = italicFromFont? 1 : 0;
164 // Sanitize the invalid value passed from public API.
166 weight = SkFontStyle::kNormal_Weight;
169 result->fBaseWeight = weight;
170 result->fSkiaStyle = computeSkiaStyle(weight, italic);
171 result->fStyle = computeMinikinStyle(weight, italic);
175 void Typeface::setDefault(Typeface* face) {
176 gDefaultTypeface = face;
179 void Typeface::setRobotoTypefaceForTest() {
180 const char* kRobotoFont = "/system/fonts/Roboto-Regular.ttf";
182 int fd = open(kRobotoFont, O_RDONLY);
183 LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", kRobotoFont);
185 LOG_ALWAYS_FATAL_IF(fstat(fd, &st) == -1, "Failed to stat file %s", kRobotoFont);
186 void* data = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
187 std::unique_ptr<SkMemoryStream> fontData(new SkMemoryStream(data, st.st_size));
188 sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(fontData.release());
189 LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);
191 std::shared_ptr<minikin::MinikinFont> font = std::make_shared<MinikinFontSkia>(
192 std::move(typeface), data, st.st_size, 0, std::vector<minikin::FontVariation>());
193 std::shared_ptr<minikin::FontFamily> family = std::make_shared<minikin::FontFamily>(
194 std::vector<minikin::Font>({ minikin::Font(std::move(font), minikin::FontStyle()) }));
195 std::shared_ptr<minikin::FontCollection> collection =
196 std::make_shared<minikin::FontCollection>(std::move(family));
198 Typeface* hwTypeface = new Typeface();
199 hwTypeface->fFontCollection = collection;
200 hwTypeface->fSkiaStyle = SkTypeface::kNormal;
201 hwTypeface->fBaseWeight = SkFontStyle::kNormal_Weight;
202 hwTypeface->fStyle = minikin::FontStyle(4 /* weight */, false /* italic */);
204 Typeface::setDefault(hwTypeface);