Paint.cpp revision bd901dee317d10c6a921922c3d7d788b90306c82
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>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkBlurDrawLooper.h"
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkColorFilter.h"
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkMaskFilter.h"
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkRasterizer.h"
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkShader.h"
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkTypeface.h"
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkXfermode.h"
330c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt#include "unicode/ushape.h"
34f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt#include "TextLayout.h"
350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
360c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt// temporary for debugging
375c13d89c1332fcc499379b9064b891187b75ca32Chet Haase#include <Caches.h>
380c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt#include <utils/Log.h>
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct JMetricsID {
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    top;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    ascent;
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    descent;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    bottom;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID    leading;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gFontMetrics_class;
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JMetricsID gFontMetrics_fieldID;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gFontMetricsInt_class;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JMetricsID gFontMetricsInt_fieldID;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
563d63e0119dc763ed0a06fd7498375746fd391d80Mike Reedstatic void defaultSettingsForAndroid(SkPaint* paint) {
57c511bee87cda99a252d1a62487f47c8f05aee78cFabrice Di Meglio    // GlyphID encoding is required because we are using Harfbuzz shaping
58c511bee87cda99a252d1a62487f47c8f05aee78cFabrice Di Meglio    paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
593d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed}
603d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass SkPaintGlue {
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    enum MoveOpt {
640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    };
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void finalizer(JNIEnv* env, jobject clazz, SkPaint* obj) {
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete obj;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkPaint* init(JNIEnv* env, jobject clazz) {
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint* obj = new SkPaint();
733d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed        defaultSettingsForAndroid(obj);
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkPaint* intiWithPaint(JNIEnv* env, jobject clazz, SkPaint* paint) {
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint* obj = new SkPaint(*paint);
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
818451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void reset(JNIEnv* env, jobject clazz, SkPaint* obj) {
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->reset();
843d63e0119dc763ed0a06fd7498375746fd391d80Mike Reed        defaultSettingsForAndroid(obj);
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
868451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void assign(JNIEnv* env, jobject clazz, SkPaint* dst, const SkPaint* src) {
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst = *src;
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
908451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getFlags(JNIEnv* env, jobject paint) {
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativePaint(env, paint)->getFlags();
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
958451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setFlags(JNIEnv* env, jobject paint, jint flags) {
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setFlags(flags);
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1008451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
101f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    static jint getHinting(JNIEnv* env, jobject paint) {
102f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        NPE_CHECK_RETURN_ZERO(env, paint);
103f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        return GraphicsJNI::getNativePaint(env, paint)->getHinting()
104f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn                == SkPaint::kNo_Hinting ? 0 : 1;
105f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    }
106f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn
107f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    static void setHinting(JNIEnv* env, jobject paint, jint mode) {
108f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        NPE_CHECK_RETURN_VOID(env, paint);
109f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn        GraphicsJNI::getNativePaint(env, paint)->setHinting(
110f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn                mode == 0 ? SkPaint::kNo_Hinting : SkPaint::kSlight_Hinting);
111f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    }
112f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setAntiAlias(JNIEnv* env, jobject paint, jboolean aa) {
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setAntiAlias(aa);
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1178451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setLinearText(JNIEnv* env, jobject paint, jboolean linearText) {
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setLinearText(linearText);
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1228451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setSubpixelText(JNIEnv* env, jobject paint, jboolean subpixelText) {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setSubpixelText(subpixelText);
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1278451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setUnderlineText(JNIEnv* env, jobject paint, jboolean underlineText) {
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setUnderlineText(underlineText);
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1328451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrikeThruText(JNIEnv* env, jobject paint, jboolean strikeThruText) {
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setStrikeThruText(strikeThruText);
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1378451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setFakeBoldText(JNIEnv* env, jobject paint, jboolean fakeBoldText) {
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setFakeBoldText(fakeBoldText);
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1428451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setFilterBitmap(JNIEnv* env, jobject paint, jboolean filterBitmap) {
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setFilterBitmap(filterBitmap);
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1478451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setDither(JNIEnv* env, jobject paint, jboolean dither) {
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setDither(dither);
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1528451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getStyle(JNIEnv* env, jobject clazz, SkPaint* obj) {
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getStyle();
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1568451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStyle(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Style style) {
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setStyle(style);
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1608451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getColor(JNIEnv* env, jobject paint) {
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativePaint(env, paint)->getColor();
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1658451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getAlpha(JNIEnv* env, jobject paint) {
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativePaint(env, paint)->getAlpha();
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1708451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setColor(JNIEnv* env, jobject paint, jint color) {
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setColor(color);
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1758451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setAlpha(JNIEnv* env, jobject paint, jint a) {
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setAlpha(a);
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1808451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getStrokeWidth(JNIEnv* env, jobject paint) {
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeWidth());
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1858451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeWidth(JNIEnv* env, jobject paint, jfloat width) {
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setStrokeWidth(SkFloatToScalar(width));
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1908451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getStrokeMiter(JNIEnv* env, jobject paint) {
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeMiter());
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1958451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeMiter(JNIEnv* env, jobject paint, jfloat miter) {
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setStrokeMiter(SkFloatToScalar(miter));
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2008451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getStrokeCap(JNIEnv* env, jobject clazz, SkPaint* obj) {
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getStrokeCap();
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2048451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeCap(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Cap cap) {
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setStrokeCap(cap);
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2088451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getStrokeJoin(JNIEnv* env, jobject clazz, SkPaint* obj) {
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getStrokeJoin();
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2128451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setStrokeJoin(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Join join) {
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setStrokeJoin(join);
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2168451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean getFillPath(JNIEnv* env, jobject clazz, SkPaint* obj, SkPath* src, SkPath* dst) {
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getFillPath(*src, dst);
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2208451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkShader* setShader(JNIEnv* env, jobject clazz, SkPaint* obj, SkShader* shader) {
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setShader(shader);
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2248451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkColorFilter* setColorFilter(JNIEnv* env, jobject clazz, SkPaint* obj, SkColorFilter* filter) {
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setColorFilter(filter);
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2288451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkXfermode* setXfermode(JNIEnv* env, jobject clazz, SkPaint* obj, SkXfermode* xfermode) {
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setXfermode(xfermode);
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2328451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkPathEffect* setPathEffect(JNIEnv* env, jobject clazz, SkPaint* obj, SkPathEffect* effect) {
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setPathEffect(effect);
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2368451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkMaskFilter* setMaskFilter(JNIEnv* env, jobject clazz, SkPaint* obj, SkMaskFilter* maskfilter) {
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setMaskFilter(maskfilter);
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2408451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkTypeface* setTypeface(JNIEnv* env, jobject clazz, SkPaint* obj, SkTypeface* typeface) {
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setTypeface(typeface);
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2448451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkRasterizer* setRasterizer(JNIEnv* env, jobject clazz, SkPaint* obj, SkRasterizer* rasterizer) {
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->setRasterizer(rasterizer);
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2488451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getTextAlign(JNIEnv* env, jobject clazz, SkPaint* obj) {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obj->getTextAlign();
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2528451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextAlign(JNIEnv* env, jobject clazz, SkPaint* obj, SkPaint::Align align) {
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        obj->setTextAlign(align);
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2568451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getTextSize(JNIEnv* env, jobject paint) {
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize());
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2618451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextSize(JNIEnv* env, jobject paint, jfloat textSize) {
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setTextSize(SkFloatToScalar(textSize));
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2668451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getTextScaleX(JNIEnv* env, jobject paint) {
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextScaleX());
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2718451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextScaleX(JNIEnv* env, jobject paint, jfloat scaleX) {
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setTextScaleX(SkFloatToScalar(scaleX));
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2768451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getTextSkewX(JNIEnv* env, jobject paint) {
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSkewX());
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2818451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setTextSkewX(JNIEnv* env, jobject paint, jfloat skewX) {
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, paint);
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->setTextSkewX(SkFloatToScalar(skewX));
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2868451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat ascent(JNIEnv* env, jobject paint) {
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics    metrics;
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(metrics.fAscent);
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2938451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat descent(JNIEnv* env, jobject paint) {
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics    metrics;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(metrics.fDescent);
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3008451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat getFontMetrics(JNIEnv* env, jobject paint, jobject metricsObj) {
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics metrics;
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar             spacing = GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (metricsObj) {
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT(env->IsInstanceOf(metricsObj, gFontMetrics_class));
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.top, SkScalarToFloat(metrics.fTop));
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.ascent, SkScalarToFloat(metrics.fAscent));
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.descent, SkScalarToFloat(metrics.fDescent));
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.bottom, SkScalarToFloat(metrics.fBottom));
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetFloatField(metricsObj, gFontMetrics_fieldID.leading, SkScalarToFloat(metrics.fLeading));
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SkScalarToFloat(spacing);
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3168451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jint getFontMetricsInt(JNIEnv* env, jobject paint, jobject metricsObj) {
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, paint);
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::FontMetrics metrics;
3208451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::getNativePaint(env, paint)->getFontMetrics(&metrics);
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int ascent = SkScalarRound(metrics.fAscent);
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int descent = SkScalarRound(metrics.fDescent);
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int leading = SkScalarRound(metrics.fLeading);
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (metricsObj) {
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT(env->IsInstanceOf(metricsObj, gFontMetricsInt_class));
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.top, SkScalarFloor(metrics.fTop));
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.ascent, ascent);
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.descent, descent);
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.bottom, SkScalarCeil(metrics.fBottom));
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->SetIntField(metricsObj, gFontMetricsInt_fieldID.leading, leading);
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return descent - ascent + leading;
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat measureText_CII(JNIEnv* env, jobject jpaint, jcharArray text, int index, int count) {
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, text);
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t textLength = env->GetArrayLength(text);
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((index | count) < 0 || (size_t)(index + count) > textLength) {
3438451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes            doThrowAIOOBE(env);
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3466ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
3476ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
3486ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3504f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
3524f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat result = 0;
353b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio
3548fb507171f68d4170cfeb1187ee7d1f70f98917dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, textArray, index, count, textLength,
35579df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), NULL /* dont need all advances */, &result);
356b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio
3574f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
3584f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        return result;
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3608451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat measureText_StringII(JNIEnv* env, jobject jpaint, jstring text, int start, int end) {
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, text);
3648451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
36551f383d65f9ee3c7d73d0508b576550e7998c5b5Fabrice Di Meglio        size_t textLength = env->GetStringLength(text);
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = end - start;
36751f383d65f9ee3c7d73d0508b576550e7998c5b5Fabrice Di Meglio        if ((start | count) < 0 || (size_t)end > textLength) {
3686ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
3696ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
3706ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
3716ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
3726ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
3736ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
3746ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
3756ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        const jchar* textArray = env->GetStringChars(text, NULL);
3766ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
3774f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat width = 0;
3788451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
37951f383d65f9ee3c7d73d0508b576550e7998c5b5Fabrice Di Meglio        TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength,
38079df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), NULL /* dont need all advances */, &width);
3814f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return width;
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3858451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jfloat measureText_String(JNIEnv* env, jobject jpaint, jstring text) {
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, text);
3898451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t textLength = env->GetStringLength(text);
3916ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (textLength == 0) {
3926ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
3936ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
3946ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
3956ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        const jchar* textArray = env->GetStringChars(text, NULL);
3966ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
3974f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat width = 0;
3986ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
3998fb507171f68d4170cfeb1187ee7d1f70f98917dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, textArray, 0, textLength, textLength,
40079df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), NULL /* dont need all advances */, &width);
401b02d0ca5553300063e4332192632312600caf4b9Fabrice Di Meglio
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return width;
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4058451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int dotextwidths(JNIEnv* env, SkPaint* paint, const jchar text[], int count, jfloatArray widths) {
4076ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
4086ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
4096ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4106ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count < 0 || !widths) {
4116ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4126ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4136ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4146ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
4156ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4166ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4176ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        size_t widthsLength = env->GetArrayLength(widths);
4186ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((size_t)count > widthsLength) {
4196ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4206ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4216ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4226ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaFloatArray autoWidths(env, widths, count);
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jfloat* widthsArray = autoWidths.ptr();
4256ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4268fb507171f68d4170cfeb1187ee7d1f70f98917dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, text, 0, count, count,
42779df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                paint->getFlags(), widthsArray, NULL /* dont need totalAdvance */);
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4318451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int getTextWidths___CII_F(JNIEnv* env, jobject clazz, SkPaint* paint, jcharArray text, int index, int count, jfloatArray widths) {
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count = dotextwidths(env, paint, textArray + index, count, widths);
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
4368451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes                                      JNI_ABORT);
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4398451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
4409f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    static int getTextWidths__StringII_F(JNIEnv* env, jobject clazz, SkPaint* paint, jstring text,
4419f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            int start, int end, jfloatArray widths) {
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetStringChars(text, NULL);
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = dotextwidths(env, paint, textArray + start, end - start, widths);
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4470c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
4489f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    static int doTextGlyphs(JNIEnv* env, SkPaint* paint, const jchar* text, jint start, jint count,
4499f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            jint contextCount, jint flags, jcharArray glyphs) {
4506ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
4516ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
4526ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4536ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((start | count | contextCount) < 0 || contextCount < count || !glyphs) {
4546ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4556ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4566ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4576ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
4586ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4596ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4606ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        size_t glypthsLength = env->GetArrayLength(glyphs);
4616ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((size_t)count > glypthsLength) {
4626ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4636ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4646ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4656ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4669f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        jchar* glyphsArray = env->GetCharArrayElements(glyphs, NULL);
4675c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio
4680af10b54bf110653b74cb92793484b412a90b657Fabrice Di Meglio        TextLayoutCacheValue value(contextCount);
469bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        TextLayoutEngine::getInstance().computeValues(&value, paint, text, start, count,
470bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                contextCount, flags);
4715c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio        const jchar* shapedGlyphs = value.getGlyphs();
4725c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio        size_t glyphsCount = value.getGlyphsCount();
4735c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio        memcpy(glyphsArray, shapedGlyphs, sizeof(jchar) * glyphsCount);
4745c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio
475eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        env->ReleaseCharArrayElements(glyphs, glyphsArray, JNI_ABORT);
4765c863f741e8e484bb39decd516c9fa4c6322e671Fabrice Di Meglio        return glyphsCount;
4779f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    }
4789f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio
4799f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    static int getTextGlyphs__StringIIIII_C(JNIEnv* env, jobject clazz, SkPaint* paint,
4809f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint flags,
4819f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio            jcharArray glyphs) {
4829f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        const jchar* textArray = env->GetStringChars(text, NULL);
4839f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        int count = doTextGlyphs(env, paint, textArray + contextStart, start - contextStart,
4849f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio                end - start, contextEnd - contextStart, flags, glyphs);
4859f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        env->ReleaseStringChars(text, textArray);
4869f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        return count;
4879f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    }
4889f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio
489f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static jfloat doTextRunAdvances(JNIEnv *env, SkPaint *paint, const jchar *text,
490f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt                                    jint start, jint count, jint contextCount, jint flags,
4910c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                                    jfloatArray advances, jint advancesIndex) {
4926ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
4936ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
4946ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
4956ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
4966ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
4976ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
4986ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
4996ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
5006ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5016ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5026ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (advances) {
5036ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            size_t advancesLength = env->GetArrayLength(advances);
5046ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            if ((size_t)count > advancesLength) {
5056ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                doThrowAIOOBE(env);
5066ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                return 0;
5076ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            }
5086ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5090c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jfloat advancesArray[count];
5106ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        jfloat totalAdvance = 0;
5110c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
512f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        TextLayout::getTextRunAdvances(paint, text, start, count, contextCount, flags,
51379df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                                       advancesArray, &totalAdvance);
5140c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
5150c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        if (advances != NULL) {
5160c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray);
5170c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        }
5180c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return totalAdvance;
5190c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
5200c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
521eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio    static jfloat doTextRunAdvancesICU(JNIEnv *env, SkPaint *paint, const jchar *text,
522eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio                                    jint start, jint count, jint contextCount, jint flags,
523eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio                                    jfloatArray advances, jint advancesIndex) {
5246ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, paint);
5256ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        NPE_CHECK_RETURN_ZERO(env, text);
5266ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
5276ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if ((start | count | contextCount | advancesIndex) < 0 || contextCount < count) {
5286ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            doThrowAIOOBE(env);
5296ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5306ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5316ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (count == 0) {
5326ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            return 0;
5336ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5346ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        if (advances) {
5356ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            size_t advancesLength = env->GetArrayLength(advances);
5366ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            if ((size_t)count > advancesLength) {
5376ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                doThrowAIOOBE(env);
5386ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio                return 0;
5396ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio            }
5406ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        }
5416ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio
542eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        jfloat advancesArray[count];
5436ab90ed017fb52aac4493a2fac897299d345874fFabrice Di Meglio        jfloat totalAdvance = 0;
544eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
545eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        TextLayout::getTextRunAdvancesICU(paint, text, start, count, contextCount, flags,
546eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio                                       advancesArray, totalAdvance);
547eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
548eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        if (advances != NULL) {
549eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio            env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray);
550eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        }
551eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        return totalAdvance;
552eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio    }
553eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
5540a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio    static float getTextRunAdvances___CIIIII_FII(JNIEnv* env, jobject clazz, SkPaint* paint,
5550c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jcharArray text, jint index, jint count, jint contextIndex, jint contextCount,
5560a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio            jint flags, jfloatArray advances, jint advancesIndex, jint reserved) {
5570c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jchar* textArray = env->GetCharArrayElements(text, NULL);
5580a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio        jfloat result = (reserved == 0) ?
5590a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvances(env, paint, textArray + contextIndex, index - contextIndex,
5600a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        count, contextCount, flags, advances, advancesIndex) :
5610a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvancesICU(env, paint, textArray + contextIndex, index - contextIndex,
5620a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        count, contextCount, flags, advances, advancesIndex);
5630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
5640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return result;
5650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
5660c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
5670a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio    static float getTextRunAdvances__StringIIIII_FII(JNIEnv* env, jobject clazz, SkPaint* paint,
5680c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jstring text, jint start, jint end, jint contextStart, jint contextEnd, jint flags,
5690a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio            jfloatArray advances, jint advancesIndex, jint reserved) {
5700c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        const jchar* textArray = env->GetStringChars(text, NULL);
5710a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio        jfloat result = (reserved == 0) ?
5720a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvances(env, paint, textArray + contextStart, start - contextStart,
5730a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        end - start, contextEnd - contextStart, flags, advances, advancesIndex) :
5740a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                doTextRunAdvancesICU(env, paint, textArray + contextStart, start - contextStart,
5750a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio                        end - start, contextEnd - contextStart, flags, advances, advancesIndex);
576eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        env->ReleaseStringChars(text, textArray);
577eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio        return result;
578eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio    }
579eee49c699c035ffba188417489f40d34f587d65cFabrice Di Meglio
5800c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static jint doTextRunCursor(JNIEnv *env, SkPaint* paint, const jchar *text, jint start,
5810c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jint count, jint flags, jint offset, jint opt) {
5824f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        jfloat scalarArray[count];
5839c418dbc56efd334c68872d281f75138e16eae46Fabrice Di Meglio
5844f810c8535055bd9a8d89a7d1ba0a7c712a8843dFabrice Di Meglio        TextLayout::getTextRunAdvances(paint, text, start, count, count, flags,
58579df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio                scalarArray, NULL /* dont need totalAdvance */);
5860c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
5870c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint pos = offset - start;
5880c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        switch (opt) {
5890c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AFTER:
5900c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          if (pos < count) {
5910c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            pos += 1;
5920c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
5930c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          // fall through
5940c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AT_OR_AFTER:
5950c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          while (pos < count && scalarArray[pos] == 0) {
5960c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            ++pos;
5970c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
5980c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          break;
5990c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case BEFORE:
6000c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          if (pos > 0) {
6010c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            --pos;
6020c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6030c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          // fall through
6040c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AT_OR_BEFORE:
6050c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          while (pos > 0 && scalarArray[pos] == 0) {
6060c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            --pos;
6070c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6080c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          break;
6090c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        case AT:
6100c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        default:
6110c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          if (scalarArray[pos] == 0) {
6120c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            pos = -1;
6130c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          }
6140c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          break;
6150c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        }
6160c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6170c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        if (pos != -1) {
6180c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt          pos += start;
6190c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        }
6200c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6210c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return pos;
6220c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6230c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6240c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, SkPaint* paint, jcharArray text,
6250c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jint contextStart, jint contextCount, jint flags, jint offset, jint cursorOpt) {
6260c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jchar* textArray = env->GetCharArrayElements(text, NULL);
6270c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint result = doTextRunCursor(env, paint, textArray, contextStart, contextCount, flags,
6280c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                offset, cursorOpt);
6290c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
6300c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return result;
6310c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6320c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
6330c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static jint getTextRunCursor__String(JNIEnv* env, jobject clazz, SkPaint* paint, jstring text,
6340c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt            jint contextStart, jint contextEnd, jint flags, jint offset, jint cursorOpt) {
6350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        const jchar* textArray = env->GetStringChars(text, NULL);
6360c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint result = doTextRunCursor(env, paint, textArray, contextStart,
6370c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                contextEnd - contextStart, flags, offset, cursorOpt);
6380c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        env->ReleaseStringChars(text, textArray);
6390c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        return result;
6400c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    }
6410c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
642f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static void getTextPath(JNIEnv* env, SkPaint* paint, const jchar* text, jint count,
643f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt                            jint bidiFlags, jfloat x, jfloat y, SkPath *path) {
644f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        TextLayout::getTextPath(paint, text, count, bidiFlags, x, y, path);
645f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    }
646f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
647f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static void getTextPath___C(JNIEnv* env, jobject clazz, SkPaint* paint, jint bidiFlags,
648f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt            jcharArray text, int index, int count, jfloat x, jfloat y, SkPath* path) {
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
650f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        getTextPath(env, paint, textArray + index, count, bidiFlags, x, y, path);
651f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
653f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
654f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    static void getTextPath__String(JNIEnv* env, jobject clazz, SkPaint* paint, jint bidiFlags,
655f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt            jstring text, int start, int end, jfloat x, jfloat y, SkPath* path) {
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetStringChars(text, NULL);
657f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt        getTextPath(env, paint, textArray + start, end - start, bidiFlags, x, y, path);
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
660f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setShadowLayer(JNIEnv* env, jobject jpaint, jfloat radius,
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               jfloat dx, jfloat dy, int color) {
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jpaint);
6648451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
6658451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (radius <= 0) {
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint->setLooper(NULL);
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else {
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint->setLooper(new SkBlurDrawLooper(SkFloatToScalar(radius),
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  SkFloatToScalar(dx),
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  SkFloatToScalar(dy),
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  (SkColor)color))->unref();
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
677bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio    static int breakText(JNIEnv* env, SkPaint& paint, const jchar text[],
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         int count, float maxWidth, jfloatArray jmeasured,
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkPaint::TextBufferDirection tbd) {
680bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        sp<TextLayoutCacheValue> value;
681bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio#if USE_TEXT_LAYOUT_CACHE
682bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        value = TextLayoutCache::getInstance().getValue(&paint, text, 0, count,
683bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                count, paint.getFlags());
684bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        if (value == NULL) {
685bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio            ALOGE("Cannot get TextLayoutCache value for text = '%s'",
686bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                    String8(text, count).string());
687bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        }
688bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio#else
689bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        value = new TextLayoutCacheValue(count);
690bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        TextLayoutEngine::getInstance().computeValues(value.get(), &paint,
691bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                reinterpret_cast<const UChar*>(text), 0, count, count, paint.getFlags());
692bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio#endif
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar     measured;
695bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        size_t       bytes = paint.breakText(value->getGlyphs(), value->getGlyphsCount() << 1,
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   SkFloatToScalar(maxWidth), &measured, tbd);
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkASSERT((bytes & 1) == 0);
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            jfloat* array = autoMeasured.ptr();
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            array[0] = SkScalarToFloat(measured);
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bytes >> 1;
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int breakTextC(JNIEnv* env, jobject jpaint, jcharArray jtext,
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int index, int count, float maxWidth, jfloatArray jmeasuredWidth) {
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jtext);
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::TextBufferDirection tbd;
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (count < 0) {
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tbd = SkPaint::kBackward_TextBufferDirection;
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            count = -count;
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else {
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tbd = SkPaint::kForward_TextBufferDirection;
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((index < 0) || (index + count > env->GetArrayLength(jtext))) {
7228451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes            doThrowAIOOBE(env);
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint*     paint = GraphicsJNI::getNativePaint(env, jpaint);
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* text = env->GetCharArrayElements(jtext, NULL);
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count = breakText(env, *paint, text + index, count, maxWidth,
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          jmeasuredWidth, tbd);
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseCharArrayElements(jtext, const_cast<jchar*>(text),
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      JNI_ABORT);
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int breakTextS(JNIEnv* env, jobject jpaint, jstring jtext,
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                bool forwards, float maxWidth, jfloatArray jmeasuredWidth) {
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jpaint);
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jtext);
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint::TextBufferDirection tbd = forwards ?
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        SkPaint::kForward_TextBufferDirection :
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        SkPaint::kBackward_TextBufferDirection;
7439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = env->GetStringLength(jtext);
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* text = env->GetStringChars(jtext, NULL);
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count = breakText(env, *paint, text, count, maxWidth,
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          jmeasuredWidth, tbd);
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(jtext, text);
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return count;
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void doTextBounds(JNIEnv* env, const jchar* text, int count,
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jobject bounds, const SkPaint& paint)
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect  r;
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkIRect ir;
7588451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
759bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        sp<TextLayoutCacheValue> value;
760bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio#if USE_TEXT_LAYOUT_CACHE
761bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        value = TextLayoutCache::getInstance().getValue(&paint, text, 0, count,
762bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                count, paint.getFlags());
763bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        if (value == NULL) {
764bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio            ALOGE("Cannot get TextLayoutCache value for text = '%s'",
765bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                    String8(text, count).string());
766bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        }
767bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio#else
768bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        value = new TextLayoutCacheValue(count);
769bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        TextLayoutEngine::getInstance().computeValues(value.get(), &paint,
770bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio                reinterpret_cast<const UChar*>(text), 0, count, count, paint.getFlags());
771bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio#endif
772bd901dee317d10c6a921922c3d7d788b90306c82Fabrice Di Meglio        paint.measureText(value->getGlyphs(), value->getGlyphsCount() << 1, &r);
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        r.roundOut(&ir);
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::irect_to_jrect(ir, env, bounds);
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void getStringBounds(JNIEnv* env, jobject, const SkPaint* paint,
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                jstring text, int start, int end, jobject bounds)
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetStringChars(text, NULL);
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doTextBounds(env, textArray + start, end - start, bounds, *paint);
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseStringChars(text, textArray);
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7848451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void getCharArrayBounds(JNIEnv* env, jobject, const SkPaint* paint,
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        jcharArray text, int index, int count, jobject bounds)
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const jchar* textArray = env->GetCharArrayElements(text, NULL);
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doTextBounds(env, textArray + index, count, bounds, *paint);
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray),
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      JNI_ABORT);
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7938451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod methods[] = {
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"finalizer", "(I)V", (void*) SkPaintGlue::finalizer},
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_init","()I", (void*) SkPaintGlue::init},
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_initWithPaint","(I)I", (void*) SkPaintGlue::intiWithPaint},
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_reset","(I)V", (void*) SkPaintGlue::reset},
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_set","(II)V", (void*) SkPaintGlue::assign},
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getFlags","()I", (void*) SkPaintGlue::getFlags},
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setFlags","(I)V", (void*) SkPaintGlue::setFlags},
804f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    {"getHinting","()I", (void*) SkPaintGlue::getHinting},
805f43fa5746ee5b81a6e386d36594094d079ac8160Dianne Hackborn    {"setHinting","(I)V", (void*) SkPaintGlue::setHinting},
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setAntiAlias","(Z)V", (void*) SkPaintGlue::setAntiAlias},
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setSubpixelText","(Z)V", (void*) SkPaintGlue::setSubpixelText},
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setLinearText","(Z)V", (void*) SkPaintGlue::setLinearText},
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setUnderlineText","(Z)V", (void*) SkPaintGlue::setUnderlineText},
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setStrikeThruText","(Z)V", (void*) SkPaintGlue::setStrikeThruText},
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setFakeBoldText","(Z)V", (void*) SkPaintGlue::setFakeBoldText},
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setFilterBitmap","(Z)V", (void*) SkPaintGlue::setFilterBitmap},
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setDither","(Z)V", (void*) SkPaintGlue::setDither},
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getStyle","(I)I", (void*) SkPaintGlue::getStyle},
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setStyle","(II)V", (void*) SkPaintGlue::setStyle},
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getColor","()I", (void*) SkPaintGlue::getColor},
8179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setColor","(I)V", (void*) SkPaintGlue::setColor},
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getAlpha","()I", (void*) SkPaintGlue::getAlpha},
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setAlpha","(I)V", (void*) SkPaintGlue::setAlpha},
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getStrokeWidth","()F", (void*) SkPaintGlue::getStrokeWidth},
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setStrokeWidth","(F)V", (void*) SkPaintGlue::setStrokeWidth},
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getStrokeMiter","()F", (void*) SkPaintGlue::getStrokeMiter},
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setStrokeMiter","(F)V", (void*) SkPaintGlue::setStrokeMiter},
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getStrokeCap","(I)I", (void*) SkPaintGlue::getStrokeCap},
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setStrokeCap","(II)V", (void*) SkPaintGlue::setStrokeCap},
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getStrokeJoin","(I)I", (void*) SkPaintGlue::getStrokeJoin},
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setStrokeJoin","(II)V", (void*) SkPaintGlue::setStrokeJoin},
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getFillPath","(III)Z", (void*) SkPaintGlue::getFillPath},
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setShader","(II)I", (void*) SkPaintGlue::setShader},
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setColorFilter","(II)I", (void*) SkPaintGlue::setColorFilter},
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setXfermode","(II)I", (void*) SkPaintGlue::setXfermode},
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setPathEffect","(II)I", (void*) SkPaintGlue::setPathEffect},
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setMaskFilter","(II)I", (void*) SkPaintGlue::setMaskFilter},
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setTypeface","(II)I", (void*) SkPaintGlue::setTypeface},
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setRasterizer","(II)I", (void*) SkPaintGlue::setRasterizer},
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getTextAlign","(I)I", (void*) SkPaintGlue::getTextAlign},
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setTextAlign","(II)V", (void*) SkPaintGlue::setTextAlign},
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getTextSize","()F", (void*) SkPaintGlue::getTextSize},
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setTextSize","(F)V", (void*) SkPaintGlue::setTextSize},
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getTextScaleX","()F", (void*) SkPaintGlue::getTextScaleX},
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setTextScaleX","(F)V", (void*) SkPaintGlue::setTextScaleX},
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getTextSkewX","()F", (void*) SkPaintGlue::getTextSkewX},
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setTextSkewX","(F)V", (void*) SkPaintGlue::setTextSkewX},
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"ascent","()F", (void*) SkPaintGlue::ascent},
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"descent","()F", (void*) SkPaintGlue::descent},
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getFontMetrics", "(Landroid/graphics/Paint$FontMetrics;)F", (void*)SkPaintGlue::getFontMetrics},
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"getFontMetricsInt", "(Landroid/graphics/Paint$FontMetricsInt;)I", (void*)SkPaintGlue::getFontMetricsInt},
848afa78967b8553443aa32579d78970a076d7581f6Dianne Hackborn    {"native_measureText","([CII)F", (void*) SkPaintGlue::measureText_CII},
849afa78967b8553443aa32579d78970a076d7581f6Dianne Hackborn    {"native_measureText","(Ljava/lang/String;)F", (void*) SkPaintGlue::measureText_String},
850afa78967b8553443aa32579d78970a076d7581f6Dianne Hackborn    {"native_measureText","(Ljava/lang/String;II)F", (void*) SkPaintGlue::measureText_StringII},
851afa78967b8553443aa32579d78970a076d7581f6Dianne Hackborn    {"native_breakText","([CIIF[F)I", (void*) SkPaintGlue::breakTextC},
852afa78967b8553443aa32579d78970a076d7581f6Dianne Hackborn    {"native_breakText","(Ljava/lang/String;ZF[F)I", (void*) SkPaintGlue::breakTextS},
8539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getTextWidths","(I[CII[F)I", (void*) SkPaintGlue::getTextWidths___CII_F},
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_getTextWidths","(ILjava/lang/String;II[F)I", (void*) SkPaintGlue::getTextWidths__StringII_F},
8550a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio    {"native_getTextRunAdvances","(I[CIIIII[FII)F",
8560a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio        (void*) SkPaintGlue::getTextRunAdvances___CIIIII_FII},
8570a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio    {"native_getTextRunAdvances","(ILjava/lang/String;IIIII[FII)F",
8580a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio        (void*) SkPaintGlue::getTextRunAdvances__StringIIIII_FII},
8590a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio
8600a1413e4bf9dcda2a8abb2287e43f612a7fb2453Fabrice Di Meglio
8619f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio    {"native_getTextGlyphs","(ILjava/lang/String;IIIII[C)I",
8629f82b580d744ce4baf057b061994394dcf239eedFabrice Di Meglio        (void*) SkPaintGlue::getTextGlyphs__StringIIIII_C},
8630c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    {"native_getTextRunCursor", "(I[CIIIII)I", (void*) SkPaintGlue::getTextRunCursor___C},
8640c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    {"native_getTextRunCursor", "(ILjava/lang/String;IIIII)I",
8650c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        (void*) SkPaintGlue::getTextRunCursor__String},
866f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    {"native_getTextPath","(II[CIIFFI)V", (void*) SkPaintGlue::getTextPath___C},
867f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt    {"native_getTextPath","(IILjava/lang/String;IIFFI)V", (void*) SkPaintGlue::getTextPath__String},
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"nativeGetStringBounds", "(ILjava/lang/String;IILandroid/graphics/Rect;)V",
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        (void*) SkPaintGlue::getStringBounds },
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"nativeGetCharArrayBounds", "(I[CIILandroid/graphics/Rect;)V",
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    (void*) SkPaintGlue::getCharArrayBounds },
8721e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    {"nSetShadowLayer", "(FFFI)V", (void*)SkPaintGlue::setShadowLayer}
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID req_fieldID(jfieldID id) {
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(id);
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return id;
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_graphics_Paint(JNIEnv* env) {
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_class = env->FindClass("android/graphics/Paint$FontMetrics");
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(gFontMetrics_class);
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_class = (jclass)env->NewGlobalRef(gFontMetrics_class);
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_fieldID.top = req_fieldID(env->GetFieldID(gFontMetrics_class, "top", "F"));
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetrics_class, "ascent", "F"));
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetrics_class, "descent", "F"));
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetrics_class, "bottom", "F"));
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetrics_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetrics_class, "leading", "F"));
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_class = env->FindClass("android/graphics/Paint$FontMetricsInt");
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(gFontMetricsInt_class);
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_class = (jclass)env->NewGlobalRef(gFontMetricsInt_class);
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_fieldID.top = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "top", "I"));
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "ascent", "I"));
8979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "descent", "I"));
8989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "bottom", "I"));
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gFontMetricsInt_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "leading", "I"));
9009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Paint", methods,
9029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sizeof(methods) / sizeof(methods[0]));
9039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return result;
9049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
907