1 /*
2 * Copyright (C) 2014 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 "MinikinUtils.h"
18
19 #include <string>
20
21 #include <log/log.h>
22
23 #include "Paint.h"
24 #include "SkPathMeasure.h"
25 #include "Typeface.h"
26
27 namespace android {
28
prepareMinikinPaint(minikin::MinikinPaint * minikinPaint,const Paint * paint,Typeface * typeface)29 minikin::FontStyle MinikinUtils::prepareMinikinPaint(minikin::MinikinPaint* minikinPaint,
30 const Paint* paint, Typeface* typeface) {
31 const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
32 minikin::FontStyle resolved = resolvedFace->fStyle;
33
34 /* Prepare minikin FontStyle */
35 minikin::FontVariant minikinVariant = (paint->getFontVariant() == minikin::VARIANT_ELEGANT) ?
36 minikin::VARIANT_ELEGANT : minikin::VARIANT_COMPACT;
37 const uint32_t langListId = paint->getMinikinLangListId();
38 minikin::FontStyle minikinStyle(langListId, minikinVariant, resolved.getWeight(),
39 resolved.getItalic());
40
41 /* Prepare minikin Paint */
42 // Note: it would be nice to handle fractional size values (it would improve smooth zoom
43 // behavior), but historically size has been treated as an int.
44 // TODO: explore whether to enable fractional sizes, possibly when linear text flag is set.
45 minikinPaint->size = (int)paint->getTextSize();
46 minikinPaint->scaleX = paint->getTextScaleX();
47 minikinPaint->skewX = paint->getTextSkewX();
48 minikinPaint->letterSpacing = paint->getLetterSpacing();
49 minikinPaint->wordSpacing = paint->getWordSpacing();
50 minikinPaint->paintFlags = MinikinFontSkia::packPaintFlags(paint);
51 minikinPaint->fontFeatureSettings = paint->getFontFeatureSettings();
52 minikinPaint->hyphenEdit = minikin::HyphenEdit(paint->getHyphenEdit());
53 return minikinStyle;
54 }
55
doLayout(const Paint * paint,int bidiFlags,Typeface * typeface,const uint16_t * buf,size_t start,size_t count,size_t bufSize)56 minikin::Layout MinikinUtils::doLayout(const Paint* paint, int bidiFlags,
57 Typeface* typeface, const uint16_t* buf, size_t start, size_t count,
58 size_t bufSize) {
59 minikin::MinikinPaint minikinPaint;
60 minikin::FontStyle minikinStyle = prepareMinikinPaint(&minikinPaint, paint, typeface);
61 minikin::Layout layout;
62 layout.doLayout(buf, start, count, bufSize, bidiFlags, minikinStyle, minikinPaint,
63 Typeface::resolveDefault(typeface)->fFontCollection);
64 return layout;
65 }
66
measureText(const Paint * paint,int bidiFlags,Typeface * typeface,const uint16_t * buf,size_t start,size_t count,size_t bufSize,float * advances)67 float MinikinUtils::measureText(const Paint* paint, int bidiFlags, Typeface* typeface,
68 const uint16_t* buf, size_t start, size_t count, size_t bufSize, float *advances) {
69 minikin::MinikinPaint minikinPaint;
70 minikin::FontStyle minikinStyle = prepareMinikinPaint(&minikinPaint, paint, typeface);
71 Typeface* resolvedTypeface = Typeface::resolveDefault(typeface);
72 return minikin::Layout::measureText(buf, start, count, bufSize, bidiFlags, minikinStyle,
73 minikinPaint, resolvedTypeface->fFontCollection, advances);
74 }
75
hasVariationSelector(Typeface * typeface,uint32_t codepoint,uint32_t vs)76 bool MinikinUtils::hasVariationSelector(Typeface* typeface, uint32_t codepoint, uint32_t vs) {
77 const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
78 return resolvedFace->fFontCollection->hasVariationSelector(codepoint, vs);
79 }
80
xOffsetForTextAlign(Paint * paint,const minikin::Layout & layout)81 float MinikinUtils::xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout) {
82 switch (paint->getTextAlign()) {
83 case Paint::kCenter_Align:
84 return layout.getAdvance() * -0.5f;
85 break;
86 case Paint::kRight_Align:
87 return -layout.getAdvance();
88 break;
89 default:
90 break;
91 }
92 return 0;
93 }
94
hOffsetForTextAlign(Paint * paint,const minikin::Layout & layout,const SkPath & path)95 float MinikinUtils::hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
96 const SkPath& path) {
97 float align = 0;
98 switch (paint->getTextAlign()) {
99 case Paint::kCenter_Align:
100 align = -0.5f;
101 break;
102 case Paint::kRight_Align:
103 align = -1;
104 break;
105 default:
106 return 0;
107 }
108 SkPathMeasure measure(path, false);
109 return align * (layout.getAdvance() - measure.getLength());
110 }
111
112 }
113