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