• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 namespace 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 
44 enum {
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 
55 enum {
56     kDirection_LTR = 0,
57     kDirection_RTL = 1,
58 
59     kDirection_Mask = 0x1
60 };
61 
62 class TextLayout {
63 public:
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 drawText(SkPaint* paint, const jchar* text, jsize len,
81                          jint bidiFlags, jfloat x, jfloat y, SkCanvas* canvas);
82 
83     static void getTextPath(SkPaint* paint, const jchar* text, jsize len,
84                             jint bidiFlags, jfloat x, jfloat y, SkPath* path);
85 
86     static void drawTextOnPath(SkPaint* paint, const jchar* text, jsize len,
87                                int bidiFlags, jfloat hOffset, jfloat vOffset,
88                                SkPath* path, SkCanvas* canvas);
89 
90     static bool prepareText(SkPaint* paint, const jchar* text, jsize len, jint bidiFlags,
91         const jchar** outText, int32_t* outBytes, jchar** outBuffer);
92 
93     static bool prepareRtlTextRun(const jchar* context, jsize start, jsize& count,
94         jsize contextCount, jchar* shaped);
95 
96 
97 private:
98     static bool needsLayout(const jchar* text, jint len, jint bidiFlags);
99     static int shapeRtlText(const jchar* context, jsize start, jsize count, jsize contextCount,
100                             jchar* shaped, UErrorCode& status);
101     static jint layoutLine(const jchar* text, jint len, jint flags, int &dir, jchar* buffer,
102                            UErrorCode &status);
103     static void handleText(SkPaint* paint, const jchar* text, jsize len,
104                            int bidiFlags, jfloat x, jfloat y, SkCanvas* canvas, SkPath* path);
105 
106     static void computeAdvancesWithICU(SkPaint* paint, const UChar* chars,
107             size_t start, size_t count, size_t contextCount, int dirFlags,
108             jfloat* outAdvances, jfloat* outTotalAdvance);
109 };
110 } // namespace android
111