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