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_GRAPHICS_MINIKIN_UTILS_H_
25#define _ANDROID_GRAPHICS_MINIKIN_UTILS_H_
26
27#include <cutils/compiler.h>
28#include <minikin/Layout.h>
29#include "Paint.h"
30#include "MinikinSkia.h"
31#include "Typeface.h"
32
33namespace android {
34
35class MinikinUtils {
36public:
37    ANDROID_API static FontStyle prepareMinikinPaint(MinikinPaint* minikinPaint, FontCollection** pFont,
38            const Paint* paint, Typeface* typeface);
39
40    ANDROID_API static void doLayout(Layout* layout, const Paint* paint, int bidiFlags,
41            Typeface* typeface, const uint16_t* buf, size_t start, size_t count,
42            size_t bufSize);
43
44    ANDROID_API static float measureText(const Paint* paint, int bidiFlags, Typeface* typeface,
45            const uint16_t* buf, size_t start, size_t count, size_t bufSize, float *advances);
46
47    ANDROID_API static bool hasVariationSelector(Typeface* typeface, uint32_t codepoint, uint32_t vs);
48
49    ANDROID_API static float xOffsetForTextAlign(Paint* paint, const Layout& layout);
50
51    ANDROID_API static float hOffsetForTextAlign(Paint* 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    ANDROID_API static void forFontRun(const Layout& layout, Paint* 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_GRAPHICS_MINIKIN_UTILS_H_
83