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