Unverified Commit ed2e75d3 authored by alexander-gorshenev's avatar alexander-gorshenev Committed by GitHub

Implemented native TextStyle.SetFontFamilies (#294)

* Implemented native TextStyle.SetFontFamilies

* Added a test for TextStyle.setFontFamilies
Moved the test to commonTest

* Defined Native.equals() for native and js

* cosmetics in native and js Native.equals()

* Make Native._nativeEquals return false on js and native the same way it does no jvm
parent d2c2fa33
......@@ -238,7 +238,7 @@ class TextStyle internal constructor(ptr: NativePointer) : Managed(ptr, _Finaliz
fun setFontFamilies(families: Array<String>?): TextStyle {
Stats.onNativeCall()
interopScope {
_nSetFontFamilies(_ptr, toInterop(families))
_nSetFontFamilies(_ptr, toInterop(families), families?.size ?: 0)
}
return this
}
......@@ -466,7 +466,7 @@ private external fun _nAddFontFeature(ptr: NativePointer, name: String?, value:
private external fun _nClearFontFeatures(ptr: NativePointer)
@ExternalSymbolName("org_jetbrains_skia_paragraph_TextStyle__1nSetFontFamilies")
private external fun _nSetFontFamilies(ptr: NativePointer, families: InteropPointer)
private external fun _nSetFontFamilies(ptr: NativePointer, families: InteropPointer, familiesSize: Int)
@ExternalSymbolName("org_jetbrains_skia_paragraph_TextStyle__1nGetLetterSpacing")
private external fun _nGetLetterSpacing(ptr: NativePointer): Float
......
package org.jetbrains.skiko.paragraph
import org.jetbrains.skia.impl.use
import org.jetbrains.skia.paragraph.TextStyle
import org.jetbrains.skia.paragraph.TextStyleAttribute
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.test.*
class TextStyleTest {
......@@ -25,8 +23,8 @@ class TextStyleTest {
TextStyleAttribute.WORD_SPACING,
TextStyleAttribute.FONT_EXACT
)) {
assertEquals(true, ts1.equals(attr, ts2), "$attr")
assertEquals(true, ts2.equals(attr, ts1), "$attr")
assertTrue(ts1.equals(attr, ts2), "$attr")
assertTrue(ts2.equals(attr, ts1), "$attr")
}
}
}
......@@ -38,5 +36,16 @@ class TextStyleTest {
assertTrue(
TextStyle().setColor(-0x33cd00).equals(TextStyleAttribute.BACKGROUND, TextStyle().setColor(-0xff33cd))
)
TextStyle().use { ts1 ->
TextStyle().use { ts2 ->
ts1.fontFamilies = arrayOf("foo", "qux")
ts2.fontFamilies = arrayOf("foo", "qux")
assertEquals(ts1, ts2)
ts1.fontFamilies = arrayOf("foo", "qux")
ts2.fontFamilies = arrayOf("bar", "zig")
assertNotEquals(ts1, ts2)
}
}
}
}
......@@ -5,7 +5,18 @@ import org.khronos.webgl.ArrayBufferView
actual abstract class Native actual constructor(ptr: NativePointer) {
actual var _ptr: NativePointer
actual open fun _nativeEquals(other: Native?): Boolean = TODO()
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (null == other) return false
if (other !is Native) return false
return if (_ptr == other._ptr) true else _nativeEquals(other)
}
override fun hashCode(): Int = _ptr
actual open fun _nativeEquals(other: Native?): Boolean {
return false
}
actual companion object {
actual val NullPointer: NativePointer
......
......@@ -196,7 +196,7 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skia_paragraph_Text
}
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_paragraph_TextStyleKt__1nSetFontFamilies
(JNIEnv* env, jclass jclass, jlong ptr, jobjectArray familiesArray) {
(JNIEnv* env, jclass jclass, jlong ptr, jobjectArray familiesArray, int familiesArraySize) {
TextStyle* instance = reinterpret_cast<TextStyle*>(static_cast<uintptr_t>(ptr));
instance->setFontFamilies(skStringVector(env, familiesArray));
}
......
......@@ -68,6 +68,7 @@ namespace skija {
std::unique_ptr<SkMatrix> skMatrix(KFloat* matrixArray);
std::unique_ptr<SkM44> skM44(KFloat* matrixArray);
SkString skString(KNativePointer str);
std::vector<SkString> skStringVector(KInteropPointerArray arr, KInt size);
template <typename T>
inline T interopToPtr(KNativePointer ptr) {
......
......@@ -111,6 +111,19 @@ SkString skString(KInteropPointer s) {
}
}
std::vector<SkString> skStringVector(KInteropPointerArray arr, KInt len) {
if (arr == nullptr) {
return std::vector<SkString>(0);
} else {
std::vector<SkString> res(len);
char** strings = reinterpret_cast<char**>(arr);
for (KInt i = 0; i < len; ++i) {
res[i] = skString(strings[i]);
}
return res;
}
}
namespace skija {
namespace RRect {
SkRRect toSkRRect(KFloat left, KFloat top, KFloat right, KFloat bottom, KFloat* radii, KInt radiiSize) {
......
......@@ -241,17 +241,10 @@ SKIKO_EXPORT KInteropPointerArray org_jetbrains_skia_paragraph_TextStyle__1nGetF
SKIKO_EXPORT void org_jetbrains_skia_paragraph_TextStyle__1nSetFontFamilies
(KNativePointer ptr, KInteropPointerArray familiesArray) {
TODO("implement org_jetbrains_skia_paragraph_TextStyle__1nSetFontFamilies");
}
#if 0
SKIKO_EXPORT void org_jetbrains_skia_paragraph_TextStyle__1nSetFontFamilies
(KNativePointer ptr, KInteropPointerArray familiesArray) {
(KNativePointer ptr, KInteropPointerArray familiesArray, KInt familiesArraySize) {
TextStyle* instance = reinterpret_cast<TextStyle*>((ptr));
instance->setFontFamilies(skStringVector(env, familiesArray));
instance->setFontFamilies(skStringVector(familiesArray, familiesArraySize));
}
#endif
......
......@@ -8,7 +8,20 @@ import kotlin.native.internal.NativePtr
actual abstract class Native actual constructor(ptr: NativePointer) {
actual var _ptr: NativePointer
actual open fun _nativeEquals(other: Native?): Boolean = this._ptr == other?._ptr ?: Native.NullPointer
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (null == other) return false
if (other !is Native) return false
return if (_ptr == other._ptr) true else _nativeEquals(other)
}
override fun hashCode(): Int {
return _ptr.toLong().hashCode()
}
actual open fun _nativeEquals(other: Native?): Boolean {
return false
}
actual companion object {
actual val NullPointer: NativePointer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment