summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/kanbun/kanbun.sty
blob: 9f5f8f8d56837a0691d0eb54d78262c1bb08d814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
%% Copyright 2022 Yuanhao Chen
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Yuanhao Chen.
%
% This work consists of the files kanbun.sty, kanbun.lua,
% kanbun-example.tex and kanbun.tex.
% 
\NeedsTeXFormat{LaTeX2e}[2021/06/01]
\RequirePackage{expl3,xparse,l3keys2e,ifluatex}
\ProvidesExplClass{kanbun}
    {2022/02/13} {1.2} {漢文の訓点文の組版}

\ExplSyntaxOn
\cs_generate_variant:Nn \str_if_eq:nnTF { V }
\cs_generate_variant:Nn \str_case:nnTF { V }
\cs_generate_variant:Nn \str_case:nnTF { f }

% vars used in options
\@ifpackageloaded{luatexja-fontspec}{
    \tl_set:Nn \kanbun_rubyfontcmd_default { \addjfontfeatures{RawFeature={+ruby}} }
    \tl_set:Nn \kanbun_fontcmd_default { \addjfontfeatures{RawFeature={+trad}} }
}{
    \tl_set:Nn \kanbun_rubyfontcmd_default {}
    \tl_set:Nn \kanbun_fontcmd_default {}
}

% options
\keys_define:nn { kanbun }
    {    
        , scale .fp_set:N = \kanbun_scale
        , scale .initial:n = { 2 }
        , fontcmd .tl_set:N = \kanbun_fontcmd
        , fontcmd .initial:n = \kanbun_fontcmd_default
        , rubyfontcmd .tl_set:N = \kanbun_rubyfontcmd
        , rubyfontcmd .initial:n = \kanbun_rubyfontcmd_default
        , unit .dim_set:N = \kanbun_zw
        , unit .initial:n = { 1em }
        , yokoaki .fp_set:N = \kanbun_yokoaki
        , yokoaki .initial:n = { 2 }
        , tateaki .fp_set:N = \kanbun_tateaki
        , tateaki .initial:n = { 2 }
        , okuriintrusion .fp_set:N = \kanbun_okuriintrusion
        , okuriintrusion .initial:n = { 1 }
        , kumi .str_set:N = \kanbun_kumi
        , kumi .initial:n = { aki }
        , aki .code:n = \keys_set:nn { kanbun } { kumi = aki }
        , beta .code:n = \keys_set:nn { kanbun } { kumi = beta }
    }
