summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/typo-rub.mkxl
blob: 8e5d12e7febd27ccab076a5e57a03828072a06f3 (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
%D \module
%D   [       file=typo-rub,
%D        version=2016.10.10,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Rubies,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Experimental and unfinished.

% todo: distance

\writestatus{loading}{ConTeXt Typesetting Macros / Rubies}

\unprotect

\registerctxluafile{typo-rub}{autosuffix}

\definesystemattribute[ruby][public]

\installcorenamespace {ruby}
\installcorenamespace {rubyanalyze}
\installcorenamespace {rubyplacement}
\installcorenamespace {rubynumber}

\installcommandhandler \??ruby {ruby} \??ruby

% \appendtoks
%     \clf_defineruby
%         align   {\rubyparameter\c!align}
%         stretch {\rubyparameter\c!stretch}
%         hoffset  \rubyparameter\c!hoffset
%         voffset  \rubyparameter\c!voffset
%     \relax
% \to \everydefineruby

\permanent\tolerant\protected\def\ruby[#1]#:#2#3%
  {\dontleavehmode
   \begingroup
   \enforced\let\ruby\noruby
   \cdef\currentruby{#1}%
   \edef\p_location{\rubyparameter\c!location}%
 % \enforced\let|\relax
   \enforced\letcharcode\barasciicode\relax
   \ifcsname\??rubyanalyze\p_location\endcsname
     \expandafter\lastnamedcs\else\expandafter\typo_ruby_analyze
   \fi{#2}{#3}%
   \endgroup}

\permanent\tolerant\protected\def\noruby[#1]#:#2#3%
  {#2}

\def\typo_ruby_analyze#1#2%
  {\clf_ruby
     base    {#1}%
     comment {#2}
   \relax}

\defcsname\??rubyanalyze\v!top\endcsname#1#2%
  {\clf_ruby
     align   {\rubyparameter\c!align}%
     stretch {\rubyparameter\c!stretch}%
     hoffset  \rubyparameter\c!hoffset
     voffset  \rubyparameter\c!voffset
     base    {#1}%
     comment {#2}
   \relax}

\defcsname\??rubyplacement\v!top\endcsname#1#2%
  {\setbox\scratchbox\hbox\bgroup
     \userubystyleandcolor\c!style\c!color
     #2%
   \egroup
   \clf_setruby \scratchbox
   \relax
   #1}

\defcsname\??rubyplacement\v!right\endcsname#1#2%
  {#1%
   \edef\p_distance{\rubyparameter\c!distance}%
   \ifempty\p_distance\orelse\ifx\p_distance\v!none\else\hskip\p_distance\fi
   \begingroup
   \userubystyleandcolor\c!style\c!color
   \rubyparameter\c!left#2\rubyparameter\c!right
   \endgroup}

\defcsname\??rubyplacement\v!left\endcsname#1#2%
  {\begingroup
   \userubystyleandcolor\c!style\c!color
   \rubyparameter\c!left#2\rubyparameter\c!right
   \endgroup
   \edef\p_distance{\rubyparameter\c!distance}%
   \ifempty\p_distance\orelse\ifx\p_distance\v!none\else\hskip\p_distance\fi
   #1}

\permanent\protected\def\setruby#1#2%
  {\begingroup
   \ifcsname\??rubyplacement\p_location\endcsname
     \lastnamedcs{#1}{#2}%
   \else
     #1%
   \fi
   \endgroup}

\permanent\tolerant\protected\def\startruby[#1]%
  {\dontleavehmode
   \begingroup
   \enforced\aliased\startruby\begingroup
   \cdef\currentruby{#1}%
   \clf_startruby
        align   {\rubyparameter\c!align}%
        stretch {\rubyparameter\c!stretch}%
        hoffset  \rubyparameter\c!hoffset
        voffset  \rubyparameter\c!voffset
   \relax}

\permanent\protected\def\stopruby
  {\endgroup}

\setupruby
  [\c!style=\txx,
   \c!location=\v!top,
   \c!left=(,
   \c!right=),
   \c!distance=\zeropoint, % \v!none means no skip at all so no break either
   \c!hoffset=\zeropoint,
   \c!voffset=-2\exheight]

%D Experiment too:

\permanent\protected\def\numberedruby[#1]#2%
  {\doifnotcounter{\??rubynumber#1}{\definecounter[\??rubynumber#1]\setcounter[\??rubynumber#1][1]}%
   \ruby[#1]{#2}{\convertedcounter[\??rubynumber#1]}%
   \incrementcounter[\??rubynumber#1]%
   \relax}

\protect \endinput

% \usemodule[art-01]\setupbodyfont[dejavu,12pt]
%
% \defineruby[auto]  [align=auto,color=darkred]
% \defineruby[left]  [align=flushleft,color=darkred]
% \defineruby[right] [align=flushright,color=darkred]
% \defineruby[spread][stretch=yes]
%
% \showframe \showglyphs \showfontkerns \setupinterlinespace[22pt]
%
% \starttext
%
% \startbuffer
%     \dorecurse{20}{\ruby{XYZ}{a|bc|d} }\par
%     \dorecurse{20}{\ruby{PQR}{p|q|r} }\par
%     \dorecurse{20}{\ruby{XYZ}{1|22|333} }\par
%     \dorecurse{20}{\ruby{XYZ}{111|222|333} }\par
%     \dorecurse{20}{\ruby{XYZ}{foobar} }\par
%     \dorecurse{20}{\ruby{XYZ}{fooledbar} }\par
%     \dorecurse{20}{\ruby[spread]{XYZ}{fooledbar} }\par
%     \dorecurse{20}{\ruby{extremely}{wide} }\par
%     \dorecurse{20}{\ruby{wide}{extremely} }\par
%  stopbuffer
%
% \testfeatureonce{1}{\start \setupinterlinespace[16pt] \setupruby[location=none]                   \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[16pt] \setupruby[location=right]                  \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=auto,color=darkred]        \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=flushleft,color=darkgreen] \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=flushright,color=darkblue] \getbuffer \stop \page}
% \testfeatureonce{1}{\start \setupinterlinespace[28pt] \setupruby[align=middle,color=darkyellow]   \getbuffer \stop \page}
%
% \stoptext