Skip to content

Commit

Permalink
allow to change font rendering settings in paragraph (#597)
Browse files Browse the repository at this point in the history
* allow to change font rendering settings in paragraph

* add test

* rename

* fix js/native signatures

* workaround for kotlinjs
  • Loading branch information
SergeevPavel authored Sep 23, 2022
1 parent bacfb30 commit 851d07d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 12 deletions.
24 changes: 12 additions & 12 deletions skiko/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ deploy.version=0.0.0
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false

dependencies.skia.windows-x64=m105-f204b137b9-5
dependencies.skia.linux-x64=m105-f204b137b9-5
dependencies.skia.macos-x64=m105-f204b137b9-5
dependencies.skia.linux-arm64=m105-f204b137b9-5
dependencies.skia.macos-arm64=m105-f204b137b9-5
dependencies.skia.wasm-wasm=m105-f204b137b9-5
dependencies.skia.ios-x64=m105-f204b137b9-5
dependencies.skia.ios-arm64=m105-f204b137b9-5
dependencies.skia.iosSim-arm64=m105-f204b137b9-5
dependencies.skia.iosSim-x64=m105-f204b137b9-5
dependencies.skia.android-x64=m105-f204b137b9-5
dependencies.skia.android-arm64=m105-f204b137b9-5
dependencies.skia.windows-x64=m105-305b7c02
dependencies.skia.linux-x64=m105-305b7c02
dependencies.skia.macos-x64=m105-305b7c02
dependencies.skia.linux-arm64=m105-305b7c02
dependencies.skia.macos-arm64=m105-305b7c02
dependencies.skia.wasm-wasm=m105-305b7c02
dependencies.skia.ios-x64=m105-305b7c02
dependencies.skia.ios-arm64=m105-305b7c02
dependencies.skia.iosSim-arm64=m105-305b7c02
dependencies.skia.iosSim-x64=m105-305b7c02
dependencies.skia.android-x64=m105-305b7c02
dependencies.skia.android-arm64=m105-305b7c02

org.gradle.jvmargs=-Xmx3G -XX:MaxMetaspaceSize=512m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jetbrains.skia.paragraph

import org.jetbrains.skia.FontEdging
import org.jetbrains.skia.FontHinting

data class FontRastrSettings(val edging: FontEdging,
val hinting: FontHinting,
val subpixel: Boolean)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.jetbrains.skia.paragraph

import org.jetbrains.skia.impl.Library.Companion.staticLoad
import org.jetbrains.skia.ExternalSymbolName
import org.jetbrains.skia.FontEdging
import org.jetbrains.skia.FontHinting
import org.jetbrains.skia.impl.*
import org.jetbrains.skia.impl.getPtr
import org.jetbrains.skia.impl.interopScope
Expand Down Expand Up @@ -167,6 +169,28 @@ class ParagraphStyle : Managed(ParagraphStyle_nMake(), _FinalizerHolder.PTR) {
return this
}

var fontRastrSettings: FontRastrSettings
get() = try {
Stats.onNativeCall()
val edging = FontEdging.values()[_nGetEdging(_ptr)]
Stats.onNativeCall()
val hinting = FontHinting.values()[_nGetHinting(_ptr)]
Stats.onNativeCall()
// by some obscure reason kotlinjs makes difference between number encoded booleans returned from `_nGetSubpixel` and regular booleans
// AssertionError: Expected <FontRastrSettings(edging=ALIAS, hinting=NONE, subpixel=false)>, actual <FontRastrSettings(edging=ALIAS, hinting=NONE, subpixel=0)>
val subpixel = _nGetSubpixel(_ptr).not().not()
FontRastrSettings(edging, hinting, subpixel)
} finally {
reachabilityBarrier(this)
}
set(value) = try {
Stats.onNativeCall()
_nSetFontRastrSettings(_ptr, value.edging.ordinal, value.hinting.ordinal, value.subpixel)
} finally {
reachabilityBarrier(this)
}


var textIndent: TextIndent
get() = try {
Stats.onNativeCall()
Expand Down Expand Up @@ -259,6 +283,18 @@ private external fun _nIsHintingEnabled(ptr: NativePointer): Boolean
@ExternalSymbolName("org_jetbrains_skia_paragraph_ParagraphStyle__1nDisableHinting")
private external fun _nDisableHinting(ptr: NativePointer)

@ExternalSymbolName("org_jetbrains_skia_paragraph_ParagraphStyle__1nSetFontRastrSettings")
private external fun _nSetFontRastrSettings(ptr: NativePointer, edging: Int, hinting: Int, subpixel: Boolean)

@ExternalSymbolName("org_jetbrains_skia_paragraph_ParagraphStyle__1nGetEdging")
private external fun _nGetEdging(ptr: NativePointer): Int

@ExternalSymbolName("org_jetbrains_skia_paragraph_ParagraphStyle__1nGetHinting")
private external fun _nGetHinting(ptr: NativePointer): Int

@ExternalSymbolName("org_jetbrains_skia_paragraph_ParagraphStyle__1nGetSubpixel")
private external fun _nGetSubpixel(ptr: NativePointer): Boolean

@ExternalSymbolName("org_jetbrains_skia_paragraph_ParagraphStyle__1nSetTextIndent")
private external fun _nSetTextIndent(ptr: NativePointer, firstLine: Float, restLine: Float)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jetbrains.skiko.paragraph

import org.jetbrains.skia.FontEdging
import org.jetbrains.skia.FontHinting
import org.jetbrains.skia.impl.use
import org.jetbrains.skia.paragraph.FontRastrSettings
import org.jetbrains.skia.paragraph.ParagraphStyle
import org.jetbrains.skia.paragraph.TextIndent
import kotlin.test.Test
Expand Down Expand Up @@ -32,4 +35,24 @@ class ParagraphStyleTests {
assertEquals(indent2, paragraphStyle.textIndent)
}
}

@Test
fun paragraphStyleFontRastrSettingsTests() {
ParagraphStyle().use { paragraphStyle ->
val poorRasterSettings = FontRastrSettings(
edging = FontEdging.ALIAS,
hinting = FontHinting.NONE,
subpixel = false)
paragraphStyle.fontRastrSettings = poorRasterSettings
assertEquals(poorRasterSettings, paragraphStyle.fontRastrSettings)

val gloriousRasterSettings = FontRastrSettings(
edging = FontEdging.SUBPIXEL_ANTI_ALIAS,
hinting = FontHinting.FULL,
subpixel = true)

paragraphStyle.fontRastrSettings = gloriousRasterSettings
assertEquals(gloriousRasterSettings, paragraphStyle.fontRastrSettings)
}
}
}
31 changes: 31 additions & 0 deletions skiko/src/jvmMain/cpp/common/paragraph/ParagraphStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@ extern "C" JNIEXPORT jboolean JNICALL Java_org_jetbrains_skia_paragraph_Paragrap
return instance->hintingIsOn();
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_ParagraphStyleKt__1nSetFontRastrSettings
(JNIEnv* env, jclass jclass, jlong ptr, jint edging, jint hinting, jboolean subpixel) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(static_cast<uintptr_t>(ptr));
FontRastrSettings fontRastrSettings;
fontRastrSettings.fEdging = static_cast<SkFont::Edging>(edging);
fontRastrSettings.fHinting = static_cast<SkFontHinting>(hinting);
fontRastrSettings.fSubpixel = subpixel;
instance->setFontRastrSettings(fontRastrSettings);
}

extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_paragraph_ParagraphStyleKt__1nGetEdging
(JNIEnv* env, jclass jclass, jlong ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(static_cast<uintptr_t>(ptr));
FontRastrSettings fontRastrSettings = instance->getFontRastrSettings();
return static_cast<jint>(fontRastrSettings.fEdging);
}

extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_paragraph_ParagraphStyleKt__1nGetHinting
(JNIEnv* env, jclass jclass, jlong ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(static_cast<uintptr_t>(ptr));
FontRastrSettings fontRastrSettings = instance->getFontRastrSettings();
return static_cast<jint>(fontRastrSettings.fHinting);
}

extern "C" JNIEXPORT jboolean JNICALL Java_org_jetbrains_skia_paragraph_ParagraphStyleKt__1nGetSubpixel
(JNIEnv* env, jclass jclass, jlong ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(static_cast<uintptr_t>(ptr));
FontRastrSettings fontRastrSettings = instance->getFontRastrSettings();
return fontRastrSettings.fSubpixel;
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_ParagraphStyleKt__1nDisableHinting
(JNIEnv* env, jclass jclass, jlong ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(static_cast<uintptr_t>(ptr));
Expand Down
31 changes: 31 additions & 0 deletions skiko/src/nativeJsMain/cpp/paragraph/ParagraphStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ SKIKO_EXPORT void org_jetbrains_skia_paragraph_ParagraphStyle__1nDisableHinting
instance->turnHintingOff();
}

SKIKO_EXPORT void org_jetbrains_skia_paragraph_ParagraphStyle__1nSetFontRastrSettings
(KNativePointer ptr, KInt edging, KInt hinting, KBoolean subpixel) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(ptr);
FontRastrSettings fontRastrSettings;
fontRastrSettings.fEdging = static_cast<SkFont::Edging>(edging);
fontRastrSettings.fHinting = static_cast<SkFontHinting>(hinting);
fontRastrSettings.fSubpixel = subpixel;
instance->setFontRastrSettings(fontRastrSettings);
}

SKIKO_EXPORT KInt org_jetbrains_skia_paragraph_ParagraphStyle__1nGetEdging
(KNativePointer ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(ptr);
FontRastrSettings fontRastrSettings = instance->getFontRastrSettings();
return static_cast<KInt>(fontRastrSettings.fEdging);
}

SKIKO_EXPORT KInt org_jetbrains_skia_paragraph_ParagraphStyle__1nGetHinting
(KNativePointer ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(ptr);
FontRastrSettings fontRastrSettings = instance->getFontRastrSettings();
return static_cast<KInt>(fontRastrSettings.fHinting);
}

SKIKO_EXPORT KBoolean org_jetbrains_skia_paragraph_ParagraphStyle__1nGetSubpixel
(KNativePointer ptr) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>(ptr);
FontRastrSettings fontRastrSettings = instance->getFontRastrSettings();
return fontRastrSettings.fSubpixel;
}

SKIKO_EXPORT void org_jetbrains_skia_paragraph_ParagraphStyle__1nSetTextIndent
(KNativePointer ptr, KFloat firstLine, KFloat restLine) {
ParagraphStyle* instance = reinterpret_cast<ParagraphStyle*>((ptr));
Expand Down

0 comments on commit 851d07d

Please sign in to comment.