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