blob: b77236c572625f65a0b53def44f0319ab7e345f5 [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
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080017#define LOG_TAG "TextLayout"
18
Doug Feltf7cb1f72010-07-01 16:20:43 -070019#include "TextLayout.h"
Fabrice Di Megliod313c662011-02-24 19:56:18 -080020#include "TextLayoutCache.h"
Doug Feltf7cb1f72010-07-01 16:20:43 -070021
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 Feltf7cb1f72010-07-01 16:20:43 -070029namespace android {
Fabrice Di Megliod313c662011-02-24 19:56:18 -080030
Romain Guye8e62a42010-07-23 18:55:21 -070031// 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.
34void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080035 jfloat x, jfloat y, SkPath *path) {
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080036 sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080037 text, 0, len, len);
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080038 if (value == NULL) {
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080039 return ;
Romain Guye8e62a42010-07-23 18:55:21 -070040 }
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080041 SkScalar x_ = SkFloatToScalar(x);
42 SkScalar y_ = SkFloatToScalar(y);
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -080043 // Beware: this needs Glyph encoding (already done on the Paint constructor)
44 paint->getTextPath(value->getGlyphs(), value->getGlyphsCount() * 2, x_, y_, path);
Romain Guy61c8c9c2010-08-09 20:48:09 -070045}
46
Fabrice Di Megliod313c662011-02-24 19:56:18 -080047void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080048 jint count, jint contextCount,
Fabrice Di Meglio79df5322011-09-19 15:17:56 -070049 jfloat* resultAdvances, jfloat* resultTotalAdvance) {
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080050 sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080051 chars, start, count, contextCount);
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080052 if (value == NULL) {
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080053 return ;
54 }
55 if (resultAdvances) {
56 memcpy(resultAdvances, value->getAdvances(), value->getAdvancesCount() * sizeof(jfloat));
57 }
58 if (resultTotalAdvance) {
59 *resultTotalAdvance = value->getTotalAdvance();
Fabrice Di Meglioaf033ca2011-06-06 11:51:46 -070060 }
Doug Feltf7cb1f72010-07-01 16:20:43 -070061}
62
Doug Feltf7cb1f72010-07-01 16:20:43 -070063void TextLayout::getTextPath(SkPaint *paint, const jchar *text, jsize len,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080064 jfloat x, jfloat y, SkPath *path) {
65 handleText(paint, text, len, x, y, path);
Doug Feltf7cb1f72010-07-01 16:20:43 -070066}
67
Doug Feltf7cb1f72010-07-01 16:20:43 -070068void TextLayout::drawTextOnPath(SkPaint* paint, const jchar* text, int count,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080069 jfloat hOffset, jfloat vOffset,
Doug Feltf7cb1f72010-07-01 16:20:43 -070070 SkPath* path, SkCanvas* canvas) {
71
72 SkScalar h_ = SkFloatToScalar(hOffset);
73 SkScalar v_ = SkFloatToScalar(vOffset);
74
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080075 sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
Fabrice Di Meglio6d9fe5b2013-02-11 18:27:34 -080076 text, 0, count, count);
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080077 if (value == NULL) {
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080078 return;
Doug Feltf7cb1f72010-07-01 16:20:43 -070079 }
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080080
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -080081 // Beware: this needs Glyph encoding (already done on the Paint constructor)
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080082 canvas->drawTextOnPathHV(value->getGlyphs(), value->getGlyphsCount() * 2, *path, h_, v_, *paint);
Doug Feltf7cb1f72010-07-01 16:20:43 -070083}
84
Doug Feltf7cb1f72010-07-01 16:20:43 -070085}