概要
SATySFi に比較的最近追加された set-math-char
という数式モード内での入出力文字の対応を指定するためのプリミティブで遊んでみたという話
set-math-char の仕様 (推測込み)
SATySFi version 0.0.7 で追加された set-math-char というプリミティブがある. 以下は,CHANGELOGからの抜粋
Add primitive
set-math-char : int -> int -> math-class -> context -> context
for handling various Unicode code points in the math mode.
CHANGELOGの説明にあるように,数式モード内でUnicode文字を扱うためのプリミティブであるようである. しかし,比較的新しいプリミティブであるため既存のドキュメントには記載がないようである.
動作を確認するため,ソースをのぞいてみると次の箇所が関係ありそうである.
let def = Instruction.( [ (* ... *) ; inst "PrimitiveSetMathChar" ~name:"set-math-char" ~type_:Type.(tI @-> tI @-> tMATHCLS @-> tCTX @-> tCTX) ~fields:[ ] ~params:[ param "cp_from" ~type_:"int"; param "cp_to" ~type_:"int"; param "mk" ~type_:"math_class"; param "(ctx, ctxsub)" ~type_:"context"; ] ~is_pdf_mode_primitive:true ~is_text_mode_primitive:true ~code:{| let uch_from = Uchar.of_int cp_from in let uch_to = Uchar.of_int cp_to in let mkmap = ctx.HorzBox.math_class_map in Context(HorzBox.({ ctx with math_class_map = mkmap |> MathClassMap.add uch_from (uch_to, mk); }), ctxsub) |} (* ... *) ]
このあたりを見て推測するに,set-math-char
は次のような仕様と思われる.
ctx |> set-math-char cp-from cp-to mk
によってコンテキストctx
を変換すると,変換後のコンテキストでは,数式(${...}
)中で Unicode コードポイントcp-from
をもつ入力文字をcp-to
の Unicode文字として出力する.空白の扱いはmk
で指定した数式クラスに準ずる.
cp
は code point の略のようである.
遊んでみる
set-math-char
を使って,数式内の文字 a,b,c をギリシャ文字 に変化させてみる.
使用したソースは以下
@require: stdjareport let-inline ctx \alph-to-greek m = let cp-table = [ (0x0061, 0x1D6FC); % a,alpha (0x0062, 0x1D6FD); % b,beta (0x0063, 0x1D6FE); % c,gamma ] in let-rec aux ctx cp-table = match cp-table with | [] -> ctx | (cp-from,cp-to) :: tail -> let nctx = ctx |> set-math-char cp-from cp-to MathOrd in aux nctx tail in let nctx = aux ctx cp-table in embed-math nctx m in document (| title = {Test of set-math-char primitive}; author = {sankantsu}; |) '< +p { before set-math-char: ${abc} } +p { after set-math-char: \alph-to-greek(${abc}); } >
alph-to-greek
の中身を簡単に説明すると,cp-table
という変数に格納したコードポイントの対応を set-math-char
を使って順番に適用してコンテキストを変換し,最後に,変換後のコンテキストnctx
で数式を読み込んで文書中に埋め込む.
satysfi
でコンパイルすると以下のような出力が得られ,文字が変換されていることがわかる.