Debian Codegraph @

texmf-dist/source/latex/boustrophedon/boustrophedon.dtx

raw
1 % \iffalse meta-comment
2 %<*driver>
3 %
4 % Copyright (c) 2026 Aris Chatzichristos, PhD
5 %
6 % This work may be distributed and/or modified under the conditions of
7 % the LaTeX Project Public License, either version 1.3c of this license
8 % or, at your option, any later version.
9 %
10 % This work has the LPPL maintenance status "maintained".
11 %
12 % The Current Maintainer of this work is Aris Chatzichristos.
13 %
14 \documentclass{ltxdoc}
15 \usepackage[a4paper,margin=2.5cm]{geometry}
16 \usepackage{fontspec}
17 \IfFontExistsTF{Segoe UI}
18 {\setmonofont{Segoe UI}[Scale=MatchLowercase]}
19 {
20 \IfFontExistsTF{DejaVu Sans Mono}
21 {\setmonofont{DejaVu Sans Mono}[Scale=MatchLowercase]}
22 {
23 \IfFontExistsTF{Noto Sans Mono}
24 {\setmonofont{Noto Sans Mono}[Scale=MatchLowercase]}
25 {}
26 }
27 }
28 \EnableCrossrefs
29 \CodelineIndex
30 \RecordChanges
31 \begin{document}
32 \DocInput{boustrophedon.dtx}
33 \PrintIndex
34 \end{document}
35 %</driver>
36 % \fi
37 %
38 % \GetFileInfo{boustrophedon.sty}
39 %
40 % \title{The \textsf{boustrophedon} package}
41 % \author{Aris Chatzichristos, PhD}
42 % \date{Version 1.1.1, 2026-05-20}
43 % \maketitle
44 %
45 % \begin{abstract}
46 % \textsf{boustrophedon} typesets text in boustrophedon style, alternating
47 % writing direction line by line. It supports generic mirrored text, Archaic
48 % and Classical Greek glyph rendering, automatic wrapping, explicit line
49 % breaks, and inline bracketed spans.
50 % \end{abstract}
51 %
52 % \section{Overview}
53 %
54 % This package typesets text in \emph{boustrophedon} style, alternating the
55 % writing direction on each line.
56 %
57 % Have you ever wondered why most Western languages are written left-to-right,
58 % while many Middle Eastern languages are written right-to-left? Both
59 % conventions may seem arbitrary today---but this was not always the case.
60 %
61 % In one of the earliest fully developed alphabets, the Archaic Greek alphabet
62 % (from \emph{alpha} and \emph{beta}, hence ``alphabet''), writing was often
63 % performed \emph{boustrophedon}, meaning ``as the ox turns in ploughing.''
64 % Text was written left-to-right on one line, then right-to-left on the next,
65 % with both the direction of the text \emph{and the letterforms themselves}
66 % mirrored accordingly.
67 %
68 % This method creates a continuous flow of reading, as the reader does not need
69 % to jump back to the start of each new line, but instead follows the text in a
70 % natural back-and-forth motion.
71 %
72 % Reading takes some getting used to, but after a while it becomes surprisingly
73 % natural. One may also observe that:
74 %
75 % \begin{itemize}
76 % \item Capital letters (both in the Latin alphabet and especially in the
77 % Greek alphabet) are particularly well-suited to this type of writing, and
78 % are much easier to read than lowercase letters (which were developed later
79 % in both systems).
80 % \item For example, pairs such as \emph{b} vs \emph{d}, or comparable
81 % look-alikes in polytonic Greek, can make reading more difficult, as the
82 % reader must keep mental track of the text direction.
83 % \item In contrast, capital Greek letters---already in use during the
84 % boustrophedon period---have the appropriate symmetry to be read in both
85 % directions without ambiguity.
86 % \end{itemize}
87 %
88 % This package brings an ancient writing paradigm into modern digital
89 % typography.
90 %
91 % \section{Installation}
92 %
93 % To generate the package file from this documented source, run:
94 %
95 % \begin{verbatim}
96 % latex boustrophedon.ins
97 % \end{verbatim}
98 %
99 % This produces \texttt{boustrophedon.sty}. Place it beside your document or
100 % install it in a TeX tree searched by your distribution.
101 %
102 % \section{Requirements}
103 %
104 % XeLaTeX or LuaLaTeX is recommended, especially for Greek Unicode input. The
105 % package requires \texttt{xparse}, \texttt{expl3}, \texttt{graphicx},
106 % \texttt{iftex}, \texttt{greek6cbc}, and \texttt{greek4cbc}.
107 %
108 % \section{Usage}
109 %
110 % The main command is:
111 %
112 % \begin{verbatim}
113 % \boustrophedon[<keys>]{<text>}
114 % \end{verbatim}
115 %
116 % Important keys are:
117 % \begin{itemize}
118 % \item \texttt{Type=Any}, \texttt{Type=Archaic}, or \texttt{Type=Classical}
119 % \item \texttt{start=LTR} or \texttt{start=RTL}
120 % \item \texttt{resetDirection=Page} or \texttt{resetDirection=Paragraph}
121 % \item \texttt{LineWidth=Auto} or an explicit dimension
122 % \item \texttt{ObscureLetters=On} or \texttt{ObscureLetters=Off}
123 % \item \texttt{inLine=True} or \texttt{inLine=False}
124 % \end{itemize}
125 %
126 % Inline mode keeps surrounding text normal and transforms only bracketed spans:
127 %
128 % \begin{verbatim}
129 % \boustrophedon[inLine=True]{Normal text with [THIS PART] mirrored inline.}
130 % \boustrophedon[Type=Classical,inLine=True]{A sentence with [DHMOS ATHNAIWN].}
131 % \end{verbatim}
132 %
133 % The start/stop form is also available:
134 %
135 % \begin{verbatim}
136 % \startBoustrophedon[Type=Archaic]
137 % ANDRA MOI ENNEPE MOUSA POLYTROPON ...
138 % \stopBoustrophedon
139 % \end{verbatim}
140 %
141 % \section{Examples}
142 %
143 % See \texttt{examples/boustrophedon\_template.tex} for a minimal starter and
144 % \texttt{examples/boustrophedon\_demos.tex} for the full demonstration and
145 % regression document.
146 %
147 % \section{License and maintenance}
148 %
149 % This work is distributed under the LaTeX Project Public License, version 1.3c
150 % or later. The work has LPPL maintenance status ``maintained''. The Current
151 % Maintainer is Aris Chatzichristos.
152 %
153 % \StopEventually{\PrintChanges}
154 %
155 % \section{Implementation}
156 %
157 % \begin{macrocode}
158 %<*package>
159 \NeedsTeXFormat{LaTeX2e}
160 \ProvidesPackage{boustrophedon}[2026/05/20 v1.1.1 Documented source distribution]
161
162 \RequirePackage{xparse}
163 \RequirePackage{expl3}
164 \RequirePackage{graphicx}
165 \RequirePackage{greek6cbc}
166 \RequirePackage{greek4cbc}
167 \RequirePackage{iftex}
168
169 \ExplSyntaxOn
170
171 % --------------------------------------------------------------------------
172 % Options
173 % --------------------------------------------------------------------------
174 \bool_new:N \g_boustro_start_ltr_bool
175 \bool_new:N \g_boustro_obscure_letters_bool
176 \bool_new:N \g_boustro_reset_page_bool
177 \bool_new:N \g_boustro_inline_bool
178 \int_new:N \g_boustro_line_int
179 \tl_new:N \g_boustro_type_tl
180 \dim_new:N \g_boustro_linewidth_dim
181
182 \bool_set_true:N \g_boustro_start_ltr_bool
183 \bool_set_true:N \g_boustro_obscure_letters_bool
184 \bool_set_true:N \g_boustro_reset_page_bool
185 \bool_set_false:N \g_boustro_inline_bool
186 \int_gzero:N \g_boustro_line_int
187 \tl_set:Nn \g_boustro_type_tl {any}
188 \dim_set:Nn \g_boustro_linewidth_dim { 0pt }
189
190 \cs_new_protected:Npn \boustro_set_linewidth:n #1
191 {
192 \tl_set:Nn \l_tmpb_tl {#1}
193 \tl_trim_spaces:N \l_tmpb_tl
194 \tl_set:NV \l_tmpa_tl \l_tmpb_tl
195 \tl_set:Nx \l_tmpa_tl { \tl_lower_case:n { \l_tmpa_tl } }
196 \tl_if_blank:VTF \l_tmpa_tl
197 { \dim_set:Nn \g_boustro_linewidth_dim { 0pt } }
198 {
199 \str_if_eq:VnTF \l_tmpa_tl { auto }
200 { \dim_set:Nn \g_boustro_linewidth_dim { 0pt } }
201 {
202 \dim_set:Nn \g_boustro_linewidth_dim { \l_tmpb_tl }
203 }
204 }
205 }
206
207 \keys_define:nn { boustro }
208 {
209 start .choice:,
210 start / LTR .code:n = { \bool_set_true:N \g_boustro_start_ltr_bool },
211 start / RTL .code:n = { \bool_set_false:N \g_boustro_start_ltr_bool },
212 Type .choice:,
213 Type / Any .code:n = { \tl_set:Nn \g_boustro_type_tl {any} },
214 Type / Archaic .code:n = { \tl_set:Nn \g_boustro_type_tl {archaic} },
215 Type / Classical .code:n= { \tl_set:Nn \g_boustro_type_tl {classical} },
216 LineWidth .code:n = { \boustro_set_linewidth:n {#1} },
217 ObscureLetters .choice:,
218 ObscureLetters / On .code:n = { \bool_set_true:N \g_boustro_obscure_letters_bool },
219 ObscureLetters / Off .code:n = { \bool_set_false:N \g_boustro_obscure_letters_bool },
220 % Backwards-compat alias (deprecated):
221 digammaAndKoppa .choice:,
222 digammaAndKoppa / On .code:n = { \bool_set_true:N \g_boustro_obscure_letters_bool },
223 digammaAndKoppa / Off .code:n = { \bool_set_false:N \g_boustro_obscure_letters_bool },
224 resetDirection .choice:,
225 resetDirection / Page .code:n = { \bool_set_true:N \g_boustro_reset_page_bool },
226 resetDirection / Paragraph .code:n = { \bool_set_false:N \g_boustro_reset_page_bool },
227 % Backwards-compat alias (deprecated):
228 reset .choice:,
229 reset / page .code:n = { \bool_set_true:N \g_boustro_reset_page_bool },
230 reset / paragraph .code:n = { \bool_set_false:N \g_boustro_reset_page_bool },
231 inLine .choice:,
232 inLine / True .code:n = { \bool_set_true:N \g_boustro_inline_bool },
233 inLine / False .code:n = { \bool_set_false:N \g_boustro_inline_bool },
234 inLine / true .code:n = { \bool_set_true:N \g_boustro_inline_bool },
235 inLine / false .code:n = { \bool_set_false:N \g_boustro_inline_bool },
236 inline .choice:,
237 inline / True .code:n = { \bool_set_true:N \g_boustro_inline_bool },
238 inline / False .code:n = { \bool_set_false:N \g_boustro_inline_bool },
239 inline / true .code:n = { \bool_set_true:N \g_boustro_inline_bool },
240 inline / false .code:n = { \bool_set_false:N \g_boustro_inline_bool },
241 }
242 \ProcessKeyOptions [ boustro ]
243
244 \AddToHook{shipout/after}
245 {
246 \bool_if:NT \g_boustro_reset_page_bool
247 { \int_gzero:N \g_boustro_line_int }
248 }
249
250 % --------------------------------------------------------------------------
251 % Variables
252 % --------------------------------------------------------------------------
253 \seq_new:N \l__boustro_lines_seq
254 \seq_new:N \l__boustro_symbols_seq
255 \seq_new:N \l__boustro_tmp_seq
256 \seq_new:N \l__boustro_words_seq
257 \seq_new:N \l__boustro_pars_seq
258 \bool_new:N \l__boustro_input_has_greek_bool
259 \tl_new:N \l__boustro_norm_tl
260 \tl_new:N \l__boustro_scan_tl
261 \tl_new:N \l__boustro_scan_tail_tl
262 \tl_new:N \l__boustro_head_tl
263 \tl_new:N \l__boustro_next_tl
264 \tl_new:N \l__boustro_combined_tl
265 \tl_new:N \l__boustro_current_line_tl
266 \tl_new:N \l__boustro_candidate_tl
267 \tl_new:N \l__boustro_words_tl
268 \tl_new:N \l__boustro_input_tl
269 \tl_new:N \l__boustro_inline_mode_tl
270 \box_new:N \l__boustro_measure_box
271 \dim_new:N \l__boustro_width_dim
272 \dim_new:N \l__boustro_target_dim
273
274 \cs_new_protected:Npn \boustro_set_target_width:n #1
275 {
276 \dim_compare:nNnTF { \g_boustro_linewidth_dim } > { 0pt }
277 { \dim_set_eq:NN \l__boustro_target_dim \g_boustro_linewidth_dim }
278 {
279 \str_case:nnF {#1}
280 {
281 {any}{\dim_set:Nn \l__boustro_target_dim { .95\linewidth }}
282 {archaic}{\dim_set:Nn \l__boustro_target_dim { .95\linewidth }}
283 {classical}{\dim_set:Nn \l__boustro_target_dim { .95\linewidth }}
284 }
285 { \dim_set:Nn \l__boustro_target_dim { .95\linewidth } }
286 }
287 \dim_compare:nNnT { \l__boustro_target_dim } > { \linewidth }
288 { \dim_set:Nn \l__boustro_target_dim { \linewidth } }
289 }
290
291 \cs_new_protected:Npn \boustro_maybe_reset:
292 {
293 \bool_if:NF \g_boustro_reset_page_bool
294 { \int_gzero:N \g_boustro_line_int }
295 }
296
297 \cs_new:Npn \boustro_line_is_reverse:
298 {
299 \bool_if:NTF \g_boustro_start_ltr_bool
300 { \int_if_even_p:n { \g_boustro_line_int } }
301 { \int_if_odd_p:n { \g_boustro_line_int } }
302 }
303
304 \cs_new:Npn \boustro_line_is_reverse_n:n #1
305 {
306 \bool_if:NTF \g_boustro_start_ltr_bool
307 { \int_if_even_p:n {#1} }
308 { \int_if_odd_p:n {#1} }
309 }
310
311 % --------------------------------------------------------------------------
312 % Explicit line splitting and paragraph splitting
313 % --------------------------------------------------------------------------
314 \cs_new_protected:Npn \boustro_split_explicit_lines:n #1
315 {
316 \tl_set:Nn \l__boustro_input_tl {#1}
317 \tl_replace_all:Nnn \l__boustro_input_tl {\\} { <BLINEBREAK> }
318 \seq_clear:N \l__boustro_pars_seq
319 \seq_set_split:NnV \l__boustro_tmp_seq { <BLINEBREAK> } \l__boustro_input_tl
320 \seq_map_inline:Nn \l__boustro_tmp_seq
321 {
322 \tl_set:Nn \l_tmpa_tl {##1}
323 \tl_trim_spaces:N \l_tmpa_tl
324 \tl_if_blank:VF \l_tmpa_tl
325 { \seq_put_right:NV \l__boustro_pars_seq \l_tmpa_tl }
326 }
327 }
328
329 % --------------------------------------------------------------------------
330 % Unicode Greek normalization to internal symbols.
331 % Sentinels: 1 theta, 2 phi, 3 chi, 4 psi, 5 xi, 6 omega, 7 koppa, 8 digamma
332 % --------------------------------------------------------------------------
333 \cs_new_protected:Npn \boustro_normalize_unicode_greek:
334 {
335 % remove common accent marks first
336 \regex_replace_all:nnN { [άὰᾶἀἁἂἃἄἅἆἇᾳᾀᾁᾂᾃᾄᾅᾆᾇᾱᾰ] } { a } \l__boustro_norm_tl
337 \regex_replace_all:nnN { [ΆᾺἈἉἊἋἌἍἎἏᾼ] } { A } \l__boustro_norm_tl
338 \regex_replace_all:nnN { [έὲἐἑἒἓἔἕ] } { e } \l__boustro_norm_tl
339 \regex_replace_all:nnN { [ΈῈἘἙἚἛἜἝ] } { E } \l__boustro_norm_tl
340 \regex_replace_all:nnN { [ήὴῆἠἡἢἣἤἥἦἧῃᾐᾑᾒᾓᾔᾕᾖᾗ] } { h } \l__boustro_norm_tl
341 \regex_replace_all:nnN { [ΉῊἨἩἪἫἬἭἮἯῌ] } { H } \l__boustro_norm_tl
342 \regex_replace_all:nnN { [ίὶῖϊῒῗἰἱἲἳἴἵἶἷ] } { i } \l__boustro_norm_tl
343 \regex_replace_all:nnN { [ΊῚΪἸἹἺἻἼἽἾἿ] } { I } \l__boustro_norm_tl
344 \regex_replace_all:nnN { [όὸὀὁὂὃὄὅ] } { o } \l__boustro_norm_tl
345 \regex_replace_all:nnN { [ΌῸὈὉὊὋὌὍ] } { O } \l__boustro_norm_tl
346 \regex_replace_all:nnN { [ύὺῦϋῢῧὐὑὒὓὔὕὖὗ] } { u } \l__boustro_norm_tl
347 \regex_replace_all:nnN { [ΎῪΫὙὛὝὟ] } { U } \l__boustro_norm_tl
348 \regex_replace_all:nnN { [ώὼῶὠὡὢὣὤὥὦὧῳᾠᾡᾢᾣᾤᾥᾦᾧ] } { 6 } \l__boustro_norm_tl
349 \regex_replace_all:nnN { [ΏῺὨὩὪὫὬὭὮὯῼ] } { 6 } \l__boustro_norm_tl
350
351 \bool_if:NT \g_boustro_obscure_letters_bool
352 {
353 % Unicode koppa / digamma (if user inputs them directly)
354 \regex_replace_all:nnN { Ϙ|ϙ } { 7 } \l__boustro_norm_tl
355 \regex_replace_all:nnN { Ϝ|ϝ } { 8 } \l__boustro_norm_tl
356 }
357
358 % plain Greek letters
359 \regex_replace_all:nnN { α } { a } \l__boustro_norm_tl
360 \regex_replace_all:nnN { Α } { A } \l__boustro_norm_tl
361 \regex_replace_all:nnN { β } { b } \l__boustro_norm_tl
362 \regex_replace_all:nnN { Β } { B } \l__boustro_norm_tl
363 \regex_replace_all:nnN { γ } { g } \l__boustro_norm_tl
364 \regex_replace_all:nnN { Γ } { G } \l__boustro_norm_tl
365 \regex_replace_all:nnN { δ } { d } \l__boustro_norm_tl
366 \regex_replace_all:nnN { Δ } { D } \l__boustro_norm_tl
367 \regex_replace_all:nnN { ε } { e } \l__boustro_norm_tl
368 \regex_replace_all:nnN { Ε } { E } \l__boustro_norm_tl
369 \regex_replace_all:nnN { ζ } { z } \l__boustro_norm_tl
370 \regex_replace_all:nnN { Ζ } { Z } \l__boustro_norm_tl
371 \regex_replace_all:nnN { η } { h } \l__boustro_norm_tl
372 \regex_replace_all:nnN { Η } { H } \l__boustro_norm_tl
373 \regex_replace_all:nnN { θ } { 1 } \l__boustro_norm_tl
374 \regex_replace_all:nnN { Θ } { 1 } \l__boustro_norm_tl
375 \regex_replace_all:nnN { ι|ϊ|ΐ } { i } \l__boustro_norm_tl
376 \regex_replace_all:nnN { Ι|Ϊ } { I } \l__boustro_norm_tl
377 \regex_replace_all:nnN { κ } { k } \l__boustro_norm_tl
378 \regex_replace_all:nnN { Κ } { K } \l__boustro_norm_tl
379 \regex_replace_all:nnN { λ } { l } \l__boustro_norm_tl
380 \regex_replace_all:nnN { Λ } { L } \l__boustro_norm_tl
381 \regex_replace_all:nnN { μ } { m } \l__boustro_norm_tl
382 \regex_replace_all:nnN { Μ } { M } \l__boustro_norm_tl
383 \regex_replace_all:nnN { ν } { n } \l__boustro_norm_tl
384 \regex_replace_all:nnN { Ν } { N } \l__boustro_norm_tl
385 \regex_replace_all:nnN { ξ } { 5 } \l__boustro_norm_tl
386 \regex_replace_all:nnN { Ξ } { 5 } \l__boustro_norm_tl
387 \regex_replace_all:nnN { ο } { o } \l__boustro_norm_tl
388 \regex_replace_all:nnN { Ο } { O } \l__boustro_norm_tl
389 \regex_replace_all:nnN { π } { p } \l__boustro_norm_tl
390 \regex_replace_all:nnN { Π } { P } \l__boustro_norm_tl
391 \regex_replace_all:nnN { ρ } { r } \l__boustro_norm_tl
392 \regex_replace_all:nnN { Ρ } { R } \l__boustro_norm_tl
393 \regex_replace_all:nnN { σ|ς } { s } \l__boustro_norm_tl
394 \regex_replace_all:nnN { Σ } { S } \l__boustro_norm_tl
395 \regex_replace_all:nnN { τ } { t } \l__boustro_norm_tl
396 \regex_replace_all:nnN { Τ } { T } \l__boustro_norm_tl
397 \regex_replace_all:nnN { υ|ϋ|ΰ } { u } \l__boustro_norm_tl
398 \regex_replace_all:nnN { Υ|Ϋ } { U } \l__boustro_norm_tl
399 \regex_replace_all:nnN { φ } { 2 } \l__boustro_norm_tl
400 \regex_replace_all:nnN { Φ } { 2 } \l__boustro_norm_tl
401 \regex_replace_all:nnN { χ } { 3 } \l__boustro_norm_tl
402 \regex_replace_all:nnN { Χ } { 3 } \l__boustro_norm_tl
403 \regex_replace_all:nnN { ψ } { 4 } \l__boustro_norm_tl
404 \regex_replace_all:nnN { Ψ } { 4 } \l__boustro_norm_tl
405 \regex_replace_all:nnN { ω } { 6 } \l__boustro_norm_tl
406 \regex_replace_all:nnN { Ω } { 6 } \l__boustro_norm_tl
407 }
408
409 \cs_new_protected:Npn \boustro_normalize_input:n #1
410 {
411 \tl_set:Nn \l__boustro_norm_tl {#1}
412 \regex_match:nnTF { [\x{0370}-\x{03FF}\x{1F00}-\x{1FFF}] } {#1}
413 { \bool_set_true:N \l__boustro_input_has_greek_bool }
414 { \bool_set_false:N \l__boustro_input_has_greek_bool }
415 \boustro_normalize_unicode_greek:
416
417 \bool_if:NF \l__boustro_input_has_greek_bool
418 {
419 \regex_replace_all:nnN { J } { I } \l__boustro_norm_tl
420 \regex_replace_all:nnN { j } { i } \l__boustro_norm_tl
421 \regex_replace_all:nnN { Ō|ō|Ô|ô } { 6 } \l__boustro_norm_tl
422 \bool_if:NT \g_boustro_obscure_letters_bool
423 {
424 \regex_replace_all:nnN { TH|Th|tH|th } { 1 } \l__boustro_norm_tl
425 \regex_replace_all:nnN { PH|Ph|pH|ph } { 2 } \l__boustro_norm_tl
426 \regex_replace_all:nnN { CH|Ch|cH|ch|KH|Kh|kH|kh } { 3 } \l__boustro_norm_tl
427 \regex_replace_all:nnN { PS|Ps|pS|ps } { 4 } \l__boustro_norm_tl
428 \regex_replace_all:nnN { KS|Ks|kS|ks } { 5 } \l__boustro_norm_tl
429 \regex_replace_all:nnN { KA } { 7A } \l__boustro_norm_tl
430 \regex_replace_all:nnN { Ka } { 7a } \l__boustro_norm_tl
431 \regex_replace_all:nnN { kA } { 7A } \l__boustro_norm_tl
432 \regex_replace_all:nnN { ka } { 7a } \l__boustro_norm_tl
433 \regex_replace_all:nnN { KO } { 7O } \l__boustro_norm_tl
434 \regex_replace_all:nnN { Ko } { 7o } \l__boustro_norm_tl
435 \regex_replace_all:nnN { kO } { 7O } \l__boustro_norm_tl
436 \regex_replace_all:nnN { ko } { 7o } \l__boustro_norm_tl
437 \regex_replace_all:nnN { W } { 8 } \l__boustro_norm_tl
438 \regex_replace_all:nnN { w } { 8 } \l__boustro_norm_tl
439 }
440 }
441
442 \regex_replace_all:nnN { [.,;:!?] } { ~ } \l__boustro_norm_tl
443 \regex_replace_all:nnN { [-()\[\]"'] } { ~ } \l__boustro_norm_tl
444 \regex_replace_all:nnN { [^A-Za-z0-9~ ] } { } \l__boustro_norm_tl
445 % Normalize any remaining whitespace to a visible/measureable separator.
446 % Using `~` lets us map it to an explicit `\space` during rendering so it
447 % isn't lost inside macro expansions (important for width-based wrapping).
448 \regex_replace_all:nnN { \s+ } { ~ } \l__boustro_norm_tl
449 \regex_replace_all:nnN { ~+ } { ~ } \l__boustro_norm_tl
450 }
451
452 \cs_new_protected:Npn \boustro_line_to_seq:n #1
453 {
454 \seq_clear:N \l__boustro_symbols_seq
455 \boustro_normalize_input:n {#1}
456 % Treat certain digraph encodings as atomic tokens (e.g. 7A/7O for koppa+vowel)
457 % so reversal doesn't split them into separate characters.
458 \tl_set_eq:NN \l__boustro_scan_tl \l__boustro_norm_tl
459 \boustro_scan_norm_into_symbols:
460 }
461
462 \cs_new_protected:Npn \boustro_scan_norm_into_symbols:
463 {
464 \tl_if_empty:NTF \l__boustro_scan_tl
465 { }
466 {
467 \tl_set:Nx \l__boustro_head_tl { \tl_head:N \l__boustro_scan_tl }
468 \tl_set:Nx \l__boustro_scan_tail_tl { \tl_tail:N \l__boustro_scan_tl }
469
470 \tl_if_empty:NTF \l__boustro_scan_tail_tl
471 {
472 \seq_put_right:NV \l__boustro_symbols_seq \l__boustro_head_tl
473 \tl_clear:N \l__boustro_scan_tl
474 }
475 {
476 \tl_set:Nx \l__boustro_next_tl { \tl_head:N \l__boustro_scan_tail_tl }
477 \tl_if_eq:NnTF \l__boustro_head_tl { 7 }
478 {
479 \str_case:nnF { \l__boustro_next_tl }
480 {
481 {A}{ \tl_set:Nn \l__boustro_combined_tl { 7A } }
482 {a}{ \tl_set:Nn \l__boustro_combined_tl { 7a } }
483 {O}{ \tl_set:Nn \l__boustro_combined_tl { 7O } }
484 {o}{ \tl_set:Nn \l__boustro_combined_tl { 7o } }
485 }
486 { \tl_clear:N \l__boustro_combined_tl }
487
488 \tl_if_blank:VTF \l__boustro_combined_tl
489 {
490 \seq_put_right:NV \l__boustro_symbols_seq \l__boustro_head_tl
491 \tl_set_eq:NN \l__boustro_scan_tl \l__boustro_scan_tail_tl
492 }
493 {
494 \seq_put_right:NV \l__boustro_symbols_seq \l__boustro_combined_tl
495 \tl_set:Nx \l__boustro_scan_tl { \tl_tail:N \l__boustro_scan_tail_tl }
496 }
497 }
498 {
499 \seq_put_right:NV \l__boustro_symbols_seq \l__boustro_head_tl
500 \tl_set_eq:NN \l__boustro_scan_tl \l__boustro_scan_tail_tl
501 }
502 }
503 \boustro_scan_norm_into_symbols:
504 }
505 }
506
507 % --------------------------------------------------------------------------
508 % Glyph mapping helpers
509 % --------------------------------------------------------------------------
510 \cs_new:Npn \boustro_archaic_map:n #1
511 {
512 \str_case:nnF {#1}
513 {
514 {~}{\space}
515 {A}{\Aalpha}{a}{\Aalpha}
516 {B}{\Abeta}{b}{\Abeta}
517 {G}{\Agamma}{g}{\Agamma}
518 {D}{\Adelta}{d}{\Adelta}
519 {E}{\Aepsilon}{e}{\Aepsilon}
520 {Z}{\Azeta}{z}{\Azeta}
521 {H}{\Aeta}{h}{\Aeta}
522 {1}{\Atheta}
523 {I}{\Aiota}{i}{\Aiota}
524 {K}{\Akappa}{k}{\Akappa}
525 {7}{\Akoppa}
526 {7A}{\Akoppa\Aalpha}{7a}{\Akoppa\Aalpha}
527 {7O}{\Akoppa\Aomicron}{7o}{\Akoppa\Aomicron}
528 {L}{\Alambda}{l}{\Alambda}
529 {M}{\Amu}{m}{\Amu}
530 {N}{\Anu}{n}{\Anu}
531 {X}{\Axi}{x}{\Axi}{5}{\Axi}
532 {O}{\Aomicron}{o}{\Aomicron}
533 {6}{\Aomega}
534 {P}{\Api}{p}{\Api}
535 {R}{\Arho}{r}{\Arho}
536 {S}{\Asigma}{s}{\Asigma}
537 {T}{\Atau}{t}{\Atau}
538 {U}{\Aupsilon}{u}{\Aupsilon}
539 {Y}{\Aupsilon}{y}{\Aupsilon}
540 {3}{\Achi}
541 {2}{\Aphi}
542 {4}{\Apsi}
543 {8}{\Adigamma}
544 }
545 {#1}
546 }
547
548 \cs_new:Npn \boustro_classical_normal_map:n #1
549 {
550 \str_case:nnF {#1}
551 {
552 {~}{\space}
553 {A}{\Aalpha}{a}{\Aalpha}
554 {B}{\Abeta}{b}{\Abeta}
555 {G}{\Agamma}{g}{\Agamma}
556 {D}{\Adelta}{d}{\Adelta}
557 {E}{\Aepsilon}{e}{\Aepsilon}
558 {Z}{\Azeta}{z}{\Azeta}
559 {H}{\Aeta}{h}{\Aeta}
560 {1}{\Atheta}
561 {I}{\Aiota}{i}{\Aiota}
562 {K}{\Akappa}{k}{\Akappa}
563 {L}{\Alambda}{l}{\Alambda}
564 {M}{\Amu}{m}{\Amu}
565 {N}{\Anu}{n}{\Anu}
566 {X}{\Axi}{x}{\Axi}{5}{\Axi}
567 {O}{\Aomicron}{o}{\Aomicron}
568 {6}{\Aomega}
569 {P}{\Api}{p}{\Api}
570 {R}{\Arho}{r}{\Arho}
571 {S}{\Asigma}{s}{\Asigma}
572 {T}{\Atau}{t}{\Atau}
573 {U}{\Aupsilon}{u}{\Aupsilon}
574 {Y}{\Aupsilon}{y}{\Aupsilon}
575 {3}{\Achi}
576 {2}{\Aphi}
577 {4}{\Apsi}
578 }
579 {
580 \str_if_eq:nnTF {#1}{7A}
581 { \textgvibc{\Akoppa}\Aalpha }
582 {
583 \str_if_eq:nnTF {#1}{7a}
584 { \textgvibc{\Akoppa}\Aalpha }
585 {
586 \str_if_eq:nnTF {#1}{7O}
587 { \textgvibc{\Akoppa}\Aomicron }
588 {
589 \str_if_eq:nnTF {#1}{7o}
590 { \textgvibc{\Akoppa}\Aomicron }
591 {
592 \str_if_eq:nnTF {#1}{7}
593 {\textgvibc{\Akoppa}}
594 {
595 \str_if_eq:nnTF {#1}{8}
596 {\textgvibc{\Adigamma}}
597 {#1}
598 }
599 }
600 }
601 }
602 }
603 }
604 }
605
606 \cs_new:Npn \boustro_classical_reverse_map:n #1
607 {
608 \str_case:nnF {#1}
609 {
610 {~}{\space}
611 {A}{\ARalpha}{a}{\ARalpha}
612 {B}{\ARbeta}{b}{\ARbeta}
613 {G}{\ARgamma}{g}{\ARgamma}
614 {D}{\ARdelta}{d}{\ARdelta}
615 {E}{\ARepsilon}{e}{\ARepsilon}
616 {Z}{\ARzeta}{z}{\ARzeta}
617 {H}{\AReta}{h}{\AReta}
618 {1}{\ARtheta}
619 {I}{\ARiota}{i}{\ARiota}
620 {K}{\ARkappa}{k}{\ARkappa}
621 {L}{\ARlambda}{l}{\ARlambda}
622 {M}{\ARmu}{m}{\ARmu}
623 {N}{\ARnu}{n}{\ARnu}
624 {X}{\ARxi}{x}{\ARxi}{5}{\ARxi}
625 {O}{\ARomicron}{o}{\ARomicron}
626 {6}{\ARomega}
627 {P}{\ARpi}{p}{\ARpi}
628 {R}{\ARrho}{r}{\ARrho}
629 {S}{\ARsigma}{s}{\ARsigma}
630 {T}{\ARtau}{t}{\ARtau}
631 {U}{\ARupsilon}{u}{\ARupsilon}
632 {Y}{\ARupsilon}{y}{\ARupsilon}
633 {3}{\ARchi}
634 {2}{\ARphi}
635 {4}{\ARpsi}
636 }
637 {
638 \str_if_eq:nnTF {#1}{7A}
639 { \textgvibc{\Akoppa}\ARalpha }
640 {
641 \str_if_eq:nnTF {#1}{7a}
642 { \textgvibc{\Akoppa}\ARalpha }
643 {
644 \str_if_eq:nnTF {#1}{7O}
645 { \textgvibc{\Akoppa}\ARomicron }
646 {
647 \str_if_eq:nnTF {#1}{7o}
648 { \textgvibc{\Akoppa}\ARomicron }
649 {
650 \str_if_eq:nnTF {#1}{7}
651 {\textgvibc{\Akoppa}}
652 {
653 \str_if_eq:nnTF {#1}{8}
654 {\textgvibc{\Adigamma}}
655 {#1}
656 }
657 }
658 }
659 }
660 }
661 }
662 }
663
664 \cs_new_protected:Npn \boustro_render_archaic_forward:
665 { {\textgvibc{\seq_map_inline:Nn \l__boustro_symbols_seq {\boustro_archaic_map:n{##1}}}} }
666 \cs_new_protected:Npn \boustro_render_archaic_reverse:
667 {
668 \seq_set_eq:NN \l__boustro_tmp_seq \l__boustro_symbols_seq
669 \seq_reverse:N \l__boustro_tmp_seq
670 {
671 \textgvibc
672 {
673 \seq_map_inline:Nn \l__boustro_tmp_seq
674 {
675 \str_if_eq:nnTF {##1} {~}
676 { \space }
677 { \reflectbox { \boustro_archaic_map:n {##1} } }
678 }
679 }
680 }
681 }
682 \cs_new_protected:Npn \boustro_render_classical_forward:
683 { {\textgivbc{\seq_map_inline:Nn \l__boustro_symbols_seq {\boustro_classical_normal_map:n{##1}}}} }
684 \cs_new_protected:Npn \boustro_render_classical_reverse:
685 {
686 \seq_set_eq:NN \l__boustro_tmp_seq \l__boustro_symbols_seq
687 \seq_reverse:N \l__boustro_tmp_seq
688 {
689 \textgivbc
690 {
691 \seq_map_inline:Nn \l__boustro_tmp_seq
692 {
693 \str_if_eq:nnTF {##1} {~}
694 { \space }
695 { \reflectbox { \boustro_classical_normal_map:n {##1} } }
696 }
697 }
698 }
699 }
700
701 % --------------------------------------------------------------------------
702 % Width measurement and wrapping
703 % --------------------------------------------------------------------------
704 \cs_new_protected:Npn \boustro_measure_any:nN #1#2
705 {
706 \hbox_set:Nn \l__boustro_measure_box {#1}
707 \dim_set:Nn #2 { \box_wd:N \l__boustro_measure_box }
708 }
709
710 \cs_new_protected:Npn \boustro_measure_archaic:nN #1#2
711 {
712 \boustro_line_to_seq:n {#1}
713 \hbox_set:Nn \l__boustro_measure_box { \boustro_render_archaic_forward: }
714 \dim_set:Nn #2 { \box_wd:N \l__boustro_measure_box }
715 }
716
717 \cs_new_protected:Npn \boustro_measure_classical:nN #1#2
718 {
719 \boustro_line_to_seq:n {#1}
720 \hbox_set:Nn \l__boustro_measure_box { \boustro_render_classical_forward: }
721 \dim_set:Nn #2 { \box_wd:N \l__boustro_measure_box }
722 }
723 \cs_generate_variant:Nn \boustro_measure_any:nN { VN }
724 \cs_generate_variant:Nn \boustro_measure_archaic:nN { VN }
725 \cs_generate_variant:Nn \boustro_measure_classical:nN { VN }
726
727 \cs_new:Npn \boustro_map_string_archaic:n #1
728 {
729 \boustro_line_to_seq:n {#1}
730 \seq_map_inline:Nn \l__boustro_symbols_seq { \boustro_archaic_map:n{##1} }
731 }
732
733 \cs_new:Npn \boustro_map_string_classical:n #1
734 {
735 \boustro_line_to_seq:n {#1}
736 \seq_map_inline:Nn \l__boustro_symbols_seq { \boustro_classical_normal_map:n{##1} }
737 }
738
739 \cs_new_protected:Npn \boustro_wrap_text:nnN #1#2#3
740 {
741 \seq_clear:N #3
742 \boustro_set_target_width:n {#1}
743 \tl_set:Nx \l__boustro_words_tl { \tl_to_str:n {#2} }
744 \regex_replace_all:nnN { \s+ } { <BSPACE> } \l__boustro_words_tl
745 \seq_set_split:NnV \l__boustro_words_seq { <BSPACE> } \l__boustro_words_tl
746 \seq_remove_all:Nn \l__boustro_words_seq { }
747 \tl_clear:N \l__boustro_current_line_tl
748 \seq_map_inline:Nn \l__boustro_words_seq
749 {
750 \tl_if_blank:VTF \l__boustro_current_line_tl
751 { \tl_set:Nn \l__boustro_candidate_tl {##1} }
752 { \tl_set:Ne \l__boustro_candidate_tl { \l__boustro_current_line_tl \c_space_tl ##1 } }
753 \use:c { boustro_measure_#1:VN } \l__boustro_candidate_tl \l__boustro_width_dim
754 \dim_compare:nNnTF { \l__boustro_width_dim } > { \l__boustro_target_dim }
755 {
756 \tl_if_blank:VTF \l__boustro_current_line_tl
757 { \seq_put_right:Nn #3 {##1} }
758 {
759 \seq_put_right:NV #3 \l__boustro_current_line_tl
760 \tl_set:Nn \l__boustro_current_line_tl {##1}
761 }
762 }
763 { \tl_set:NV \l__boustro_current_line_tl \l__boustro_candidate_tl }
764 }
765 \tl_if_blank:VF \l__boustro_current_line_tl
766 { \seq_put_right:NV #3 \l__boustro_current_line_tl }
767 }
768
769 \cs_new_protected:Npn \boustro_wrap_epigraphic_text:nnN #1#2#3
770 {
771 \seq_clear:N #3
772 \boustro_normalize_input:n {#2}
773 \tl_set_eq:NN \l__boustro_words_tl \l__boustro_norm_tl
774 \regex_replace_all:nnN { \s+ } { <BSPACE> } \l__boustro_words_tl
775 \seq_set_split:NnV \l__boustro_words_seq { <BSPACE> } \l__boustro_words_tl
776 \seq_remove_all:Nn \l__boustro_words_seq { }
777 \boustro_set_target_width:n {#1}
778 \tl_clear:N \l__boustro_current_line_tl
779 \seq_map_inline:Nn \l__boustro_words_seq
780 {
781 \tl_if_blank:VTF \l__boustro_current_line_tl
782 { \tl_set:Nn \l__boustro_candidate_tl {##1} }
783 { \tl_set:Ne \l__boustro_candidate_tl { \l__boustro_current_line_tl \c_space_tl ##1 } }
784 \use:c { boustro_measure_#1:VN } \l__boustro_candidate_tl \l__boustro_width_dim
785 \dim_compare:nNnTF { \l__boustro_width_dim } > { \l__boustro_target_dim }
786 {
787 \tl_if_blank:VTF \l__boustro_current_line_tl
788 { \seq_put_right:Nn #3 {##1} }
789 {
790 \seq_put_right:NV #3 \l__boustro_current_line_tl
791 \tl_set:Nn \l__boustro_current_line_tl {##1}
792 }
793 }
794 { \tl_set:NV \l__boustro_current_line_tl \l__boustro_candidate_tl }
795 }
796 \tl_if_blank:VF \l__boustro_current_line_tl
797 { \seq_put_right:NV #3 \l__boustro_current_line_tl }
798 }
799
800 \cs_new_protected:Npn \boustro_prepare_lines:nn #1#2
801 {
802 \seq_clear:N \l__boustro_lines_seq
803 \tl_set:Nn \l__boustro_input_tl {#2}
804 \tl_if_in:NnTF \l__boustro_input_tl {\\}
805 {
806 \boustro_split_explicit_lines:n {#2}
807 \seq_map_inline:Nn \l__boustro_pars_seq
808 {
809 \boustro_wrap_text:nnN {#1} {##1} \l__boustro_tmp_seq
810 \seq_map_inline:Nn \l__boustro_tmp_seq
811 { \seq_put_right:Nn \l__boustro_lines_seq {####1} }
812 }
813 }
814 {
815 \str_case:nnF {#1}
816 {
817 {any}{\boustro_wrap_text:nnN {any} {#2} \l__boustro_lines_seq}
818 {archaic}{\boustro_wrap_text:nnN {archaic} {#2} \l__boustro_lines_seq}
819 {classical}{\boustro_wrap_text:nnN {classical} {#2} \l__boustro_lines_seq}
820 }
821 { \boustro_wrap_text:nnN {#1} {#2} \l__boustro_lines_seq }
822 }
823 }
824
825 % --------------------------------------------------------------------------
826 % Printing line by line
827 % --------------------------------------------------------------------------
828 \cs_new_protected:Npn \boustro_print_any_line:nn #1#2
829 {
830 \par\noindent
831 \bool_if:nTF { \boustro_line_is_reverse_n:n {#1} }
832 { \makebox[\linewidth][c]{\makebox[\l__boustro_target_dim][r]{\reflectbox{#2}}}\par }
833 { \makebox[\linewidth][c]{\makebox[\l__boustro_target_dim][l]{#2}}\par }
834 }
835
836 \cs_new_protected:Npn \boustro_print_archaic_line:nn #1#2
837 {
838 \par\noindent
839 \boustro_line_to_seq:n {#2}
840 \bool_if:nTF { \boustro_line_is_reverse_n:n {#1} }
841 { \makebox[\linewidth][c]{\makebox[\l__boustro_target_dim][r]{\boustro_render_archaic_reverse:}}\par }
842 { \makebox[\linewidth][c]{\makebox[\l__boustro_target_dim][l]{\boustro_render_archaic_forward:}}\par }
843 }
844
845 \cs_new_protected:Npn \boustro_print_classical_line:nn #1#2
846 {
847 \par\noindent
848 \boustro_line_to_seq:n {#2}
849 \bool_if:nTF { \boustro_line_is_reverse_n:n {#1} }
850 {
851 \makebox[\linewidth][c]{\makebox[\l__boustro_target_dim][r]{\boustro_render_classical_reverse:}}\par
852 }
853 {
854 \makebox[\linewidth][c]{\makebox[\l__boustro_target_dim][l]{\boustro_render_classical_forward:}}\par
855 }
856 }
857
858 % --------------------------------------------------------------------------
859 % Public typesetter (mode, text)
860 % --------------------------------------------------------------------------
861 \cs_new_protected:Npn \boustro_typeset_block:nn #1#2
862 {
863 \boustro_maybe_reset:
864 \boustro_set_target_width:n {#1}
865 \boustro_prepare_lines:nn {#1} {#2}
866 \int_set:Nn \l_tmpa_int { \g_boustro_line_int }
867 \str_case:nnF {#1}
868 {
869 {any}
870 {
871 \seq_map_indexed_inline:Nn \l__boustro_lines_seq
872 { \boustro_print_any_line:nn { \int_eval:n { \l_tmpa_int + ##1 } } {##2} }
873 }
874 {archaic}
875 {
876 \seq_map_indexed_inline:Nn \l__boustro_lines_seq
877 { \boustro_print_archaic_line:nn { \int_eval:n { \l_tmpa_int + ##1 } } {##2} }
878 }
879 {classical}
880 {
881 \seq_map_indexed_inline:Nn \l__boustro_lines_seq
882 { \boustro_print_classical_line:nn { \int_eval:n { \l_tmpa_int + ##1 } } {##2} }
883 }
884 }{}
885 \int_gadd:Nn \g_boustro_line_int { \seq_count:N \l__boustro_lines_seq }
886 \par
887 }
888
889 % --------------------------------------------------------------------------
890 % Inline markup: normal text outside [ ... ], boustrophedon only inside.
891 % The square brackets are markup delimiters and are not printed.
892 % --------------------------------------------------------------------------
893 \cs_new_protected:Npn \boustro_render_inline:nn #1#2
894 {
895 \group_begin:
896 \str_case:VnF \l__boustro_inline_mode_tl
897 {
898 {any}{ \reflectbox {#2} }
899 {archaic}
900 {
901 \boustro_line_to_seq:n {#2}
902 \boustro_render_archaic_reverse:
903 }
904 {classical}
905 {
906 \boustro_line_to_seq:n {#2}
907 \boustro_render_classical_reverse:
908 }
909 }
910 { \reflectbox {#2} }
911 \group_end:
912 }
913
914 \cs_new_protected:Npn \boustro_typeset_inline:nn #1#2
915 {
916 \tl_set:Nn \l__boustro_inline_mode_tl {#1}
917 \boustro_inline_parse:w #2 [ \q_no_value ] \q_stop
918 }
919
920 \cs_new_protected:Npn \boustro_inline_parse:w #1[#2]#3 \q_stop
921 {
922 #1
923 \quark_if_no_value:nF {#2}
924 {
925 \boustro_render_inline:nn { \l__boustro_inline_mode_tl } {#2}
926 \boustro_inline_parse:w #3 \q_stop
927 }
928 }
929
930 % --------------------------------------------------------------------------
931 % Public API (single command)
932 % --------------------------------------------------------------------------
933 \NewDocumentCommand{\boustrophedon}{ O{} +m }
934 {
935 \group_begin:
936 \keys_set:nn { boustro } {#1}
937 \bool_if:NTF \g_boustro_inline_bool
938 { \exp_args:Nx \boustro_typeset_inline:nn { \tl_use:N \g_boustro_type_tl } {#2} }
939 { \exp_args:Nx \boustro_typeset_block:nn { \tl_use:N \g_boustro_type_tl } {#2} }
940 \group_end:
941 }
942
943 % Convenience aliases (v0.76 compatibility)
944 \NewDocumentCommand{\boustrophedonAny}{+m}
945 { \boustrophedon[Type=Any]{#1} }
946 \NewDocumentCommand{\boustrophedonArchaic}{+m}
947 { \boustrophedon[Type=Archaic]{#1} }
948 \NewDocumentCommand{\boustrophedonClassical}{+m}
949 { \boustrophedon[Type=Classical]{#1} }
950
951 % Block style API with start/stop commands (single command, optional [keys])
952 \makeatletter
953 \def\startBoustrophedon{%
954 \@ifnextchar[{\boustro_start_with_keys@}{\boustro_start_with_keys@[]}%
955 }
956 \long\def\boustro_start_with_keys@[#1]#2\stopBoustrophedon{%
957 \boustrophedon[#1]{#2}%
958 }
959 \makeatother
960
961 % --------------------------------------------------------------------------
962 % Block style API with start/stop commands
963 % --------------------------------------------------------------------------
964 \long\def\startBoustrophedonAny#1\stopBoustrophedonAny{\boustrophedonAny{#1}}
965 \long\def\startBoustrophedonArchaic#1\stopBoustrophedonArchaic{\boustrophedonArchaic{#1}}
966 \long\def\startBoustrophedonClassical#1\stopBoustrophedonClassical{\boustrophedonClassical{#1}}
967
968 % --------------------------------------------------------------------------
969 % Engine notice
970 % --------------------------------------------------------------------------
971 \AtBeginDocument{
972 \ifPDFTeX
973 \PackageWarningNoLine{boustrophedon}{Greek Unicode input works best with XeLaTeX or LuaLaTeX; transliteration input is still fine in pdfLaTeX}
974 \fi
975 }
976
977 \ExplSyntaxOff
978
979 %</package>
980 % \end{macrocode}
981 % \Finale