Skip to content

Commit

Permalink
Replace half leading to top ratio (#989)
Browse files Browse the repository at this point in the history
Support configurable vertical centering for implementing Compose's
`LineHeightStyle.Alignment`.

https://youtrack.jetbrains.com/issue/CMP-2602

See also:
- JetBrains/skia#5
- JetBrains/compose-multiplatform-core#1569
  • Loading branch information
MatkovIvan authored Sep 18, 2024
1 parent 1ca83c1 commit 355ede9
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion skiko/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kotlin.code.style=official
deploy.version=0.0.0


dependencies.skia=m126-1d69d9b-2
dependencies.skia=m126-d2aaacc35d-1

# you can override general skia dependencies by passing platform-specific property:
# dependencies.skia.android-arm64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class StrutStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finali
return this
}

@Deprecated("Replaced by topRatio")
var isHalfLeading: Boolean
get() = try {
Stats.onNativeCall()
Expand All @@ -222,6 +223,8 @@ class StrutStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finali
setHalfLeading(value)
}

// Same as topRatio = halfLeading ? 0.5f : -1.0f
@Deprecated("Replaced by topRatio")
fun setHalfLeading(value: Boolean): StrutStyle {
try {
Stats.onNativeCall()
Expand All @@ -232,6 +235,29 @@ class StrutStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finali
return this
}

// [0..1]: the ratio of ascent to ascent+descent
// -1: proportional to the ascent/descent
var topRatio: Float
get() = try {
Stats.onNativeCall()
StrutStyle_nGetTopRatio(_ptr)
} finally {
reachabilityBarrier(this)
}
set(value) {
setTopRatio(value)
}

fun setTopRatio(topRatio: Float): StrutStyle {
try {
Stats.onNativeCall()
StrutStyle_nSetTopRatio(_ptr, topRatio)
} finally {
reachabilityBarrier(this)
}
return this
}

private object _FinalizerHolder {
val PTR = StrutStyle_nGetFinalizer()
}
Expand Down Expand Up @@ -321,3 +347,11 @@ private external fun _nIsHalfLeading(ptr: NativePointer): Boolean
@ExternalSymbolName("org_jetbrains_skia_paragraph_StrutStyle__1nSetHalfLeading")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_StrutStyle__1nSetHalfLeading")
private external fun _nSetHalfLeading(ptr: NativePointer, value: Boolean)

@ExternalSymbolName("org_jetbrains_skia_paragraph_StrutStyle__1nGetTopRatio")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_StrutStyle__1nGetTopRatio")
private external fun StrutStyle_nGetTopRatio(ptr: NativePointer): Float

@ExternalSymbolName("org_jetbrains_skia_paragraph_StrutStyle__1nSetTopRatio")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_StrutStyle__1nSetTopRatio")
private external fun StrutStyle_nSetTopRatio(ptr: NativePointer, value: Float)
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class TextStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finaliz
return this
}

@Deprecated("Replaced by topRatio")
var isHalfLeading: Boolean
get() = try {
Stats.onNativeCall()
Expand All @@ -334,6 +335,8 @@ class TextStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finaliz
setHalfLeading(value)
}

