Skip to content

Commit

Permalink
Font.measureTextWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Oct 23, 2020
1 parent bec6cb7 commit db62ccd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/cc/Font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_Font__1nMeasureTex
return skija::Rect::fromSkRect(env, bounds);
}

extern "C" JNIEXPORT jfloat JNICALL Java_org_jetbrains_skija_Font__1nMeasureTextWidth
(JNIEnv* env, jclass jclass, jlong ptr, jstring str, jlong paintPtr) {
SkFont* instance = reinterpret_cast<SkFont*>(static_cast<uintptr_t>(ptr));
SkPaint* paint = reinterpret_cast<SkPaint*>(static_cast<uintptr_t>(paintPtr));
jsize len = env->GetStringLength(str);
const jchar* chars = env->GetStringChars(str, nullptr);
float width = instance->measureText(chars, len * sizeof(jchar), SkTextEncoding::kUTF16, nullptr, paint);
env->ReleaseStringChars(str, chars);
return width;
}

extern "C" JNIEXPORT jfloatArray JNICALL Java_org_jetbrains_skija_Font__1nGetWidths
(JNIEnv* env, jclass jclass, jlong ptr, jshortArray glyphsArr) {
SkFont* instance = reinterpret_cast<SkFont*>(static_cast<uintptr_t>(ptr));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/jetbrains/skija/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ public Rect measureText(String s, Paint p) {
return _nMeasureText(_ptr, s, Native.getPtr(p));
}

public float measureTextWidth(String s) {
Stats.onNativeCall();
return measureTextWidth(s, null);
}

public float measureTextWidth(String s, Paint p) {
Stats.onNativeCall();
return _nMeasureTextWidth(_ptr, s, Native.getPtr(p));
}

/**
* Retrieves the advances for each glyph
*/
Expand Down Expand Up @@ -504,6 +514,7 @@ public TextBlob shape(String str, float width) {
@ApiStatus.Internal public static native short[] _nGetUTF32GlyphIds(long ptr, int[] uni);
@ApiStatus.Internal public static native int _nGetStringGlyphsCount(long ptr, String str);
@ApiStatus.Internal public static native Rect _nMeasureText(long ptr, String str, long paintPtr);
@ApiStatus.Internal public static native float _nMeasureTextWidth(long ptr, String str, long paintPtr);
@ApiStatus.Internal public static native float[] _nGetWidths(long ptr, short[] glyphs);
@ApiStatus.Internal public static native Rect[] _nGetBounds(long ptr, short[] glyphs, long paintPtr);
@ApiStatus.Internal public static native Point[] _nGetPositions(long ptr, short[] glyphs, float x, float y);
Expand Down

0 comments on commit db62ccd

Please sign in to comment.