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 "MinikinSkia.h"
18 
19 #include <SkFontDescriptor.h>
20 #include <SkFont.h>
21 #include <SkFontMetrics.h>
22 #include <SkFontMgr.h>
23 #include <SkTypeface.h>
24 #include <log/log.h>
25 
26 #include <minikin/Font.h>
27 #include <minikin/MinikinExtent.h>
28 #include <minikin/MinikinPaint.h>
29 #include <minikin/MinikinRect.h>
30 
31 namespace android {
32 
MinikinFontSkia(sk_sp<SkTypeface> typeface,const void * fontData,size_t fontSize,std::string_view filePath,int ttcIndex,const std::vector<minikin::FontVariation> & axes)33 MinikinFontSkia::MinikinFontSkia(sk_sp<SkTypeface> typeface, const void* fontData, size_t fontSize,
34                                  std::string_view filePath, int ttcIndex,
35                                  const std::vector<minikin::FontVariation>& axes)
36         : minikin::MinikinFont(typeface->uniqueID())
37         , mTypeface(std::move(typeface))
38         , mFontData(fontData)
39         , mFontSize(fontSize)
40         , mTtcIndex(ttcIndex)
41         , mAxes(axes)
42         , mFilePath(filePath) {}
43 
MinikinFontSkia_SetSkiaFont(const minikin::MinikinFont * font,SkFont * skFont,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery)44 static void MinikinFontSkia_SetSkiaFont(const minikin::MinikinFont* font, SkFont* skFont,
45                                         const minikin::MinikinPaint& paint,
46                                         const minikin::FontFakery& fakery) {
47     skFont->setSize(paint.size);
48     skFont->setScaleX(paint.scaleX);
49     skFont->setSkewX(paint.skewX);
50     MinikinFontSkia::unpackFontFlags(skFont, paint.fontFlags);
51     // Apply font fakery on top of user-supplied flags.
52     MinikinFontSkia::populateSkFont(skFont, font, fakery);
53 }
54 
GetHorizontalAdvance(uint32_t glyph_id,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery) const55 float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id, const minikin::MinikinPaint& paint,
56                                             const minikin::FontFakery& fakery) const {
57     SkFont skFont;
58     uint16_t glyph16 = glyph_id;
59     SkScalar skWidth;
60     MinikinFontSkia_SetSkiaFont(this, &skFont, paint, fakery);
61     skFont.getWidths(&glyph16, 1, &skWidth);
62 #ifdef VERBOSE
63     ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id, skWidth);
64 #endif
65     return skWidth;
66 }
67 
GetHorizontalAdvances(uint16_t * glyph_ids,uint32_t count,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery,float * outAdvances) const68 void MinikinFontSkia::GetHorizontalAdvances(uint16_t* glyph_ids, uint32_t count,
69                                             const minikin::MinikinPaint& paint,
70                                             const minikin::FontFakery& fakery,
71                                             float* outAdvances) const {
72     SkFont skFont;
73     MinikinFontSkia_SetSkiaFont(this, &skFont, paint, fakery);
74     skFont.getWidths(glyph_ids, count, outAdvances);
75 }
76 
GetBounds(minikin::MinikinRect * bounds,uint32_t glyph_id,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery) const77 void MinikinFontSkia::GetBounds(minikin::MinikinRect* bounds, uint32_t glyph_id,
78                                 const minikin::MinikinPaint& paint,
79                                 const minikin::FontFakery& fakery) const {
80     SkFont skFont;
81     uint16_t glyph16 = glyph_id;
82     SkRect skBounds;
83     MinikinFontSkia_SetSkiaFont(this, &skFont, paint, fakery);
84     skFont.getWidths(&glyph16, 1, nullptr, &skBounds);
85     bounds->mLeft = skBounds.fLeft;
86     bounds->mTop = skBounds.fTop;
87     bounds->mRight = skBounds.fRight;
88     bounds->mBottom = skBounds.fBottom;
89 }
90 
GetFontExtent(minikin::MinikinExtent * extent,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery) const91 void MinikinFontSkia::GetFontExtent(minikin::MinikinExtent* extent,
92                                     const minikin::MinikinPaint& paint,
93                                     const minikin::FontFakery& fakery) const {
94     SkFont skFont;
95     MinikinFontSkia_SetSkiaFont(this, &skFont, paint, fakery);
96     SkFontMetrics metrics;
97     skFont.getMetrics(&metrics);
98     extent->ascent = metrics.fAscent;
99     extent->descent = metrics.fDescent;
100 }
101 
GetSkTypeface() const102 SkTypeface* MinikinFontSkia::GetSkTypeface() const {
103     return mTypeface.get();
104 }
105 
RefSkTypeface() const106 sk_sp<SkTypeface> MinikinFontSkia::RefSkTypeface() const {
107     return mTypeface;
108 }
109 
GetFontData() const110 const void* MinikinFontSkia::GetFontData() const {
111     return mFontData;
112 }
113 
GetFontSize() const114 size_t MinikinFontSkia::GetFontSize() const {
115     return mFontSize;
116 }
117 
GetFontIndex() const118 int MinikinFontSkia::GetFontIndex() const {
119     return mTtcIndex;
120 }
121 
GetAxes() const122 const std::vector<minikin::FontVariation>& MinikinFontSkia::GetAxes() const {
123     return mAxes;
124 }
125 
createFontWithVariation(const std::vector<minikin::FontVariation> & variations) const126 std::shared_ptr<minikin::MinikinFont> MinikinFontSkia::createFontWithVariation(
127         const std::vector<minikin::FontVariation>& variations) const {
128     SkFontArguments params;
129 
130     int ttcIndex;
131     std::unique_ptr<SkStreamAsset> stream(mTypeface->openStream(&ttcIndex));
132     LOG_ALWAYS_FATAL_IF(stream == nullptr, "openStream failed");
133 
134     params.setCollectionIndex(ttcIndex);
135     std::vector<SkFontArguments::Axis> skAxes;
136     skAxes.resize(variations.size());
137     for (size_t i = 0; i < variations.size(); i++) {
138         skAxes[i].fTag = variations[i].axisTag;
139         skAxes[i].fStyleValue = SkFloatToScalar(variations[i].value);
140     }
141     params.setAxes(skAxes.data(), skAxes.size());
142     sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
143     sk_sp<SkTypeface> face(fm->makeFromStream(std::move(stream), params));
144 
145     return std::make_shared<MinikinFontSkia>(std::move(face), mFontData, mFontSize, mFilePath,
146                                              ttcIndex, variations);
147 }
148 
149 // hinting<<16 | edging<<8 | bools:5bits
packFontFlags(const SkFont & font)150 uint32_t MinikinFontSkia::packFontFlags(const SkFont& font) {
151     uint32_t flags = (unsigned)font.getHinting() << 16;
152     flags |= (unsigned)font.getEdging() << 8;
153     flags |= font.isEmbolden()          << minikin::Embolden_Shift;
154     flags |= font.isLinearMetrics()     << minikin::LinearMetrics_Shift;
155     flags |= font.isSubpixel()          << minikin::Subpixel_Shift;
156     flags |= font.isEmbeddedBitmaps()   << minikin::EmbeddedBitmaps_Shift;
157     flags |= font.isForceAutoHinting()  << minikin::ForceAutoHinting_Shift;
158     return flags;
159 }
160 
unpackFontFlags(SkFont * font,uint32_t flags)161 void MinikinFontSkia::unpackFontFlags(SkFont* font, uint32_t flags) {
162     // We store hinting in the top 16 bits (only need 2 of them)
163     font->setHinting((SkFontHinting)(flags >> 16));
164     // We store edging in bits 8:15 (only need 2 of them)
165     font->setEdging((SkFont::Edging)((flags >> 8) & 0xFF));
166     font->setEmbolden(        (flags & minikin::Embolden_Flag) != 0);
167     font->setLinearMetrics(   (flags & minikin::LinearMetrics_Flag) != 0);
168     font->setSubpixel(        (flags & minikin::Subpixel_Flag) != 0);
169     font->setEmbeddedBitmaps( (flags & minikin::EmbeddedBitmaps_Flag) != 0);
170     font->setForceAutoHinting((flags & minikin::ForceAutoHinting_Flag) != 0);
171 }
172 
populateSkFont(SkFont * skFont,const MinikinFont * font,minikin::FontFakery fakery)173 void MinikinFontSkia::populateSkFont(SkFont* skFont, const MinikinFont* font,
174                                      minikin::FontFakery fakery) {
175     skFont->setTypeface(reinterpret_cast<const MinikinFontSkia*>(font)->RefSkTypeface());
176     skFont->setEmbolden(skFont->isEmbolden() || fakery.isFakeBold());
177     if (fakery.isFakeItalic()) {
178         skFont->setSkewX(skFont->getSkewX() - 0.25f);
179     }
180 }
181 }
182