• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <log/log.h>
20 #include <SkFontDescriptor.h>
21 #include <SkFontMgr.h>
22 #include <SkPaint.h>
23 #include <SkTypeface.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()), mTypeface(std::move(typeface)), mFontData(fontData),
30     mFontSize(fontSize), mTtcIndex(ttcIndex), mAxes(axes) {
31 }
32 
MinikinFontSkia_SetSkiaPaint(const minikin::MinikinFont * font,SkPaint * skPaint,const minikin::MinikinPaint & paint)33 static void MinikinFontSkia_SetSkiaPaint(const minikin::MinikinFont* font, SkPaint* skPaint,
34         const minikin::MinikinPaint& paint) {
35     skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
36     skPaint->setTextSize(paint.size);
37     skPaint->setTextScaleX(paint.scaleX);
38     skPaint->setTextSkewX(paint.skewX);
39     MinikinFontSkia::unpackPaintFlags(skPaint, paint.paintFlags);
40     // Apply font fakery on top of user-supplied flags.
41     MinikinFontSkia::populateSkPaint(skPaint, font, paint.fakery);
42 }
43 
GetHorizontalAdvance(uint32_t glyph_id,const minikin::MinikinPaint & paint) const44 float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id,
45         const minikin::MinikinPaint &paint) const {
46     SkPaint skPaint;
47     uint16_t glyph16 = glyph_id;
48     SkScalar skWidth;
49     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint);
50     skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL);
51 #ifdef VERBOSE
52     ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id, skWidth);
53 #endif
54     return skWidth;
55 }
56 
GetBounds(minikin::MinikinRect * bounds,uint32_t glyph_id,const minikin::MinikinPaint & paint) const57 void MinikinFontSkia::GetBounds(minikin::MinikinRect* bounds, uint32_t glyph_id,
58         const minikin::MinikinPaint& paint) const {
59     SkPaint skPaint;
60     uint16_t glyph16 = glyph_id;
61     SkRect skBounds;
62     MinikinFontSkia_SetSkiaPaint(this, &skPaint, paint);
63     skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds);
64     bounds->mLeft = skBounds.fLeft;
65     bounds->mTop = skBounds.fTop;
66     bounds->mRight = skBounds.fRight;
67     bounds->mBottom = skBounds.fBottom;
68 }
69 
GetSkTypeface() const70 SkTypeface *MinikinFontSkia::GetSkTypeface() const {
71     return mTypeface.get();
72 }
73 
RefSkTypeface() const74 sk_sp<SkTypeface> MinikinFontSkia::RefSkTypeface() const {
75     return mTypeface;
76 }
77 
GetFontData() const78 const void* MinikinFontSkia::GetFontData() const {
79     return mFontData;
80 }
81 
GetFontSize() const82 size_t MinikinFontSkia::GetFontSize() const {
83     return mFontSize;
84 }
85 
GetFontIndex() const86 int MinikinFontSkia::GetFontIndex() const {
87     return mTtcIndex;
88 }
89 
GetAxes() const90 const std::vector<minikin::FontVariation>& MinikinFontSkia::GetAxes() const {
91     return mAxes;
92 }
93 
createFontWithVariation(const std::vector<minikin::FontVariation> & variations) const94 std::shared_ptr<minikin::MinikinFont> MinikinFontSkia::createFontWithVariation(
95         const std::vector<minikin::FontVariation>& variations) const {
96     SkFontMgr::FontParameters params;
97 
98     int ttcIndex;
99     SkStreamAsset* stream = mTypeface->openStream(&ttcIndex);
100     LOG_ALWAYS_FATAL_IF(stream == nullptr, "openStream failed");
101 
102     params.setCollectionIndex(ttcIndex);
103     std::vector<SkFontMgr::FontParameters::Axis> skAxes;
104     skAxes.resize(variations.size());
105     for (size_t i = 0; i < variations.size(); i++) {
106         skAxes[i].fTag = variations[i].axisTag;
107         skAxes[i].fStyleValue = SkFloatToScalar(variations[i].value);
108     }
109     params.setAxes(skAxes.data(), skAxes.size());
110     sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
111     sk_sp<SkTypeface> face(fm->createFromStream(stream, params));
112 
113     return std::make_shared<MinikinFontSkia>(std::move(face), mFontData, mFontSize, ttcIndex,
114             variations);
115 }
116 
packPaintFlags(const SkPaint * paint)117 uint32_t MinikinFontSkia::packPaintFlags(const SkPaint* paint) {
118     uint32_t flags = paint->getFlags();
119     SkPaint::Hinting hinting = paint->getHinting();
120     // select only flags that might affect text layout
121     flags &= (SkPaint::kAntiAlias_Flag | SkPaint::kFakeBoldText_Flag | SkPaint::kLinearText_Flag |
122             SkPaint::kSubpixelText_Flag | SkPaint::kDevKernText_Flag |
123             SkPaint::kEmbeddedBitmapText_Flag | SkPaint::kAutoHinting_Flag |
124             SkPaint::kVerticalText_Flag);
125     flags |= (hinting << 16);
126     return flags;
127 }
128 
unpackPaintFlags(SkPaint * paint,uint32_t paintFlags)129 void MinikinFontSkia::unpackPaintFlags(SkPaint* paint, uint32_t paintFlags) {
130     paint->setFlags(paintFlags & SkPaint::kAllFlags);
131     paint->setHinting(static_cast<SkPaint::Hinting>(paintFlags >> 16));
132 }
133 
populateSkPaint(SkPaint * paint,const MinikinFont * font,minikin::FontFakery fakery)134 void MinikinFontSkia::populateSkPaint(SkPaint* paint, const MinikinFont* font,
135         minikin::FontFakery fakery) {
136     paint->setTypeface(reinterpret_cast<const MinikinFontSkia*>(font)->RefSkTypeface());
137     paint->setFakeBoldText(paint->isFakeBoldText() || fakery.isFakeBold());
138     if (fakery.isFakeItalic()) {
139         paint->setTextSkewX(paint->getTextSkewX() - 0.25f);
140     }
141 }
142 
143 }
144