-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose ICUs u_hasBinaryProperty #1029
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
skiko/src/commonMain/kotlin/org/jetbrains/skia/CodePoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jetbrains.skia | ||
|
||
// TODO Remove once it's available in common stdlib https://youtrack.jetbrains.com/issue/KT-23251 | ||
internal typealias CodePoint = Int | ||
|
||
/** | ||
* The minimum value of a supplementary code point, `\u0x10000`. | ||
*/ | ||
private const val MIN_SUPPLEMENTARY_CODE_POINT: Int = 0x10000 | ||
|
||
/** | ||
* Converts a surrogate pair to a unicode code point. | ||
*/ | ||
internal fun toCodePoint(high: Char, low: Char): CodePoint = | ||
(((high - Char.MIN_HIGH_SURROGATE) shl 10) or (low - Char.MIN_LOW_SURROGATE)) + MIN_SUPPLEMENTARY_CODE_POINT | ||
|
||
internal fun Int.charCount(): Int = if (this >= MIN_SUPPLEMENTARY_CODE_POINT) 2 else 1 | ||
|
||
internal val CharSequence.codePoints | ||
get() = codePointsAt(0) | ||
|
||
internal fun CharSequence.codePointsAt(index: Int) = sequence { | ||
var current = index | ||
while (current < length) { | ||
val codePoint = codePointAt(current) | ||
yield(codePoint) | ||
current += codePoint.charCount() | ||
} | ||
} | ||
|
||
internal val CharSequence.codePointsAsIntArray: IntArray | ||
get() = codePoints.toList().toIntArray() | ||
|
||
/** | ||
* Returns the character (Unicode code point) at the specified index. | ||
*/ | ||
internal fun CharSequence.codePointAt(index: Int): CodePoint { | ||
val high = this[index] | ||
if (high.isHighSurrogate() && index + 1 < this.length) { | ||
val low = this[index + 1] | ||
if (low.isLowSurrogate()) { | ||
return toCodePoint(high, low) | ||
} | ||
} | ||
return high.code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 19 additions & 1 deletion
20
skiko/src/commonTest/kotlin/org/jetbrains/skia/icu/UnicodeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
package org.jetbrains.skia.icu | ||
|
||
import org.jetbrains.skia.codePoints | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class UnicodeTest { | ||
|
||
@Test | ||
fun directionality() { | ||
assertEquals(CharDirection.EUROPEAN_NUMBER, CharDirection.of('0'.code)) // Number | ||
assertEquals(CharDirection.LEFT_TO_RIGHT, CharDirection.of('A'.code)) // Latin | ||
assertEquals(CharDirection.RIGHT_TO_LEFT, CharDirection.of('א'.code)) // Hebrew | ||
assertEquals(CharDirection.RIGHT_TO_LEFT_ARABIC, CharDirection.of('؈'.code)) // Arabic | ||
} | ||
|
||
@Test | ||
fun binaryProperties() { | ||
fun String.firstCodePointHasProperty(property: Int): Boolean { | ||
val codePoint = this.codePoints.first() | ||
println(codePoint.toString(16)) | ||
return CharProperties.codePointHasBinaryProperty(codePoint, property) | ||
} | ||
|
||
assertTrue("⌚".firstCodePointHasProperty(CharProperties.EMOJI)) | ||
assertTrue("✅".firstCodePointHasProperty(CharProperties.EMOJI_PRESENTATION)) | ||
assertTrue("♥️".firstCodePointHasProperty(CharProperties.EXTENDED_PICTOGRAPHIC)) | ||
assertTrue("🇮🇱".firstCodePointHasProperty(CharProperties.EMOJI)) // flag | ||
|
||
assertFalse("x".firstCodePointHasProperty(CharProperties.EMOJI)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
#include <jni.h> | ||
#include "third_party/externals/icu/source/common/unicode/uchar.h" | ||
|
||
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_icu_UnicodeKt_charDirection | ||
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_icu_UnicodeKt__1nCharDirection | ||
(JNIEnv* env, jclass jclass, jint codePoint) { | ||
return u_charDirection(codePoint); | ||
} | ||
|
||
extern "C" JNIEXPORT jboolean JNICALL Java_org_jetbrains_skia_icu_UnicodeKt__1nCodePointHasBinaryProperty | ||
(JNIEnv* env, jclass jclass, jint codePoint, jint property) { | ||
return u_hasBinaryProperty(codePoint, (UProperty)property); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
#include "common.h" | ||
#include "third_party/externals/icu/source/common/unicode/uchar.h" | ||
|
||
SKIKO_EXPORT KInt org_jetbrains_skia_icu_Unicode_charDirection(KInt codePoint) { | ||
SKIKO_EXPORT KInt org_jetbrains_skia_icu_Unicode__1nCharDirection(KInt codePoint) { | ||
return u_charDirection(codePoint); | ||
} | ||
|
||
SKIKO_EXPORT KBoolean org_jetbrains_skia_icu_Unicode__1nCodePointHasBinaryProperty(KInt codePoint, KInt property) { | ||
return u_hasBinaryProperty(codePoint, (UProperty)property); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing KDoc for public API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.