working cangjie IME implementation

This commit is contained in:
minjaesong
2021-10-27 15:22:43 +09:00
parent 69aaca702d
commit 3ca96bce7f
6 changed files with 458 additions and 56 deletions

View File

@@ -56,12 +56,12 @@ object IME {
init {
context.getBindings("js").putMember("IMEProvider", IMEProviderDelegate(this))
File(KEYLAYOUT_DIR).listFiles { file, s -> s.endsWith(".$KEYLAYOUT_EXTENSION") }.forEach {
File(KEYLAYOUT_DIR).listFiles { file, s -> s.endsWith(".$KEYLAYOUT_EXTENSION") }.sortedBy { it.name }.forEach {
printdbg(this, "Registering Low layer ${it.nameWithoutExtension.lowercase()}")
lowLayers[it.nameWithoutExtension.lowercase()] = parseKeylayoutFile(it)
}
File(KEYLAYOUT_DIR).listFiles { file, s -> s.endsWith(".$IME_EXTENSION") }.forEach {
File(KEYLAYOUT_DIR).listFiles { file, s -> s.endsWith(".$IME_EXTENSION") }.sortedBy { it.name }.forEach {
printdbg(this, "Registering High layer ${it.nameWithoutExtension.lowercase()}")
highLayers[it.nameWithoutExtension.lowercase()] = parseImeFile(it)
}

View File

@@ -1,5 +1,7 @@
package net.torvald.terrarum.gamecontroller
import net.torvald.terrarum.App.printdbg
import net.torvald.util.SortedArrayList
import java.io.File
import java.io.FileReader
@@ -13,11 +15,14 @@ class IMEProviderDelegate(val ime: IME) {
}
class IMEDictionary(filename: String) {
class IMEDictionary(private val filename: String) {
private val candidates = HashMap<String, String>()
private val candidates = HashMap<String, String>(16384)
private val keys = SortedArrayList<String>(16384)
init {
private var dictLoaded = false
private fun loadDict() {
val reader = FileReader(File("assets/keylayout/", filename))
reader.forEachLine {
val (key, value) = it.split(',')
@@ -26,10 +31,38 @@ class IMEDictionary(filename: String) {
}
else {
candidates[key] = value
keys.add(key)
}
}
printdbg(this, "Dictionary loaded: $filename")
dictLoaded = true
}
operator fun get(key: String): String = candidates[key] ?: ""
init {
loadDict() // loading the dict doesn't take too long so no need to do it lazily
}
operator fun get(key: String): String {
//if (!dictLoaded) loadDict()
val out = StringBuilder()
var outsize = 0
var index = keys.searchForInterval(key) { it }.second
while (outsize < 10) {
val keysym = keys[index]
if (!keysym.startsWith(key)) break
val outstr = ",${candidates[keysym]}"
outsize += outstr.count { it == ',' }
out.append(outstr)
index += 1
}
return if (out.isNotEmpty()) out.substring(1) else ""
}
}