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