// Same as topRatio = halfLeading ? 0.5f : -1.0f
@Deprecated("Replaced by topRatio")
fun setHalfLeading(value: Boolean): TextStyle {
try {
Stats.onNativeCall()
Expand All @@ -344,6 +347,29 @@ class TextStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finaliz
return this
}

// [0..1]: the ratio of ascent to ascent+descent
// -1: proportional to the ascent/descent
var topRatio: Float
get() = try {
Stats.onNativeCall()
TextStyle_nGetTopRatio(_ptr)
} finally {
reachabilityBarrier(this)
}
set(value) {
setTopRatio(value)
}

fun setTopRatio(topRatio: Float): TextStyle {
try {
Stats.onNativeCall()
TextStyle_nSetTopRatio(_ptr, topRatio)
} finally {
reachabilityBarrier(this)
}
return this
}

var letterSpacing: Float
get() = try {
Stats.onNativeCall()
Expand Down Expand Up @@ -560,6 +586,14 @@ private external fun TextStyle_nGetHalfLeading(ptr: NativePointer): Boolean
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_TextStyle__1nSetHalfLeading")
private external fun TextStyle_nSetHalfLeading(ptr: NativePointer, value: Boolean)

@ExternalSymbolName("org_jetbrains_skia_paragraph_TextStyle__1nGetTopRatio")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_TextStyle__1nGetTopRatio")
private external fun TextStyle_nGetTopRatio(ptr: NativePointer): Float

@ExternalSymbolName("org_jetbrains_skia_paragraph_TextStyle__1nSetTopRatio")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_TextStyle__1nSetTopRatio")
private external fun TextStyle_nSetTopRatio(ptr: NativePointer, value: Float)

@ExternalSymbolName("org_jetbrains_skia_paragraph_TextStyle__1nGetBaselineShift")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_paragraph_TextStyle__1nGetBaselineShift")
private external fun TextStyle_nGetBaselineShift(ptr: NativePointer): Float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ class TextStyleTest {
}
}

@Test
fun textStyleTopRatioTest() {
TextStyle().use { textStyle ->
assertEquals(-1f, textStyle.topRatio)
textStyle.topRatio = 0.42f
assertEquals(0.42f, textStyle.topRatio, 0.001f)
}
}

@Test
fun textStyleMetricsContainsMeaningfulValues() = runTest {
val jbMono = Typeface.makeFromResource(jbMonoPath)
Expand Down
12 changes: 12 additions & 0 deletions skiko/src/jvmMain/cpp/common/paragraph/StrutStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_StrutStyleKt
StrutStyle* instance = reinterpret_cast<StrutStyle*>(static_cast<uintptr_t>(ptr));
instance->setHalfLeading(value);
}

extern "C" JNIEXPORT jfloat JNICALL Java_org_jetbrains_skia_paragraph_StrutStyleKt__1nGetTopRatio
(JNIEnv* env, jclass jclass, jlong ptr) {
StrutStyle* instance = reinterpret_cast<StrutStyle*>(static_cast<uintptr_t>(ptr));
return instance->getTopRatio();
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_StrutStyleKt__1nSetTopRatio
(JNIEnv* env, jclass jclass, jlong ptr, jfloat value) {
StrutStyle* instance = reinterpret_cast<StrutStyle*>(static_cast<uintptr_t>(ptr));
instance->setTopRatio(value);
}
12 changes: 12 additions & 0 deletions skiko/src/jvmMain/cpp/common/paragraph/TextStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_TextStyleKt_
instance->setHalfLeading(value);
}

extern "C" JNIEXPORT jfloat JNICALL Java_org_jetbrains_skia_paragraph_TextStyleKt_TextStyle_1nGetTopRatio
(JNIEnv* env, jclass jclass, jlong ptr) {
TextStyle* instance = reinterpret_cast<TextStyle*>(static_cast<uintptr_t>(ptr));
return instance->getTopRatio();
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_TextStyleKt_TextStyle_1nSetTopRatio
(JNIEnv* env, jclass jclass, jlong ptr, jfloat value) {
TextStyle* instance = reinterpret_cast<TextStyle*>(static_cast<uintptr_t>(ptr));
instance->setTopRatio(value);
}

extern "C" JNIEXPORT jfloat JNICALL Java_org_jetbrains_skia_paragraph_TextStyleKt_TextStyle_1nGetBaselineShift
(JNIEnv* env, jclass jclass, jlong ptr) {
TextStyle* instance = reinterpret_cast<TextStyle*>(static_cast<uintptr_t>(ptr));
Expand Down
12 changes: 12 additions & 0 deletions skiko/src/nativeJsMain/cpp/paragraph/StrutStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ SKIKO_EXPORT void org_jetbrains_skia_paragraph_StrutStyle__1nSetHalfLeading
StrutStyle* instance = reinterpret_cast<StrutStyle*>(ptr);
instance->setHalfLeading(value);
}

SKIKO_EXPORT KFloat org_jetbrains_skia_paragraph_StrutStyle__1nGetTopRatio
(KNativePointer ptr) {
StrutStyle* instance = reinterpret_cast<StrutStyle*>(ptr);
return instance->getTopRatio();
}

SKIKO_EXPORT void org_jetbrains_skia_paragraph_StrutStyle__1nSetTopRatio
(KNativePointer ptr, KFloat topRatio) {
StrutStyle* instance = reinterpret_cast<StrutStyle*>(ptr);
instance->setTopRatio(topRatio);
}
12 changes: 12 additions & 0 deletions skiko/src/nativeJsMain/cpp/paragraph/TextStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ SKIKO_EXPORT void org_jetbrains_skia_paragraph_TextStyle__1nSetHalfLeading
instance->setHalfLeading(halfLeading);
}

SKIKO_EXPORT KFloat org_jetbrains_skia_paragraph_TextStyle__1nGetTopRatio
(KNativePointer ptr) {
TextStyle* instance = reinterpret_cast<TextStyle*>(ptr);
return instance->getTopRatio();
}

SKIKO_EXPORT void org_jetbrains_skia_paragraph_TextStyle__1nSetTopRatio
(KNativePointer ptr, KFloat topRatio) {
TextStyle* instance = reinterpret_cast<TextStyle*>(ptr);
instance->setTopRatio(topRatio);
}

SKIKO_EXPORT KFloat org_jetbrains_skia_paragraph_TextStyle__1nGetBaselineShift
(KNativePointer ptr) {
TextStyle* instance = reinterpret_cast<TextStyle*>(ptr);
Expand Down

0 comments on commit 355ede9

Please sign in to comment.