blob: 3646c1a5c0aa86ff2148f2139416c868d330853a [file] [log] [blame]
Raph Levien1a73f7322014-01-30 16:06:28 -08001/*
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 Esfahbod6ba30b82014-07-15 16:22:32 -040019 * Paint and so on.
Raph Levien1a73f7322014-01-30 16:06:28 -080020 **/
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 Esfahbode21e2a22014-07-29 15:06:26 -040027#include <minikin/Layout.h>
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040028#include "Paint.h"
Behdad Esfahbode21e2a22014-07-29 15:06:26 -040029#include "MinikinSkia.h"
30#include "TypefaceImpl.h"
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040031
Raph Levien1a73f7322014-01-30 16:06:28 -080032namespace android {
33
Raph Levien36607892014-06-25 16:32:13 -070034// TODO: these should be defined in Minikin's Layout.h
35enum {
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 Levien1a73f7322014-01-30 16:06:28 -080046class MinikinUtils {
47public:
Behdad Esfahbod63c5c782014-07-25 14:54:46 -040048 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 Leviene95b5852014-05-31 00:02:19 -070050
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040051 static float xOffsetForTextAlign(Paint* paint, const Layout& layout);
Raph Levienf2114d52014-06-01 15:54:47 -070052
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040053 static float hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path);
Raph Levien1fc0fa82014-06-06 18:05:22 -070054 // f is a functor of type void f(size_t start, size_t end);
Raph Levienf2114d52014-06-01 15:54:47 -070055 template <typename F>
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040056 static void forFontRun(const Layout& layout, Paint* paint, F& f) {
Raph Levien0627ec02014-06-06 22:31:15 -070057 float saveSkewX = paint->getTextSkewX();
58 bool savefakeBold = paint->isFakeBoldText();
Raph Levien1fc0fa82014-06-06 18:05:22 -070059 MinikinFont* curFont = NULL;
Raph Levienf2114d52014-06-01 15:54:47 -070060 size_t start = 0;
61 size_t nGlyphs = layout.nGlyphs();
62 for (size_t i = 0; i < nGlyphs; i++) {
Raph Levien1fc0fa82014-06-06 18:05:22 -070063 MinikinFont* nextFont = layout.getFont(i);
64 if (i > 0 && nextFont != curFont) {
65 MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
66 f(start, i);
Raph Levien0627ec02014-06-06 22:31:15 -070067 paint->setTextSkewX(saveSkewX);
68 paint->setFakeBoldText(savefakeBold);
Raph Levienf2114d52014-06-01 15:54:47 -070069 start = i;
70 }
Raph Levien1fc0fa82014-06-06 18:05:22 -070071 curFont = nextFont;
Raph Levienf2114d52014-06-01 15:54:47 -070072 }
73 if (nGlyphs > start) {
Raph Levien1fc0fa82014-06-06 18:05:22 -070074 MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
75 f(start, nGlyphs);
Raph Levien0627ec02014-06-06 22:31:15 -070076 paint->setTextSkewX(saveSkewX);
77 paint->setFakeBoldText(savefakeBold);
Raph Levienf2114d52014-06-01 15:54:47 -070078 }
79 }
Raph Levien1a73f7322014-01-30 16:06:28 -080080};
81
82} // namespace android
83
84#endif // ANDROID_MINIKIN_UTILS_H