• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <minikin/MeasuredText.h>
24 #include "Paint.h"
25 #include "SkPathMeasure.h"
26 #include "Typeface.h"
27 
28 namespace android {
29 
prepareMinikinPaint(const Paint * paint,const Typeface * typeface)30 minikin::MinikinPaint MinikinUtils::prepareMinikinPaint(const Paint* paint,
31                                                         const Typeface* typeface) {
32     const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
33     const SkFont& font = paint->getSkFont();
34 
35     minikin::MinikinPaint minikinPaint(resolvedFace->fFontCollection);
36     /* Prepare minikin Paint */
37     minikinPaint.size =
38             font.isLinearMetrics() ? font.getSize() : static_cast<int>(font.getSize());
39     minikinPaint.scaleX = font.getScaleX();
40     minikinPaint.skewX = font.getSkewX();
41     minikinPaint.letterSpacing = paint->getLetterSpacing();
42     minikinPaint.wordSpacing = paint->getWordSpacing();
43     minikinPaint.fontFlags = MinikinFontSkia::packFontFlags(font);
44     minikinPaint.localeListId = paint->getMinikinLocaleListId();
45     minikinPaint.familyVariant = paint->getFamilyVariant();
46     minikinPaint.fontStyle = resolvedFace->fStyle;
47     minikinPaint.fontFeatureSettings = paint->getFontFeatureSettings();
48     return minikinPaint;
49 }
50 
doLayout(const Paint * paint,minikin::Bidi bidiFlags,const Typeface * typeface,const uint16_t * buf,size_t bufSize,size_t start,size_t count,size_t contextStart,size_t contextCount,minikin::MeasuredText * mt)51 minikin::Layout MinikinUtils::doLayout(const Paint* paint, minikin::Bidi bidiFlags,
52                                        const Typeface* typeface, const uint16_t* buf,
53                                        size_t bufSize, size_t start, size_t count,
54                                        size_t contextStart, size_t contextCount,
55                                        minikin::MeasuredText* mt) {
56     minikin::MinikinPaint minikinPaint = prepareMinikinPaint(paint, typeface);
57 
58     const minikin::U16StringPiece textBuf(buf, bufSize);
59     const minikin::Range range(start, start + count);
60     const minikin::Range contextRange(contextStart, contextStart + contextCount);
61     const minikin::StartHyphenEdit startHyphen = paint->getStartHyphenEdit();
62     const minikin::EndHyphenEdit endHyphen = paint->getEndHyphenEdit();
63 
64     if (mt == nullptr) {
65         return minikin::Layout(textBuf.substr(contextRange), range - contextStart, bidiFlags,
66                                minikinPaint, startHyphen, endHyphen);
67     } else {
68         return mt->buildLayout(textBuf, range, contextRange, minikinPaint, startHyphen, endHyphen);
69     }
70 }
71 
measureText(const Paint * paint,minikin::Bidi bidiFlags,const Typeface * typeface,const uint16_t * buf,size_t start,size_t count,size_t bufSize,float * advances)72 float MinikinUtils::measureText(const Paint* paint, minikin::Bidi bidiFlags,
73                                 const Typeface* typeface, const uint16_t* buf, size_t start,
74                                 size_t count, size_t bufSize, float* advances) {
75     minikin::MinikinPaint minikinPaint = prepareMinikinPaint(paint, typeface);
76     const minikin::U16StringPiece textBuf(buf, bufSize);
77     const minikin::Range range(start, start + count);
78     const minikin::StartHyphenEdit startHyphen = paint->getStartHyphenEdit();
79     const minikin::EndHyphenEdit endHyphen = paint->getEndHyphenEdit();
80 
81     return minikin::Layout::measureText(textBuf, range, bidiFlags, minikinPaint, startHyphen,
82                                         endHyphen, advances);
83 }
84 
hasVariationSelector(const Typeface * typeface,uint32_t codepoint,uint32_t vs)85 bool MinikinUtils::hasVariationSelector(const Typeface* typeface, uint32_t codepoint, uint32_t vs) {
86     const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
87     return resolvedFace->fFontCollection->hasVariationSelector(codepoint, vs);
88 }
89 
xOffsetForTextAlign(Paint * paint,const minikin::Layout & layout)90 float MinikinUtils::xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout) {
91     switch (paint->getTextAlign()) {
92         case Paint::kCenter_Align:
93             return layout.getAdvance() * -0.5f;
94             break;
95         case Paint::kRight_Align:
96             return -layout.getAdvance();
97             break;
98         default:
99             break;
100     }
101     return 0;
102 }
103 
hOffsetForTextAlign(Paint * paint,const minikin::Layout & layout,const SkPath & path)104 float MinikinUtils::hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
105                                         const SkPath& path) {
106     float align = 0;
107     switch (paint->getTextAlign()) {
108         case Paint::kCenter_Align:
109             align = -0.5f;
110             break;
111         case Paint::kRight_Align:
112             align = -1;
113             break;
114         default:
115             return 0;
116     }
117     SkPathMeasure measure(path, false);
118     return align * (layout.getAdvance() - measure.getLength());
119 }
120 }  // namespace android
121