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