| 1 |
\defactiveparameter page {% |
| 2 |
\settovalue\pdfpagewidth #1 : width |
| 3 |
\settovalue\pdfpageheight #1 : height |
| 4 |
\settovalue\baselineskip #1 : baselineskip |
| 5 |
\settovalueor\topskip #1 : topskip {\topskip=\baselineskip}% |
| 6 |
\settovalue\pdfhorigin #1 : left |
| 7 |
\ifattribute #1 : right |
| 8 |
{\hsize = \dimexpr(\pdfpagewidth-\pdfhorigin-\usevalue #1 : right )\relax} |
| 9 |
{\settovalue\hsize #1 : hsize }% |
| 10 |
\settovalue\pdfvorigin #1 : top |
| 11 |
\settovalue\parindent #1 : parindent |
| 12 |
\settovalue\parskip #1 : parskip |
| 13 |
\ifattribute #1 : lines {\vsize=\usevalue #1 : lines \baselineskip}{}% |
| 14 |
} |
| 15 |
|
| 16 |
\restrictparameter page : |
| 17 |
width |
| 18 |
height |
| 19 |
hsize |
| 20 |
baselineskip |
| 21 |
topskip |
| 22 |
left |
| 23 |
right |
| 24 |
top |
| 25 |
lines |
| 26 |
parindent |
| 27 |
parskip |
| 28 |
\par |
| 29 |
|
| 30 |
% Defaults... they don't produce anything beautiful. |
| 31 |
% I redefine them on every job. |
| 32 |
\setparameter page: |
| 33 |
height = 28cm |
| 34 |
width = 21cm |
| 35 |
hsize = 15cm |
| 36 |
baselineskip = 12pt |
| 37 |
lines = 42 |
| 38 |
left = 1in |
| 39 |
top = 1in |
| 40 |
parskip = 0pt |
| 41 |
|
| 42 |
\widowpenalty151 |
| 43 |
\clubpenalty0 |
| 44 |
\holdinginserts1 |
| 45 |
\newdimen\outputsize |
| 46 |
\newif\ifheader |
| 47 |
|
| 48 |
\gates new \OutputRoutine {OutputRoutine}% |
| 49 |
|
| 50 |
\OutputRoutine list {output} |
| 51 |
[precheck] |
| 52 |
{% |
| 53 |
\global\holdinginserts0 |
| 54 |
\OutputRoutine skip {precheck, shipout}{output}% |
| 55 |
\ifnum\outputpenalty<\widowpenalty |
| 56 |
\global\outputsize=\vsize |
| 57 |
\else |
| 58 |
\ifnum\outputpenalty=10000 |
| 59 |
\global\outputsize=\vsize |
| 60 |
\else |
| 61 |
\ifdim\pagegoal<\vsize |
| 62 |
\global\advance\vsize-\baselineskip |
| 63 |
\global\outputsize=\dimexpr\vsize+\baselineskip\relax |
| 64 |
\else |
| 65 |
\global\advance\vsize\baselineskip |
| 66 |
\global\outputsize=\dimexpr\vsize-\baselineskip\relax |
| 67 |
\fi |
| 68 |
\fi |
| 69 |
\fi |
| 70 |
\unvbox\outputbox |
| 71 |
\ifnum\outputpenalty=10000 |
| 72 |
\penalty0 |
| 73 |
\else |
| 74 |
\penalty\outputpenalty |
| 75 |
\fi} |
| 76 |
% |
| 77 |
(shipout) |
| 78 |
. (inserts) |
| 79 |
. . [inserts_figures] ?{conditional = \unless\ifvoid\ptx@insert_figures} {% |
| 80 |
\setbox\outputbox=\vbox to \outputsize{% |
| 81 |
\box\ptx@insert_figures |
| 82 |
\unvbox\outputbox}} |
| 83 |
. . [inserts_footnotes] ?{conditional = \unless\ifvoid\ptx@insert_footnotes} {% |
| 84 |
\setbox\outputbox=\vbox to \outputsize{% |
| 85 |
\unvbox\outputbox |
| 86 |
\vfil |
| 87 |
\vskip.5\skip\ptx@insert_footnotes |
| 88 |
\vbox to0pt{\vss\hrule width .3\hsize}% |
| 89 |
\vskip.5\skip\ptx@insert_footnotes |
| 90 |
\unvbox\ptx@insert_footnotes}} |
| 91 |
% |
| 92 |
. [headers] {% |
| 93 |
\setbox\outputbox=\vbox{% |
| 94 |
\kern-3\baselineskip |
| 95 |
\vbox to3\baselineskip{% |
| 96 |
\hbox to\hsize{\strut\baselineskip {0pt}% |
| 97 |
\normalsize\rm\sc |
| 98 |
\ifodd\pageno |
| 99 |
\hfill\usevalue chapter : inner_title \rlap{\kern1em \the\pageno}\kern-3em |
| 100 |
\else |
| 101 |
\kern-3em\llap{\the\pageno\kern1em }\usevalue chapter : inner_title \hfill |
| 102 |
\fi} |
| 103 |
\vfill} |
| 104 |
\nointerlineskip |
| 105 |
\box\outputbox}} |
| 106 |
% |
| 107 |
. [ship] {% |
| 108 |
\settovalue\pdfhorigin page : left |
| 109 |
\shipout\box\outputbox} |
| 110 |
% |
| 111 |
. [postship] {% |
| 112 |
\global\holdinginserts1 |
| 113 |
\global\vsize\outputsize |
| 114 |
\global\advance\count0 1 |
| 115 |
\ifnum\insertpenalties>0 |
| 116 |
\ifnum\outputpenalty=\clearpagepenalty |
| 117 |
\hbox{}\vfil\penalty\outputpenalty |
| 118 |
\fi |
| 119 |
\fi} |
| 120 |
|
| 121 |
|
| 122 |
\output{% |
| 123 |
\OutputRoutine execute {output}% |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
% MARGIN NOTES |
| 129 |
\setparameter marginnote : |
| 130 |
hsize = 3cm |
| 131 |
hpos = fr |
| 132 |
font = \it |
| 133 |
parindent = 0pt |
| 134 |
side = right |
| 135 |
gap = 1em |
| 136 |
|
| 137 |
\newcount\notecount |
| 138 |
\newattribute\ptx@marginnote_attribute |
| 139 |
\def\marginnote{% |
| 140 |
\ifnext[ |
| 141 |
{\ptx@marginnote} |
| 142 |
{\ptx@marginnote[]}% |
| 143 |
} |
| 144 |
|
| 145 |
\long\def\ptx@marginnote[#1]#2{% |
| 146 |
\setparameterlist{marginnote@temp}{meta = marginnote, #1}% |
| 147 |
\global\advance\notecount 1 |
| 148 |
\global\expandafter\newbox\csname marginnote_\the\notecount\endcsname |
| 149 |
\global\expandafter\setbox\csname marginnote_\the\notecount\endcsname= |
| 150 |
\vtop{% |
| 151 |
\settovalue\hsize marginnote@temp : hsize |
| 152 |
\settovalue{\advance\hsize} marginnote@temp : gap |
| 153 |
\leftskip=0pt \rightskip=0pt plus 1 fil \parfillskip=0pt\relax |
| 154 |
\ifcasevalue marginnote@temp : hpos |
| 155 |
\val ff \rightskip=0pt \parfillskip=0pt plus 1 fil |
| 156 |
\val rf \leftskip=0pt plus 1 fil \rightskip=1pt |
| 157 |
\val rr \leftskip=0pt plus 1 fil |
| 158 |
\endval |
| 159 |
\ifvalue marginnote@temp : side = left |
| 160 |
{\settovalue{\advance\rightskip} marginnote@temp : gap } |
| 161 |
{\settovalue{\advance\leftskip} marginnote@temp : gap }% |
| 162 |
\settovalue\baselineskip marginnote@temp : baselineskip |
| 163 |
\settovalue\parindent marginnote@temp : parindent |
| 164 |
\usevalue marginnote@temp : font |
| 165 |
#2}% |
| 166 |
\quitvmode % Must be outside the attribute's scope, otherwise the |
| 167 |
% paragraphs DIR whatsit might get in the way. |
| 168 |
\bgroup |
| 169 |
\ifvalue marginnote@temp : side = left |
| 170 |
{\ptx@marginnote_attribute=-\expandafter\the\csname marginnote_\the\notecount\endcsname} |
| 171 |
{\ptx@marginnote_attribute=\expandafter\the\csname marginnote_\the\notecount\endcsname}\relax |
| 172 |
\pdfliteral{}% |
| 173 |
\egroup |
| 174 |
\deleteparameter marginnote@temp:% |
| 175 |
} |
| 176 |
|
| 177 |
\luacode |
| 178 |
local WHAT, HLIST, KERN = node.id("whatsit"), node.id("hlist"), node.id("kern") |
| 179 |
local GLUE, SPEC = node.id("glue"), node.id("glue_spec") |
| 180 |
local marginnote_table = {} |
| 181 |
function pitex.misc.mark_lines (head) |
| 182 |
for line in node.traverse_id(HLIST, head) do |
| 183 |
local t = {left = {}, right = {}} |
| 184 |
local item = line.head |
| 185 |
while item do |
| 186 |
local next = item.next |
| 187 |
if item.id == WHAT then |
| 188 |
local attr = node.has_attribute(item, \attributenumber\ptx@marginnote_attribute) |
| 189 |
if attr then |
| 190 |
if attr < 0 then |
| 191 |
table.insert(t.left, -attr) |
| 192 |
else |
| 193 |
table.insert(t.right, attr) |
| 194 |
end |
| 195 |
line.head = node.remove(line.head, item) |
| 196 |
end |
| 197 |
end |
| 198 |
item = next |
| 199 |
end |
| 200 |
if #t.left > 0 or #t.right > 0 then |
| 201 |
table.insert(marginnote_table, t) |
| 202 |
node.set_attribute(line, \attributenumber\ptx@marginnote_attribute, #marginnote_table) |
| 203 |
end |
| 204 |
end |
| 205 |
return head |
| 206 |
end |
| 207 |
|
| 208 |
pitex.callback.register("post_linebreak_filter", "pitex.misc:mark_lines") |
| 209 |
|
| 210 |
local lheight, rheight = 0, 0 |
| 211 |
local function update_height (h) |
| 212 |
lheight = lheight + h |
| 213 |
rheight = rheight + h |
| 214 |
end |
| 215 |
|
| 216 |
local function first_dim (n, t) |
| 217 |
while n do |
| 218 |
if node.has_field(n, "kern") then |
| 219 |
return n.kern |
| 220 |
elseif node.has_field(n, "spec") then |
| 221 |
return n.spec.width |
| 222 |
elseif node.has_field(n, "height") then |
| 223 |
return t == "height" and n.height or n.depth |
| 224 |
else |
| 225 |
n = t == "height" and n.next or n.prev |
| 226 |
end |
| 227 |
end |
| 228 |
return 0 |
| 229 |
end |
| 230 |
|
| 231 |
function process_marginalia (head) |
| 232 |
lheight, rheight = 0, 0 |
| 233 |
local item, first = node.slide(head), true |
| 234 |
while item do |
| 235 |
if node.has_field(item, "kern") then |
| 236 |
if not first then |
| 237 |
update_height(item.kern) |
| 238 |
end |
| 239 |
elseif node.has_field(item, "spec") then |
| 240 |
if not first then |
| 241 |
update_height(item.spec.width) |
| 242 |
end |
| 243 |
elseif node.has_field(item, "height") then |
| 244 |
if first then |
| 245 |
first = false |
| 246 |
else |
| 247 |
update_height(item.depth) |
| 248 |
end |
| 249 |
local attr = node.has_attribute(item, \attributenumber\ptx@marginnote_attribute) |
| 250 |
if attr then |
| 251 |
for side, notes in pairs (marginnote_table[attr]) do |
| 252 |
if #notes > 0 then |
| 253 |
local note |
| 254 |
for _, n in ipairs(notes) do |
| 255 |
if note then |
| 256 |
local n, g, s = tex.box[n], node.new(GLUE), node.new(SPEC) |
| 257 |
local d = first_dim(node.tail(note.head), "depth") |
| 258 |
local h = first_dim(n.head, "height") |
| 259 |
s.width = tex.baselineskip.width - (d + h) |
| 260 |
g.spec = s |
| 261 |
note.depth = note.depth + s.width + n.height + n.depth |
| 262 |
node.insert_after(note.head, node.tail(note.head), g) |
| 263 |
g.next, n.head.prev = n.head, g |
| 264 |
else |
| 265 |
note = tex.box[n] |
| 266 |
end |
| 267 |
end |
| 268 |
first = true |
| 269 |
local upward = note.depth - first_dim(node.tail(note.head), "depth") |
| 270 |
local remainingheight = side == "left" and lheight or rheight |
| 271 |
if upward > remainingheight then |
| 272 |
upward = remainingheight - upward |
| 273 |
else |
| 274 |
upward = 0 |
| 275 |
end |
| 276 |
local kern = node.new(KERN, 1) |
| 277 |
kern.kern = upward - note.height - item.depth |
| 278 |
node.insert_before(note.list, note.list, kern) |
| 279 |
note.list = kern |
| 280 |
if side == "left" then |
| 281 |
note.shift = -note.width |
| 282 |
lheight = upward |
| 283 |
else |
| 284 |
note.shift = item.width |
| 285 |
rheight = upward |
| 286 |
end |
| 287 |
note.height, note.depth = 0, 0 |
| 288 |
node.insert_after(head, item, note) |
| 289 |
else |
| 290 |
if side == "left" then |
| 291 |
lheight = lheight + item.height |
| 292 |
else |
| 293 |
rheight = rheight + item.height |
| 294 |
end |
| 295 |
end |
| 296 |
end |
| 297 |
else |
| 298 |
update_height(item.height) |
| 299 |
end |
| 300 |
end |
| 301 |
item = item.prev |
| 302 |
end |
| 303 |
end |
| 304 |
\luacode/ |
| 305 |
|
| 306 |
\OutputRoutine def {processmarginalia}{% |
| 307 |
\ptx@lua{process_marginalia(tex.box[255].list)}% |
| 308 |
} |
| 309 |
|
| 310 |
\OutputRoutine add {processmarginalia}[first]{shipout} |
| 311 |
|