\ProcessKeysOptions { kanbun }
\DeclareDocumentCommand \setkanbun { m } { 
    \keys_set:nn { kanbun } { #1 } 
}

% other vars
\tl_set:Nn \kanbun_rubyfontsize_rubyfontcmd { \kanbun_rubyfontcmd\fontsize{\fp_eval:n {1/\kanbun_scale}\kanbun_zw}{0pt}\selectfont }
\newlength{\kanbun_furilen}
\newlength{\kanbun_okurilen}
\newlength{\kanbun_furiokuri_firstline_len}
\int_new:N \kanbun_furiokuri_firstline_start_index
\tl_set:Nn \kanbun_tateten_width { 1/30 }
\tl_set:Nn \kanbunzwtosp { \fp_eval:n { \dim_ratio:nn { 1 \kanbun_zw } { 1 sp } } }
\tl_set:Nn \kanbun_unit_glue { 
    \str_if_eq:VnTF \kanbun_kumi { aki } {
        \penalty0
    }
    {
        \hskip 0\kanbun_zw plus 0.25\kanbun_zw minus 0\kanbun_zw
    }
}
\tl_set:Nn \kanbun_punct_glue { 
    \hskip 0\kanbun_zw plus 0.5\kanbun_zw minus 0\kanbun_zw
}

% \kanjiunit and other macros to be used in a \kanjiunit
\NewDocumentCommand { \kanbun_ensure_height } { m m }
    {
        \vbox to #1{\vss{#2}\vss}
    }
\newbox\kanbun_current_kanjiunit
\NewDocumentCommand { \kanjiunit } { m m m m m m }
    {
        \tl_set:Nn \ninojiten_punct {}
        \tl_set:Nn \right_punct {}
        \tl_set:Nn \central_punct {}
        \process_punct{ #4 }
        \tl_set:Nn \kanbun_central_punct_box {
            \kanbun_ensure_height{\kanbun_zw} {
                \str_if_eq:VnTF \kanbun_kumi { aki } 
                {
                    \hbox{\kern \kanbun_zw{\hbox to \fp_eval:n { \kanbun_tateaki/\kanbun_scale }\kanbun_zw{\hss{\central_punct}\hss}}}
                }
                {
                    \hbox{\kern \kanbun_zw{\central_punct}}
                }
            }
        }
        \tl_set:Nn \kanbun_left_punct_phantom {
            \kanbun_ensure_height{\kanbun_zw} {
                \str_if_eq:VnTF \kanbun_kumi { aki } { }
                {
                    \str_if_eq:nnTF { #2 } { } { } {
                        \hbox{\phantom{\hbox to 0.5\kanbun_zw{\hss#2\hfil}}}
                    }
                }
            }
        }
        \tl_set:Nn \kanbun_right_punct_box {
            \kanbun_ensure_height{\kanbun_zw} {
                \vfill % make sure punct stay on baseline in yoko direction (xeCJK)
                \str_if_eq:VnTF \right_punct { } { } {
                    \hbox{
                        \kern \kanbun_zw
                        \bool_if:NTF \kanbun_first_right_punct_is_kagi {
                            \phantom{ \kanbun_rubyfontsize_rubyfontcmd\kaeriten{#5} }
                        } { }
                        \right_punct
                    }
                }
            }
        }
        % 
        \tl_set:Nn \unit_content {
            \kanbun_left_punct_phantom % compensate for width of left punct
            \vbox{
                \bool_set_true:N \kanbun_is_right_kana
                \kanbun_ensure_height{ 0pt }{\hbox{\kanbun_rubyfontsize_rubyfontcmd{#1}}} % right kana
                \nointerlineskip %
                \vspace*{\fp_eval:n { 0.5/\kanbun_scale }\kanbun_zw}
                \kanbun_ensure_height{\kanbun_zw}{\llap{#2}} % left punct
                \nointerlineskip %
                \vspace*{-1 \kanbun_zw}
                \kanbun_ensure_height{\kanbun_zw}{
                        \hbox to \kanbun_zw{\hss{#3}\hss}
                } % kanji
                \nointerlineskip %
                \vspace*{-1 \kanbun_zw}
                \kanbun_ensure_height{\fp_eval:n { 1/\kanbun_scale }\kanbun_zw}{\hbox{\kern \kanbun_zw{\ninojiten_punct}}} % ninojiten
                \nointerlineskip %
                \vspace*{\fp_eval:n { -1/\kanbun_scale }\kanbun_zw}
                \kanbun_right_punct_box % right punct
                \nointerlineskip %
                \vspace*{-\kanbun_zw}
                \kanbun_central_punct_box % central punct
                \nointerlineskip %
                \vspace*{ \fp_eval:n { -1/\kanbun_scale }\kanbun_zw }
                \kanbun_ensure_height{\fp_eval:n { 1/\kanbun_scale }\kanbun_zw}{\hbox{\kanbun_rubyfontsize_rubyfontcmd\kern \kanbun_zw{\kaeriten{#5}}}} % kaeriten
                \nointerlineskip %
                \vspace*{\fp_eval:n { 0.5/\kanbun_scale }\kanbun_zw}
                \bool_set_false:N \kanbun_is_right_kana
                \kanbun_ensure_height{ 0pt }{\hbox{\kanbun_rubyfontsize_rubyfontcmd{#6}}} % left kana
            }
        }
        \str_if_eq:VnTF \kanbun_kumi { aki } {
            \setbox\kanbun_current_kanjiunit \hbox to \kanbun_zw{ \unit_content \hss}
        }{
            \setbox\kanbun_current_kanjiunit \hbox{ \unit_content }
        }
        % 
        % output
        \str_if_eq:VnTF \kanbun_kumi { aki } { }
            {
                \str_if_eq:nnTF { #2 } { } { } {
                    \kanbun_punct_glue
                }
            }
        \nobreak
        \str_if_eq:VnTF \kanbun_kumi { aki } 
            {
                \usebox\kanbun_current_kanjiunit
                \hskip\fp_eval:n { \kanbun_tateaki/\kanbun_scale }\kanbun_zw
            }
            {
                \discretionary{
                    \usebox\kanbun_current_kanjiunit
                    \kern\dim_eval:n { \kanbun_zw - \box_wd:N \kanbun_current_kanjiunit }
                }{
                    % 
                }{
                    \usebox\kanbun_current_kanjiunit
                }
            }
        \nobreak
        \str_if_eq:VnTF \kanbun_kumi { aki } { }
            {
                \str_if_eq:VnTF \right_punct { } { } {
                    \kanbun_punct_glue
                }
            }
        \nobreak
        \kanbun_unit_glue
    }
\cs_new:Nn \kanbun_dim_to_fp_and_round:n {
    \fp_eval:n { round( \dim_to_fp:n { #1 }, 0 ) }
}
\cs_new:Nn \kanbun_typeset_normal_kana:nn {
    \dim_compare:nNnTF { \kanbun_furilen } < { \fp_eval:n { 1-\kanbun_okuriintrusion/\kanbun_scale }\kanbun_zw }
        { \makebox[\fp_eval:n { 1-\kanbun_okuriintrusion/\kanbun_scale }\kanbun_zw][l]{ #1 } }
        { #1 }
    #2
}
\cs_new:Nn \kanbun_typeset_long_kana_by_raising_okurigana:nn {
        \makebox[ \dim_eval:n { \fp_eval:n { 1 + \kanbun_tateaki / \kanbun_scale }\kanbun_zw - \kanbun_okurilen } ][l]{ #1 }
        #2
}
\cs_new:Nn \kanbun_typeset_long_kana:n {
    \str_set:Nn \kanbun_furiokuri_all { #1 }
    \int_set:Nn \kanbun_furiokuri_firstline_start_index { -1 }
    \settowidth{ \kanbun_furiokuri_firstline_len }{ }
    % 
    \fp_while_do:nn { 
        \kanbun_dim_to_fp_and_round:n { \kanbun_furiokuri_firstline_len } < \kanbun_dim_to_fp_and_round:n { \fp_eval:n { (\kanbun_okuriintrusion + \kanbun_tateaki) / \kanbun_scale }\kanbun_zw }
        % \kanbun_dim_to_fp_and_round:n { \kanbun_furiokuri_firstline_len } < \kanbun_dim_to_fp_and_round:n { ( \kanbun_furilen + \kanbun_okurilen ) / 2 } 
    } {
        \int_decr:N \kanbun_furiokuri_firstline_start_index
        \settowidth{ \kanbun_furiokuri_firstline_len }{ \str_range:Nnn \kanbun_furiokuri_all { \kanbun_furiokuri_firstline_start_index } { -1 } }
    }
    % output
    \rlap{\raisebox{ \bool_if:NTF \kanbun_is_right_kana { } { - } \fp_eval:n { 1 / \kanbun_scale }\kanbun_zw }[0pt][0pt]{
        \kern \fp_eval:n { .5/\kanbun_scale }\kanbun_zw
        \kern \fp_to_dim:n { min( 0, \kanbun_dim_to_fp_and_round:n { \fp_eval:n { 2 * (\kanbun_okuriintrusion + \kanbun_tateaki) / \kanbun_scale }\kanbun_zw - \kanbun_furilen - \kanbun_okurilen } ) }
        \str_range:Nnn \kanbun_furiokuri_all { 1 } { \kanbun_furiokuri_firstline_start_index - 1 }
    }}
    \kern \fp_eval:n { 1-\kanbun_okuriintrusion/\kanbun_scale }\kanbun_zw \str_range:Nnn \kanbun_furiokuri_all { \kanbun_furiokuri_firstline_start_index } { -1 } 
}
\NewDocumentCommand { \furiokuri } { m m }
    {
        \settowidth{ \kanbun_furilen }{ #1 }
        \settowidth{ \kanbun_okurilen }{ #2 }

        \str_if_eq:VnTF \kanbun_kumi { aki } 
            { 
                \fp_compare:nTF { \kanbun_dim_to_fp_and_round:n { \kanbun_furilen + \kanbun_okurilen } > \kanbun_dim_to_fp_and_round:n {\fp_eval:n { 1 + \kanbun_tateaki / \kanbun_scale }\kanbun_zw} } {
                    \kanbun_typeset_long_kana:n { #1 #2 }
                } {
                    \fp_compare:nTF { \kanbun_dim_to_fp_and_round:n { \kanbun_okurilen } >= \kanbun_dim_to_fp_and_round:n { \fp_eval:n { (\kanbun_okuriintrusion + \kanbun_tateaki) / \kanbun_scale }\kanbun_zw } } {
                        \fp_compare:nTF { \kanbun_dim_to_fp_and_round:n { \kanbun_okurilen } <= \kanbun_dim_to_fp_and_round:n { \fp_eval:n { 1 + (\kanbun_tateaki - 1) / \kanbun_scale }\kanbun_zw } } {
                            \kanbun_typeset_long_kana_by_raising_okurigana:nn { #1 } { #2 }
                        } {
                            \kanbun_typeset_long_kana:n { #1 #2 }
                        }
                    } {
                        \kanbun_typeset_normal_kana:nn { #1 } { #2 }
                    }
                }
            }
            {
                \kanbun_typeset_normal_kana:nn { #1 } { #2 }
            }
    }
\NewDocumentCommand { \kaeriten } { m }
    {
        \tl_set:Nn \kaeriten_output { #1 }
        \sys_if_engine_xetex:TF {} {
            \platex_if_direction_tate:TF {
                \str_case:VnTF \kaeriten_output {
                    { 一レ } { \tl_set:Nn \kaeriten_output { \kern \fp_eval:n {-0.25/\kanbun_scale}\kanbun_zw 一\kern \fp_eval:n {-0.75/\kanbun_scale}\kanbun_zw レ } }
                    { 甲レ } { \tl_set:Nn \kaeriten_output { 甲\kern \fp_eval:n {-0.25/\kanbun_scale}\kanbun_zw レ } }
                    { 上レ } { \tl_set:Nn \kaeriten_output { 上\kern \fp_eval:n {-0.25/\kanbun_scale}\kanbun_zw レ } }
                    { 天レ } { \tl_set:Nn \kaeriten_output { 天\kern \fp_eval:n {-0.25/\kanbun_scale}\kanbun_zw レ } }
                } {} {}
            } {}
        }
        \tl_use:N \kaeriten_output
    }
\NewDocumentCommand { \process_punct_map } { m } {
    \str_case:nnTF { #1 } {
        { — } { \tl_put_right:Nn \central_punct { \tateten } }
        { ― } { \tl_put_right:Nn \central_punct { \tateten } }
        { ㆐ } { \tl_put_right:Nn \central_punct { \tateten } }
        % { : } { \tl_put_right:Nn \central_punct { \hbox to \kanbun_zw{\hss{:}\hss} } }
        % { ・ } { \tl_put_right:Nn \central_punct { \hbox to \kanbun_zw{\hss{・}\hss} } }
        { : } { \tl_put_right:Nn \right_punct { \hbox to 0.5\kanbun_zw{\hss{:}\hss} } }
        { ・ } { \tl_put_right:Nn \right_punct { \hbox to 0.5\kanbun_zw{\hss{・}\hss} } }
        { … } { \tl_put_right:Nn \central_punct { \hbox to \kanbun_zw{\hss{…}\hss} } }
        { ! } { \tl_put_right:Nn \central_punct { \hbox to \kanbun_zw{\hss{!}\hss} } }
        { ? } { \tl_put_right:Nn \central_punct { \hbox to \kanbun_zw{\hss{?}\hss} } }
        { 〻 } { 
            \tl_put_right:Nn \ninojiten_punct { \ninojiten } 
            \tl_put_right:Nn \right_punct { \phantom{\ninojiten} }
        }
    } {} {
        \tl_put_right:Nn \right_punct { \hbox to 0.5\kanbun_zw{\hfil#1\hss} } 
    }
}
% \bool_new:N 
\NewDocumentCommand { \process_punct } { m }
    {
        \bool_set_false:N \kanbun_first_right_punct_is_kagi
        \str_case:fnTF { \str_range:nnn { #1 } { 1 } { 1 } } {
            { 」 } { \bool_set_true:N \kanbun_first_right_punct_is_kagi }
            { 』 } { \bool_set_true:N \kanbun_first_right_punct_is_kagi }
        } { } { }
        \str_map_function:nN { #1 } \process_punct_map
    }
\NewDocumentCommand { \multifuriokuri } { O{2\kanbun_zw} m m }
    {
        \kanbun_rubyfontcmd
        \kern \fp_eval:n { -(#1) }\kanbun_zw
        \makebox[\fp_eval:n { (#1) + 1 - \kanbun_okuriintrusion/\kanbun_scale }\kanbun_zw][s]{#2}
        #3
    }

% these commands are used as punct
\NewDocumentCommand { \tateten } {} 
    {
        \str_if_eq:VnTF \kanbun_kumi { aki } {
            \makebox[\fp_eval:n {\kanbun_tateaki/\kanbun_scale}\kanbun_zw][c]{
                \rule[\fp_eval:n {-\kanbun_tateten_width/2}\kanbun_zw]{\fp_eval:n {\kanbun_tateaki/\kanbun_scale}\kanbun_zw}{\fp_eval:n {\kanbun_tateten_width}\kanbun_zw}
            }
        }{
            \makebox[\fp_eval:n {1/\kanbun_scale}\kanbun_zw][c]{
                \rule[\fp_eval:n {-\kanbun_tateten_width/2}\kanbun_zw]{\fp_eval:n {1/\kanbun_scale}\kanbun_zw}{\fp_eval:n {\kanbun_tateten_width}\kanbun_zw}
            }
        }
    }
\NewDocumentCommand { \ninojiten } {} 
    { \kanbun_rubyfontsize_rubyfontcmd 〻 }

% corrects fontsize of ruby base
% \NewDocumentCommand { \kanbunfont } {} { \parindent=0pt\kanbun_fontcmd\fontsize{\kanbun_zw}{\fp_eval:n {1+\kanbun_yokoaki/\kanbun_scale}\kanbun_zw}\selectfont}
\NewDocumentCommand { \kanbunfont } {} { \kanbun_fontcmd\fontsize{\kanbun_zw}{\fp_eval:n {1+\kanbun_yokoaki/\kanbun_scale}\kanbun_zw}\selectfont}
\NewDocumentEnvironment { kanjipar } { +b }
    {
        {
            \kanbunfont
            #1
            \par
        }
    }   {}

% for LuaLaTeX to parse Kanbun annotation
\NewDocumentCommand{\matchkana}{ m }{
    \regex_set:Nn \c_kana_regex {[\x{3040}-\x{30FA}\x{30FC}-\x{30FF}\x{31F0}-\x{31FF}\x{FF66}-\x{FF9F}\x{1B100}-\x{1B122}\x{1AFF0}-\x{1AFFF}\x{1B000}-\x{1B0FF}\x{1B130}-\x{1B16F}]}
    \regex_match:NnTF \c_kana_regex { #1 } { \bool_set_true:N \g_kana_bool } { \bool_set_false:N \g_kana_bool }
}
% 
\ExplSyntaxOff
% 
\ifluatex
% verbatim reader adapted from https://tex.stackexchange.com/a/361759
\directlua{
verb_table = {}
function store_lines (str)
    if string.find (str , "\noexpand\\EndKanbun" ) then
        luatexbase.remove_from_callback (
            "process_input_buffer" , "store_lines")
        return "\\newif\\ifcontinue\\continuetrue\\directlua { co = coroutine.create(main_loop) }\\loop\\directlua{ ok,b=coroutine.resume(co) tex.sprint(b) }\\ifcontinue\\repeat"
    else
        table.insert(verb_table, str)
    end
    return ""
end
function register_verbatim ()
    verb_table = {}
    luatexbase.add_to_callback(
        "process_input_buffer" , store_lines , "store_lines")
end
% require main loop
kanbunzwtosp = \luaescapestring{\kanbunzwtosp}
require("kanbun.lua")
}
\def\Kanbun{\directlua{register_verbatim()}}
\def\createcatcodes{%
    \bgroup%
        \catcode`\\=12 \catcode`\{=12 \catcode`\}=12%
        \catcode`\$=12 \catcode`\&=12 \catcode`\^^M=13%
        \catcode`\#=12 \catcode`\^=12 \catcode`\_=12%
        \catcode`\ =13 \catcode`\~=12 \catcode`\%=12%
        \savecatcodetable 1%
    \egroup%
}
\createcatcodes
\bgroup%
    \catcode`\^^M=13\gdef^^M{\quitvmode\par}%
    \catcode`\ = 13\gdef {\quitvmode\Space}%
\egroup%
\def\Space{ }
% 
\fi