19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* libs/android_runtime/android/graphics/Paint.cpp
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Copyright 2006, The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
58451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** Licensed under the Apache License, Version 2.0 (the "License");
68451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** you may not use this file except in compliance with the License.
78451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** You may obtain a copy of the License at
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
98451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes**     http://www.apache.org/licenses/LICENSE-2.0
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
118451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** Unless required by applicable law or agreed to in writing, software
128451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** distributed under the License is distributed on an "AS IS" BASIS,
138451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes** See the License for the specific language governing permissions and
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** limitations under the License.
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project*/
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
18f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn#define LOG_TAG "Paint"
19f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn
20f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn#include <utils/Log.h>
21f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "GraphicsJNI.h"
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
25ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett#include <ScopedUtfChars.h>
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkBlurDrawLooper.h"
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkColorFilter.h"
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkMaskFilter.h"
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkRasterizer.h"
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkShader.h"
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkTypeface.h"
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkXfermode.h"
34ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett#include "unicode/uloc.h"
350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt#include "unicode/ushape.h"
36f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt#include "TextLayout.h"
370c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
380c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt// temporary for debugging
395c13d89c1332fcc499379b9064b891187b75ca32Chet Haase#include <Caches.h>
400c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt#include <utils/Log.h>
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct JMetricsID {
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    top;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    ascent;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    descent;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    bottom;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    leading;
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gFontMetrics_class;
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JMetricsID gFontMetrics_fieldID;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gFontMetricsInt_class;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JMetricsID gFontMetricsInt_fieldID;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
583d63e0119dc763ed0a06fd7498375746fd391d80Mike Reedstatic void defaultSettingsForAndroid(SkPaint* paint) {
59c511bee87cda99a252d1a62487f47c8f05aee78cFabrice Di Meglio    // GlyphID encoding is required because we are using Harfbuzz shaping
60c511bee87cda99a252d1a62487f47c8f05aee78cFabrice Di Meglio    paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
613d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed}
623d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass SkPaintGlue {
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    enum MoveOpt {
660c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
670c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    };
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void finalizer(JNIEnv* env, jobject clazz, SkPaint* obj) {
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete obj;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkPaint* init(JNIEnv* env, jobject clazz) {
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint* obj = new SkPaint();
753d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed        defaultSettingsForAndroid(obj);
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj;
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkPaint* intiWithPaint(JNIEnv* env, jobject clazz, SkPaint* paint) {
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint* obj = new SkPaint(*paint);
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj;
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
838451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void reset(JNIEnv* env, jobject clazz, SkPaint* obj) {
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->reset();
863d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed        defaultSettingsForAndroid(obj);
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
888451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void assign(JNIEnv* env, jobject clazz, SkPaint* dst, const SkPaint* src) {
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst = *src;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
928451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getFlags(JNIEnv* env, jobject paint) {
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativePaint(env, paint)->getFlags();
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
978451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setFlags(JNIEnv* env, jobject paint, jint flags) {
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setFlags(flags);
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1028451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
103f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    static jint getHinting(JNIEnv* env, jobject paint) {
104f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        NPE_CHECK_RETURN_ZERO(env, paint);
105f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        return GraphicsJNI::getNativePaint(env, paint)->getHinting()
106f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn                == SkPaint::kNo_Hinting ? 0 : 1;
107f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    }
108f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn
109f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    static void setHinting(JNIEnv* env, jobject paint, jint mode) {
110f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        NPE_CHECK_RETURN_VOID(env, paint);
111f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        GraphicsJNI::getNativePaint(env, paint)->setHinting(
112f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn                mode == 0 ? SkPaint::kNo_Hinting : SkPaint::kSlight_Hinting);
113f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    }
114f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setAntiAlias(JNIEnv* env, jobject paint, jboolean aa) {
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setAntiAlias(aa);
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1198451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setLinearText(JNIEnv* env, jobject paint, jboolean linearText) {
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setLinearText(linearText);
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1248451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setSubpixelText(JNIEnv* env, jobject paint, jboolean subpixelText) {
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setSubpixelText(subpixelText);
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1298451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setUnderlineText(JNIEnv* env, jobject paint, jboolean underlineText) {
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setUnderlineText(underlineText);
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1348451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrikeThruText(JNIEnv* env, jobject paint, jboolean strikeThruText) {
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setStrikeThruText(strikeThruText);
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1398451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setFakeBoldText(JNIEnv* env, jobject paint, jboolean fakeBoldText) {
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setFakeBoldText(fakeBoldText);
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1448451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setFilterBitmap(JNIEnv* env, jobject paint, jboolean filterBitmap) {
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setFilterBitmap(filterBitmap);
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1498451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setDither(JNIEnv* env, jobject paint, jboolean dither) {
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setDither(dither);
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1548451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getStyle(JNIEnv* env, jobject clazz, SkPaint* obj) {
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getStyle();
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1588451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStyle(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Style style) {
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setStyle(style);
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1628451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getColor(JNIEnv* env, jobject paint) {
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativePaint(env, paint)->getColor();
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1678451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getAlpha(JNIEnv* env, jobject paint) {
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativePaint(env, paint)->getAlpha();
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1728451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setColor(JNIEnv* env, jobject paint, jint color) {
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setColor(color);
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1778451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setAlpha(JNIEnv* env, jobject paint, jint a) {
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setAlpha(a);
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1828451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getStrokeWidth(JNIEnv* env, jobject paint) {
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeWidth());
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1878451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeWidth(JNIEnv* env, jobject paint, jfloat width) {
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setStrokeWidth(SkFloatToScalar(width));
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1928451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getStrokeMiter(JNIEnv* env, jobject paint) {
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeMiter());
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1978451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeMiter(JNIEnv* env, jobject paint, jfloat miter) {
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setStrokeMiter(SkFloatToScalar(miter));
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2028451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getStrokeCap(JNIEnv* env, jobject clazz, SkPaint* obj) {
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getStrokeCap();
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2068451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeCap(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Cap cap) {
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setStrokeCap(cap);
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2108451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getStrokeJoin(JNIEnv* env, jobject clazz, SkPaint* obj) {
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getStrokeJoin();
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2148451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeJoin(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Join join) {
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setStrokeJoin(join);
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2188451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean getFillPath(JNIEnv* env, jobject clazz, SkPaint* obj, SkPath* src, SkPath* dst) {
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getFillPath(*src, dst);
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2228451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkShader* setShader(JNIEnv* env, jobject clazz, SkPaint* obj, SkShader* shader) {
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setShader(shader);
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2268451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkColorFilter* setColorFilter(JNIEnv* env, jobject clazz, SkPaint* obj, SkColorFilter* filter) {
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setColorFilter(filter);
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2308451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkXfermode* setXfermode(JNIEnv* env, jobject clazz, SkPaint* obj, SkXfermode* xfermode) {
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setXfermode(xfermode);
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2348451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkPathEffect* setPathEffect(JNIEnv* env, jobject clazz, SkPaint* obj, SkPathEffect* effect) {
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setPathEffect(effect);
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2388451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkMaskFilter* setMaskFilter(JNIEnv* env, jobject clazz, SkPaint* obj, SkMaskFilter* maskfilter) {
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setMaskFilter(maskfilter);
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2428451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkTypeface* setTypeface(JNIEnv* env, jobject clazz, SkPaint* obj, SkTypeface* typeface) {
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setTypeface(typeface);
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2468451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkRasterizer* setRasterizer(JNIEnv* env, jobject clazz, SkPaint* obj, SkRasterizer* rasterizer) {
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setRasterizer(rasterizer);
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2508451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getTextAlign(JNIEnv* env, jobject clazz, SkPaint* obj) {
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getTextAlign();
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2548451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextAlign(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Align align) {
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setTextAlign(align);
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2588451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
259ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett    // generate bcp47 identifier for the supplied locale
260ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett    static void toLanguageTag(char* output, size_t outSize,
261ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            const char* locale) {
262ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        if (output == NULL || outSize <= 0) {
263ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            return;
264ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        }
265ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        if (locale == NULL) {
266ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            output[0] = '\0';
267ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            return;
268ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        }
269ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        char canonicalChars[ULOC_FULLNAME_CAPACITY];
270ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        UErrorCode uErr = U_ZERO_ERROR;
271ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        uloc_canonicalize(locale, canonicalChars, ULOC_FULLNAME_CAPACITY,
272ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                &uErr);
273ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        if (U_SUCCESS(uErr)) {
274ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            char likelyChars[ULOC_FULLNAME_CAPACITY];
275ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            uErr = U_ZERO_ERROR;
276ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            uloc_addLikelySubtags(canonicalChars, likelyChars,
277ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                    ULOC_FULLNAME_CAPACITY, &uErr);
278ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            if (U_SUCCESS(uErr)) {
279ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                uErr = U_ZERO_ERROR;
280ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr);
281ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                if (U_SUCCESS(uErr)) {
282ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                    return;
283ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                } else {
284ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                    ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars,
285ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                            u_errorName(uErr));
286ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                }
287ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            } else {
288ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s",
289ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                        canonicalChars, u_errorName(uErr));
290ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            }
291ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        } else {
292ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett            ALOGD("uloc_canonicalize(\"%s\") failed: %s", locale,
293ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett                    u_errorName(uErr));
294ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        }
295ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        // unable to build a proper language identifier
296ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        output[0] = '\0';
297ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett    }
298ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett
299517825f1a9f14f92908bd7859b91b927c2eec6d9Fabrice Di Meglio    static void setTextLocale(JNIEnv* env, jobject clazz, SkPaint* obj, jstring locale) {
300ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        ScopedUtfChars localeChars(env, locale);
301ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        char langTag[ULOC_FULLNAME_CAPACITY];
302ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeChars.c_str());
303ac1cbaf2e5575ac75a0160e13089d51a0bb232faBilly Hewlett        obj->setLanguage(SkLanguage(langTag));
304517825f1a9f14f92908bd7859b91b927c2eec6d9Fabrice Di Meglio    }
305517825f1a9f14f92908bd7859b91b927c2eec6d9Fabrice Di Meglio
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getTextSize(JNIEnv* env, jobject paint) {
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize());
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3108451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextSize(JNIEnv* env, jobject paint, jfloat textSize) {
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setTextSize(SkFloatToScalar(textSize));
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3158451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getTextScaleX(JNIEnv* env, jobject paint) {
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextScaleX());
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3208451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextScaleX(JNIEnv* env, jobject paint, jfloat scaleX) {
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setTextScaleX(SkFloatToScalar(scaleX));
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3258451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getTextSkewX(JNIEnv* env, jobject paint) {
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSkewX());
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3308451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextSkewX(JNIEnv* env, jobject paint, jfloat skewX) {
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setTextSkewX(SkFloatToScalar(skewX));
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3358451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat ascent(JNIEnv* env, jobject paint) {
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics    metrics;
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(metrics.fAscent);
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3428451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat descent(JNIEnv* env, jobject paint) {
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics    metrics;
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(metrics.fDescent);
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3498451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getFontMetrics(JNIEnv* env, jobject paint, jobject metricsObj) {
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics metrics;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar             spacing = GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (metricsObj) {
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class));
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop));
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent));
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent));
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom));
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading));
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(spacing);
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3658451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getFontMetricsInt(JNIEnv* env, jobject paint, jobject metricsObj) {
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics metrics;
3698451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int ascent = SkScalarRound(metrics.fAscent);
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int descent = SkScalarRound(metrics.fDescent);
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int leading = SkScalarRound(metrics.fLeading);
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (metricsObj) {
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class));
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloor(metrics.fTop));
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent);
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent);
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeil(metrics.fBottom));
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading);
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return descent - ascent + leading;
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat measureText_CII(JNIEnv* env, jobject jpaint, jcharArray text, int index, int count) {
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, text);
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t textLength = env->GetArrayLength(text);
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((index | count) < 0 || (size_t)(index + count) > textLength) {
3928451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes            doThrowAIOOBE(env);
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3956ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
3966ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
3976ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3994f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
4014f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat result = 0;
402b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio
4038fb507171f68d4170cfeb1187ee7d1f70f98917dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, textArray, index, count, textLength,
40479df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), NULL /* dont need all advances */, &result);
405b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio
4064f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
4074f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        return result;
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4098451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat measureText_StringII(JNIEnv* env, jobject jpaint, jstring text, int start, int end) {
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, text);
4138451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
41451f383d65f9ee3c7d73d0508b576550e7998c5b5Fabrice Di Meglio        size_t textLength = env->GetStringLength(text);
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = end - start;
41651f383d65f9ee3c7d73d0508b576550e7998c5b5Fabrice Di Meglio        if ((start | count) < 0 || (size_t)end > textLength) {
4176ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4186ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4196ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4206ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
4216ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4226ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4236ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4246ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        const jchar* textArray = env->GetStringChars(text, NULL);
4256ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
4264f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat width = 0;
4278451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
42851f383d65f9ee3c7d73d0508b576550e7998c5b5Fabrice Di Meglio        TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength,
42979df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), NULL /* dont need all advances */, &width);
4304f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return width;
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4348451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat measureText_String(JNIEnv* env, jobject jpaint, jstring text) {
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, text);
4388451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t textLength = env->GetStringLength(text);
4406ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (textLength == 0) {
4416ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4426ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4436ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4446ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        const jchar* textArray = env->GetStringChars(text, NULL);
4456ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
4464f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat width = 0;
4476ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4488fb507171f68d4170cfeb1187ee7d1f70f98917dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, textArray, 0, textLength, textLength,
44979df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), NULL /* dont need all advances */, &width);
450b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return width;
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4548451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int dotextwidths(JNIEnv* env, SkPaint* paint, const jchar text[], int count, jfloatArray widths) {
4566ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
4576ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
4586ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4596ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count < 0 || !widths) {
4606ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4616ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4626ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4636ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
4646ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4656ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4666ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        size_t widthsLength = env->GetArrayLength(widths);
4676ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((size_t)count > widthsLength) {
4686ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4696ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4706ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4716ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaFloatArray autoWidths(env, widths, count);
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jfloat* widthsArray = autoWidths.ptr();
4746ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4758fb507171f68d4170cfeb1187ee7d1f70f98917dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, text, 0, count, count,
47679df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), widthsArray, NULL /* dont need totalAdvance */);
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4808451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int getTextWidths___CII_F(JNIEnv* env, jobject clazz, SkPaint* paint, jcharArray text, int index, int count, jfloatArray widths) {
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count = dotextwidths(env, paint, textArray + index, count, widths);
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
4858451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes                                      JNI_ABORT);
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4888451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4899f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    static int getTextWidths__StringII_F(JNIEnv* env, jobject clazz, SkPaint* paint, jstring text,
4909f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            int start, int end, jfloatArray widths) {
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetStringChars(text, NULL);
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = dotextwidths(env, paint, textArray + start, end - start, widths);
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4960c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
4979f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    static int doTextGlyphs(JNIEnv* env, SkPaint* paint, const jchar* text, jint start, jint count,
4989f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            jint contextCount, jint flags, jcharArray glyphs) {
4996ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
5006ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
5016ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
5026ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((start | count | contextCount) < 0 || contextCount < count || !glyphs) {
5036ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
5046ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5056ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5066ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
5076ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5086ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5096ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        size_t glypthsLength = env->GetArrayLength(glyphs);
5106ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((size_t)count > glypthsLength) {
5116ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
5126ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5136ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5146ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
5159f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        jchar* glyphsArray = env->GetCharArrayElements(glyphs, NULL);
5165c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio
517a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
518a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio                text, start, count, contextCount, flags);
519a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio        const jchar* shapedGlyphs = value->getGlyphs();
520a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio        size_t glyphsCount = value->getGlyphsCount();
5215c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio        memcpy(glyphsArray, shapedGlyphs, sizeof(jchar) * glyphsCount);
5225c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio
523eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        env->ReleaseCharArrayElements(glyphs, glyphsArray, JNI_ABORT);
5245c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio        return glyphsCount;
5259f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    }
5269f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio
5279f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    static int getTextGlyphs__StringIIIII_C(JNIEnv* env, jobject clazz, SkPaint* paint,
5289f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint flags,
5299f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            jcharArray glyphs) {
5309f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        const jchar* textArray = env->GetStringChars(text, NULL);
5319f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        int count = doTextGlyphs(env, paint, textArray + contextStart, start - contextStart,
5329f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio                end - start, contextEnd - contextStart, flags, glyphs);
5339f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        env->ReleaseStringChars(text, textArray);
5349f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        return count;
5359f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    }
5369f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio
537f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static jfloat doTextRunAdvances(JNIEnv *env, SkPaint *paint, const jchar *text,
538f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt                                    jint start, jint count, jint contextCount, jint flags,
5390c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                                    jfloatArray advances, jint advancesIndex) {
5406ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
5416ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
5426ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
5436ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
5446ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
5456ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5466ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5476ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
5486ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5496ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5506ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (advances) {
5516ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            size_t advancesLength = env->GetArrayLength(advances);
5526ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            if ((size_t)count > advancesLength) {
5536ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                doThrowAIOOBE(env);
5546ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                return 0;
5556ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            }
5566ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5570c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jfloat advancesArray[count];
5586ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        jfloat totalAdvance = 0;
5590c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
560f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        TextLayout::getTextRunAdvances(paint, text, start, count, contextCount, flags,
56179df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                                       advancesArray, &totalAdvance);
5620c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
5630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        if (advances != NULL) {
5640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray);
5650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        }
5660c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return totalAdvance;
5670c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
5680c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
569eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio    static jfloat doTextRunAdvancesICU(JNIEnv *env, SkPaint *paint, const jchar *text,
570eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio                                    jint start, jint count, jint contextCount, jint flags,
571eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio                                    jfloatArray advances, jint advancesIndex) {
5726ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
5736ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
5746ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
5756ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
5766ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
5776ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5786ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5796ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
5806ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5816ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5826ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (advances) {
5836ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            size_t advancesLength = env->GetArrayLength(advances);
5846ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            if ((size_t)count > advancesLength) {
5856ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                doThrowAIOOBE(env);
5866ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                return 0;
5876ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            }
5886ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5896ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
590eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        jfloat advancesArray[count];
5916ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        jfloat totalAdvance = 0;
592eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
593eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        TextLayout::getTextRunAdvancesICU(paint, text, start, count, contextCount, flags,
594eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio                                       advancesArray, totalAdvance);
595eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
596eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        if (advances != NULL) {
597eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio            env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray);
598eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        }
599eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        return totalAdvance;
600eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio    }
601eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
6020a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio    static float getTextRunAdvances___CIIIII_FII(JNIEnv* env, jobject clazz, SkPaint* paint,
6030c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
6040a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio            jint flags, jfloatArray advances, jint advancesIndex, jint reserved) {
6050c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jchar* textArray = env->GetCharArrayElements(text, NULL);
6060a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio        jfloat result = (reserved == 0) ?
6070a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvances(env, paint, textArray + contextIndex, index - contextIndex,
6080a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        count, contextCount, flags, advances, advancesIndex) :
6090a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvancesICU(env, paint, textArray + contextIndex, index - contextIndex,
6100a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        count, contextCount, flags, advances, advancesIndex);
6110c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
6120c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return result;
6130c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6140c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6150a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio    static float getTextRunAdvances__StringIIIII_FII(JNIEnv* env, jobject clazz, SkPaint* paint,
6160c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint flags,
6170a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio            jfloatArray advances, jint advancesIndex, jint reserved) {
6180c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        const jchar* textArray = env->GetStringChars(text, NULL);
6190a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio        jfloat result = (reserved == 0) ?
6200a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvances(env, paint, textArray + contextStart, start - contextStart,
6210a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        end - start, contextEnd - contextStart, flags, advances, advancesIndex) :
6220a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvancesICU(env, paint, textArray + contextStart, start - contextStart,
6230a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        end - start, contextEnd - contextStart, flags, advances, advancesIndex);
624eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        env->ReleaseStringChars(text, textArray);
625eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        return result;
626eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio    }
627eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
6280c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static jint doTextRunCursor(JNIEnv *env, SkPaint* paint, const jchar *text, jint start,
6290c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jint count, jint flags, jint offset, jint opt) {
6304f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat scalarArray[count];
6319c418dbc56efd334c68872d281f75138e16eae46Fabrice Di Meglio
632577b07197b1bf1b3903e0bc0f3d48fabfd927b2aRaph Levien        TextLayout::getTextRunAdvances(paint, text, start, count, start + count, flags,
63379df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                scalarArray, NULL /* dont need totalAdvance */);
6340c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint pos = offset - start;
6360c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        switch (opt) {
6370c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AFTER:
6380c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          if (pos < count) {
6390c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            pos += 1;
6400c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6410c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          // fall through
6420c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AT_OR_AFTER:
6430c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          while (pos < count && scalarArray[pos] == 0) {
6440c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            ++pos;
6450c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6460c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          break;
6470c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case BEFORE:
6480c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          if (pos > 0) {
6490c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            --pos;
6500c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6510c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          // fall through
6520c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AT_OR_BEFORE:
6530c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          while (pos > 0 && scalarArray[pos] == 0) {
6540c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            --pos;
6550c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6560c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          break;
6570c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AT:
6580c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        default:
6590c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          if (scalarArray[pos] == 0) {
6600c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            pos = -1;
6610c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6620c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          break;
6630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        }
6640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        if (pos != -1) {
6660c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          pos += start;
6670c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        }
6680c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6690c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return pos;
6700c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6710c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6720c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, SkPaint* paint, jcharArray text,
6730c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jint contextStart, jint contextCount, jint flags, jint offset, jint cursorOpt) {
6740c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jchar* textArray = env->GetCharArrayElements(text, NULL);
6750c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, flags,
6760c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                offset, cursorOpt);
6770c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
6780c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return result;
6790c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6800c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6810c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, SkPaint* paint, jstring text,
6820c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jint contextStart, jint contextEnd, jint flags, jint offset, jint cursorOpt) {
6830c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        const jchar* textArray = env->GetStringChars(text, NULL);
6840c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint result = doTextRunCursor(env, paint, textArray, contextStart,
6850c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                contextEnd - contextStart, flags, offset, cursorOpt);
6860c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        env->ReleaseStringChars(text, textArray);
6870c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return result;
6880c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6890c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
690f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static void getTextPath(JNIEnv* env, SkPaint* paint, const jchar* text, jint count,
691f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt                            jint bidiFlags, jfloat x, jfloat y, SkPath *path) {
692f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        TextLayout::getTextPath(paint, text, count, bidiFlags, x, y, path);
693f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    }
694f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
695f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static void getTextPath___C(JNIEnv* env, jobject clazz, SkPaint* paint, jint bidiFlags,
696f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt            jcharArray text, int index, int count, jfloat x, jfloat y, SkPath* path) {
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
698f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        getTextPath(env, paint, textArray + index, count, bidiFlags, x, y, path);
699f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
701f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
702f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static void getTextPath__String(JNIEnv* env, jobject clazz, SkPaint* paint, jint bidiFlags,
703f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt            jstring text, int start, int end, jfloat x, jfloat y, SkPath* path) {
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetStringChars(text, NULL);
705f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        getTextPath(env, paint, textArray + start, end - start, bidiFlags, x, y, path);
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
708f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setShadowLayer(JNIEnv* env, jobject jpaint, jfloat radius,
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               jfloat dx, jfloat dy, int color) {
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jpaint);
7128451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
7138451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (radius <= 0) {
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint->setLooper(NULL);
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else {
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint->setLooper(new SkBlurDrawLooper(SkFloatToScalar(radius),
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  SkFloatToScalar(dx),
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  SkFloatToScalar(dy),
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  (SkColor)color))->unref();
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
725bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio    static int breakText(JNIEnv* env, SkPaint& paint, const jchar text[],
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         int count, float maxWidth, jfloatArray jmeasured,
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkPaint::TextBufferDirection tbd) {
728a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint,
729a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio                text, 0, count, count, paint.getFlags());
730bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        if (value == NULL) {
731a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio            return 0;
732bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        }
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar     measured;
734bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        size_t       bytes = paint.breakText(value->getGlyphs(), value->getGlyphsCount() << 1,
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   SkFloatToScalar(maxWidth), &measured, tbd);
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkASSERT((bytes & 1) == 0);
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jfloat* array = autoMeasured.ptr();
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            array[0] = SkScalarToFloat(measured);
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bytes >> 1;
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int breakTextC(JNIEnv* env, jobject jpaint, jcharArray jtext,
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int index, int count, float maxWidth, jfloatArray jmeasuredWidth) {
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jtext);
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::TextBufferDirection tbd;
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (count < 0) {
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tbd = SkPaint::kBackward_TextBufferDirection;
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            count = -count;
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else {
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tbd = SkPaint::kForward_TextBufferDirection;
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
7618451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes            doThrowAIOOBE(env);
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint*     paint = GraphicsJNI::getNativePaint(env, jpaint);
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* text = env->GetCharArrayElements(jtext, NULL);
7679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count = breakText(env, *paint, text + index, count, maxWidth,
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          jmeasuredWidth, tbd);
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      JNI_ABORT);
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int breakTextS(JNIEnv* env, jobject jpaint, jstring jtext,
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                bool forwards, float maxWidth, jfloatArray jmeasuredWidth) {
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jtext);
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::TextBufferDirection tbd = forwards ?
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        SkPaint::kForward_TextBufferDirection :
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        SkPaint::kBackward_TextBufferDirection;
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = env->GetStringLength(jtext);
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* text = env->GetStringChars(jtext, NULL);
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count = breakText(env, *paint, text, count, maxWidth,
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          jmeasuredWidth, tbd);
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(jtext, text);
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void doTextBounds(JNIEnv* env, const jchar* text, int count,
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jobject bounds, const SkPaint& paint)
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
79546e942d500d5ce67a58269cbac3307deca6a5c9fZhou Chang        SkRect  r{0,0,0,0};
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkIRect ir;
7978451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
798a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint,
799a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio                text, 0, count, count, paint.getFlags());
800bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        if (value == NULL) {
801a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio            return;
802bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        }
803bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        paint.measureText(value->getGlyphs(), value->getGlyphsCount() << 1, &r);
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        r.roundOut(&ir);
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::irect_to_jrect(ir, env, bounds);
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void getStringBounds(JNIEnv* env, jobject, const SkPaint* paint,
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                jstring text, int start, int end, jobject bounds)
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetStringChars(text, NULL);
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doTextBounds(env, textArray + start, end - start, bounds, *paint);
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8158451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void getCharArrayBounds(JNIEnv* env, jobject, const SkPaint* paint,
8179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        jcharArray text, int index, int count, jobject bounds)
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doTextBounds(env, textArray + index, count, bounds, *paint);
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
8229066cfe9886ac131c34d59e