summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkxl/m-crappyspec.mkxl
blob: 48314114158e28cffd07f10906d31fcbe73c143c (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
%D \module
%D   [       file=m-crappyspec,
%D        version=2022.07.01, % about
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Meeting weird demands,
%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 This module is for Benjamin Buchmuller who has to meet rather weird demands with
%D respect to the number of characters per line. So, it's not about quality but more
%D sort of previous century typewriter specification.
%D
%D \setmainparbuilder[crappyspec]

\startluacode

local type = type

local nuts            = nodes.nuts

local glyph_code      = nodes.nodecodes.glyph
local hlist_code      = nodes.nodecodes.hlist
local vlist_code      = nodes.nodecodes.vlist
local disc_code       = nodes.nodecodes.disc

local line_code       = nodes.listcodes.line

local traverselist    = nuts.traverselist
local traversecontent = nuts.traversecontent

local flushlist       = nuts.flushlist
local copylist        = nuts.copylist
local getcharspec     = nuts.getcharspec
local getreplace      = nuts.getreplace
local getparstate     = nuts.getparstate
local getlist         = nuts.getlist
local getwidth        = nuts.getwidth
local setwidth        = nuts.setwidth

local texset          = tex.set
local texsetdimen     = tex.setdimen
local texgetdimen     = tex.getdimen
local texgetcount     = tex.getcount
local texlinebreak    = tex.linebreak

local characters      = fonts.hashes.characters

local function countglyphs(h)
    local c = 0
    if h then
        for n, t, s in traversecontent(h) do
            if t == glyph_code then
                if true then
                    local g, f = getcharspec(n)
                    local d = characters[f][g] -- maybe cache
                    local u = d and d.unicode
                    if type(u) == "table" then
                       c = c + #u
                    else
                       c = c + 1
                    end
                else
                    c = c + 1
                end
            elseif t == disc_code then
                c = c + countglyphs(getreplace(n),c)
            elseif t == hlist_code or s == vlist_code then
                c = c + countglyphs(getlist(h),c)
            else
                -- leader
            end
        end
    end
    return c
end

local function overflow(h, max)
    for n, t, s in traverselist(h) do
        if t == hlist_code and s == line_code then
            local c = countglyphs(getlist(n))
            if c > max then
                return c, getwidth(n)
            end
        end
    end
    return false
end

function builders.paragraphs.constructors.methods.crappyspec(head)
    -- todo: prevent useless calls
    local p = getparstate(head)
    if p then
        local hsize = p.hsize
        local max   = texgetcount("crappyspeccount")
        local step  = texgetdimen("crappyspecstep")
        if step <= 65536 then
            step = 65536
        end
        while true do
            local n = copylist(head)
            local l, d = texlinebreak(n, { hsize = hsize })
            local c, w = overflow(l,max)
            if w and hsize > 10*step then -- safeguard
                flushlist(l)
                hsize = hsize - step
            else
                texsetdimen("crappyspecdimen",hsize)
                flushlist(head)
                head = l
                local hsize = p.hsize
                for n, t, s in traverselist(head) do
                    if t == hlist_code and s == line_code then
                     -- todo: add delta to rightskip or so
                        setwidth(n,hsize)
                    end
                end
                texset("prevdepth",d.prevdepth)
                break
            end
        end
    end
    return head
end
\stopluacode

\newcount\crappyspeccount
\newdimen\crappyspecstep   \crappyspecstep \onepoint
\newdimen\crappyspecdimen

\defineparbuilder [crappyspec] % implemented in the builder namespace
\defineparbuilder [default]    % implemented in the builder namespace

\continueifinputfile{m-crappyspec.mkxl}

\setupbodyfont
  [pagella]

\setuptolerance
  [verytolerant,stretch]

\showmakeup
  [line]

\setmainparbuilder
  [crappyspec]

\dontcomplain

\protected\def\CrappyTraced
  {\par \strut \rlap \bgroup\infofont
     (\enspace
         max   = \the\crappyspeccount \quad
         step  = \the\crappyspecstep  \quad
         hsize = \the\hsize           \quad
         used  = \the\crappyspecdimen \enspace
     )
   \egroup \par}

\starttext

\crappyspeccount60  \samplefile{tufte} \CrappyTraced \par
\crappyspeccount40  \samplefile{tufte} \CrappyTraced \par
%   \crappyspecstep 2pt \samplefile{tufte} \CrappyTraced \par

\samplefile{tufte} \CrappyTraced
\startitemize
    \startitem
        \samplefile{tufte} \CrappyTraced
    \stopitem
    \startitem
        \samplefile{ward}  \CrappyTraced
    \stopitem
\stopitemize

\startnarrower[6*left,right]
    \samplefile{tufte} \CrappyTraced
\stopnarrower

\starthanging [distance=4em,n=2] {test}
    \samplefile{tufte} \CrappyTraced
\stophanging

\page % \stoptext

\setuppapersize[landscape,letter]

\samplefile{knuth} \CrappyTraced
\startitemize[width=5em]
    \startitem
        \samplefile{knuth} \CrappyTraced
    \stopitem
    \startitem
        {\smallcaps \darkblue \samplefile{knuth}} \CrappyTraced
    \stopitem
\stopitemize
\crappyspeccount60
\startitemize[width=5em]
    \startitem
        \samplefile{knuth} \CrappyTraced
    \stopitem
    \startitem
        {\smallcaps \darkgreen \samplefile{knuth}} \CrappyTraced
    \stopitem
\stopitemize

\page

\crappyspecstep  1pt \crappyspeccount 60 % 5.5
\crappyspecstep  2pt \crappyspeccount 60 % 3.2
\crappyspecstep  5pt \crappyspeccount 60 % 1.4
\crappyspecstep 10pt \crappyspeccount 60 % 0.9

\crappyspecstep  1pt \crappyspeccount 80 % 0.8
\crappyspecstep  2pt \crappyspeccount 80 % 0.8
\crappyspecstep  5pt \crappyspeccount 80 % 0.8
\crappyspecstep 10pt \crappyspeccount 80 % 0.6

% \crappyspecstep  1pt \crappyspeccount 120 % 0.25
% \crappyspecstep  2pt \crappyspeccount 120 % 0.25
% \crappyspecstep  5pt \crappyspeccount 120 % 0.25

% \testfeatureonce{100}{\samplefile{tufte}\par\CrappyTraced} % \page \elapsedtime

\stoptext