TextLayout.h revision 54dc642cc1e188b9eeecadb648b9e8c610f4b857
1/*
2 * Copyright (C) 2010 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#include "jni.h"
18
19#include "SkCanvas.h"
20#include "SkPaint.h"
21#include "unicode/utypes.h"
22
23#include "TextLayoutCache.h"
24
25namespace android {
26
27#define UNICODE_NOT_A_CHAR              0xffff
28#define UNICODE_ZWSP                    0x200b
29#define UNICODE_FIRST_LOW_SURROGATE     0xdc00
30#define UNICODE_FIRST_HIGH_SURROGATE    0xd800
31#define UNICODE_FIRST_PRIVATE_USE       0xe000
32#define UNICODE_FIRST_RTL_CHAR          0x0590
33
34/*
35 * Temporary buffer size
36 */
37#define CHAR_BUFFER_SIZE 80
38
39/**
40 * Turn on for using the Cache
41 */
42#define USE_TEXT_LAYOUT_CACHE 1
43
44enum {
45    kBidi_LTR = 0,
46    kBidi_RTL = 1,
47    kBidi_Default_LTR = 2,
48    kBidi_Default_RTL = 3,
49    kBidi_Force_LTR = 4,
50    kBidi_Force_RTL = 5,
51
52    kBidi_Mask = 0x7
53};
54
55enum {
56    kDirection_LTR = 0,
57    kDirection_RTL = 1,
58
59    kDirection_Mask = 0x1
60};
61
62class TextLayout {
63public:
64
65    /*
66     * Draws a unidirectional run of text.
67     */
68    static void drawTextRun(SkPaint* paint, const jchar* chars,
69                            jint start, jint count, jint contextCount,
70                            int dirFlags, jfloat x, jfloat y, SkCanvas* canvas);
71
72    static void getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
73                                   jint count, jint contextCount, jint dirFlags,
74                                   jfloat* resultAdvances, jfloat& resultTotalAdvance);
75
76    static void getTextRunAdvancesICU(SkPaint* paint, const jchar* chars, jint start,
77                                   jint count, jint contextCount, jint dirFlags,
78                                   jfloat* resultAdvances, jfloat& resultTotalAdvance);
79
80    static void getTextRunAdvancesHB(SkPaint* paint, const jchar* chars, jint start,
81                                   jint count, jint contextCount, jint dirFlags,
82                                   jfloat* resultAdvances, jfloat& resultTotalAdvance);
83
84    static void drawText(SkPaint* paint, const jchar* text, jsize len,
85                         jint bidiFlags, jfloat x, jfloat y, SkCanvas* canvas);
86
87    static void getTextPath(SkPaint* paint, const jchar* text, jsize len,
88                            jint bidiFlags, jfloat x, jfloat y, SkPath* path);
89
90    static void drawTextOnPath(SkPaint* paint, const jchar* text, jsize len,
91                               int bidiFlags, jfloat hOffset, jfloat vOffset,
92                               SkPath* path, SkCanvas* canvas);
93
94    static bool prepareText(SkPaint* paint, const jchar* text, jsize len, jint bidiFlags,
95        const jchar** outText, int32_t* outBytes, jchar** outBuffer);
96
97    static bool prepareRtlTextRun(const jchar* context, jsize start, jsize& count,
98        jsize contextCount, jchar* shaped);
99
100
101private:
102    static bool needsLayout(const jchar* text, jint len, jint bidiFlags);
103    static int shapeRtlText(const jchar* context, jsize start, jsize count, jsize contextCount,
104                            jchar* shaped, UErrorCode& status);
105    static jint layoutLine(const jchar* text, jint len, jint flags, int &dir, jchar* buffer,
106                           UErrorCode &status);
107    static void handleText(SkPaint* paint, const jchar* text, jsize len,
108                           int bidiFlags, jfloat x, jfloat y, SkCanvas* canvas, SkPath* path);
109
110    static void computeAdvancesWithICU(SkPaint* paint, const UChar* chars,
111            size_t start, size_t count, size_t contextCount, int dirFlags,
112            jfloat* outAdvances, jfloat* outTotalAdvance);
113};
114} // namespace android
115