MinikinUtils.h 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/**
18 * Utilities for making Minikin work, especially from existing objects like
19 * Paint and so on.
20 **/
21
22 // TODO: does this really need to be separate from MinikinSkia?
23
24#ifndef ANDROID_MINIKIN_UTILS_H
25#define ANDROID_MINIKIN_UTILS_H
26
27#include "Paint.h"
28
29namespace android {
30
31// TODO: these should be defined in Minikin's Layout.h
32enum {
33    kBidi_LTR = 0,
34    kBidi_RTL = 1,
35    kBidi_Default_LTR = 2,
36    kBidi_Default_RTL = 3,
37    kBidi_Force_LTR = 4,
38    kBidi_Force_RTL = 5,
39
40    kBidi_Mask = 0x7
41};
42
43class Layout;
44class TypefaceImpl;
45
46class MinikinUtils {
47public:
48    static std::string setLayoutProperties(Layout* layout, const Paint* paint, int bidiFlags,
49            TypefaceImpl* typeface);
50
51    static float xOffsetForTextAlign(Paint* paint, const Layout& layout);
52
53    static float hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path);
54    // f is a functor of type void f(size_t start, size_t end);
55    template <typename F>
56    static void forFontRun(const Layout& layout, Paint* paint, F& f) {
57        float saveSkewX = paint->getTextSkewX();
58        bool savefakeBold = paint->isFakeBoldText();
59        MinikinFont* curFont = NULL;
60        size_t start = 0;
61        size_t nGlyphs = layout.nGlyphs();
62        for (size_t i = 0; i < nGlyphs; i++) {
63            MinikinFont* nextFont = layout.getFont(i);
64            if (i > 0 && nextFont != curFont) {
65                MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
66                f(start, i);
67                paint->setTextSkewX(saveSkewX);
68                paint->setFakeBoldText(savefakeBold);
69                start = i;
70            }
71            curFont = nextFont;
72        }
73        if (nGlyphs > start) {
74            MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
75            f(start, nGlyphs);
76            paint->setTextSkewX(saveSkewX);
77            paint->setFakeBoldText(savefakeBold);
78        }
79    }
80};
81
82}  // namespace android
83
84#endif  // ANDROID_MINIKIN_UTILS_H
85