Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 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 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 23 | #include "TextLayoutCache.h" |
| 24 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Fabrice Di Meglio | dd347df | 2011-02-17 13:22:08 -0800 | [diff] [blame] | 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 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Turn on for using the Cache |
| 41 | */ |
| 42 | #define USE_TEXT_LAYOUT_CACHE 1 |
| 43 | |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 44 | |
| 45 | #if USE_TEXT_LAYOUT_CACHE |
| 46 | static TextLayoutCache gTextLayoutCache; |
| 47 | #endif |
| 48 | |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 49 | enum { |
| 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 | |
| 60 | enum { |
| 61 | kDirection_LTR = 0, |
| 62 | kDirection_RTL = 1, |
| 63 | |
| 64 | kDirection_Mask = 0x1 |
| 65 | }; |
| 66 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 67 | class TextLayout { |
| 68 | public: |
| 69 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 70 | /* |
| 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 Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 77 | static void getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start, |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 78 | jint count, jint contextCount, jint dirFlags, |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 79 | jfloat* resultAdvances, jfloat& resultTotalAdvance); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 80 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 81 | 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 Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 89 | static void drawText(SkPaint* paint, const jchar* text, jsize len, |
| 90 | jint bidiFlags, jfloat x, jfloat y, SkCanvas* canvas); |
| 91 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 92 | static void getTextPath(SkPaint* paint, const jchar* text, jsize len, |
| 93 | jint bidiFlags, jfloat x, jfloat y, SkPath* path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 94 | |
| 95 | static void drawTextOnPath(SkPaint* paint, const jchar* text, jsize len, |
| 96 | int bidiFlags, jfloat hOffset, jfloat vOffset, |
| 97 | SkPath* path, SkCanvas* canvas); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 98 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 99 | static bool prepareText(SkPaint* paint, const jchar* text, jsize len, jint bidiFlags, |
Romain Guy | 9226298 | 2010-07-26 11:17:54 -0700 | [diff] [blame] | 100 | const jchar** outText, int32_t* outBytes, jchar** outBuffer); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 101 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 102 | static bool prepareRtlTextRun(const jchar* context, jsize start, jsize& count, |
| 103 | jsize contextCount, jchar* shaped); |
| 104 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 105 | |
| 106 | private: |
| 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 Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 109 | jchar* shaped, UErrorCode& status); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 110 | static jint layoutLine(const jchar* text, jint len, jint flags, int &dir, jchar* buffer, |
| 111 | UErrorCode &status); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 112 | static void handleText(SkPaint* paint, const jchar* text, jsize len, |
| 113 | int bidiFlags, jfloat x, jfloat y, SkCanvas* canvas, SkPath* path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 114 | }; |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 115 | } // namespace android |