MinikinUtils.h revision ae1aa85d0c7305bb621f1f8003bd674285aa3b63
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 minikin::FontStyle prepareMinikinPaint(minikin::MinikinPaint* minikinPaint,
38            minikin::FontCollection** pFont, const Paint* paint, Typeface* typeface);
39
40    ANDROID_API static void doLayout(minikin::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,
48            uint32_t vs);
49
50    ANDROID_API static float xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout);
51
52    ANDROID_API static float hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
53            const SkPath& path);
54    // f is a functor of type void f(size_t start, size_t end);
55    template <typename F>
56    ANDROID_API static void forFontRun(const minikin::Layout& layout, Paint* paint, F& f) {
57        float saveSkewX = paint->getTextSkewX();
58        bool savefakeBold = paint->isFakeBoldText();
59        minikin::MinikinFont* curFont = NULL;
60        size_t start = 0;
61        size_t nGlyphs = layout.nGlyphs();
62        for (size_t i = 0; i < nGlyphs; i++) {
63          minikin::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_GRAPHICS_MINIKIN_UTILS_H_
85