MinikinUtils.cpp revision c7064146f959caec058980ced144942c8044a169
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
27namespace android {
28
29minikin::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
56minikin::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(Typeface::resolveDefault(typeface)->fFontCollection);
62    layout.doLayout(buf, start, count, bufSize, bidiFlags, minikinStyle, minikinPaint);
63    return layout;
64}
65
66float MinikinUtils::measureText(const Paint* paint, int bidiFlags, Typeface* typeface,
67        const uint16_t* buf, size_t start, size_t count, size_t bufSize, float *advances) {
68    minikin::MinikinPaint minikinPaint;
69    minikin::FontStyle minikinStyle = prepareMinikinPaint(&minikinPaint, paint, typeface);
70    Typeface* resolvedTypeface = Typeface::resolveDefault(typeface);
71    return minikin::Layout::measureText(buf, start, count, bufSize, bidiFlags, minikinStyle,
72            minikinPaint, resolvedTypeface->fFontCollection, advances);
73}
74
75bool MinikinUtils::hasVariationSelector(Typeface* typeface, uint32_t codepoint, uint32_t vs) {
76    const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
77    return resolvedFace->fFontCollection->hasVariationSelector(codepoint, vs);
78}
79
80float MinikinUtils::xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout) {
81    switch (paint->getTextAlign()) {
82        case Paint::kCenter_Align:
83            return layout.getAdvance() * -0.5f;
84            break;
85        case Paint::kRight_Align:
86            return -layout.getAdvance();
87            break;
88        default:
89            break;
90    }
91    return 0;
92}
93
94float MinikinUtils::hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
95        const SkPath& path) {
96    float align = 0;
97    switch (paint->getTextAlign()) {
98        case Paint::kCenter_Align:
99            align = -0.5f;
100            break;
101        case Paint::kRight_Align:
102            align = -1;
103            break;
104        default:
105            return 0;
106    }
107    SkPathMeasure measure(path, false);
108    return align * (layout.getAdvance() - measure.getLength());
109}
110
111}
112