Skip to content

Latest commit

 

History

History
87 lines (51 loc) · 4.12 KB

File metadata and controls

87 lines (51 loc) · 4.12 KB

ComposingText API

AzooKeyKanaKanjiConverterにおいて、変換を要求するにはComposingTextのAPIを使う必要があります。このComposingText APIについて説明します。

基本的なアイデア

ComposingTextの基本的なアイデアは「入力操作との対応」です。ユーザが日本語IMEを操作するとき、「きょうはいいてんきですね」と一文字ずつ入力することもあれば、「kyouhaiitenkidesune」のようにローマ字入力を行うこともあります。azooKeyでは前者をダイレクト入力、後者をローマ字入力と呼んでいます。ComposingTextはこのような入力操作をうまく扱いながら、変換を逐次的に実行するために役立ちます。

基本的な使い方

ComposingTextを使い始めるには、まず空の値を作ります。

var composingText = ComposingText()

次に、末尾に文字を追加します。このために使うのがinsertAtCursorPositionです。

composingText.insertAtCursorPosition("", inputStyle: .direct)

このとき、ComposingTextの内部状態は次のようになっています。

print(composingText.input)                        // [InputElement("あ", .direct)]
print(composingText.convertTargetCursorPosition)  // 1 (あ|)
print(composingText.convertTarget)                // あ

非常に自明です。ではローマ字入力の場合はどうなるでしょうか。

composingText.insertAtCursorPosition("o", inputStyle: .roman2kana)

この場合は少し異なることが起こります。input"o"が正しく保存されるのです。

print(composingText.input)                        // [InputElement("あ", .direct), InputElement("o", .roman2kana)]
print(composingText.convertTargetCursorPosition)  // 2 (あお|)
print(composingText.convertTarget)                // あお

一方、convertTargetの方は正しくローマ字入力した仮名表記になっています。このようにconvertTargetの方はユーザに実際に見える「見かけの文字列」であり、実装側はこれが実際にユーザに見えているよう保障する必要があります。convertTargetCursorPositionについても同様で、実装側はconvertTargetCursorPositionに示されたカーソル位置が実際にユーザに見えているカーソル位置と一致するよう配慮する必要があります。

操作するAPI

削除

deleteForwardFromCursorPositionおよびdeleteBackwardFromCursorPositionが使えます。

カーソル移動

moveCursorFromCursorPositionが使えます。

文頭の削除

prefixCompleteが使えます。

置換

専用のAPIはありません。削除と挿入で代用してください。

PCのキー入力を扱う(試験的)

PCキーボードからの入力を、キーと修飾キーの組み合わせとして扱うために、内部的に InputPiece.key(intention: Character?, modifiers: Set<Modifier>) をサポートしています(現状の修飾は shift のみ)。

カスタム入力テーブルでは {shift 0}{shift _} の2トークンのみを特別扱いでサポートしており、それぞれ .key(intention: "0", [.shift]).key(intention: "_", [.shift]) に対応します。ComposingText に投入する場合は、以下のように InputElement(piece: …, inputStyle: …) を使います。

let table = try InputStyleManager.loadTable(from: url)
InputStyleManager.registerInputStyle(table: table, for: "custom_table")

var c = ComposingText()
c.insertAtCursorPosition([
    .init(piece: .key(intention: "0", modifiers: [.shift]), inputStyle: .mapped(id: .tableName("custom_table")))
])

マッチングの優先順位は「キー規則(例: {shift 0})」が「文字規則(例: 0)」よりも優先されます。同じ意図文字を持つキー入力は、文字規則にもフォールバックして一致します。また、{any character} はキー入力の意図文字にも一致し、出力の {any character} に代入されます。