| 1 |
-- |
| 2 |
-- This is file `marginalia.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- marginalia.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- This is a generated file. |
| 10 |
-- |
| 11 |
-- Copyright (C) 2025--26 Alan J. Cain |
| 12 |
-- |
| 13 |
-- This file may be distributed and/or modified under the |
| 14 |
-- conditions of the LaTeX Project Public License, either |
| 15 |
-- version 1.3c of this license or (at your option) any later |
| 16 |
-- version. The latest version of this license is in: |
| 17 |
-- |
| 18 |
-- http://www.latex-project.org/lppl.txt |
| 19 |
-- |
| 20 |
-- and version 1.3c or later is part of all distributions of |
| 21 |
-- LaTeX version 2008-05-04 or later. |
| 22 |
-- |
| 23 |
local PAGE_DATA_MAIN_TABLE = {} |
| 24 |
local ITEM_DATA_MAIN_TABLE = {} |
| 25 |
local PROBLEM_REPORT_TABLE = {} |
| 26 |
local PAGE_CHANGE_REPORT_TABLE = {} |
| 27 |
local ITEM_CHANGE_REPORT_TABLE = {} |
| 28 |
local PROBLEM_REPORT_MAX_LENGTH = 40 |
| 29 |
local PAGE_CHANGE_REPORT_MAX_LENGTH = 10 |
| 30 |
local ITEM_CHANGE_REPORT_MAX_LENGTH = 10 |
| 31 |
local TYPE_NORMAL = 1 |
| 32 |
local TYPE_FIXED = 2 |
| 33 |
local TYPE_OPTFIXED = 3 |
| 34 |
local POS_AUTO = 1 |
| 35 |
local POS_REVERSE = 2 |
| 36 |
local POS_LEFT = 3 |
| 37 |
local POS_RIGHT = 4 |
| 38 |
local POS_NEAREST = 5 |
| 39 |
local KEY_ABSPAGENO = 'abspageno' |
| 40 |
local KEY_CHECKED = 'checked' |
| 41 |
local KEY_PAGEDATANO = 'pagedatano' |
| 42 |
local KEY_HOFFSETORIGIN = 'hoffsetorigin' |
| 43 |
local KEY_VOFFSETORIGIN = 'voffsetorigin' |
| 44 |
local KEY_HOFFSET = 'hoffset' |
| 45 |
local KEY_VOFFSET = 'voffset' |
| 46 |
local KEY_PAGEHEIGHT = 'pageheight' |
| 47 |
local KEY_ODDSIDEMARGIN = 'oddsidemargin' |
| 48 |
local KEY_EVENSIDEMARGIN = 'evensidemargin' |
| 49 |
local KEY_TEXTWIDTH = 'textwidth' |
| 50 |
local KEY_COLUMNWIDTH = 'columnwidth' |
| 51 |
local KEY_COLUMNSEP = 'columnsep' |
| 52 |
local KEY_COLUMNCOUNT = 'columncount' |
| 53 |
local KEY_TWOSIDE = 'twoside' |
| 54 |
local KEY_ITEMNO = 'itemno' |
| 55 |
local KEY_PAGENO = 'pageno' |
| 56 |
local KEY_XPOS = 'xpos' |
| 57 |
local KEY_YPOS = 'ypos' |
| 58 |
local KEY_HEIGHT = 'height' |
| 59 |
local KEY_DEPTH = 'depth' |
| 60 |
local KEY_TYPE = 'type' |
| 61 |
local KEY_POS = 'pos' |
| 62 |
local KEY_COLUMN = 'column' |
| 63 |
local KEY_YSHIFT = 'yshift' |
| 64 |
local KEY_YSEP_ABOVE = 'ysep above' |
| 65 |
local KEY_YSEP_BELOW = 'ysep below' |
| 66 |
local KEY_YSEP_PAGE_TOP = 'ysep page top' |
| 67 |
local KEY_YSEP_PAGE_BOTTOM = 'ysep page bottom' |
| 68 |
local KEY_COLNO_COMPUTED = 'colno computed' |
| 69 |
local KEY_XSHIFT_COMPUTED = 'xshift computed' |
| 70 |
local KEY_YSHIFT_COMPUTED = 'yshift computed' |
| 71 |
local KEY_SIDE_COMPUTED = 'side computed' |
| 72 |
local KEY_MARGINNO_COMPUTED = 'marginno computed' |
| 73 |
local KEY_ENABLED_COMPUTED = 'enabled computed' |
| 74 |
local function list_filter(t, f) |
| 75 |
local j = 1 |
| 76 |
local n = #t |
| 77 |
|
| 78 |
for i=1,n do |
| 79 |
if (f(t[i])) then |
| 80 |
if (i ~= j) then |
| 81 |
t[j] = t[i] |
| 82 |
t[i] = nil |
| 83 |
end |
| 84 |
j = j + 1 |
| 85 |
else |
| 86 |
t[i] = nil |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
end |
| 91 |
local function toboolean(s) |
| 92 |
return s == "true" |
| 93 |
end |
| 94 |
local function get_data_page_number(data) |
| 95 |
local pageno = data[KEY_PAGENO] |
| 96 |
if pageno ~= nil then |
| 97 |
return 'p' .. pageno .. ' (' .. data[KEY_ABSPAGENO] .. ')' |
| 98 |
else |
| 99 |
return data[KEY_ABSPAGENO] |
| 100 |
end |
| 101 |
end |
| 102 |
local function parse_data(keyvalue_string,conversion_table,defaults_table) |
| 103 |
|
| 104 |
local key |
| 105 |
local value |
| 106 |
local result = {} |
| 107 |
|
| 108 |
for s in string.gmatch(keyvalue_string,'([^,]+)') do |
| 109 |
|
| 110 |
key,value = string.match(s,'^(.+)=(.+)$') |
| 111 |
local conv = conversion_table[key] |
| 112 |
if conv ~= nil then |
| 113 |
result[key] = conv(value) |
| 114 |
end |
| 115 |
|
| 116 |
end |
| 117 |
|
| 118 |
for key,value in pairs(defaults_table) do |
| 119 |
if not(result[key] ~= nil) then |
| 120 |
result[key] = value |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
return result |
| 125 |
|
| 126 |
end |
| 127 |
local function check_data(keyvalue_string,conversion_table,defaults_table, |
| 128 |
data_table,data_table_key_field,report_table) |
| 129 |
|
| 130 |
local new_data = parse_data(keyvalue_string, |
| 131 |
conversion_table,defaults_table) |
| 132 |
|
| 133 |
local data_table_key = new_data[data_table_key_field] |
| 134 |
|
| 135 |
local stored_data = data_table[data_table_key] |
| 136 |
if stored_data == nil then |
| 137 |
table.insert( |
| 138 |
report_table, |
| 139 |
get_data_page_number(new_data) .. ' New' |
| 140 |
) |
| 141 |
else |
| 142 |
local change_report = '' |
| 143 |
for k,_ in pairs(conversion_table) do |
| 144 |
if stored_data[k] ~= new_data[k] then |
| 145 |
change_report = change_report |
| 146 |
.. ' ' .. k .. ':' .. |
| 147 |
tostring(stored_data[k]) .. '->' .. tostring(new_data[k]) |
| 148 |
end |
| 149 |
end |
| 150 |
if change_report ~= '' then |
| 151 |
table.insert( |
| 152 |
report_table, |
| 153 |
get_data_page_number(new_data) .. ' ' .. change_report |
| 154 |
) |
| 155 |
end |
| 156 |
stored_data[KEY_CHECKED] = true |
| 157 |
end |
| 158 |
|
| 159 |
end |
| 160 |
local function check_removed_data(data_table,report_table) |
| 161 |
for _,data in pairs(data_table) do |
| 162 |
if not data[KEY_CHECKED] then |
| 163 |
table.insert( |
| 164 |
report_table, |
| 165 |
' Removed' |
| 166 |
) |
| 167 |
break |
| 168 |
end |
| 169 |
end |
| 170 |
end |
| 171 |
local PAGE_DATA_CONVERSION_TABLE = { |
| 172 |
[KEY_PAGEDATANO] = tonumber, |
| 173 |
[KEY_ABSPAGENO] = tonumber, |
| 174 |
[KEY_HOFFSETORIGIN] = tonumber, |
| 175 |
[KEY_VOFFSETORIGIN] = tonumber, |
| 176 |
[KEY_HOFFSET] = tonumber, |
| 177 |
[KEY_VOFFSET] = tonumber, |
| 178 |
[KEY_PAGEHEIGHT] = tonumber, |
| 179 |
[KEY_ODDSIDEMARGIN] = tonumber, |
| 180 |
[KEY_EVENSIDEMARGIN] = tonumber, |
| 181 |
[KEY_COLUMNCOUNT] = tonumber, |
| 182 |
[KEY_COLUMNWIDTH] = tonumber, |
| 183 |
[KEY_COLUMNSEP] = tonumber, |
| 184 |
[KEY_TEXTWIDTH] = tonumber, |
| 185 |
[KEY_TWOSIDE] = toboolean, |
| 186 |
} |
| 187 |
local PAGE_DATA_DEFAULT_TABLE = { |
| 188 |
[KEY_PAGEDATANO] = 0, |
| 189 |
[KEY_ABSPAGENO] = 0, |
| 190 |
[KEY_HOFFSETORIGIN] = tex.sp('1in'), |
| 191 |
[KEY_VOFFSETORIGIN] = tex.sp('1in'), |
| 192 |
[KEY_HOFFSET] = tex.dimen['hoffset'], |
| 193 |
[KEY_VOFFSET] = tex.dimen['voffset'], |
| 194 |
[KEY_PAGEHEIGHT] = tex.dimen['pageheight'], |
| 195 |
[KEY_ODDSIDEMARGIN] = tex.dimen['oddsidemargin'], |
| 196 |
[KEY_EVENSIDEMARGIN] = tex.dimen['evensidemargin'], |
| 197 |
[KEY_TEXTWIDTH] = tex.dimen['textwidth'], |
| 198 |
[KEY_COLUMNWIDTH] = tex.dimen['columnwidth'], |
| 199 |
[KEY_COLUMNSEP] = tex.dimen['columnsep'], |
| 200 |
[KEY_COLUMNCOUNT] = 1, |
| 201 |
[KEY_TWOSIDE] = false, |
| 202 |
[KEY_CHECKED] = false, |
| 203 |
} |
| 204 |
local function store_page_data(keyvalue_string) |
| 205 |
|
| 206 |
local page_data = parse_data(keyvalue_string, |
| 207 |
PAGE_DATA_CONVERSION_TABLE, |
| 208 |
PAGE_DATA_DEFAULT_TABLE) |
| 209 |
|
| 210 |
PAGE_DATA_MAIN_TABLE[page_data[KEY_PAGEDATANO]] = page_data |
| 211 |
|
| 212 |
end |
| 213 |
local function store_default_page_data() |
| 214 |
|
| 215 |
default_page_data = parse_data('', |
| 216 |
PAGE_DATA_CONVERSION_TABLE, |
| 217 |
PAGE_DATA_DEFAULT_TABLE) |
| 218 |
|
| 219 |
default_page_data[KEY_ABSPAGENO] = 1 |
| 220 |
default_page_data[KEY_CHECKED] = true |
| 221 |
|
| 222 |
PAGE_DATA_MAIN_TABLE[0] = default_page_data |
| 223 |
|
| 224 |
end |
| 225 |
local function check_page_data(keyvalue_string) |
| 226 |
|
| 227 |
check_data(keyvalue_string, |
| 228 |
PAGE_DATA_CONVERSION_TABLE,PAGE_DATA_DEFAULT_TABLE, |
| 229 |
PAGE_DATA_MAIN_TABLE,KEY_PAGEDATANO, |
| 230 |
PAGE_CHANGE_REPORT_TABLE) |
| 231 |
|
| 232 |
end |
| 233 |
local ITEM_DATA_CONVERSIONS = { |
| 234 |
[KEY_ITEMNO] = tonumber, |
| 235 |
[KEY_ABSPAGENO] = tonumber, |
| 236 |
[KEY_PAGENO] = tonumber, |
| 237 |
[KEY_XPOS] = tonumber, |
| 238 |
[KEY_YPOS] = tonumber, |
| 239 |
[KEY_HEIGHT] = tonumber, |
| 240 |
[KEY_DEPTH] = tonumber, |
| 241 |
[KEY_TYPE] = tonumber, |
| 242 |
[KEY_POS] = tonumber, |
| 243 |
[KEY_COLUMN] = tonumber, |
| 244 |
[KEY_YSHIFT] = tonumber, |
| 245 |
[KEY_YSEP_ABOVE] = tonumber, |
| 246 |
[KEY_YSEP_BELOW] = tonumber, |
| 247 |
[KEY_YSEP_PAGE_TOP] = tonumber, |
| 248 |
[KEY_YSEP_PAGE_BOTTOM] = tonumber, |
| 249 |
[KEY_CHECKED] = toboolean, |
| 250 |
} |
| 251 |
local ITEM_DATA_DEFAULTS = { |
| 252 |
[KEY_ITEMNO] = 0, |
| 253 |
[KEY_ABSPAGENO] = 1, |
| 254 |
[KEY_PAGENO] = 1, |
| 255 |
[KEY_XPOS] = 0, |
| 256 |
[KEY_YPOS] = 0, |
| 257 |
[KEY_HEIGHT] = 0, |
| 258 |
[KEY_DEPTH] = 0, |
| 259 |
[KEY_TYPE] = 0, |
| 260 |
[KEY_POS] = 0, |
| 261 |
[KEY_COLUMN] = -1, |
| 262 |
[KEY_YSHIFT] = 0, |
| 263 |
[KEY_YSEP_ABOVE] = tex.dimen['marginparpush'], |
| 264 |
[KEY_YSEP_BELOW] = tex.dimen['marginparpush'], |
| 265 |
[KEY_YSEP_PAGE_TOP] = tex.dimen['marginparpush'], |
| 266 |
[KEY_YSEP_PAGE_BOTTOM] = tex.dimen['marginparpush'], |
| 267 |
[KEY_COLNO_COMPUTED] = 0, |
| 268 |
[KEY_XSHIFT_COMPUTED] = 0, |
| 269 |
[KEY_YSHIFT_COMPUTED] = 0, |
| 270 |
[KEY_SIDE_COMPUTED] = 0, |
| 271 |
[KEY_MARGINNO_COMPUTED] = 0, |
| 272 |
[KEY_ENABLED_COMPUTED] = true, |
| 273 |
[KEY_CHECKED] = false, |
| 274 |
} |
| 275 |
|
| 276 |
local function store_item_data(keyvalue_string) |
| 277 |
|
| 278 |
local item = parse_data(keyvalue_string, |
| 279 |
ITEM_DATA_CONVERSIONS, |
| 280 |
ITEM_DATA_DEFAULTS) |
| 281 |
|
| 282 |
ITEM_DATA_MAIN_TABLE[item[KEY_ITEMNO]] = item |
| 283 |
|
| 284 |
end |
| 285 |
local function check_item_data(keyvalue_string) |
| 286 |
|
| 287 |
check_data(keyvalue_string, |
| 288 |
ITEM_DATA_CONVERSIONS,ITEM_DATA_DEFAULTS, |
| 289 |
ITEM_DATA_MAIN_TABLE,KEY_ITEMNO, |
| 290 |
ITEM_CHANGE_REPORT_TABLE) |
| 291 |
|
| 292 |
end |
| 293 |
local function write_report(report_table,max_length,noun) |
| 294 |
|
| 295 |
if #report_table > 0 then |
| 296 |
local report_text |
| 297 |
local report_length |
| 298 |
|
| 299 |
if #report_table <= max_length then |
| 300 |
report_length = #report_table |
| 301 |
report_text = ' Here are the ' .. noun .. ':\n' |
| 302 |
else |
| 303 |
report_length = max_length |
| 304 |
report_text = ' Here are the first ' .. report_length .. ' ' .. noun .. ':\n' |
| 305 |
end |
| 306 |
|
| 307 |
for i=1,report_length do |
| 308 |
report_text = report_text .. report_table[i] |
| 309 |
if i < report_length then |
| 310 |
report_text = report_text .. '\n' |
| 311 |
end |
| 312 |
end |
| 313 |
|
| 314 |
tex.print(report_text) |
| 315 |
end |
| 316 |
|
| 317 |
end |
| 318 |
local function write_problem_report() |
| 319 |
|
| 320 |
write_report(PROBLEM_REPORT_TABLE,PROBLEM_REPORT_MAX_LENGTH,'problems') |
| 321 |
|
| 322 |
end |
| 323 |
local function write_item_change_report() |
| 324 |
|
| 325 |
check_removed_data(ITEM_DATA_MAIN_TABLE,ITEM_CHANGE_REPORT_TABLE) |
| 326 |
write_report(ITEM_CHANGE_REPORT_TABLE,ITEM_CHANGE_REPORT_MAX_LENGTH,'changes') |
| 327 |
|
| 328 |
end |
| 329 |
local function write_page_change_report() |
| 330 |
|
| 331 |
check_removed_data(PAGE_DATA_MAIN_TABLE,PAGE_CHANGE_REPORT_TABLE) |
| 332 |
write_report(PAGE_CHANGE_REPORT_TABLE,PAGE_CHANGE_REPORT_MAX_LENGTH,'changes') |
| 333 |
|
| 334 |
end |
| 335 |
local RIGHTSIDE_LOOKUP_TABLE = { |
| 336 |
[true] = { |
| 337 |
[0] = { |
| 338 |
[POS_AUTO] = true, |
| 339 |
[POS_REVERSE] = false, |
| 340 |
}, |
| 341 |
[1] = { |
| 342 |
[POS_AUTO] = false, |
| 343 |
[POS_REVERSE] = true, |
| 344 |
}, |
| 345 |
[2] = { |
| 346 |
[POS_AUTO] = true, |
| 347 |
[POS_REVERSE] = false, |
| 348 |
}, |
| 349 |
}, |
| 350 |
[false] = { |
| 351 |
[0] = { |
| 352 |
[POS_AUTO] = false, |
| 353 |
[POS_REVERSE] = true, |
| 354 |
}, |
| 355 |
[1] = { |
| 356 |
[POS_AUTO] = true, |
| 357 |
[POS_REVERSE] = false, |
| 358 |
}, |
| 359 |
[2] = { |
| 360 |
[POS_AUTO] = false, |
| 361 |
[POS_REVERSE] = true, |
| 362 |
}, |
| 363 |
}, |
| 364 |
} |
| 365 |
local MARGINNO_LOOKUP_TABLE = { |
| 366 |
[true] = { |
| 367 |
[0] = { |
| 368 |
[false] = 1, |
| 369 |
[true] = 0, |
| 370 |
}, |
| 371 |
[1] = { |
| 372 |
[false] = 1, |
| 373 |
[true] = 5, |
| 374 |
}, |
| 375 |
[2] = { |
| 376 |
[false] = 4, |
| 377 |
[true] = 0, |
| 378 |
}, |
| 379 |
}, |
| 380 |
[false] = { |
| 381 |
[0] = { |
| 382 |
[false] = 2, |
| 383 |
[true] = 3, |
| 384 |
}, |
| 385 |
[1] = { |
| 386 |
[false] = 2, |
| 387 |
[true] = 5, |
| 388 |
}, |
| 389 |
[2] = { |
| 390 |
[false] = 4, |
| 391 |
[true] = 3, |
| 392 |
}, |
| 393 |
}, |
| 394 |
} |
| 395 |
local function compute_items_horizontal(item_data_list,page_data) |
| 396 |
if #item_data_list == 0 then |
| 397 |
return |
| 398 |
end |
| 399 |
local pageno = item_data_list[1][KEY_PAGENO] |
| 400 |
local twoside = page_data[KEY_TWOSIDE] |
| 401 |
local recto = ((pageno % 2) == 1) or (not twoside) |
| 402 |
local columncount = page_data[KEY_COLUMNCOUNT] |
| 403 |
local x_textleft = {} |
| 404 |
local x_textright = {} |
| 405 |
local x_textmiddle = {} |
| 406 |
if recto then |
| 407 |
x_textleft[0] = ( |
| 408 |
page_data[KEY_HOFFSETORIGIN] |
| 409 |
+ page_data[KEY_HOFFSET] |
| 410 |
+ page_data[KEY_ODDSIDEMARGIN] |
| 411 |
) |
| 412 |
x_textright[0] = ( |
| 413 |
x_textleft[0] |
| 414 |
+ page_data[KEY_TEXTWIDTH] |
| 415 |
) |
| 416 |
else |
| 417 |
x_textleft[0] = ( |
| 418 |
page_data[KEY_HOFFSETORIGIN] |
| 419 |
+ page_data[KEY_HOFFSET] |
| 420 |
+ page_data[KEY_EVENSIDEMARGIN] |
| 421 |
) |
| 422 |
x_textright[0] = ( |
| 423 |
x_textleft[0] |
| 424 |
+ page_data[KEY_TEXTWIDTH] |
| 425 |
) |
| 426 |
end |
| 427 |
x_textmiddle[0] = (x_textleft[0] + x_textright[0])/2 |
| 428 |
|
| 429 |
if columncount == 1 then |
| 430 |
for i=1,#item_data_list do |
| 431 |
item_data_list[i][KEY_COLNO_COMPUTED] = 0 |
| 432 |
end |
| 433 |
else |
| 434 |
x_textleft[1] = x_textleft[0] |
| 435 |
x_textright[1] = ( |
| 436 |
x_textleft[1] |
| 437 |
+ page_data[KEY_COLUMNWIDTH] |
| 438 |
) |
| 439 |
x_textmiddle[1] = (x_textleft[1] + x_textright[1])/2 |
| 440 |
|
| 441 |
x_textleft[2] = ( |
| 442 |
x_textright[1] |
| 443 |
+ page_data[KEY_COLUMNSEP] |
| 444 |
) |
| 445 |
x_textright[2] = ( |
| 446 |
x_textleft[2] |
| 447 |
+ page_data[KEY_COLUMNWIDTH] |
| 448 |
) |
| 449 |
x_textmiddle[2] = (x_textleft[2] + x_textright[2])/2 |
| 450 |
|
| 451 |
local left_column_x_limit = ( |
| 452 |
x_textright[1] |
| 453 |
+ .5*page_data[KEY_COLUMNSEP] |
| 454 |
) |
| 455 |
for i=1,#item_data_list do |
| 456 |
local item_data = item_data_list[i] |
| 457 |
|
| 458 |
if item_data[KEY_COLUMN] >= 0 then |
| 459 |
item_data[KEY_COLNO_COMPUTED] = item_data[KEY_COLUMN] |
| 460 |
else |
| 461 |
if item_data[KEY_XPOS] <= left_column_x_limit then |
| 462 |
item_data[KEY_COLNO_COMPUTED] = 1 |
| 463 |
else |
| 464 |
item_data[KEY_COLNO_COMPUTED] = 2 |
| 465 |
end |
| 466 |
end |
| 467 |
end |
| 468 |
|
| 469 |
end |
| 470 |
for i=1,#item_data_list do |
| 471 |
local item = item_data_list[i] |
| 472 |
|
| 473 |
local pos = item[KEY_POS] |
| 474 |
local colnocomputed = item[KEY_COLNO_COMPUTED] |
| 475 |
|
| 476 |
if pos == POS_LEFT then |
| 477 |
rightside = false |
| 478 |
elseif pos == POS_RIGHT then |
| 479 |
rightside = true |
| 480 |
elseif pos == POS_NEAREST then |
| 481 |
rightside = (item[KEY_XPOS] >= x_textmiddle[colnocomputed]) |
| 482 |
else |
| 483 |
rightside = RIGHTSIDE_LOOKUP_TABLE[recto][colnocomputed][pos] |
| 484 |
end |
| 485 |
|
| 486 |
local marginno = MARGINNO_LOOKUP_TABLE[recto][colnocomputed][rightside] |
| 487 |
|
| 488 |
if rightside then |
| 489 |
item[KEY_SIDE_COMPUTED] = 0 |
| 490 |
item[KEY_XSHIFT_COMPUTED] = -item[KEY_XPOS] |
| 491 |
+ x_textright[colnocomputed] |
| 492 |
else |
| 493 |
item[KEY_SIDE_COMPUTED] = 1 |
| 494 |
item[KEY_XSHIFT_COMPUTED] = -item[KEY_XPOS] |
| 495 |
+ x_textleft[colnocomputed] |
| 496 |
end |
| 497 |
item[KEY_MARGINNO_COMPUTED] = marginno |
| 498 |
|
| 499 |
end |
| 500 |
|
| 501 |
end |
| 502 |
local function get_y_item_top(item_data) |
| 503 |
return item_data[KEY_YPOS] |
| 504 |
+ item_data[KEY_YSHIFT_COMPUTED] |
| 505 |
+ item_data[KEY_HEIGHT] |
| 506 |
end |
| 507 |
local function get_y_item_bottom(item_data) |
| 508 |
return item_data[KEY_YPOS] |
| 509 |
- item_data[KEY_DEPTH] |
| 510 |
+ item_data[KEY_YSHIFT_COMPUTED] |
| 511 |
end |
| 512 |
local function get_ysep_list(item_data_list) |
| 513 |
|
| 514 |
local ysep_list = {} |
| 515 |
|
| 516 |
ysep_list[0] = 0 |
| 517 |
for i=1,#item_data_list-1 do |
| 518 |
ysep_list[i] = math.max( |
| 519 |
item_data_list[i][KEY_YSEP_BELOW], |
| 520 |
item_data_list[i+1][KEY_YSEP_ABOVE] |
| 521 |
) |
| 522 |
end |
| 523 |
ysep_list[#item_data_list] = 0 |
| 524 |
|
| 525 |
return ysep_list |
| 526 |
|
| 527 |
end |
| 528 |
local function compute_items_vertical_optfixed_enabled(item_data_list) |
| 529 |
|
| 530 |
local optfixed_item_data_list = {} |
| 531 |
local fixed_item_data_list = {} |
| 532 |
|
| 533 |
for _,item_data in pairs(item_data_list) do |
| 534 |
if item_data[KEY_TYPE] == TYPE_OPTFIXED then |
| 535 |
optfixed_item_data_list[#optfixed_item_data_list+1] = item_data |
| 536 |
elseif item_data[KEY_TYPE] == TYPE_FIXED then |
| 537 |
fixed_item_data_list[#fixed_item_data_list+1] = item_data |
| 538 |
end |
| 539 |
end |
| 540 |
|
| 541 |
for _,optfixed_item_data in pairs(optfixed_item_data_list) do |
| 542 |
local optfixed_y_item_top = get_y_item_top(optfixed_item_data) |
| 543 |
local optfixed_y_item_bottom = get_y_item_bottom(optfixed_item_data) |
| 544 |
|
| 545 |
for _,fixed_item_data in pairs(fixed_item_data_list) do |
| 546 |
local fixed_y_item_top = get_y_item_top(fixed_item_data) |
| 547 |
local fixed_y_item_bottom = get_y_item_bottom(fixed_item_data) |
| 548 |
|
| 549 |
if ( |
| 550 |
( |
| 551 |
(fixed_y_item_bottom - optfixed_y_item_top) |
| 552 |
< |
| 553 |
math.max( |
| 554 |
fixed_item_data[KEY_YSEP_BELOW], |
| 555 |
optfixed_item_data[KEY_YSEP_ABOVE] |
| 556 |
) |
| 557 |
) |
| 558 |
and |
| 559 |
( |
| 560 |
(optfixed_y_item_bottom - fixed_y_item_top) |
| 561 |
< |
| 562 |
math.max( |
| 563 |
optfixed_item_data[KEY_YSEP_BELOW], |
| 564 |
fixed_item_data[KEY_YSEP_ABOVE] |
| 565 |
) |
| 566 |
) |
| 567 |
) then |
| 568 |
optfixed_item_data[KEY_ENABLED_COMPUTED] = false |
| 569 |
break |
| 570 |
end |
| 571 |
end |
| 572 |
end |
| 573 |
|
| 574 |
end |
| 575 |
local function compute_items_vertical_adjustment(item_data_list,page_data) |
| 576 |
if #item_data_list == 0 then |
| 577 |
return |
| 578 |
end |
| 579 |
|
| 580 |
local ysep_list = get_ysep_list(item_data_list) |
| 581 |
local y_limit_above = ( |
| 582 |
page_data[KEY_VOFFSET] |
| 583 |
+ page_data[KEY_PAGEHEIGHT] |
| 584 |
- item_data_list[1][KEY_YSEP_PAGE_TOP] |
| 585 |
) |
| 586 |
|
| 587 |
for i=1,#item_data_list do |
| 588 |
local item_data = item_data_list[i] |
| 589 |
|
| 590 |
local y_item_top = get_y_item_top(item_data) |
| 591 |
|
| 592 |
if y_item_top > y_limit_above then |
| 593 |
if item_data[KEY_TYPE] == TYPE_NORMAL then |
| 594 |
item_data[KEY_YSHIFT_COMPUTED] = item_data[KEY_YSHIFT_COMPUTED] |
| 595 |
+ (y_limit_above - y_item_top) |
| 596 |
end |
| 597 |
end |
| 598 |
|
| 599 |
y_limit_above = get_y_item_bottom(item_data) - ysep_list[i] |
| 600 |
end |
| 601 |
local y_limit_below = ( |
| 602 |
page_data[KEY_VOFFSET] |
| 603 |
+ item_data_list[#item_data_list][KEY_YSEP_PAGE_BOTTOM] |
| 604 |
) |
| 605 |
|
| 606 |
for i=#item_data_list,1,-1 do |
| 607 |
local item_data = item_data_list[i] |
| 608 |
|
| 609 |
local y_item_bottom = get_y_item_bottom(item_data) |
| 610 |
|
| 611 |
if y_item_bottom < y_limit_below then |
| 612 |
if item_data[KEY_TYPE] == TYPE_NORMAL then |
| 613 |
item_data[KEY_YSHIFT_COMPUTED] = item_data[KEY_YSHIFT_COMPUTED] |
| 614 |
+ (y_limit_below - y_item_bottom) |
| 615 |
end |
| 616 |
end |
| 617 |
|
| 618 |
y_limit_below = get_y_item_top(item_data) + ysep_list[i-1] |
| 619 |
end |
| 620 |
|
| 621 |
end |
| 622 |
local ITEM_PASSED_YSEP_PAGE_TOP_MESSAGES = { |
| 623 |
[TYPE_NORMAL] = 'Moveable item > ysep page top', |
| 624 |
[TYPE_FIXED] = 'Topmost fixed item > ysep page top', |
| 625 |
[TYPE_OPTFIXED] = 'Topmost optfixed item > ysep page top', |
| 626 |
} |
| 627 |
local ITEM_CLASH_MESSAGES = { |
| 628 |
[TYPE_NORMAL] = { |
| 629 |
[TYPE_NORMAL] = 'moveable items' |
| 630 |
.. ' (this shouldn\'t happen)', |
| 631 |
[TYPE_FIXED] = 'moveable item above fixed item', |
| 632 |
[TYPE_OPTFIXED] = 'moveable item above optfixed item', |
| 633 |
}, |
| 634 |
[TYPE_FIXED] = { |
| 635 |
[TYPE_NORMAL] = 'moveable item below fixed item', |
| 636 |
[TYPE_FIXED] = 'fixed items', |
| 637 |
[TYPE_OPTFIXED] = 'fixed item above optfixed item ' |
| 638 |
.. '(this shouldn\'t happen)', |
| 639 |
}, |
| 640 |
[TYPE_OPTFIXED] = { |
| 641 |
[TYPE_NORMAL] = 'moveable items below optfixed item', |
| 642 |
[TYPE_FIXED] = 'fixed item below optfixed item ' |
| 643 |
.. '(this shouldn\'t happen)', |
| 644 |
[TYPE_OPTFIXED] = 'optfixed items ' |
| 645 |
.. '(this shouldn\'t happen)', |
| 646 |
}, |
| 647 |
} |
| 648 |
local ITEM_PASSED_YSEP_PAGE_BOTTOM_MESSAGE = { |
| 649 |
[TYPE_NORMAL] = 'Moveable item < ysep page bottom', |
| 650 |
[TYPE_FIXED] = 'Bottommost fixed item < ysep page bottom', |
| 651 |
[TYPE_OPTFIXED] = 'Bottommost optfixed item < ysep page bottom', |
| 652 |
} |
| 653 |
local function check_items_vertical(item_data_list,page_data) |
| 654 |
if (#item_data_list) == 0 then |
| 655 |
return |
| 656 |
end |
| 657 |
|
| 658 |
local ysep_list = get_ysep_list(item_data_list) |
| 659 |
|
| 660 |
local item_data |
| 661 |
|
| 662 |
item_data = item_data_list[1] |
| 663 |
if ( |
| 664 |
get_y_item_top(item_data) > page_data[KEY_VOFFSET] |
| 665 |
+ page_data[KEY_PAGEHEIGHT] |
| 666 |
- item_data[KEY_YSEP_PAGE_TOP] |
| 667 |
) then |
| 668 |
table.insert( |
| 669 |
PROBLEM_REPORT_TABLE, |
| 670 |
get_data_page_number(item_data) |
| 671 |
.. ' ' .. ITEM_PASSED_YSEP_PAGE_TOP_MESSAGES[item_data[KEY_TYPE]] |
| 672 |
) |
| 673 |
end |
| 674 |
|
| 675 |
for i=2,#item_data_list do |
| 676 |
local item_data = item_data_list[i] |
| 677 |
local prev_item_data = item_data_list[i-1] |
| 678 |
if ( |
| 679 |
get_y_item_top(item_data) > get_y_item_bottom(prev_item_data) |
| 680 |
- ysep_list[i-1] |
| 681 |
) then |
| 682 |
table.insert( |
| 683 |
PROBLEM_REPORT_TABLE, |
| 684 |
get_data_page_number(item_data) |
| 685 |
.. ' Clash: ' .. |
| 686 |
ITEM_CLASH_MESSAGES[prev_item_data[KEY_TYPE]][item_data[KEY_TYPE]] |
| 687 |
) |
| 688 |
end |
| 689 |
end |
| 690 |
item_data = item_data_list[#item_data_list] |
| 691 |
if ( |
| 692 |
get_y_item_bottom(item_data) < page_data[KEY_VOFFSET] |
| 693 |
+ item_data[KEY_YSEP_PAGE_BOTTOM] |
| 694 |
) then |
| 695 |
table.insert( |
| 696 |
PROBLEM_REPORT_TABLE, |
| 697 |
get_data_page_number(item_data) |
| 698 |
.. ' ' .. ITEM_PASSED_YSEP_PAGE_BOTTOM_MESSAGE[item_data[KEY_TYPE]] |
| 699 |
) |
| 700 |
end |
| 701 |
|
| 702 |
end |
| 703 |
local function compute_items_vertical(item_data_list,page_data) |
| 704 |
for i=1,#item_data_list do |
| 705 |
local item_data = item_data_list[i] |
| 706 |
|
| 707 |
item_data[KEY_YSHIFT_COMPUTED] = item_data[KEY_YSHIFT] |
| 708 |
end |
| 709 |
compute_items_vertical_optfixed_enabled(item_data_list) |
| 710 |
list_filter(item_data_list,function(item_data) |
| 711 |
return item_data[KEY_ENABLED_COMPUTED] |
| 712 |
end) |
| 713 |
table.sort( |
| 714 |
item_data_list, |
| 715 |
function(left,right) |
| 716 |
local y_diff = left[KEY_YPOS] - right[KEY_YPOS] |
| 717 |
|
| 718 |
if y_diff > 0 then |
| 719 |
return true |
| 720 |
elseif y_diff < 0 then |
| 721 |
return false |
| 722 |
end |
| 723 |
|
| 724 |
local x_diff = left[KEY_XPOS] - right[KEY_XPOS] |
| 725 |
|
| 726 |
if x_diff < 0 then |
| 727 |
return true |
| 728 |
elseif x_diff > 0 then |
| 729 |
return false |
| 730 |
end |
| 731 |
|
| 732 |
return (left[KEY_ITEMNO] < right[KEY_ITEMNO]) |
| 733 |
end |
| 734 |
) |
| 735 |
|
| 736 |
compute_items_vertical_adjustment(item_data_list,page_data) |
| 737 |
|
| 738 |
check_items_vertical(item_data_list,page_data) |
| 739 |
|
| 740 |
end |
| 741 |
local function compute_items() |
| 742 |
local max_abspageno = 0 |
| 743 |
|
| 744 |
for k,v in pairs(ITEM_DATA_MAIN_TABLE) do |
| 745 |
max_abspageno = math.max(v[KEY_ABSPAGENO],max_abspageno) |
| 746 |
end |
| 747 |
local per_abspage_item_data_list = {} |
| 748 |
for i=1,max_abspageno do |
| 749 |
per_abspage_item_data_list[i] = {} |
| 750 |
end |
| 751 |
for _,item_data in pairs(ITEM_DATA_MAIN_TABLE) do |
| 752 |
local temp_table = per_abspage_item_data_list[item_data[KEY_ABSPAGENO]] |
| 753 |
temp_table[#temp_table+1] = item_data |
| 754 |
end |
| 755 |
local per_abspage_page_data_list = {} |
| 756 |
local pagedatano = 0 |
| 757 |
for abspageno = 1,max_abspageno do |
| 758 |
while ( |
| 759 |
PAGE_DATA_MAIN_TABLE[pagedatano+1] ~= nil |
| 760 |
and |
| 761 |
PAGE_DATA_MAIN_TABLE[pagedatano+1][KEY_ABSPAGENO] == abspageno |
| 762 |
) do |
| 763 |
pagedatano = pagedatano+1 |
| 764 |
end |
| 765 |
per_abspage_page_data_list[abspageno] = PAGE_DATA_MAIN_TABLE[pagedatano] |
| 766 |
end |
| 767 |
for abspageno=1,#per_abspage_item_data_list do |
| 768 |
local current_page_data = per_abspage_page_data_list[abspageno] |
| 769 |
local current_page_item_data_list = per_abspage_item_data_list[abspageno] |
| 770 |
compute_items_horizontal(current_page_item_data_list,current_page_data) |
| 771 |
local current_page_item_data_sublists = {} |
| 772 |
|
| 773 |
for i=0,5 do |
| 774 |
current_page_item_data_sublists[i] = {} |
| 775 |
end |
| 776 |
|
| 777 |
for _,item_data in pairs(current_page_item_data_list) do |
| 778 |
table.insert( |
| 779 |
current_page_item_data_sublists[item_data[KEY_MARGINNO_COMPUTED]], |
| 780 |
item_data |
| 781 |
) |
| 782 |
end |
| 783 |
for i=0,5 do |
| 784 |
compute_items_vertical( |
| 785 |
current_page_item_data_sublists[i], |
| 786 |
current_page_data |
| 787 |
) |
| 788 |
end |
| 789 |
end |
| 790 |
end |
| 791 |
local function load_item_data(itemno) |
| 792 |
|
| 793 |
item = ITEM_DATA_MAIN_TABLE[tonumber(itemno)] |
| 794 |
if item == nil then |
| 795 |
item = ITEM_DATA_DEFAULTS |
| 796 |
end |
| 797 |
|
| 798 |
tex.count['l__marginalia_page_int'] = item[KEY_PAGENO] |
| 799 |
tex.count['l__marginalia_column_computed_int'] = item[KEY_COLNO_COMPUTED] |
| 800 |
tex.dimen['l__marginalia_xshift_computed_dim'] = item[KEY_XSHIFT_COMPUTED] |
| 801 |
tex.dimen['l__marginalia_yshift_computed_dim'] = item[KEY_YSHIFT_COMPUTED] |
| 802 |
tex.count['l__marginalia_side_computed_int'] = item[KEY_SIDE_COMPUTED] |
| 803 |
tex.count['l__marginalia_marginno_computed_int'] |
| 804 |
= item[KEY_MARGINNO_COMPUTED] |
| 805 |
if item[KEY_ENABLED_COMPUTED] then |
| 806 |
tex.count['l__marginalia_enabled_computed_int'] = 1 |
| 807 |
else |
| 808 |
tex.count['l__marginalia_enabled_computed_int'] = 0 |
| 809 |
end |
| 810 |
|
| 811 |
end |
| 812 |
return { |
| 813 |
store_default_page_data = store_default_page_data, |
| 814 |
store_page_data = store_page_data, |
| 815 |
check_page_data = check_page_data, |
| 816 |
|
| 817 |
store_item_data = store_item_data, |
| 818 |
check_item_data = check_item_data, |
| 819 |
|
| 820 |
compute_items = compute_items, |
| 821 |
|
| 822 |
load_item_data = load_item_data, |
| 823 |
|
| 824 |
write_problem_report = write_problem_report, |
| 825 |
|
| 826 |
write_page_change_report = write_page_change_report, |
| 827 |
write_item_change_report = write_item_change_report, |
| 828 |
} |
| 829 |
|