typo-rub.mkxl /size: 5279 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=typo-rub,
3%D        version=2016.10.10,
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Rubies,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D Experimental and unfinished.
15
16% todo: distance
17
18\writestatus{loading}{ConTeXt Typesetting Macros / Rubies}
19
20\unprotect
21
22\registerctxluafile{typo-rub}{autosuffix}
23
24\definesystemattribute[ruby][public]
25
26\installcorenamespace {ruby}
27\installcorenamespace {rubyanalyze}
28\installcorenamespace {rubyplacement}
29\installcorenamespace {rubynumber}
30
31\installcommandhandler \??ruby {ruby} \??ruby
32
33% \appendtoks
34%     \clf_defineruby
35%         align   {\rubyparameter\c!align}
36%         stretch {\rubyparameter\c!stretch}
37%         hoffset  \rubyparameter\c!hoffset
38%         voffset  \rubyparameter\c!voffset
39%     \relax
40% \to \everydefineruby
41
42\permanent\tolerant\protected\def\ruby[#1]#:#2#3%
43  {\dontleavehmode
44   \begingroup
45   \enforced\let\ruby\noruby
46   \cdef\currentruby{#1}%
47   \edef\p_location{\rubyparameter\c!location}%
48 % \enforced\let|\relax
49   \enforced\letcharcode\barasciicode\relax
50   \ifcsname\??rubyanalyze\p_location\endcsname
51     \expandafter\lastnamedcs\else\expandafter\typo_ruby_analyze
52   \fi{#2}{#3}%
53   \endgroup}
54
55\permanent\tolerant\protected\def\noruby[#1]#:#2#3%
56  {#2}
57
58\def\typo_ruby_analyze#1#2%
59  {\clf_ruby
60     base    {#1}%
61     comment {#2}
62   \relax}
63
64\defcsname\??rubyanalyze\v!top\endcsname#1#2%
65  {\clf_ruby
66     align   {\rubyparameter\c!align}%
67     stretch {\rubyparameter\c!stretch}%
68     hoffset  \rubyparameter\c!hoffset
69     voffset  \rubyparameter\c!voffset
70     base    {#1}%
71     comment {#2}
72   \relax}
73
74\defcsname\??rubyplacement\v!top\endcsname#1#2%
75  {\setbox\scratchbox\hbox\bgroup
76     \userubystyleandcolor\c!style\c!color
77     #2%
78   \egroup
79   \clf_setruby \scratchbox
80   \relax
81   #1}
82
83\defcsname\??rubyplacement\v!right\endcsname#1#2%
84  {#1%
85   \edef\p_distance{\rubyparameter\c!distance}%
86   \ifempty\p_distance\orelse\ifx\p_distance\v!none\else\hskip\p_distance\fi
87   \begingroup
88   \userubystyleandcolor\c!style\c!color
89   \rubyparameter\c!left#2\rubyparameter\c!right
90   \endgroup}
91
92\defcsname\??rubyplacement\v!left\endcsname#1#2%
93  {\begingroup
94   \userubystyleandcolor\c!style\c!color
95   \rubyparameter\c!left#2\rubyparameter\c!right
96   \endgroup
97   \edef\p_distance{\rubyparameter\c!distance}%
98   \ifempty\p_distance\orelse\ifx\p_distance\v!none\else\hskip\p_distance\fi
99   #1}
100
101\permanent\protected\def\setruby#1#2%
102  {\begingroup
103   \ifcsname\??rubyplacement\p_location\endcsname
104     \lastnamedcs{#1}{#2}%
105   \else
106     #1%
107   \fi
108   \endgroup}
109
110\permanent\tolerant\protected\def\startruby[#1]%
111  {\dontleavehmode
112   \begingroup
113   \enforced\aliased\startruby\begingroup
114   \cdef\currentruby{#1}%
115   \clf_startruby
116        align   {\rubyparameter\c!align}%
117        stretch {\rubyparameter\c!stretch}%
118        hoffset  \rubyparameter\c!hoffset
119        voffset  \rubyparameter\c!voffset
120   \relax}
121
122\permanent\protected\def\stopruby
123  {\endgroup}
124
125\setupruby
126  [\c!style=\txx,
127   \c!location=\v!top,
128   \c!left=(,
129   \c!right=),
130   \c!distance=\zeropoint, % \v!none means no skip at all so no break either
131   \c!hoffset=\zeropoint,
132   \c!voffset=-2\exheight]
133
134%D Experiment too:
135
136\permanent\protected\def\numberedruby[#1]#2%
137  {\doifnotcounter{\??rubynumber#1}{\definecounter[\??rubynumber#1]\setcounter[\??rubynumber#1][1]}%
138   \ruby[#1]{#2}{\convertedcounter[\??rubynumber#1]}%
139   \incrementcounter[\??rubynumber#1]%
140   \relax}
141
142\protect \endinput
143
144% \usemodule[art-01]\setupbodyfont[dejavu,12pt]
145%
146% \defineruby[auto]  [align=auto,color=darkred]
147% \defineruby[left]  [align=flushleft,color=darkred]
148% \defineruby[right] [align=flushright,color=darkred]
149% \defineruby[spread][stretch=yes]
150%
151% \showframe \showglyphs \showfontkerns \setupinterlinespace[22pt]
152%
153% \starttext
154%
155% \startbuffer
156%     \dorecurse{20}{\ruby{XYZ}{a|bc|d} }\par
157%     \dorecurse{20}{\ruby{PQR}{p|q|r} }\par
158%     \dorecurse{20}{\ruby{XYZ}{1|22|333} }\par
159%     \dorecurse{20}{\ruby{XYZ}{111|222|333} }\par
160%     \dorecurse{20}{\ruby{XYZ}{foobar} }\par
161%     \dorecurse{20}{\ruby{XYZ}{fooledbar} }\par
162%     \dorecurse{20}{\ruby[spread]{XYZ}{fooledbar} }\par
163%     \dorecurse{20}{\ruby{extremely}{wide} }\par
164%     \dorecurse{20}{\ruby{wide}{extremely} }\par
165%  stopbuffer
166%
167% \testfeatureonce{1}{\start \setupinterlinespace[16pt] \setupruby[location=none]                   \getbuffer \stop \page}
168% \testfeatureonce{1}{\start \setupinterlinespace[16pt] \setupruby[location=right]                  \getbuffer \stop \page}
169% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=auto,color=darkred]        \getbuffer \stop \page}
170% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=flushleft,color=darkgreen] \getbuffer \stop \page}
171% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=flushright,color=darkblue] \getbuffer \stop \page}
172% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=middle,color=darkyellow]   \getbuffer \stop \page}
173%
174% \stoptext
175