blob: 9d9e91f19851e152cc317737c2da571933c4dc7f [file] [log] [blame]
Seigo Nonakaf3a19152020-09-14 15:29:42 -07001/*
2 * Copyright (C) 2020 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#undef LOG_TAG
18#define LOG_TAG "TextShaper"
19
20#include "graphics_jni_helpers.h"
21#include <nativehelper/ScopedStringChars.h>
22#include <nativehelper/ScopedPrimitiveArray.h>
23#include <set>
24#include <algorithm>
25
26#include "SkPaint.h"
27#include "SkTypeface.h"
28#include <hwui/MinikinSkia.h>
29#include <hwui/MinikinUtils.h>
30#include <hwui/Paint.h>
31#include <minikin/MinikinPaint.h>
32#include <minikin/MinikinFont.h>
33
34namespace android {
35
36struct LayoutWrapper {
37 LayoutWrapper(minikin::Layout&& layout, float ascent, float descent)
38 : layout(std::move(layout)), ascent(ascent), descent(descent) {}
39 minikin::Layout layout;
40 float ascent;
41 float descent;
42};
43
44static void releaseLayout(jlong ptr) {
45 delete reinterpret_cast<LayoutWrapper*>(ptr);
46}
47
48static jlong shapeTextRun(const uint16_t* text, int textSize, int start, int count,
49 int contextStart, int contextCount, minikin::Bidi bidiFlags,
50 const Paint& paint, const Typeface* typeface) {
51
52 minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(&paint, typeface);
53
54 minikin::Layout layout = MinikinUtils::doLayout(&paint, bidiFlags, typeface,
55 text, textSize, start, count, contextStart, contextCount, nullptr);
56
57 std::set<const minikin::Font*> seenFonts;
58 float overallAscent = 0;
59 float overallDescent = 0;
60 for (int i = 0; i < layout.nGlyphs(); ++i) {
61 const minikin::Font* font = layout.getFont(i);
62 if (seenFonts.find(font) != seenFonts.end()) continue;
63 minikin::MinikinExtent extent = {};
64 font->typeface()->GetFontExtent(&extent, minikinPaint, layout.getFakery(i));
65 overallAscent = std::min(overallAscent, extent.ascent);
66 overallDescent = std::max(overallDescent, extent.descent);
67 }
68
69 std::unique_ptr<LayoutWrapper> ptr = std::make_unique<LayoutWrapper>(
70 std::move(layout), overallAscent, overallDescent
71 );
72
73 return reinterpret_cast<jlong>(ptr.release());
74}
75
76static jlong TextShaper_shapeTextRunChars(JNIEnv *env, jobject, jcharArray charArray,
77 jint start, jint count, jint contextStart, jint contextCount, jboolean isRtl,
78 jlong paintPtr) {
79 ScopedCharArrayRO text(env, charArray);
80 Paint* paint = reinterpret_cast<Paint*>(paintPtr);
81 const Typeface* typeface = paint->getAndroidTypeface();
82 const minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
83 return shapeTextRun(
84 text.get(), text.size(),
85 start, count,
86 contextStart, contextCount,
87 bidiFlags,
88 *paint, typeface);
89
90}
91
92static jlong TextShaper_shapeTextRunString(JNIEnv *env, jobject, jstring string,
93 jint start, jint count, jint contextStart, jint contextCount, jboolean isRtl,
94 jlong paintPtr) {
95 ScopedStringChars text(env, string);
96 Paint* paint = reinterpret_cast<Paint*>(paintPtr);
97 const Typeface* typeface = paint->getAndroidTypeface();
98 const minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
99 return shapeTextRun(
100 text.get(), text.size(),
101 start, count,
102 contextStart, contextCount,
103 bidiFlags,
104 *paint, typeface);
105}
106
107// CriticalNative
108static jint TextShaper_Result_getGlyphCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
109 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
110 return layout->layout.nGlyphs();
111}
112
113// CriticalNative
114static jfloat TextShaper_Result_getTotalAdvance(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
115 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
116 return layout->layout.getAdvance();
117}
118
119// CriticalNative
120static jfloat TextShaper_Result_getAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
121 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
122 return layout->ascent;
123}
124
125// CriticalNative
126static jfloat TextShaper_Result_getDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
127 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
128 return layout->descent;
129}
130
131// CriticalNative
132static jint TextShaper_Result_getGlyphId(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
133 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
134 return layout->layout.getGlyphId(i);
135}
136
137// CriticalNative
138static jfloat TextShaper_Result_getX(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
139 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
140 return layout->layout.getX(i);
141}
142
143// CriticalNative
144static jfloat TextShaper_Result_getY(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
145 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
146 return layout->layout.getY(i);
147}
148
149// CriticalNative
150static jlong TextShaper_Result_getFont(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
151 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
152 return reinterpret_cast<jlong>(layout->layout.getFont(i));
153}
154
155// CriticalNative
156static jlong TextShaper_Result_nReleaseFunc(CRITICAL_JNI_PARAMS) {
157 return reinterpret_cast<jlong>(releaseLayout);
158}
159
160static const JNINativeMethod gMethods[] = {
161 // Fast Natives
162 {"nativeShapeTextRun", "("
163 "[C" // text
164 "I" // start
165 "I" // count
166 "I" // contextStart
167 "I" // contextCount
168 "Z" // isRtl
169 "J)" // paint
170 "J", // LayoutPtr
171 (void*) TextShaper_shapeTextRunChars},
172
173 {"nativeShapeTextRun", "("
174 "Ljava/lang/String;" // text
175 "I" // start
176 "I" // count
177 "I" // contextStart
178 "I" // contextCount
179 "Z" // isRtl
180 "J)" // paint
181 "J", // LayoutPtr
182 (void*) TextShaper_shapeTextRunString},
183
184};
185
186static const JNINativeMethod gResultMethods[] = {
187 { "nGetGlyphCount", "(J)I", (void*)TextShaper_Result_getGlyphCount },
188 { "nGetTotalAdvance", "(J)F", (void*)TextShaper_Result_getTotalAdvance },
189 { "nGetAscent", "(J)F", (void*)TextShaper_Result_getAscent },
190 { "nGetDescent", "(J)F", (void*)TextShaper_Result_getDescent },
191 { "nGetGlyphId", "(JI)I", (void*)TextShaper_Result_getGlyphId },
192 { "nGetX", "(JI)F", (void*)TextShaper_Result_getX },
193 { "nGetY", "(JI)F", (void*)TextShaper_Result_getY },
194 { "nGetFont", "(JI)J", (void*)TextShaper_Result_getFont },
195 { "nReleaseFunc", "()J", (void*)TextShaper_Result_nReleaseFunc },
196};
197
198int register_android_graphics_text_TextShaper(JNIEnv* env) {
199 return RegisterMethodsOrDie(env, "android/graphics/text/TextShaper", gMethods,
200 NELEM(gMethods))
201 + RegisterMethodsOrDie(env, "android/graphics/text/PositionedGlyphs",
202 gResultMethods, NELEM(gResultMethods));
203}
204
205}
206