sankantsuのブログ

技術メモ・競プロなど

SATySFi の set-math-char プリミティブで遊ぶ

概要

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-toUnicode文字として出力する.空白の扱いは mk で指定した数式クラスに準ずる.

cpは code point の略のようである.

遊んでみる

set-math-charを使って,数式内の文字 a,b,c をギリシャ文字  \alpha, \beta, \gamma に変化させてみる. 使用したソースは以下

@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コンパイルすると以下のような出力が得られ,文字が変換されていることがわかる.

生成したpdf