Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 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 |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 19 | * Paint and so on. |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 20 | **/ |
| 21 | |
| 22 | // TODO: does this really need to be separate from MinikinSkia? |
| 23 | |
| 24 | #ifndef ANDROID_MINIKIN_UTILS_H |
| 25 | #define ANDROID_MINIKIN_UTILS_H |
| 26 | |
Behdad Esfahbod | e21e2a2 | 2014-07-29 15:06:26 -0400 | [diff] [blame] | 27 | #include <minikin/Layout.h> |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 28 | #include "Paint.h" |
Behdad Esfahbod | e21e2a2 | 2014-07-29 15:06:26 -0400 | [diff] [blame] | 29 | #include "MinikinSkia.h" |
| 30 | #include "TypefaceImpl.h" |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 31 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Raph Levien | 3660789 | 2014-06-25 16:32:13 -0700 | [diff] [blame] | 34 | // TODO: these should be defined in Minikin's Layout.h |
| 35 | enum { |
| 36 | kBidi_LTR = 0, |
| 37 | kBidi_RTL = 1, |
| 38 | kBidi_Default_LTR = 2, |
| 39 | kBidi_Default_RTL = 3, |
| 40 | kBidi_Force_LTR = 4, |
| 41 | kBidi_Force_RTL = 5, |
| 42 | |
| 43 | kBidi_Mask = 0x7 |
| 44 | }; |
| 45 | |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 46 | class MinikinUtils { |
| 47 | public: |
Behdad Esfahbod | 63c5c78 | 2014-07-25 14:54:46 -0400 | [diff] [blame] | 48 | static void doLayout(Layout* layout, const Paint* paint, int bidiFlags, TypefaceImpl* typeface, |
| 49 | const uint16_t* buf, size_t start, size_t count, size_t bufSize); |
Raph Levien | e95b585 | 2014-05-31 00:02:19 -0700 | [diff] [blame] | 50 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 51 | static float xOffsetForTextAlign(Paint* paint, const Layout& layout); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 52 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 53 | static float hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 54 | // f is a functor of type void f(size_t start, size_t end); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 55 | template <typename F> |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 56 | static void forFontRun(const Layout& layout, Paint* paint, F& f) { |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 57 | float saveSkewX = paint->getTextSkewX(); |
| 58 | bool savefakeBold = paint->isFakeBoldText(); |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 59 | MinikinFont* curFont = NULL; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 60 | size_t start = 0; |
| 61 | size_t nGlyphs = layout.nGlyphs(); |
| 62 | for (size_t i = 0; i < nGlyphs; i++) { |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 63 | MinikinFont* nextFont = layout.getFont(i); |
| 64 | if (i > 0 && nextFont != curFont) { |
| 65 | MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start)); |
| 66 | f(start, i); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 67 | paint->setTextSkewX(saveSkewX); |
| 68 | paint->setFakeBoldText(savefakeBold); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 69 | start = i; |
| 70 | } |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 71 | curFont = nextFont; |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 72 | } |
| 73 | if (nGlyphs > start) { |
Raph Levien | 1fc0fa8 | 2014-06-06 18:05:22 -0700 | [diff] [blame] | 74 | MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start)); |
| 75 | f(start, nGlyphs); |
Raph Levien | 0627ec0 | 2014-06-06 22:31:15 -0700 | [diff] [blame] | 76 | paint->setTextSkewX(saveSkewX); |
| 77 | paint->setFakeBoldText(savefakeBold); |
Raph Levien | f2114d5 | 2014-06-01 15:54:47 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace android |
| 83 | |
| 84 | #endif // ANDROID_MINIKIN_UTILS_H |