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 <SkFontMgr.h>
21 #include <SkPaint.h>
22 #include <SkTypeface.h>
23 #include <log/log.h>
24 
25 namespace android {
26 
MinikinFontSkia(sk_sp<SkTypeface> typeface,const void * fontData,size_t fontSize,int ttcIndex,const std::vector<minikin::FontVariation> & axes)27 MinikinFontSkia::MinikinFontSkia(sk_sp<SkTypeface> typeface, const void* fontData, size_t fontSize,
28                                  int ttcIndex, const std::vector<minikin::FontVariation>& axes)
29         : minikin::MinikinFont(typeface->uniqueID())
30         , mTypeface(std::move(typeface))
31         , mFontData(fontData)
32         , mFontSize(fontSize)
33         , mTtcIndex(ttcIndex)
34         , mAxes(axes) {}
35 
MinikinFontSkia_SetSkiaPaint(const minikin::MinikinFont * font,SkPaint * skPaint,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery)36 static void MinikinFontSkia_SetSkiaPaint(const minikin::MinikinFont* font, SkPaint* skPaint,
37                                          const minikin::MinikinPaint& paint,
38                                          const minikin::FontFakery& fakery) {
39     skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
40     skPaint->setTextSize(paint.size);
41     skPaint->setTextScaleX(paint.scaleX);
42     skPaint->setTextSkewX(paint.skewX);
43     MinikinFontSkia::unpackPaintFlags(skPaint, paint.paintFlags);
44     // Apply font fakery on top of user-supplied flags.
45     MinikinFontSkia::populateSkPaint(skPaint, font, fakery);
46 }
47 
GetHorizontalAdvance(uint32_t glyph_id,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery) const48 float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id, const minikin::MinikinPaint& paint,
49                                             const minikin::FontFakery& fakery) const {
50     SkPaint skPaint;
51     uint16_t glyph16 = glyph_id;
52     SkScalar skWidth;
53     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint, fakery);
54     skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL);
55 #ifdef VERBOSE
56     ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id, skWidth);
57 #endif
58     return skWidth;
59 }
60 
GetBounds(minikin::MinikinRect * bounds,uint32_t glyph_id,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery) const61 void MinikinFontSkia::GetBounds(minikin::MinikinRect* bounds, uint32_t glyph_id,
62                                 const minikin::MinikinPaint& paint,
63                                 const minikin::FontFakery& fakery) const {
64     SkPaint skPaint;
65     uint16_t glyph16 = glyph_id;
66     SkRect skBounds;
67     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint, fakery);
68     skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds);
69     bounds->mLeft = skBounds.fLeft;
70     bounds->mTop = skBounds.fTop;
71     bounds->mRight = skBounds.fRight;
72     bounds->mBottom = skBounds.fBottom;
73 }
74 
GetFontExtent(minikin::MinikinExtent * extent,const minikin::MinikinPaint & paint,const minikin::FontFakery & fakery) const75 void MinikinFontSkia::GetFontExtent(minikin::MinikinExtent* extent,
76                                     const minikin::MinikinPaint& paint,
77                                     const minikin::FontFakery& fakery) const {
78     SkPaint skPaint;
79     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint, fakery);
80     SkPaint::FontMetrics metrics;
81     skPaint.getFontMetrics(&metrics);
82     extent->ascent = metrics.fAscent;
83     extent->descent = metrics.fDescent;
84     extent->line_gap = metrics.fLeading;
85 }
86 
GetSkTypeface() const87 SkTypeface* MinikinFontSkia::GetSkTypeface() const {
88     return mTypeface.get();
89 }
90 
RefSkTypeface() const91 sk_sp<SkTypeface> MinikinFontSkia::RefSkTypeface() const {
92     return mTypeface;
93 }
94 
GetFontData() const95 const void* MinikinFontSkia::GetFontData() const {
96     return mFontData;
97 }
98 
GetFontSize() const99 size_t MinikinFontSkia::GetFontSize() const {
100     return mFontSize;
101 }
102 
GetFontIndex() const103 int MinikinFontSkia::GetFontIndex() const {
104     return mTtcIndex;
105 }
106 
GetAxes() const107 const std::vector<minikin::FontVariation>& MinikinFontSkia::GetAxes() const {
108     return mAxes;
109 }
110 
createFontWithVariation(const std::vector<minikin::FontVariation> & variations) const111 std::shared_ptr<minikin::MinikinFont> MinikinFontSkia::createFontWithVariation(
112         const std::vector<minikin::FontVariation>& variations) const {
113     SkFontArguments params;
114 
115     int ttcIndex;
116     std::unique_ptr<SkStreamAsset> stream(mTypeface->openStream(&ttcIndex));
117     LOG_ALWAYS_FATAL_IF(stream == nullptr, "openStream failed");
118 
119     params.setCollectionIndex(ttcIndex);
120     std::vector<SkFontArguments::Axis> skAxes;
121     skAxes.resize(variations.size());
122     for (size_t i = 0; i < variations.size(); i++) {
123         skAxes[i].fTag = variations[i].axisTag;
124         skAxes[i].fStyleValue = SkFloatToScalar(variations[i].value);
125     }
126     params.setAxes(skAxes.data(), skAxes.size());
127     sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
128     sk_sp<SkTypeface> face(fm->makeFromStream(std::move(stream), params));
129 
130     return std::make_shared<MinikinFontSkia>(std::move(face), mFontData, mFontSize, ttcIndex,
131                                              variations);
132 }
133 
packPaintFlags(const SkPaint * paint)134 uint32_t MinikinFontSkia::packPaintFlags(const SkPaint* paint) {
135     uint32_t flags = paint->getFlags();
136     SkPaint::Hinting hinting = paint->getHinting();
137     // select only flags that might affect text layout
138     flags &= (SkPaint::kAntiAlias_Flag | SkPaint::kFakeBoldText_Flag | SkPaint::kLinearText_Flag |
139               SkPaint::kSubpixelText_Flag | SkPaint::kDevKernText_Flag |
140               SkPaint::kEmbeddedBitmapText_Flag | SkPaint::kAutoHinting_Flag |
141               SkPaint::kVerticalText_Flag);
142     flags |= (hinting << 16);
143     return flags;
144 }
145 
unpackPaintFlags(SkPaint * paint,uint32_t paintFlags)146 void MinikinFontSkia::unpackPaintFlags(SkPaint* paint, uint32_t paintFlags) {
147     paint->setFlags(paintFlags & SkPaint::kAllFlags);
148     paint->setHinting(static_cast<SkPaint::Hinting>(paintFlags >> 16));
149 }
150 
populateSkPaint(SkPaint * paint,const MinikinFont * font,minikin::FontFakery fakery)151 void MinikinFontSkia::populateSkPaint(SkPaint* paint, const MinikinFont* font,
152                                       minikin::FontFakery fakery) {
153     paint->setTypeface(reinterpret_cast<const MinikinFontSkia*>(font)->RefSkTypeface());
154     paint->setFakeBoldText(paint->isFakeBoldText() || fakery.isFakeBold());
155     if (fakery.isFakeItalic()) {
156         paint->setTextSkewX(paint->getTextSkewX() - 0.25f);
157     }
158 }
159 }
160