MinikinUtils.cpp revision 854363e3d2fb2a9e41d8719f4a2f0f8b89440a46
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 "SkPaint.h"
18#include "minikin/Layout.h"
19#include "TypefaceImpl.h"
20#include "MinikinSkia.h"
21
22#include "MinikinUtils.h"
23
24namespace android {
25
26void MinikinUtils::SetLayoutProperties(Layout* layout, const SkPaint* paint, int flags,
27    TypefaceImpl* typeface) {
28    TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(typeface);
29    layout->setFontCollection(resolvedFace->fFontCollection);
30    FontStyle style = resolvedFace->fStyle;
31    char css[256];
32    int off = snprintf(css, sizeof(css),
33        "font-size: %d; font-weight: %d; font-style: %s; -minikin-bidi: %d;",
34        (int)paint->getTextSize(),
35        style.getWeight() * 100,
36        style.getItalic() ? "italic" : "normal",
37        flags);
38    SkString langString = paint->getPaintOptionsAndroid().getLanguage().getTag();
39    off += snprintf(css + off, sizeof(css) - off, " lang: %s;", langString.c_str());
40    SkPaintOptionsAndroid::FontVariant var = paint->getPaintOptionsAndroid().getFontVariant();
41    const char* varstr = var == SkPaintOptionsAndroid::kElegant_Variant ? "elegant" : "compact";
42    off += snprintf(css + off, sizeof(css) - off, " -minikin-variant: %s;", varstr);
43    layout->setProperties(css);
44}
45
46float MinikinUtils::xOffsetForTextAlign(SkPaint* paint, const Layout& layout) {
47    switch (paint->getTextAlign()) {
48        case SkPaint::kCenter_Align:
49            return layout.getAdvance() * -0.5f;
50            break;
51        case SkPaint::kRight_Align:
52            return -layout.getAdvance();
53            break;
54        default:
55            break;
56    }
57    return 0;
58}
59
60}
61