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 <log/log.h>
29 #include <minikin/Layout.h>
30 #include "MinikinSkia.h"
31 #include "Paint.h"
32 #include "Typeface.h"
33 
34 namespace minikin {
35 class MeasuredText;
36 }  // namespace minikin
37 
38 namespace android {
39 
40 class MinikinUtils {
41 public:
42     static minikin::MinikinPaint prepareMinikinPaint(const Paint* paint,
43                                                                  const Typeface* typeface);
44 
45     static minikin::Layout doLayout(const Paint* paint, minikin::Bidi bidiFlags,
46                                                 const Typeface* typeface, const uint16_t* buf,
47                                                 size_t bufSize, size_t start, size_t count,
48                                                 size_t contextStart, size_t contextCount,
49                                                 minikin::MeasuredText* mt);
50 
51     static void getBounds(const Paint* paint, minikin::Bidi bidiFlags, const Typeface* typeface,
52                           const uint16_t* buf, size_t bufSize, minikin::MinikinRect* out);
53 
54     static float measureText(const Paint* paint, minikin::Bidi bidiFlags, const Typeface* typeface,
55                              const uint16_t* buf, size_t start, size_t count, size_t bufSize,
56                              float* advances, minikin::MinikinRect* bounds, uint32_t* clusterCount);
57 
58     static minikin::MinikinExtent getFontExtent(const Paint* paint, minikin::Bidi bidiFlags,
59                                                 const Typeface* typeface, const uint16_t* buf,
60                                                 size_t start, size_t count, size_t bufSize);
61 
62     static bool hasVariationSelector(const Typeface* typeface, uint32_t codepoint,
63                                                  uint32_t vs);
64 
65     static float xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout);
66 
67     static float hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
68                                                  const SkPath& path);
69     // f is a functor of type void f(size_t start, size_t end);
70     template <typename F>
forFontRun(const minikin::Layout & layout,Paint * paint,F & f)71     static void forFontRun(const minikin::Layout& layout, Paint* paint, F& f) {
72         float saveSkewX = paint->getSkFont().getSkewX();
73         bool savefakeBold = paint->getSkFont().isEmbolden();
74         const minikin::MinikinFont* curFont = nullptr;
75         size_t start = 0;
76         size_t nGlyphs = layout.nGlyphs();
77         for (size_t i = 0; i < nGlyphs; i++) {
78             const minikin::MinikinFont* nextFont = layout.typeface(i).get();
79             if (i > 0 && nextFont != curFont) {
80                 SkFont* skfont = &paint->getSkFont();
81                 MinikinFontSkia::populateSkFont(skfont, curFont, layout.getFakery(start));
82                 f(start, i);
83                 skfont->setSkewX(saveSkewX);
84                 skfont->setEmbolden(savefakeBold);
85                 start = i;
86             }
87             curFont = nextFont;
88         }
89         if (nGlyphs > start) {
90             SkFont* skfont = &paint->getSkFont();
91             MinikinFontSkia::populateSkFont(skfont, curFont, layout.getFakery(start));
92             f(start, nGlyphs);
93             skfont->setSkewX(saveSkewX);
94             skfont->setEmbolden(savefakeBold);
95         }
96     }
97 };
98 
99 }  // namespace android
100 
101 #endif  // _ANDROID_GRAPHICS_MINIKIN_UTILS_H_
102