Skip to content

Commit

Permalink
build: 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Jun 11, 2022
1 parent 989953b commit e015620
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 35 deletions.
63 changes: 60 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Be based on <https://github.com/JetBrains/skiko>

## Dependency

```
作为 Mirai Console 前置插件:
配置文件 `build.gradle.kts`
```kotlin
repositories {
mavenCentral()
}
Expand All @@ -27,10 +29,65 @@ dependencies {
compileOnly("xyz.cssxsh.mirai:mirai-skia-plugin:${version}")
}
```
定义 `dependsOn`
```kotlin
object MemeHelperPlugin : KotlinPlugin(
JvmPluginDescription(
id = "xyz.cssxsh.mirai.plugin.meme-helper",
name = "meme-helper",
version = "1.0.2",
) {
author("cssxsh")
dependsOn("xyz.cssxsh.mirai.plugin.mirai-skia-plugin", ">= 1.1.0", false)
}
)
```

作为 Mirai Core Jvm 引用:
配置文件 `build.gradle.kts`
```kotlin
repositories {
mavenCentral()
}

dependencies {
implementation("xyz.cssxsh.mirai:mirai-skia-plugin:${version}")
}
```
手动调用库加载函数
```kotlin
import xyz.cssxsh.mirai.skia.*

checkPlatform()
loadJNILibrary()
```

## GIF

由于 Skiko 没有携带 GIF 编码器,
这里提供两个实现
* [kotlin](src/main/kotlin/xyz/cssxsh/skia/gif)
* [rust](src/main/kotlin/xyz/cssxsh/gif) (Base on [JNI](https://github.com/cssxsh/gif-jni))
* [kotlin](src/main/kotlin/xyz/cssxsh/skia/gif) - [petpet](src/main/kotlin/xyz/cssxsh/skia/Example.kt)
* [rust](src/main/kotlin/xyz/cssxsh/gif) (Base on [JNI](https://github.com/cssxsh/gif-jni)) - [dear](src/main/kotlin/xyz/cssxsh/skia/Example.kt)

## 安装

### MCL 指令安装

`./mcl --update-package xyz.cssxsh.mirai:mirai-skia-plugin --channel stable --type plugin`

### 手动安装

1. 运行 [Mirai Console](https://github.com/mamoe/mirai-console) 生成`plugins`文件夹
2.[Releases](https://github.com/cssxsh/mirai-skia-plugin/releases) 下载`jar`并将其放入`plugins`文件夹中

## 兼容性

| OS/Arch | Skia | Gif |
|:----------------------:|:------:|:-------:|
| Windows-10-X64 | 0.7.20 | 2.0.0 |
| Windows-7-X64 | 0.7.12 | unknown |
| Linux-X64 | 0.7.20 | 2.0.0 |
| Linux-Arm64 | 0.7.20 | 2.0.0 |
| MacOS-X64 | 0.7.20 | 2.0.0 |
| MacOS-Arm64 | 0.7.20 | 2.0.0 |
| Termux (Android-Arm64) | 0.7.18 | 2.0.0 |
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "xyz.cssxsh.mirai"
version = "1.1.0"
version = "1.1.1"

repositories {
mavenLocal()
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/xyz/cssxsh/mirai/skia/MiraiSkiaDowloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ private val GIF_VERSION: String by lazy {

private const val ICU = "icudtl.dat"

public fun checkPlatform() {
// Termux
if (hostOs == OS.Linux && "termux" in System.getProperty("user.dir")) {
logger.info { "change platform: $hostId to android-arm64" }
try {
val kt = Class.forName("org.jetbrains.skiko.OsArch_jvmKt")
val delegate = kt.getDeclaredField("hostId\$delegate").apply { isAccessible = true }
val lazy = delegate.get(null)
val value = lazy::class.java.getDeclaredField("_value").apply { isAccessible = true }
value.set(lazy, "android-arm64")
} catch (_: Throwable) {
logger.warning { "修改 hostId 失败" }
}
}
}

public suspend fun loadJNILibrary(folder: File): Unit = withContext(Dispatchers.IO) {
@Suppress("INVISIBLE_MEMBER")
System.setProperty(Library.SKIKO_LIBRARY_PATH_PROPERTY, folder.path)
Expand Down
18 changes: 2 additions & 16 deletions src/main/kotlin/xyz/cssxsh/mirai/skia/MiraiSkiaPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,13 @@ public object MiraiSkiaPlugin : KotlinPlugin(
JvmPluginDescription(
id = "xyz.cssxsh.mirai.plugin.mirai-skia-plugin",
name = "mirai-skia-plugin",
version = "1.1.0",
version = "1.1.1",
) {
author("cssxsh")
}
) {

override fun PluginComponentStorage.onLoad() {
// Termux
if (hostOs == OS.Linux && "termux" in System.getProperty("user.dir")) {
logger.info { "change platform: $hostId to Android" }
try {
val kt = Class.forName("org.jetbrains.skiko.OsArch_jvmKt")
val delegate = kt.getDeclaredField("hostId\$delegate").apply { isAccessible = true }
val lazy = delegate.get(null)
val value = lazy::class.java.getDeclaredField("_value").apply { isAccessible = true }
value.set(lazy, "android-arm64")
} catch (_: Throwable) {
logger.warning { "修改 hostId 失败" }
}
}
}
override fun PluginComponentStorage.onLoad() { checkPlatform() }

public lateinit var loadJob: Job
private set
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/xyz/cssxsh/mirai/skia/SkiaToMirai.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public fun Surface.makeSnapshotResource(format: EncodedImageFormat = EncodedImag
*/
@JvmOverloads
public fun File.makeImageResource(format: EncodedImageFormat = EncodedImageFormat.PNG): SkiaExternalResource {
return SkiaExternalResource(image = Image.makeFromEncoded(readBytes()), format = format)
return SkiaExternalResource(image = Image.makeFromEncoded(bytes = this.readBytes()), format = format)
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/xyz/cssxsh/skia/Canvas.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xyz.cssxsh.skia

import org.jetbrains.skia.*

public fun Canvas.draw(block: Canvas.() -> Unit): Canvas {
val index = save()
block.invoke(this)
return restoreToCount(saveCount = index)
}

public fun Surface.canvas(block: Canvas.() -> Unit): Canvas {
return canvas.draw(block)
}
17 changes: 17 additions & 0 deletions src/main/kotlin/xyz/cssxsh/skia/Font.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xyz.cssxsh.skia

import org.jetbrains.skia.*

public fun FontMgr.makeFamilies(): Map<String, FontStyleSet> {
val count = familiesCount
if (count == 0) return emptyMap()
val families: MutableMap<String, FontStyleSet> = HashMap()

for (index in 0 until count) {
val name = getFamilyName(index)
val styles = makeStyleSet(index) ?: throw NoSuchElementException("${this}: ${index}.${name}")
families[name] = styles
}

return families
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ import org.jsoup.parser.*
import org.jsoup.select.*
import java.io.*

public fun FontMgr.makeFamilies(): Map<String, FontStyleSet> {
val count = familiesCount
if (count == 0) return emptyMap()
val families: MutableMap<String, FontStyleSet> = HashMap()

for (index in 0 until count) {
val name = getFamilyName(index)
val styles = makeStyleSet(index) ?: throw NoSuchElementException("${this}: ${index}.${name}")
families[name] = styles
}

return families
}

/**
* 从 xml 读取 SVGDOM
* @see org.jsoup.parser.Parser
Expand Down

0 comments on commit e015620

Please sign in to comment.