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 | |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 17 | #define LOG_TAG "TextLayout" |
| 18 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 19 | #include "TextLayout.h" |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 20 | #include "TextLayoutCache.h" |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android_runtime/AndroidRuntime.h> |
| 23 | |
| 24 | #include "SkTemplates.h" |
| 25 | #include "unicode/ubidi.h" |
| 26 | #include "unicode/ushape.h" |
| 27 | #include <utils/Log.h> |
| 28 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 29 | namespace android { |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 30 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 31 | // Draws or gets the path of a paragraph of text on a single line, running bidi and shaping. |
| 32 | // This will draw if canvas is not null, otherwise path must be non-null and it will create |
| 33 | // a path representing the text that would have been drawn. |
| 34 | void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 35 | jfloat x, jfloat y, SkPath *path) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 36 | sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 37 | text, 0, len, len); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 38 | if (value == NULL) { |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 39 | return ; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 40 | } |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 41 | SkScalar x_ = SkFloatToScalar(x); |
| 42 | SkScalar y_ = SkFloatToScalar(y); |
Fabrice Di Meglio | c511bee8 | 2012-01-05 13:30:54 -0800 | [diff] [blame] | 43 | // Beware: this needs Glyph encoding (already done on the Paint constructor) |
| 44 | paint->getTextPath(value->getGlyphs(), value->getGlyphsCount() * 2, x_, y_, path); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 47 | void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 48 | jint count, jint contextCount, |
Fabrice Di Meglio | 79df532 | 2011-09-19 15:17:56 -0700 | [diff] [blame] | 49 | jfloat* resultAdvances, jfloat* resultTotalAdvance) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 50 | sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 51 | chars, start, count, contextCount); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 52 | if (value == NULL) { |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 53 | return ; |
| 54 | } |
| 55 | if (resultAdvances) { |
| 56 | memcpy(resultAdvances, value->getAdvances(), value->getAdvancesCount() * sizeof(jfloat)); |
| 57 | } |
| 58 | if (resultTotalAdvance) { |
| 59 | *resultTotalAdvance = value->getTotalAdvance(); |
Fabrice Di Meglio | af033ca | 2011-06-06 11:51:46 -0700 | [diff] [blame] | 60 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 63 | void TextLayout::getTextPath(SkPaint *paint, const jchar *text, jsize len, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 64 | jfloat x, jfloat y, SkPath *path) { |
| 65 | handleText(paint, text, len, x, y, path); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 68 | void TextLayout::drawTextOnPath(SkPaint* paint, const jchar* text, int count, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 69 | jfloat hOffset, jfloat vOffset, |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 70 | SkPath* path, SkCanvas* canvas) { |
| 71 | |
| 72 | SkScalar h_ = SkFloatToScalar(hOffset); |
| 73 | SkScalar v_ = SkFloatToScalar(vOffset); |
| 74 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 75 | sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint, |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame^] | 76 | text, 0, count, count); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 77 | if (value == NULL) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 78 | return; |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 79 | } |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 80 | |
Fabrice Di Meglio | c511bee8 | 2012-01-05 13:30:54 -0800 | [diff] [blame] | 81 | // Beware: this needs Glyph encoding (already done on the Paint constructor) |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 82 | canvas->drawTextOnPathHV(value->getGlyphs(), value->getGlyphsCount() * 2, *path, h_, v_, *paint); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 85 | } |