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     ANDROID_API static minikin::MinikinPaint prepareMinikinPaint(const Paint* paint,
43                                                                  const Typeface* typeface);
44 
45     ANDROID_API 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     ANDROID_API static float measureText(const Paint* paint, minikin::Bidi bidiFlags,
52                                          const Typeface* typeface, const uint16_t* buf,
53                                          size_t start, size_t count, size_t bufSize,
54                                          float* advances);
55 
56     ANDROID_API static bool hasVariationSelector(const Typeface* typeface, uint32_t codepoint,
57                                                  uint32_t vs);
58 
59     ANDROID_API static float xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout);
60 
61     ANDROID_API static float hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
62                                                  const SkPath& path);
63     // f is a functor of type void f(size_t start, size_t end);
64     template <typename F>
forFontRun(const minikin::Layout & layout,Paint * paint,F & f)65     ANDROID_API static void forFontRun(const minikin::Layout& layout, Paint* paint, F& f) {
66         float saveSkewX = paint->getSkFont().getSkewX();
67         bool savefakeBold = paint->getSkFont().isEmbolden();
68         const minikin::MinikinFont* curFont = nullptr;
69         size_t start = 0;
70         size_t nGlyphs = layout.nGlyphs();
71         for (size_t i = 0; i < nGlyphs; i++) {
72             const minikin::MinikinFont* nextFont = layout.getFont(i);
73             if (i > 0 && nextFont != curFont) {
74                 SkFont* skfont = &paint->getSkFont();
75                 MinikinFontSkia::populateSkFont(skfont, curFont, layout.getFakery(start));
76                 f(start, i);
77                 skfont->setSkewX(saveSkewX);
78                 skfont->setEmbolden(savefakeBold);
79                 start = i;
80             }
81             curFont = nextFont;
82         }
83         if (nGlyphs > start) {
84             SkFont* skfont = &paint->getSkFont();
85             MinikinFontSkia::populateSkFont(skfont, curFont, layout.getFakery(start));
86             f(start, nGlyphs);
87             skfont->setSkewX(saveSkewX);
88             skfont->setEmbolden(savefakeBold);
89         }
90     }
91 };
92 
93 }  // namespace android
94 
95 #endif  // _ANDROID_GRAPHICS_MINIKIN_UTILS_H_
96