blob: 9bb1b921675a1a15e8186dd8ff22c7fa8ceaf1a1 [file] [log] [blame]
Doug Feltf7cb1f72010-07-01 16:20:43 -07001/*
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
Fabrice Di Megliod313c662011-02-24 19:56:18 -080023#include "TextLayoutCache.h"
24
Doug Feltf7cb1f72010-07-01 16:20:43 -070025namespace android {
26
Fabrice Di Megliodd347df2011-02-17 13:22:08 -080027#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
Fabrice Di Megliod313c662011-02-24 19:56:18 -080039/**
40 * Turn on for using the Cache
41 */
42#define USE_TEXT_LAYOUT_CACHE 1
43
Fabrice Di Megliofcf2be12011-04-05 17:02:36 -070044
45#if USE_TEXT_LAYOUT_CACHE
46 static TextLayoutCache gTextLayoutCache;
47#endif
48
Fabrice Di Meglio689e5152011-04-13 16:07:37 -070049enum {
50 kBidi_LTR = 0,
51 kBidi_RTL = 1,
52 kBidi_Default_LTR = 2,
53 kBidi_Default_RTL = 3,
54 kBidi_Force_LTR = 4,
55 kBidi_Force_RTL = 5,
56
57 kBidi_Mask = 0x7
58};
59
60enum {
61 kDirection_LTR = 0,
62 kDirection_RTL = 1,
63
64 kDirection_Mask = 0x1
65};
66
Doug Feltf7cb1f72010-07-01 16:20:43 -070067class TextLayout {
68public:
69
Doug Feltf7cb1f72010-07-01 16:20:43 -070070 /*
71 * Draws a unidirectional run of text.
72 */
73 static void drawTextRun(SkPaint* paint, const jchar* chars,
74 jint start, jint count, jint contextCount,
75 int dirFlags, jfloat x, jfloat y, SkCanvas* canvas);
76
Fabrice Di Megliod313c662011-02-24 19:56:18 -080077 static void getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
Doug Feltf7cb1f72010-07-01 16:20:43 -070078 jint count, jint contextCount, jint dirFlags,
Fabrice Di Megliod313c662011-02-24 19:56:18 -080079 jfloat* resultAdvances, jfloat& resultTotalAdvance);
Doug Feltf7cb1f72010-07-01 16:20:43 -070080
Fabrice Di Meglioeee49c62011-03-24 17:21:23 -070081 static void getTextRunAdvancesICU(SkPaint* paint, const jchar* chars, jint start,
82 jint count, jint contextCount, jint dirFlags,
83 jfloat* resultAdvances, jfloat& resultTotalAdvance);
84
85 static void getTextRunAdvancesHB(SkPaint* paint, const jchar* chars, jint start,
86 jint count, jint contextCount, jint dirFlags,
87 jfloat* resultAdvances, jfloat& resultTotalAdvance);
88
Doug Feltf7cb1f72010-07-01 16:20:43 -070089 static void drawText(SkPaint* paint, const jchar* text, jsize len,
90 jint bidiFlags, jfloat x, jfloat y, SkCanvas* canvas);
91
Fabrice Di Megliod313c662011-02-24 19:56:18 -080092 static void getTextPath(SkPaint* paint, const jchar* text, jsize len,
93 jint bidiFlags, jfloat x, jfloat y, SkPath* path);
Doug Feltf7cb1f72010-07-01 16:20:43 -070094
95 static void drawTextOnPath(SkPaint* paint, const jchar* text, jsize len,
96 int bidiFlags, jfloat hOffset, jfloat vOffset,
97 SkPath* path, SkCanvas* canvas);
Romain Guye8e62a42010-07-23 18:55:21 -070098
Fabrice Di Megliod313c662011-02-24 19:56:18 -080099 static bool prepareText(SkPaint* paint, const jchar* text, jsize len, jint bidiFlags,
Romain Guy92262982010-07-26 11:17:54 -0700100 const jchar** outText, int32_t* outBytes, jchar** outBuffer);
Fabrice Di Megliod313c662011-02-24 19:56:18 -0800101
Romain Guy61c8c9c2010-08-09 20:48:09 -0700102 static bool prepareRtlTextRun(const jchar* context, jsize start, jsize& count,
103 jsize contextCount, jchar* shaped);
104
Doug Feltf7cb1f72010-07-01 16:20:43 -0700105
106private:
107 static bool needsLayout(const jchar* text, jint len, jint bidiFlags);
108 static int shapeRtlText(const jchar* context, jsize start, jsize count, jsize contextCount,
Fabrice Di Megliod313c662011-02-24 19:56:18 -0800109 jchar* shaped, UErrorCode& status);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700110 static jint layoutLine(const jchar* text, jint len, jint flags, int &dir, jchar* buffer,
111 UErrorCode &status);
Fabrice Di Megliod313c662011-02-24 19:56:18 -0800112 static void handleText(SkPaint* paint, const jchar* text, jsize len,
113 int bidiFlags, jfloat x, jfloat y, SkCanvas* canvas, SkPath* path);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700114};
Fabrice Di Megliofcf2be12011-04-05 17:02:36 -0700115} // namespace android