Paint.cpp revision ae1aa85d0c7305bb621f1f8003bd674285aa3b63
1/* libs/android_runtime/android/graphics/Paint.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "Paint"
19
20#include <utils/Log.h>
21
22#include "jni.h"
23#include "GraphicsJNI.h"
24#include "core_jni_helpers.h"
25#include <ScopedStringChars.h>
26#include <ScopedUtfChars.h>
27
28#include "SkBlurDrawLooper.h"
29#include "SkColorFilter.h"
30#include "SkMaskFilter.h"
31#include "SkPath.h"
32#include "SkRasterizer.h"
33#include "SkShader.h"
34#include "SkTypeface.h"
35#include "SkXfermode.h"
36#include "unicode/uloc.h"
37#include "unicode/ushape.h"
38#include "utils/Blur.h"
39
40#include <hwui/MinikinSkia.h>
41#include <hwui/MinikinUtils.h>
42#include <hwui/Paint.h>
43#include <hwui/Typeface.h>
44#include <minikin/GraphemeBreak.h>
45#include <minikin/Measurement.h>
46#include <unicode/utf16.h>
47
48#include <cassert>
49#include <cstring>
50#include <memory>
51#include <vector>
52
53namespace android {
54
55struct JMetricsID {
56    jfieldID    top;
57    jfieldID    ascent;
58    jfieldID    descent;
59    jfieldID    bottom;
60    jfieldID    leading;
61};
62
63static jclass   gFontMetrics_class;
64static JMetricsID gFontMetrics_fieldID;
65
66static jclass   gFontMetricsInt_class;
67static JMetricsID gFontMetricsInt_fieldID;
68
69static void defaultSettingsForAndroid(Paint* paint) {
70    // GlyphID encoding is required because we are using Harfbuzz shaping
71    paint->setTextEncoding(Paint::kGlyphID_TextEncoding);
72}
73
74namespace PaintGlue {
75    enum MoveOpt {
76        AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
77    };
78
79    static void deletePaint(Paint* paint) {
80        delete paint;
81    }
82
83    static jlong getNativeFinalizer(JNIEnv*, jobject) {
84        return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deletePaint));
85    }
86
87    static jlong init(JNIEnv* env, jobject) {
88        static_assert(1 <<  0 == SkPaint::kAntiAlias_Flag,          "paint_flags_mismatch");
89        static_assert(1 <<  2 == SkPaint::kDither_Flag,             "paint_flags_mismatch");
90        static_assert(1 <<  3 == SkPaint::kUnderlineText_Flag,      "paint_flags_mismatch");
91        static_assert(1 <<  4 == SkPaint::kStrikeThruText_Flag,     "paint_flags_mismatch");
92        static_assert(1 <<  5 == SkPaint::kFakeBoldText_Flag,       "paint_flags_mismatch");
93        static_assert(1 <<  6 == SkPaint::kLinearText_Flag,         "paint_flags_mismatch");
94        static_assert(1 <<  7 == SkPaint::kSubpixelText_Flag,       "paint_flags_mismatch");
95        static_assert(1 <<  8 == SkPaint::kDevKernText_Flag,        "paint_flags_mismatch");
96        static_assert(1 << 10 == SkPaint::kEmbeddedBitmapText_Flag, "paint_flags_mismatch");
97
98        Paint* obj = new Paint();
99        defaultSettingsForAndroid(obj);
100        return reinterpret_cast<jlong>(obj);
101    }
102
103    static jlong initWithPaint(JNIEnv* env, jobject clazz, jlong paintHandle) {
104        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
105        Paint* obj = new Paint(*paint);
106        return reinterpret_cast<jlong>(obj);
107    }
108
109    static void reset(JNIEnv* env, jobject clazz, jlong objHandle) {
110        Paint* obj = reinterpret_cast<Paint*>(objHandle);
111        obj->reset();
112        defaultSettingsForAndroid(obj);
113    }
114
115    static void assign(JNIEnv* env, jobject clazz, jlong dstPaintHandle, jlong srcPaintHandle) {
116        Paint* dst = reinterpret_cast<Paint*>(dstPaintHandle);
117        const Paint* src = reinterpret_cast<Paint*>(srcPaintHandle);
118        *dst = *src;
119    }
120
121    // Equivalent to the Java Paint's FILTER_BITMAP_FLAG.
122    static const uint32_t sFilterBitmapFlag = 0x02;
123
124    static jint getFlags(JNIEnv* env, jobject, jlong paintHandle) {
125        Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle);
126        uint32_t result = nativePaint->getFlags();
127        result &= ~sFilterBitmapFlag; // Filtering no longer stored in this bit. Mask away.
128        if (nativePaint->getFilterQuality() != kNone_SkFilterQuality) {
129            result |= sFilterBitmapFlag;
130        }
131        return static_cast<jint>(result);
132    }
133
134    static void setFlags(JNIEnv* env, jobject, jlong paintHandle, jint flags) {
135        Paint* nativePaint = reinterpret_cast<Paint*>(paintHandle);
136        // Instead of modifying 0x02, change the filter level.
137        nativePaint->setFilterQuality(flags & sFilterBitmapFlag
138                ? kLow_SkFilterQuality
139                : kNone_SkFilterQuality);
140        // Don't pass through filter flag, which is no longer stored in paint's flags.
141        flags &= ~sFilterBitmapFlag;
142        // Use the existing value for 0x02.
143        const uint32_t existing0x02Flag = nativePaint->getFlags() & sFilterBitmapFlag;
144        flags |= existing0x02Flag;
145        nativePaint->setFlags(flags);
146    }
147
148    static jint getHinting(JNIEnv* env, jobject, jlong paintHandle) {
149        return reinterpret_cast<Paint*>(paintHandle)->getHinting()
150                == Paint::kNo_Hinting ? 0 : 1;
151    }
152
153    static void setHinting(JNIEnv* env, jobject, jlong paintHandle, jint mode) {
154        reinterpret_cast<Paint*>(paintHandle)->setHinting(
155                mode == 0 ? Paint::kNo_Hinting : Paint::kNormal_Hinting);
156    }
157
158    static void setAntiAlias(JNIEnv* env, jobject, jlong paintHandle, jboolean aa) {
159        reinterpret_cast<Paint*>(paintHandle)->setAntiAlias(aa);
160    }
161
162    static void setLinearText(JNIEnv* env, jobject, jlong paintHandle, jboolean linearText) {
163        reinterpret_cast<Paint*>(paintHandle)->setLinearText(linearText);
164    }
165
166    static void setSubpixelText(JNIEnv* env, jobject, jlong paintHandle, jboolean subpixelText) {
167        reinterpret_cast<Paint*>(paintHandle)->setSubpixelText(subpixelText);
168    }
169
170    static void setUnderlineText(JNIEnv* env, jobject, jlong paintHandle, jboolean underlineText) {
171        reinterpret_cast<Paint*>(paintHandle)->setUnderlineText(underlineText);
172    }
173
174    static void setStrikeThruText(JNIEnv* env, jobject, jlong paintHandle, jboolean strikeThruText) {
175        reinterpret_cast<Paint*>(paintHandle)->setStrikeThruText(strikeThruText);
176    }
177
178    static void setFakeBoldText(JNIEnv* env, jobject, jlong paintHandle, jboolean fakeBoldText) {
179        reinterpret_cast<Paint*>(paintHandle)->setFakeBoldText(fakeBoldText);
180    }
181
182    static void setFilterBitmap(JNIEnv* env, jobject, jlong paintHandle, jboolean filterBitmap) {
183        reinterpret_cast<Paint*>(paintHandle)->setFilterQuality(
184                filterBitmap ? kLow_SkFilterQuality : kNone_SkFilterQuality);
185    }
186
187    static void setDither(JNIEnv* env, jobject, jlong paintHandle, jboolean dither) {
188        reinterpret_cast<Paint*>(paintHandle)->setDither(dither);
189    }
190
191    static jint getStyle(JNIEnv* env, jobject clazz,jlong objHandle) {
192        Paint* obj = reinterpret_cast<Paint*>(objHandle);
193        return static_cast<jint>(obj->getStyle());
194    }
195
196    static void setStyle(JNIEnv* env, jobject clazz, jlong objHandle, jint styleHandle) {
197        Paint* obj = reinterpret_cast<Paint*>(objHandle);
198        Paint::Style style = static_cast<Paint::Style>(styleHandle);
199        obj->setStyle(style);
200    }
201
202    static jint getColor(JNIEnv* env, jobject, jlong paintHandle) {
203        int color;
204        color = reinterpret_cast<Paint*>(paintHandle)->getColor();
205        return static_cast<jint>(color);
206    }
207
208    static jint getAlpha(JNIEnv* env, jobject, jlong paintHandle) {
209        int alpha;
210        alpha = reinterpret_cast<Paint*>(paintHandle)->getAlpha();
211        return static_cast<jint>(alpha);
212    }
213
214    static void setColor(JNIEnv* env, jobject, jlong paintHandle, jint color) {
215        reinterpret_cast<Paint*>(paintHandle)->setColor(color);
216    }
217
218    static void setAlpha(JNIEnv* env, jobject, jlong paintHandle, jint a) {
219        reinterpret_cast<Paint*>(paintHandle)->setAlpha(a);
220    }
221
222    static jfloat getStrokeWidth(JNIEnv* env, jobject, jlong paintHandle) {
223        return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeWidth());
224    }
225
226    static void setStrokeWidth(JNIEnv* env, jobject, jlong paintHandle, jfloat width) {
227        reinterpret_cast<Paint*>(paintHandle)->setStrokeWidth(width);
228    }
229
230    static jfloat getStrokeMiter(JNIEnv* env, jobject, jlong paintHandle) {
231        return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getStrokeMiter());
232    }
233
234    static void setStrokeMiter(JNIEnv* env, jobject, jlong paintHandle, jfloat miter) {
235        reinterpret_cast<Paint*>(paintHandle)->setStrokeMiter(miter);
236    }
237
238    static jint getStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle) {
239        Paint* obj = reinterpret_cast<Paint*>(objHandle);
240        return static_cast<jint>(obj->getStrokeCap());
241    }
242
243    static void setStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle, jint capHandle) {
244        Paint* obj = reinterpret_cast<Paint*>(objHandle);
245        Paint::Cap cap = static_cast<Paint::Cap>(capHandle);
246        obj->setStrokeCap(cap);
247    }
248
249    static jint getStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle) {
250        Paint* obj = reinterpret_cast<Paint*>(objHandle);
251        return static_cast<jint>(obj->getStrokeJoin());
252    }
253
254    static void setStrokeJoin(JNIEnv* env, jobject clazz, jlong objHandle, jint joinHandle) {
255        Paint* obj = reinterpret_cast<Paint*>(objHandle);
256        Paint::Join join = (Paint::Join) joinHandle;
257        obj->setStrokeJoin(join);
258    }
259
260    static jboolean getFillPath(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jlong dstHandle) {
261        Paint* obj = reinterpret_cast<Paint*>(objHandle);
262        SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
263        SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
264        return obj->getFillPath(*src, dst) ? JNI_TRUE : JNI_FALSE;
265    }
266
267    static jlong setShader(JNIEnv* env, jobject clazz, jlong objHandle, jlong shaderHandle) {
268        Paint* obj = reinterpret_cast<Paint*>(objHandle);
269        SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
270        return reinterpret_cast<jlong>(obj->setShader(shader));
271    }
272
273    static jlong setColorFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong filterHandle) {
274        Paint* obj = reinterpret_cast<Paint *>(objHandle);
275        SkColorFilter* filter  = reinterpret_cast<SkColorFilter *>(filterHandle);
276        return reinterpret_cast<jlong>(obj->setColorFilter(filter));
277    }
278
279    static jlong setXfermode(JNIEnv* env, jobject clazz, jlong objHandle, jlong xfermodeHandle) {
280        Paint* obj = reinterpret_cast<Paint*>(objHandle);
281        SkXfermode* xfermode = reinterpret_cast<SkXfermode*>(xfermodeHandle);
282        return reinterpret_cast<jlong>(obj->setXfermode(xfermode));
283    }
284
285    static jlong setPathEffect(JNIEnv* env, jobject clazz, jlong objHandle, jlong effectHandle) {
286        Paint* obj = reinterpret_cast<Paint*>(objHandle);
287        SkPathEffect* effect  = reinterpret_cast<SkPathEffect*>(effectHandle);
288        return reinterpret_cast<jlong>(obj->setPathEffect(effect));
289    }
290
291    static jlong setMaskFilter(JNIEnv* env, jobject clazz, jlong objHandle, jlong maskfilterHandle) {
292        Paint* obj = reinterpret_cast<Paint*>(objHandle);
293        SkMaskFilter* maskfilter  = reinterpret_cast<SkMaskFilter*>(maskfilterHandle);
294        return reinterpret_cast<jlong>(obj->setMaskFilter(maskfilter));
295    }
296
297    static jlong setTypeface(JNIEnv* env, jobject clazz, jlong objHandle, jlong typefaceHandle) {
298        // TODO: in Paint refactoring, set typeface on android Paint, not Paint
299        return NULL;
300    }
301
302    static jlong setRasterizer(JNIEnv* env, jobject clazz, jlong objHandle, jlong rasterizerHandle) {
303        Paint* obj = reinterpret_cast<Paint*>(objHandle);
304        SkAutoTUnref<SkRasterizer> rasterizer(GraphicsJNI::refNativeRasterizer(rasterizerHandle));
305        return reinterpret_cast<jlong>(obj->setRasterizer(rasterizer));
306    }
307
308    static jint getTextAlign(JNIEnv* env, jobject clazz, jlong objHandle) {
309        Paint* obj = reinterpret_cast<Paint*>(objHandle);
310        return static_cast<jint>(obj->getTextAlign());
311    }
312
313    static void setTextAlign(JNIEnv* env, jobject clazz, jlong objHandle, jint alignHandle) {
314        Paint* obj = reinterpret_cast<Paint*>(objHandle);
315        Paint::Align align = static_cast<Paint::Align>(alignHandle);
316        obj->setTextAlign(align);
317    }
318
319    static jint setTextLocales(JNIEnv* env, jobject clazz, jlong objHandle, jstring locales) {
320        Paint* obj = reinterpret_cast<Paint*>(objHandle);
321        ScopedUtfChars localesChars(env, locales);
322        jint minikinLangListId = minikin::FontStyle::registerLanguageList(localesChars.c_str());
323        obj->setMinikinLangListId(minikinLangListId);
324        return minikinLangListId;
325    }
326
327    static void setTextLocalesByMinikinLangListId(JNIEnv* env, jobject clazz, jlong objHandle,
328            jint minikinLangListId) {
329        Paint* obj = reinterpret_cast<Paint*>(objHandle);
330        obj->setMinikinLangListId(minikinLangListId);
331    }
332
333    static jboolean isElegantTextHeight(JNIEnv* env, jobject, jlong paintHandle) {
334        Paint* obj = reinterpret_cast<Paint*>(paintHandle);
335        return obj->getFontVariant() == minikin::VARIANT_ELEGANT;
336    }
337
338    static void setElegantTextHeight(JNIEnv* env, jobject, jlong paintHandle, jboolean aa) {
339        Paint* obj = reinterpret_cast<Paint*>(paintHandle);
340        obj->setFontVariant(aa ? minikin::VARIANT_ELEGANT : minikin::VARIANT_DEFAULT);
341    }
342
343    static jfloat getTextSize(JNIEnv* env, jobject, jlong paintHandle) {
344        return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSize());
345    }
346
347    static void setTextSize(JNIEnv* env, jobject, jlong paintHandle, jfloat textSize) {
348        reinterpret_cast<Paint*>(paintHandle)->setTextSize(textSize);
349    }
350
351    static jfloat getTextScaleX(JNIEnv* env, jobject, jlong paintHandle) {
352        return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextScaleX());
353    }
354
355    static void setTextScaleX(JNIEnv* env, jobject, jlong paintHandle, jfloat scaleX) {
356        reinterpret_cast<Paint*>(paintHandle)->setTextScaleX(scaleX);
357    }
358
359    static jfloat getTextSkewX(JNIEnv* env, jobject, jlong paintHandle) {
360        return SkScalarToFloat(reinterpret_cast<Paint*>(paintHandle)->getTextSkewX());
361    }
362
363    static void setTextSkewX(JNIEnv* env, jobject, jlong paintHandle, jfloat skewX) {
364        reinterpret_cast<Paint*>(paintHandle)->setTextSkewX(skewX);
365    }
366
367    static jfloat getLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle) {
368        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
369        return paint->getLetterSpacing();
370    }
371
372    static void setLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat letterSpacing) {
373        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
374        paint->setLetterSpacing(letterSpacing);
375    }
376
377    static void setFontFeatureSettings(JNIEnv* env, jobject clazz, jlong paintHandle, jstring settings) {
378        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
379        if (!settings) {
380            paint->setFontFeatureSettings(std::string());
381        } else {
382            ScopedUtfChars settingsChars(env, settings);
383            paint->setFontFeatureSettings(std::string(settingsChars.c_str(), settingsChars.size()));
384        }
385    }
386
387    static jint getHyphenEdit(JNIEnv* env, jobject clazz, jlong paintHandle, jint hyphen) {
388        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
389        return paint->getHyphenEdit();
390    }
391
392    static void setHyphenEdit(JNIEnv* env, jobject clazz, jlong paintHandle, jint hyphen) {
393        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
394        paint->setHyphenEdit((uint32_t)hyphen);
395    }
396
397    static SkScalar getMetricsInternal(jlong paintHandle, jlong typefaceHandle,
398            Paint::FontMetrics *metrics) {
399        const int kElegantTop = 2500;
400        const int kElegantBottom = -1000;
401        const int kElegantAscent = 1900;
402        const int kElegantDescent = -500;
403        const int kElegantLeading = 0;
404        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
405        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
406        typeface = Typeface::resolveDefault(typeface);
407        minikin::FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
408        float saveSkewX = paint->getTextSkewX();
409        bool savefakeBold = paint->isFakeBoldText();
410        MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery);
411        SkScalar spacing = paint->getFontMetrics(metrics);
412        // The populateSkPaint call may have changed fake bold / text skew
413        // because we want to measure with those effects applied, so now
414        // restore the original settings.
415        paint->setTextSkewX(saveSkewX);
416        paint->setFakeBoldText(savefakeBold);
417        if (paint->getFontVariant() == minikin::VARIANT_ELEGANT) {
418            SkScalar size = paint->getTextSize();
419            metrics->fTop = -size * kElegantTop / 2048;
420            metrics->fBottom = -size * kElegantBottom / 2048;
421            metrics->fAscent = -size * kElegantAscent / 2048;
422            metrics->fDescent = -size * kElegantDescent / 2048;
423            metrics->fLeading = size * kElegantLeading / 2048;
424            spacing = metrics->fDescent - metrics->fAscent + metrics->fLeading;
425        }
426        return spacing;
427    }
428
429    static jfloat ascent(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle) {
430        Paint::FontMetrics metrics;
431        getMetricsInternal(paintHandle, typefaceHandle, &metrics);
432        return SkScalarToFloat(metrics.fAscent);
433    }
434
435    static jfloat descent(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle) {
436        Paint::FontMetrics metrics;
437        getMetricsInternal(paintHandle, typefaceHandle, &metrics);
438        return SkScalarToFloat(metrics.fDescent);
439    }
440
441    static jfloat getFontMetrics(JNIEnv* env, jobject, jlong paintHandle,
442            jlong typefaceHandle, jobject metricsObj) {
443        Paint::FontMetrics metrics;
444        SkScalar spacing = getMetricsInternal(paintHandle, typefaceHandle, &metrics);
445
446        if (metricsObj) {
447            SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class));
448            env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop));
449            env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent));
450            env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent));
451            env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom));
452            env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading));
453        }
454        return SkScalarToFloat(spacing);
455    }
456
457    static jint getFontMetricsInt(JNIEnv* env, jobject, jlong paintHandle,
458            jlong typefaceHandle, jobject metricsObj) {
459        Paint::FontMetrics metrics;
460
461        getMetricsInternal(paintHandle, typefaceHandle, &metrics);
462        int ascent = SkScalarRoundToInt(metrics.fAscent);
463        int descent = SkScalarRoundToInt(metrics.fDescent);
464        int leading = SkScalarRoundToInt(metrics.fLeading);
465
466        if (metricsObj) {
467            SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class));
468            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloorToInt(metrics.fTop));
469            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent);
470            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent);
471            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeilToInt(metrics.fBottom));
472            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading);
473        }
474        return descent - ascent + leading;
475    }
476
477    static jfloat doTextAdvances(JNIEnv *env, Paint *paint, Typeface* typeface,
478            const jchar *text, jint start, jint count, jint contextCount, jint bidiFlags,
479            jfloatArray advances, jint advancesIndex) {
480        NPE_CHECK_RETURN_ZERO(env, text);
481
482        if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
483            doThrowAIOOBE(env);
484            return 0;
485        }
486        if (count == 0) {
487            return 0;
488        }
489        if (advances) {
490            size_t advancesLength = env->GetArrayLength(advances);
491            if ((size_t)(count  + advancesIndex) > advancesLength) {
492                doThrowAIOOBE(env);
493                return 0;
494            }
495        }
496        std::unique_ptr<jfloat[]> advancesArray;
497        if (advances) {
498            advancesArray.reset(new jfloat[count]);
499        }
500        const float advance = MinikinUtils::measureText(paint, bidiFlags, typeface, text,
501                start, count, contextCount, advancesArray.get());
502        if (advances) {
503            env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray.get());
504        }
505        return advance;
506    }
507
508    static jfloat getTextAdvances___CIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
509            jlong typefaceHandle,
510            jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
511            jint bidiFlags, jfloatArray advances, jint advancesIndex) {
512        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
513        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
514        jchar* textArray = env->GetCharArrayElements(text, NULL);
515        jfloat result = doTextAdvances(env, paint, typeface, textArray + contextIndex,
516                index - contextIndex, count, contextCount, bidiFlags, advances, advancesIndex);
517        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
518        return result;
519    }
520
521    static jfloat getTextAdvances__StringIIIII_FI(JNIEnv* env, jobject clazz, jlong paintHandle,
522            jlong typefaceHandle,
523            jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint bidiFlags,
524            jfloatArray advances, jint advancesIndex) {
525        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
526        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
527        const jchar* textArray = env->GetStringChars(text, NULL);
528        jfloat result = doTextAdvances(env, paint, typeface, textArray + contextStart,
529                start - contextStart, end - start, contextEnd - contextStart, bidiFlags,
530                advances, advancesIndex);
531        env->ReleaseStringChars(text, textArray);
532        return result;
533    }
534
535    static jint doTextRunCursor(JNIEnv *env, Paint* paint, const jchar *text, jint start,
536            jint count, jint flags, jint offset, jint opt) {
537        minikin::GraphemeBreak::MoveOpt moveOpt = minikin::GraphemeBreak::MoveOpt(opt);
538        size_t result = minikin::GraphemeBreak::getTextRunCursor(text, start, count, offset,
539                moveOpt);
540        return static_cast<jint>(result);
541    }
542
543    static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text,
544            jint contextStart, jint contextCount, jint dir, jint offset, jint cursorOpt) {
545        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
546        jchar* textArray = env->GetCharArrayElements(text, NULL);
547        jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, dir,
548                offset, cursorOpt);
549        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
550        return result;
551    }
552
553    static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, jlong paintHandle, jstring text,
554            jint contextStart, jint contextEnd, jint dir, jint offset, jint cursorOpt) {
555        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
556        const jchar* textArray = env->GetStringChars(text, NULL);
557        jint result = doTextRunCursor(env, paint, textArray, contextStart,
558                contextEnd - contextStart, dir, offset, cursorOpt);
559        env->ReleaseStringChars(text, textArray);
560        return result;
561    }
562
563    class GetTextFunctor {
564    public:
565        GetTextFunctor(const minikin::Layout& layout, SkPath* path, jfloat x, jfloat y,
566                    Paint* paint, uint16_t* glyphs, SkPoint* pos)
567                : layout(layout), path(path), x(x), y(y), paint(paint), glyphs(glyphs), pos(pos) {
568        }
569
570        void operator()(size_t start, size_t end) {
571            for (size_t i = start; i < end; i++) {
572                glyphs[i] = layout.getGlyphId(i);
573                pos[i].fX = x + layout.getX(i);
574                pos[i].fY = y + layout.getY(i);
575            }
576            if (start == 0) {
577                paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, path);
578            } else {
579                paint->getPosTextPath(glyphs + start, (end - start) << 1, pos + start, &tmpPath);
580                path->addPath(tmpPath);
581            }
582        }
583    private:
584        const minikin::Layout& layout;
585        SkPath* path;
586        jfloat x;
587        jfloat y;
588        Paint* paint;
589        uint16_t* glyphs;
590        SkPoint* pos;
591        SkPath tmpPath;
592    };
593
594    static void getTextPath(JNIEnv* env, Paint* paint, Typeface* typeface, const jchar* text,
595            jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) {
596        minikin::Layout layout;
597        MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, text, 0, count, count);
598        size_t nGlyphs = layout.nGlyphs();
599        uint16_t* glyphs = new uint16_t[nGlyphs];
600        SkPoint* pos = new SkPoint[nGlyphs];
601
602        x += MinikinUtils::xOffsetForTextAlign(paint, layout);
603        Paint::Align align = paint->getTextAlign();
604        paint->setTextAlign(Paint::kLeft_Align);
605        paint->setTextEncoding(Paint::kGlyphID_TextEncoding);
606        GetTextFunctor f(layout, path, x, y, paint, glyphs, pos);
607        MinikinUtils::forFontRun(layout, paint, f);
608        paint->setTextAlign(align);
609        delete[] glyphs;
610        delete[] pos;
611    }
612
613    static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle,
614            jlong typefaceHandle, jint bidiFlags,
615            jcharArray text, jint index, jint count, jfloat x, jfloat y, jlong pathHandle) {
616        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
617        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
618        SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
619        const jchar* textArray = env->GetCharArrayElements(text, NULL);
620        getTextPath(env, paint, typeface, textArray + index, count, bidiFlags, x, y, path);
621        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
622    }
623
624    static void getTextPath__String(JNIEnv* env, jobject clazz, jlong paintHandle,
625            jlong typefaceHandle, jint bidiFlags,
626            jstring text, jint start, jint end, jfloat x, jfloat y, jlong pathHandle) {
627        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
628        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
629        SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
630        const jchar* textArray = env->GetStringChars(text, NULL);
631        getTextPath(env, paint, typeface, textArray + start, end - start, bidiFlags, x, y, path);
632        env->ReleaseStringChars(text, textArray);
633    }
634
635    static void setShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle, jfloat radius,
636                               jfloat dx, jfloat dy, jint color) {
637        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
638        if (radius <= 0) {
639            paint->setLooper(NULL);
640        }
641        else {
642            SkScalar sigma = android::uirenderer::Blur::convertRadiusToSigma(radius);
643            paint->setLooper(SkBlurDrawLooper::Create((SkColor)color, sigma, dx, dy))->unref();
644        }
645    }
646
647    static jboolean hasShadowLayer(JNIEnv* env, jobject clazz, jlong paintHandle) {
648        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
649        return paint->getLooper() && paint->getLooper()->asABlurShadow(NULL);
650    }
651
652    static int breakText(JNIEnv* env, const Paint& paint, Typeface* typeface, const jchar text[],
653                         int count, float maxWidth, jint bidiFlags, jfloatArray jmeasured,
654                         const bool forwardScan) {
655        size_t measuredCount = 0;
656        float measured = 0;
657
658        std::unique_ptr<float[]> advancesArray(new float[count]);
659        MinikinUtils::measureText(&paint, bidiFlags, typeface, text, 0, count, count,
660                advancesArray.get());
661
662        for (int i = 0; i < count; i++) {
663            // traverse in the given direction
664            int index = forwardScan ? i : (count - i - 1);
665            float width = advancesArray[index];
666            if (measured + width > maxWidth) {
667                break;
668            }
669            // properly handle clusters when scanning backwards
670            if (forwardScan || width != 0.0f) {
671                measuredCount = i + 1;
672            }
673            measured += width;
674        }
675
676        if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
677            AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
678            jfloat* array = autoMeasured.ptr();
679            array[0] = measured;
680        }
681        return measuredCount;
682    }
683
684    static jint breakTextC(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jcharArray jtext,
685            jint index, jint count, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
686        NPE_CHECK_RETURN_ZERO(env, jtext);
687
688        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
689        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
690
691        bool forwardTextDirection;
692        if (count < 0) {
693            forwardTextDirection = false;
694            count = -count;
695        }
696        else {
697            forwardTextDirection = true;
698        }
699
700        if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
701            doThrowAIOOBE(env);
702            return 0;
703        }
704
705        const jchar* text = env->GetCharArrayElements(jtext, NULL);
706        count = breakText(env, *paint, typeface, text + index, count, maxWidth,
707                          bidiFlags, jmeasuredWidth, forwardTextDirection);
708        env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
709                                      JNI_ABORT);
710        return count;
711    }
712
713    static jint breakTextS(JNIEnv* env, jobject clazz, jlong paintHandle, jlong typefaceHandle, jstring jtext,
714                jboolean forwards, jfloat maxWidth, jint bidiFlags, jfloatArray jmeasuredWidth) {
715        NPE_CHECK_RETURN_ZERO(env, jtext);
716
717        Paint* paint = reinterpret_cast<Paint*>(paintHandle);
718        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
719
720        int count = env->GetStringLength(jtext);
721        const jchar* text = env->GetStringChars(jtext, NULL);
722        count = breakText(env, *paint, typeface, text, count, maxWidth, bidiFlags, jmeasuredWidth, forwards);
723        env->ReleaseStringChars(jtext, text);
724        return count;
725    }
726
727    static void doTextBounds(JNIEnv* env, const jchar* text, int count, jobject bounds,
728            const Paint& paint, Typeface* typeface, jint bidiFlags) {
729        SkRect  r;
730        SkIRect ir;
731
732        minikin::Layout layout;
733        MinikinUtils::doLayout(&layout, &paint, bidiFlags, typeface, text, 0, count, count);
734        minikin::MinikinRect rect;
735        layout.getBounds(&rect);
736        r.fLeft = rect.mLeft;
737        r.fTop = rect.mTop;
738        r.fRight = rect.mRight;
739        r.fBottom = rect.mBottom;
740        r.roundOut(&ir);
741        GraphicsJNI::irect_to_jrect(ir, env, bounds);
742    }
743
744    static void getStringBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle,
745                                jstring text, jint start, jint end, jint bidiFlags, jobject bounds) {
746        const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
747        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
748        const jchar* textArray = env->GetStringChars(text, NULL);
749        doTextBounds(env, textArray + start, end - start, bounds, *paint, typeface, bidiFlags);
750        env->ReleaseStringChars(text, textArray);
751    }
752
753    static void getCharArrayBounds(JNIEnv* env, jobject, jlong paintHandle, jlong typefaceHandle,
754                        jcharArray text, jint index, jint count, jint bidiFlags, jobject bounds) {
755        const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
756        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
757        const jchar* textArray = env->GetCharArrayElements(text, NULL);
758        doTextBounds(env, textArray + index, count, bounds, *paint, typeface, bidiFlags);
759        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
760                                      JNI_ABORT);
761    }
762
763    static jboolean layoutContainsNotdef(const minikin::Layout& layout) {
764        for (size_t i = 0; i < layout.nGlyphs(); i++) {
765            if (layout.getGlyphId(i) == 0) {
766                return true;
767            }
768        }
769        return false;
770    }
771
772    // Returns true if the given string is exact one pair of regional indicators.
773    static bool isFlag(const jchar* str, size_t length) {
774        const jchar RI_LEAD_SURROGATE = 0xD83C;
775        const jchar RI_TRAIL_SURROGATE_MIN = 0xDDE6;
776        const jchar RI_TRAIL_SURROGATE_MAX = 0xDDFF;
777
778        if (length != 4) {
779            return false;
780        }
781        if (str[0] != RI_LEAD_SURROGATE || str[2] != RI_LEAD_SURROGATE) {
782            return false;
783        }
784        return RI_TRAIL_SURROGATE_MIN <= str[1] && str[1] <= RI_TRAIL_SURROGATE_MAX &&
785            RI_TRAIL_SURROGATE_MIN <= str[3] && str[3] <= RI_TRAIL_SURROGATE_MAX;
786    }
787
788    static jboolean hasGlyph(JNIEnv *env, jclass, jlong paintHandle, jlong typefaceHandle,
789            jint bidiFlags, jstring string) {
790        const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
791        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
792        ScopedStringChars str(env, string);
793
794        /* Start by rejecting unsupported base code point and variation selector pairs. */
795        size_t nChars = 0;
796        const uint32_t kStartOfString = 0xFFFFFFFF;
797        uint32_t prevCp = kStartOfString;
798        for (size_t i = 0; i < str.size(); i++) {
799            jchar cu = str[i];
800            uint32_t cp = cu;
801            if (U16_IS_TRAIL(cu)) {
802                // invalid UTF-16, unpaired trailing surrogate
803                return false;
804            } else if (U16_IS_LEAD(cu)) {
805                if (i + 1 == str.size()) {
806                    // invalid UTF-16, unpaired leading surrogate at end of string
807                    return false;
808                }
809                i++;
810                jchar cu2 = str[i];
811                if (!U16_IS_TRAIL(cu2)) {
812                    // invalid UTF-16, unpaired leading surrogate
813                    return false;
814                }
815                cp = U16_GET_SUPPLEMENTARY(cu, cu2);
816            }
817
818            if (prevCp != kStartOfString &&
819                ((0xFE00 <= cp && cp <= 0xFE0F) || (0xE0100 <= cp && cp <= 0xE01EF))) {
820                bool hasVS = MinikinUtils::hasVariationSelector(typeface, prevCp, cp);
821                if (!hasVS) {
822                    // No font has a glyph for the code point and variation selector pair.
823                    return false;
824                } else if (nChars == 1 && i + 1 == str.size()) {
825                    // The string is just a codepoint and a VS, we have an authoritative answer
826                    return true;
827                }
828            }
829            nChars++;
830            prevCp = cp;
831        }
832        minikin::Layout layout;
833        MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, str.get(), 0, str.size(),
834                str.size());
835        size_t nGlyphs = layout.nGlyphs();
836        if (nGlyphs != 1 && nChars > 1) {
837            // multiple-character input, and was not a ligature
838            // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures
839            // in joining scripts, such as Arabic and Mongolian.
840            return false;
841        }
842
843        if (nGlyphs == 0 || layoutContainsNotdef(layout)) {
844            return false;  // The collection doesn't have a glyph.
845        }
846
847        if (nChars == 2 && isFlag(str.get(), str.size())) {
848            // Some font may have a special glyph for unsupported regional indicator pairs.
849            // To return false for this case, need to compare the glyph id with the one of ZZ
850            // since ZZ is reserved for unknown or invalid territory.
851            // U+1F1FF (REGIONAL INDICATOR SYMBOL LETTER Z) is \uD83C\uDDFF in UTF16.
852            static const jchar ZZ_FLAG_STR[] = { 0xD83C, 0xDDFF, 0xD83C, 0xDDFF };
853            minikin::Layout zzLayout;
854            MinikinUtils::doLayout(&zzLayout, paint, bidiFlags, typeface, ZZ_FLAG_STR, 0, 4, 4);
855            if (zzLayout.nGlyphs() != 1 || layoutContainsNotdef(zzLayout)) {
856                // The font collection doesn't have a glyph for unknown flag. Just return true.
857                return true;
858            }
859            return zzLayout.getGlyphId(0) != layout.getGlyphId(0);
860        }
861        return true;
862    }
863
864    static jfloat doRunAdvance(const Paint* paint, Typeface* typeface, const jchar buf[],
865            jint start, jint count, jint bufSize, jboolean isRtl, jint offset) {
866        int bidiFlags = isRtl ? minikin::kBidi_Force_RTL : minikin::kBidi_Force_LTR;
867        if (offset == start + count) {
868            return MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count,
869                    bufSize, nullptr);
870        }
871        std::unique_ptr<float[]> advancesArray(new float[count]);
872        MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
873                advancesArray.get());
874        return minikin::getRunAdvance(advancesArray.get(), buf, start, count, offset);
875    }
876
877    static jfloat getRunAdvance___CIIIIZI_F(JNIEnv *env, jclass, jlong paintHandle,
878            jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart,
879            jint contextEnd, jboolean isRtl, jint offset) {
880        const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
881        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
882        jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL);
883        jfloat result = doRunAdvance(paint, typeface, textArray + contextStart,
884                start - contextStart, end - start, contextEnd - contextStart, isRtl,
885                offset - contextStart);
886        env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT);
887        return result;
888    }
889
890    static jint doOffsetForAdvance(const Paint* paint, Typeface* typeface, const jchar buf[],
891            jint start, jint count, jint bufSize, jboolean isRtl, jfloat advance) {
892        int bidiFlags = isRtl ? minikin::kBidi_Force_RTL : minikin::kBidi_Force_LTR;
893        std::unique_ptr<float[]> advancesArray(new float[count]);
894        MinikinUtils::measureText(paint, bidiFlags, typeface, buf, start, count, bufSize,
895                advancesArray.get());
896        return minikin::getOffsetForAdvance(advancesArray.get(), buf, start, count, advance);
897    }
898
899    static jint getOffsetForAdvance___CIIIIZF_I(JNIEnv *env, jclass, jlong paintHandle,
900            jlong typefaceHandle, jcharArray text, jint start, jint end, jint contextStart,
901            jint contextEnd, jboolean isRtl, jfloat advance) {
902        const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
903        Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
904        jchar* textArray = (jchar*) env->GetPrimitiveArrayCritical(text, NULL);
905        jint result = doOffsetForAdvance(paint, typeface, textArray + contextStart,
906                start - contextStart, end - start, contextEnd - contextStart, isRtl, advance);
907        result += contextStart;
908        env->ReleasePrimitiveArrayCritical(text, textArray, JNI_ABORT);
909        return result;
910    }
911
912}; // namespace PaintGlue
913
914static const JNINativeMethod methods[] = {
915    {"nGetNativeFinalizer", "()J", (void*) PaintGlue::getNativeFinalizer},
916    {"nInit","()J", (void*) PaintGlue::init},
917    {"nInitWithPaint","(J)J", (void*) PaintGlue::initWithPaint},
918
919    {"nReset","!(J)V", (void*) PaintGlue::reset},
920    {"nSet","!(JJ)V", (void*) PaintGlue::assign},
921    {"nGetFlags","!(J)I", (void*) PaintGlue::getFlags},
922    {"nSetFlags","!(JI)V", (void*) PaintGlue::setFlags},
923    {"nGetHinting","!(J)I", (void*) PaintGlue::getHinting},
924    {"nSetHinting","!(JI)V", (void*) PaintGlue::setHinting},
925    {"nSetAntiAlias","!(JZ)V", (void*) PaintGlue::setAntiAlias},
926    {"nSetSubpixelText","!(JZ)V", (void*) PaintGlue::setSubpixelText},
927    {"nSetLinearText","!(JZ)V", (void*) PaintGlue::setLinearText},
928    {"nSetUnderlineText","!(JZ)V", (void*) PaintGlue::setUnderlineText},
929    {"nSetStrikeThruText","!(JZ)V", (void*) PaintGlue::setStrikeThruText},
930    {"nSetFakeBoldText","!(JZ)V", (void*) PaintGlue::setFakeBoldText},
931    {"nSetFilterBitmap","!(JZ)V", (void*) PaintGlue::setFilterBitmap},
932    {"nSetDither","!(JZ)V", (void*) PaintGlue::setDither},
933    {"nGetStyle","!(J)I", (void*) PaintGlue::getStyle},
934    {"nSetStyle","!(JI)V", (void*) PaintGlue::setStyle},
935    {"nGetColor","!(J)I", (void*) PaintGlue::getColor},
936    {"nSetColor","!(JI)V", (void*) PaintGlue::setColor},
937    {"nGetAlpha","!(J)I", (void*) PaintGlue::getAlpha},
938    {"nSetAlpha","!(JI)V", (void*) PaintGlue::setAlpha},
939    {"nGetStrokeWidth","!(J)F", (void*) PaintGlue::getStrokeWidth},
940    {"nSetStrokeWidth","!(JF)V", (void*) PaintGlue::setStrokeWidth},
941    {"nGetStrokeMiter","!(J)F", (void*) PaintGlue::getStrokeMiter},
942    {"nSetStrokeMiter","!(JF)V", (void*) PaintGlue::setStrokeMiter},
943    {"nGetStrokeCap","!(J)I", (void*) PaintGlue::getStrokeCap},
944    {"nSetStrokeCap","!(JI)V", (void*) PaintGlue::setStrokeCap},
945    {"nGetStrokeJoin","!(J)I", (void*) PaintGlue::getStrokeJoin},
946    {"nSetStrokeJoin","!(JI)V", (void*) PaintGlue::setStrokeJoin},
947    {"nGetFillPath","!(JJJ)Z", (void*) PaintGlue::getFillPath},
948    {"nSetShader","!(JJ)J", (void*) PaintGlue::setShader},
949    {"nSetColorFilter","!(JJ)J", (void*) PaintGlue::setColorFilter},
950    {"nSetXfermode","!(JJ)J", (void*) PaintGlue::setXfermode},
951    {"nSetPathEffect","!(JJ)J", (void*) PaintGlue::setPathEffect},
952    {"nSetMaskFilter","!(JJ)J", (void*) PaintGlue::setMaskFilter},
953    {"nSetTypeface","!(JJ)J", (void*) PaintGlue::setTypeface},
954    {"nSetRasterizer","!(JJ)J", (void*) PaintGlue::setRasterizer},
955    {"nGetTextAlign","!(J)I", (void*) PaintGlue::getTextAlign},
956    {"nSetTextAlign","!(JI)V", (void*) PaintGlue::setTextAlign},
957    {"nSetTextLocales","!(JLjava/lang/String;)I", (void*) PaintGlue::setTextLocales},
958    {"nSetTextLocalesByMinikinLangListId","!(JI)V",
959            (void*) PaintGlue::setTextLocalesByMinikinLangListId},
960    {"nIsElegantTextHeight","!(J)Z", (void*) PaintGlue::isElegantTextHeight},
961    {"nSetElegantTextHeight","!(JZ)V", (void*) PaintGlue::setElegantTextHeight},
962    {"nGetTextSize","!(J)F", (void*) PaintGlue::getTextSize},
963    {"nSetTextSize","!(JF)V", (void*) PaintGlue::setTextSize},
964    {"nGetTextScaleX","!(J)F", (void*) PaintGlue::getTextScaleX},
965    {"nSetTextScaleX","!(JF)V", (void*) PaintGlue::setTextScaleX},
966    {"nGetTextSkewX","!(J)F", (void*) PaintGlue::getTextSkewX},
967    {"nSetTextSkewX","!(JF)V", (void*) PaintGlue::setTextSkewX},
968    {"nGetLetterSpacing","!(J)F", (void*) PaintGlue::getLetterSpacing},
969    {"nSetLetterSpacing","!(JF)V", (void*) PaintGlue::setLetterSpacing},
970    {"nSetFontFeatureSettings","(JLjava/lang/String;)V",
971            (void*) PaintGlue::setFontFeatureSettings},
972    {"nGetHyphenEdit", "!(J)I", (void*) PaintGlue::getHyphenEdit},
973    {"nSetHyphenEdit", "!(JI)V", (void*) PaintGlue::setHyphenEdit},
974    {"nAscent","!(JJ)F", (void*) PaintGlue::ascent},
975    {"nDescent","!(JJ)F", (void*) PaintGlue::descent},
976
977    {"nGetFontMetrics", "!(JJLandroid/graphics/Paint$FontMetrics;)F",
978            (void*)PaintGlue::getFontMetrics},
979    {"nGetFontMetricsInt", "!(JJLandroid/graphics/Paint$FontMetricsInt;)I",
980            (void*)PaintGlue::getFontMetricsInt},
981
982    {"nBreakText","(JJ[CIIFI[F)I", (void*) PaintGlue::breakTextC},
983    {"nBreakText","(JJLjava/lang/String;ZFI[F)I", (void*) PaintGlue::breakTextS},
984    {"nGetTextAdvances","(JJ[CIIIII[FI)F",
985            (void*) PaintGlue::getTextAdvances___CIIIII_FI},
986    {"nGetTextAdvances","(JJLjava/lang/String;IIIII[FI)F",
987            (void*) PaintGlue::getTextAdvances__StringIIIII_FI},
988
989    {"nGetTextRunCursor", "(J[CIIIII)I", (void*) PaintGlue::getTextRunCursor___C},
990    {"nGetTextRunCursor", "(JLjava/lang/String;IIIII)I",
991            (void*) PaintGlue::getTextRunCursor__String},
992    {"nGetTextPath", "(JJI[CIIFFJ)V", (void*) PaintGlue::getTextPath___C},
993    {"nGetTextPath", "(JJILjava/lang/String;IIFFJ)V", (void*) PaintGlue::getTextPath__String},
994    {"nGetStringBounds", "(JJLjava/lang/String;IIILandroid/graphics/Rect;)V",
995            (void*) PaintGlue::getStringBounds },
996    {"nGetCharArrayBounds", "(JJ[CIIILandroid/graphics/Rect;)V",
997            (void*) PaintGlue::getCharArrayBounds },
998    {"nHasGlyph", "(JJILjava/lang/String;)Z", (void*) PaintGlue::hasGlyph },
999    {"nGetRunAdvance", "(JJ[CIIIIZI)F", (void*) PaintGlue::getRunAdvance___CIIIIZI_F},
1000    {"nGetOffsetForAdvance", "(JJ[CIIIIZF)I",
1001            (void*) PaintGlue::getOffsetForAdvance___CIIIIZF_I},
1002
1003    {"nSetShadowLayer", "!(JFFFI)V", (void*)PaintGlue::setShadowLayer},
1004    {"nHasShadowLayer", "!(J)Z", (void*)PaintGlue::hasShadowLayer}
1005};
1006
1007int register_android_graphics_Paint(JNIEnv* env) {
1008    gFontMetrics_class = FindClassOrDie(env, "android/graphics/Paint$FontMetrics");
1009    gFontMetrics_class = MakeGlobalRefOrDie(env, gFontMetrics_class);
1010
1011    gFontMetrics_fieldID.top = GetFieldIDOrDie(env, gFontMetrics_class, "top", "F");
1012    gFontMetrics_fieldID.ascent = GetFieldIDOrDie(env, gFontMetrics_class, "ascent", "F");
1013    gFontMetrics_fieldID.descent = GetFieldIDOrDie(env, gFontMetrics_class, "descent", "F");
1014    gFontMetrics_fieldID.bottom = GetFieldIDOrDie(env, gFontMetrics_class, "bottom", "F");
1015    gFontMetrics_fieldID.leading = GetFieldIDOrDie(env, gFontMetrics_class, "leading", "F");
1016
1017    gFontMetricsInt_class = FindClassOrDie(env, "android/graphics/Paint$FontMetricsInt");
1018    gFontMetricsInt_class = MakeGlobalRefOrDie(env, gFontMetricsInt_class);
1019
1020    gFontMetricsInt_fieldID.top = GetFieldIDOrDie(env, gFontMetricsInt_class, "top", "I");
1021    gFontMetricsInt_fieldID.ascent = GetFieldIDOrDie(env, gFontMetricsInt_class, "ascent", "I");
1022    gFontMetricsInt_fieldID.descent = GetFieldIDOrDie(env, gFontMetricsInt_class, "descent", "I");
1023    gFontMetricsInt_fieldID.bottom = GetFieldIDOrDie(env, gFontMetricsInt_class, "bottom", "I");
1024    gFontMetricsInt_fieldID.leading = GetFieldIDOrDie(env, gFontMetricsInt_class, "leading", "I");
1025
1026    return RegisterMethodsOrDie(env, "android/graphics/Paint", methods, NELEM(methods));
1027}
1028
1029}
1030