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/**
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.
21 */
22
23#include "Typeface.h"
24
25#include <pthread.h>
26#include <fcntl.h>  // For tests.
27#include <sys/stat.h>  // For tests.
28#include <sys/mman.h>  // For tests.
29
30#include "MinikinSkia.h"
31#include "SkTypeface.h"
32#include "SkPaint.h"
33#include "SkStream.h"  // Fot tests.
34
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>
40
41namespace android {
42
43static 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;
47    } else {
48        return italic ? SkTypeface::kItalic : SkTypeface::kNormal;
49    }
50}
51
52static 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);
56}
57
58// Resolve the relative weight from the baseWeight and target style.
59static minikin::FontStyle computeRelativeStyle(int baseWeight, SkTypeface::Style relativeStyle) {
60    int weight = baseWeight;
61    if ((relativeStyle & SkTypeface::kBold) != 0) {
62        weight += 300;
63    }
64    bool italic = (relativeStyle & SkTypeface::kItalic) != 0;
65    return computeMinikinStyle(weight, italic);
66}
67
68Typeface* gDefaultTypeface = NULL;
69
70Typeface* Typeface::resolveDefault(Typeface* src) {
71    LOG_ALWAYS_FATAL_IF(gDefaultTypeface == nullptr);
72    return src == nullptr ? gDefaultTypeface : src;
73}
74
75Typeface* 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);
83    }
84    return result;
85}
86
87Typeface* 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);
95    }
96    return result;
97}
98
99Typeface* 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;
110        }
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;
116    }
117    return result;
118}
119
120Typeface* 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);
128    }
129    return result;
130}
131
132Typeface* 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));
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 : families[0]->getClosestMatch(defaultStyle).font;
145        if (mf != nullptr) {
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;
150        } else {
151            // We can't obtain any information from fonts. Just use default values.
152            weightFromFont = SkFontStyle::kNormal_Weight;
153            italicFromFont = false;
154        }
155
156        if (weight == RESOLVE_BY_FONT_TABLE) {
157            weight = weightFromFont;
158        }
159        if (italic == RESOLVE_BY_FONT_TABLE) {
160            italic = italicFromFont? 1 : 0;
161        }
162    }
163
164    // Sanitize the invalid value passed from public API.
165    if (weight < 0) {
166        weight = SkFontStyle::kNormal_Weight;
167    }
168
169    result->fBaseWeight = weight;
170    result->fSkiaStyle = computeSkiaStyle(weight, italic);
171    result->fStyle = computeMinikinStyle(weight, italic);
172    return result;
173}
174
175void Typeface::setDefault(Typeface* face) {
176    gDefaultTypeface = face;
177}
178
179void Typeface::setRobotoTypefaceForTest() {
180    const char* kRobotoFont = "/system/fonts/Roboto-Regular.ttf";
181
182    int fd = open(kRobotoFont, O_RDONLY);
183    LOG_ALWAYS_FATAL_IF(fd == -1, "Failed to open file %s", kRobotoFont);
184    struct stat st = {};
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);
190
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));
197
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 */);
203
204    Typeface::setDefault(hwTypeface);
205}
206
207}
208