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 "MinikinSkia.h"
30 #include "Paint.h"
31 #include "Typeface.h"
32 #include <log/log.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 start, size_t count, size_t bufSize,
48                                                 minikin::MeasuredText* mt);
49 
50     ANDROID_API static float measureText(const Paint* paint, minikin::Bidi bidiFlags,
51                                          const Typeface* typeface, const uint16_t* buf,
52                                          size_t start, size_t count, size_t bufSize,
53                                          float* advances);
54 
55     ANDROID_API static bool hasVariationSelector(const Typeface* typeface, uint32_t codepoint,
56                                                  uint32_t vs);
57 
58     ANDROID_API static float xOffsetForTextAlign(Paint* paint, const minikin::Layout& layout);
59 
60     ANDROID_API static float hOffsetForTextAlign(Paint* paint, const minikin::Layout& layout,
61                                                  const SkPath& path);
62     // f is a functor of type void f(size_t start, size_t end);
63     template <typename F>
forFontRun(const minikin::Layout & layout,Paint * paint,F & f)64     ANDROID_API static void forFontRun(const minikin::Layout& layout, Paint* paint, F& f) {
65         float saveSkewX = paint->getTextSkewX();
66         bool savefakeBold = paint->isFakeBoldText();
67         const minikin::MinikinFont* curFont = nullptr;
68         size_t start = 0;
69         size_t nGlyphs = layout.nGlyphs();
70         for (size_t i = 0; i < nGlyphs; i++) {
71             const minikin::MinikinFont* nextFont = layout.getFont(i);
72             if (i > 0 && nextFont != curFont) {
73                 MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
74                 f(start, i);
75                 paint->setTextSkewX(saveSkewX);
76                 paint->setFakeBoldText(savefakeBold);
77                 start = i;
78             }
79             curFont = nextFont;
80         }
81         if (nGlyphs > start) {
82             MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
83             f(start, nGlyphs);
84             paint->setTextSkewX(saveSkewX);
85             paint->setFakeBoldText(savefakeBold);
86         }
87     }
88 };
89 
90 }  // namespace android
91 
92 #endif  // _ANDROID_GRAPHICS_MINIKIN_UTILS_H_
93