MinikinUtils.cpp revision 6ba30b85ddfbe37c338ee8dde3dd33322eb38d47
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#define LOG_TAG "Minikin"
18#include <cutils/log.h>
19#include <string>
20
21#include "SkPathMeasure.h"
22#include "minikin/Layout.h"
23#include "Paint.h"
24#include "TypefaceImpl.h"
25#include "MinikinSkia.h"
26
27#include "MinikinUtils.h"
28
29namespace android {
30
31// Do an sprintf starting at offset n, abort on overflow
32static int snprintfcat(char* buf, int off, int size, const char* format, ...) {
33    va_list args;
34    va_start(args, format);
35    int n = vsnprintf(buf + off, size - off, format, args);
36    LOG_ALWAYS_FATAL_IF(n >= size - off, "String overflow in setting layout properties");
37    va_end(args);
38    return off + n;
39}
40
41std::string MinikinUtils::setLayoutProperties(Layout* layout, const Paint* paint, int bidiFlags,
42        TypefaceImpl* typeface) {
43    TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(typeface);
44    layout->setFontCollection(resolvedFace->fFontCollection);
45    FontStyle style = resolvedFace->fStyle;
46    char css[256];
47    int off = snprintfcat(css, 0, sizeof(css),
48        "font-size: %d; font-scale-x: %f; font-skew-x: %f; -paint-flags: %d;"
49        " font-weight: %d; font-style: %s; -minikin-bidi: %d;",
50        (int)paint->getTextSize(),
51        paint->getTextScaleX(),
52        paint->getTextSkewX(),
53        MinikinFontSkia::packPaintFlags(paint),
54        style.getWeight() * 100,
55        style.getItalic() ? "italic" : "normal",
56        bidiFlags);
57    SkString langString = paint->getPaintOptionsAndroid().getLanguage().getTag();
58    off = snprintfcat(css, off, sizeof(css), " lang: %s;", langString.c_str());
59    SkPaintOptionsAndroid::FontVariant var = paint->getPaintOptionsAndroid().getFontVariant();
60    const char* varstr = var == SkPaintOptionsAndroid::kElegant_Variant ? "elegant" : "compact";
61    off = snprintfcat(css, off, sizeof(css), " -minikin-variant: %s;", varstr);
62    return std::string(css);
63}
64
65float MinikinUtils::xOffsetForTextAlign(Paint* paint, const Layout& layout) {
66    switch (paint->getTextAlign()) {
67        case Paint::kCenter_Align:
68            return layout.getAdvance() * -0.5f;
69            break;
70        case Paint::kRight_Align:
71            return -layout.getAdvance();
72            break;
73        default:
74            break;
75    }
76    return 0;
77}
78
79float MinikinUtils::hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path) {
80    float align = 0;
81    switch (paint->getTextAlign()) {
82        case Paint::kCenter_Align:
83            align = -0.5f;
84            break;
85        case Paint::kRight_Align:
86            align = -1;
87            break;
88        default:
89            return 0;
90    }
91    SkPathMeasure measure(path, false);
92    return align * (layout.getAdvance() - measure.getLength());
93}
94
95}
96