/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #undef LOG_TAG #define LOG_TAG "LineBreaker" #include "utils/misc.h" #include "utils/Log.h" #include "graphics_jni_helpers.h" #include #include #include "scoped_nullable_primitive_array.h" #include #include #include #include #include "SkPaint.h" #include "SkTypeface.h" #include #include #include #include #include #include namespace android { static inline std::vector jintArrayToFloatVector(JNIEnv* env, jintArray javaArray) { if (javaArray == nullptr) { return std::vector(); } else { ScopedIntArrayRO intArr(env, javaArray); return std::vector(intArr.get(), intArr.get() + intArr.size()); } } static inline minikin::android::StaticLayoutNative* toNative(jlong ptr) { return reinterpret_cast(ptr); } // set text and set a number of parameters for creating a layout (width, tabstops, strategy, // hyphenFrequency) static jlong nInit(JNIEnv* env, jclass /* unused */, jint breakStrategy, jint hyphenationFrequency, jboolean isJustified, jintArray indents) { return reinterpret_cast(new minikin::android::StaticLayoutNative( static_cast(breakStrategy), static_cast(hyphenationFrequency), isJustified, jintArrayToFloatVector(env, indents))); } static void nFinish(jlong nativePtr) { delete toNative(nativePtr); } // CriticalNative static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) { return reinterpret_cast(nFinish); } static jlong nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr, // Inputs jcharArray javaText, jlong measuredTextPtr, jint length, jfloat firstWidth, jint firstWidthLineCount, jfloat restWidth, jfloatArray variableTabStops, jfloat defaultTabStop, jint indentsOffset) { minikin::android::StaticLayoutNative* builder = toNative(nativePtr); ScopedCharArrayRO text(env, javaText); ScopedNullableFloatArrayRO tabStops(env, variableTabStops); minikin::U16StringPiece u16Text(text.get(), length); minikin::MeasuredText* measuredText = reinterpret_cast(measuredTextPtr); std::unique_ptr result = std::make_unique(builder->computeBreaks( u16Text, *measuredText, firstWidth, firstWidthLineCount, restWidth, indentsOffset, tabStops.get(), tabStops.size(), defaultTabStop)); return reinterpret_cast(result.release()); } static jint nGetLineCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) { return reinterpret_cast(ptr)->breakPoints.size(); } static jint nGetLineBreakOffset(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { return reinterpret_cast(ptr)->breakPoints[i]; } static jfloat nGetLineWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { return reinterpret_cast(ptr)->widths[i]; } static jfloat nGetLineAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { return reinterpret_cast(ptr)->ascents[i]; } static jfloat nGetLineDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { return reinterpret_cast(ptr)->descents[i]; } static jint nGetLineFlag(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) { return reinterpret_cast(ptr)->flags[i]; } static void nReleaseResult(jlong ptr) { delete reinterpret_cast(ptr); } static jlong nGetReleaseResultFunc(CRITICAL_JNI_PARAMS) { return reinterpret_cast(nReleaseResult); } static const JNINativeMethod gMethods[] = { // Fast Natives {"nInit", "(" "I" // breakStrategy "I" // hyphenationFrequency "Z" // isJustified "[I" // indents ")J", (void*) nInit}, // Critical Natives {"nGetReleaseFunc", "()J", (void*) nGetReleaseFunc}, // Regular JNI {"nComputeLineBreaks", "(" "J" // nativePtr "[C" // text "J" // MeasuredParagraph ptr. "I" // length "F" // firstWidth "I" // firstWidthLineCount "F" // restWidth "[F" // variableTabStops "F" // defaultTabStop "I" // indentsOffset ")J", (void*) nComputeLineBreaks}, // Result accessors, CriticalNatives {"nGetLineCount", "(J)I", (void*)nGetLineCount}, {"nGetLineBreakOffset", "(JI)I", (void*)nGetLineBreakOffset}, {"nGetLineWidth", "(JI)F", (void*)nGetLineWidth}, {"nGetLineAscent", "(JI)F", (void*)nGetLineAscent}, {"nGetLineDescent", "(JI)F", (void*)nGetLineDescent}, {"nGetLineFlag", "(JI)I", (void*)nGetLineFlag}, {"nGetReleaseResultFunc", "()J", (void*)nGetReleaseResultFunc}, }; int register_android_graphics_text_LineBreaker(JNIEnv* env) { return RegisterMethodsOrDie(env, "android/graphics/text/LineBreaker", gMethods, NELEM(gMethods)); } }