| 1 |
-- Lua code for LaTeX typewriter.sty |
| 2 |
-- 2025-02-08 |
| 3 |
-- |
| 4 |
|
| 5 |
-- Use local functions |
| 6 |
local getmacro = token.get_macro |
| 7 |
local sp = tex.sp |
| 8 |
local definefont = tex.definefont |
| 9 |
local define_font = luaotfload.define_font |
| 10 |
local getfont = font.getfont |
| 11 |
local nextid = font.nextid |
| 12 |
local setfont = font.setfont |
| 13 |
local random, rad = math.random, math.rad |
| 14 |
local cos, sin, floor = math.cos, math.sin, math.floor |
| 15 |
local pdfprint = pdf.print |
| 16 |
local format = string.format |
| 17 |
local insert = table.insert |
| 18 |
|
| 19 |
-- set the options |
| 20 |
local ttgreybolda = getmacro("ttgreybolda") and tonumber(getmacro("ttgreybolda")) or 0.6 |
| 21 |
local ttgreyboldb = getmacro("ttgreyboldb") and tonumber(getmacro("ttgreyboldb")) or 0.3 |
| 22 |
local ttrotatebold = getmacro("ttrotatebold") and tonumber(getmacro("ttrotatebold")) or 12 |
| 23 |
local ttdownbold = getmacro("ttdownbold") and tonumber(getmacro("ttdownbold")) or 20000 |
| 24 |
local ttrightbold = getmacro("ttrightbold") and tonumber(getmacro("ttrightbold")) or 35000 |
| 25 |
local ttoverprintbolda = getmacro("ttoverprintbolda") and tonumber(getmacro("ttoverprintbolda")) or 1 |
| 26 |
local ttoverprintboldb = getmacro("ttoverprintboldb") and tonumber(getmacro("ttoverprintboldb")) or 1 |
| 27 |
local ttoverprintboldc = getmacro("ttoverprintboldc") and tonumber(getmacro("ttoverprintboldc")) or 1 |
| 28 |
|
| 29 |
local ttgreynormala = getmacro("ttgreynormala") and tonumber(getmacro("ttgreynormala")) or 0.3 |
| 30 |
local ttgreynormalb = getmacro("ttgreynormalb") and tonumber(getmacro("ttgreynormalb")) or 0.5 |
| 31 |
local ttrotatenormal = getmacro("ttrotatenormal") and tonumber(getmacro("ttrotatenormal")) or 10 |
| 32 |
local ttrightnormal = getmacro("ttrightnormal") and tonumber(getmacro("ttrightnormal")) or 20000 |
| 33 |
local ttdownnormal = getmacro("ttdownnormal") and tonumber(getmacro("ttdownnormal")) or 20000 |
| 34 |
local ttoverprintnormal = getmacro("ttoverprintnormal") and tonumber(getmacro("ttoverprintnormal")) or 1 |
| 35 |
|
| 36 |
local ttbasefont = getmacro("ttbasefont") or "cmuntt.otf" |
| 37 |
local ttmathfont = getmacro("ttmathfont") or ttbasefont |
| 38 |
local ttfontsize = getmacro("ttfontsize") and sp(getmacro("ttfontsize")) or sp("12pt") |
| 39 |
|
| 40 |
-- load the data of the base font. If the id is not passed to luaotfload.define_font |
| 41 |
-- as the third argument, the new id is the return value, otherwise |
| 42 |
-- the font data is returned. |
| 43 |
|
| 44 |
local base_id = define_font("file:" .. ttbasefont, ttfontsize) |
| 45 |
local base_math_id |
| 46 |
if ttbasefont == ttmathfont then |
| 47 |
base_math_id = base_id |
| 48 |
else |
| 49 |
base_math_id = define_font("file:" .. ttmathfont, ttfontsize) |
| 50 |
end |
| 51 |
|
| 52 |
-- define \cmuntt as a font selection macro for the |
| 53 |
-- base font, and set the base font as the current |
| 54 |
-- active font |
| 55 |
|
| 56 |
definefont("cmuntt", base_id) |
| 57 |
font.current(base_id) |
| 58 |
|
| 59 |
-- Some helper functions |
| 60 |
|
| 61 |
local function rotate(num) |
| 62 |
local r = rad(0.1*num*random(-10,10)) |
| 63 |
pdfprint(format(" q %f %f %f %f 0 0 cm ", |
| 64 |
cos(r), - sin(r), sin(r), cos(r))) |
| 65 |
end |
| 66 |
|
| 67 |
local function define_normal_tt_font(characters) |
| 68 |
for j,v in pairs(characters) do |
| 69 |
local greynormala = ttgreynormala*random() |
| 70 |
local greynormalb = ttgreynormalb*random() |
| 71 |
local cmd = {} |
| 72 |
cmd = { |
| 73 |
{'lua', function() rotate(ttrotatenormal) end}, |
| 74 |
{'pdf', ' ' .. greynormala .. ' g'}, |
| 75 |
{'push'}, |
| 76 |
{'right', random(-ttrightnormal, ttrightnormal)}, |
| 77 |
{'down', random(-ttdownnormal, ttdownnormal)}, |
| 78 |
{'char',j}, |
| 79 |
{'pop'}, |
| 80 |
{'lua', function() pdfprint(" Q ") end} |
| 81 |
} |
| 82 |
|
| 83 |
if ttoverprintnormal == 1 then |
| 84 |
insert(cmd, {'down', random(-ttdownnormal, ttdownnormal)}) |
| 85 |
insert(cmd, {'pdf', ' ' .. greynormalb .. ' g'}) |
| 86 |
insert(cmd, {'char',j}) |
| 87 |
insert(cmd, {'pdf', ' 0 g'}) |
| 88 |
end |
| 89 |
v.commands = cmd |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
local function define_bold_tt_font(characters) |
| 94 |
for j, v in pairs(characters) do |
| 95 |
local greybolda = ttgreybolda*random() |
| 96 |
local greyboldb = ttgreyboldb*random() |
| 97 |
local cmd = {} |
| 98 |
cmd = { |
| 99 |
{'lua', function() rotate(ttrotatebold) end}, |
| 100 |
{'pdf', ' ' .. format("%f", greybolda) .. ' g'}, |
| 101 |
{'push'}, |
| 102 |
{'right', random(-ttrightbold, ttrightbold)}, |
| 103 |
{'down', random(-ttdownbold, ttdownbold)}, |
| 104 |
{'char',j}, |
| 105 |
{'pop'}, |
| 106 |
{'lua', function() pdfprint(" Q ") end} |
| 107 |
} |
| 108 |
|
| 109 |
if ttoverprintbolda == 1 then |
| 110 |
insert(cmd, {'push'}) |
| 111 |
insert(cmd, {'right', random(-ttrightbold, ttrightbold)}) |
| 112 |
insert(cmd, {'down', random(-ttdownbold, ttdownbold)}) |
| 113 |
insert(cmd, {'char',j}) |
| 114 |
insert(cmd, {'pop'}) |
| 115 |
end |
| 116 |
|
| 117 |
if ttoverprintboldb == 1 then |
| 118 |
insert(cmd, {'push'}) |
| 119 |
insert(cmd, {'right', random(-ttrightbold, ttrightbold)}) |
| 120 |
insert(cmd, {'down', random(-ttdownbold, ttdownbold)}) |
| 121 |
insert(cmd, {'char',j}) |
| 122 |
insert(cmd, {'pop'}) |
| 123 |
insert(cmd, {'push'}) |
| 124 |
insert(cmd, {'down', random(-ttdownbold, ttdownbold)}) |
| 125 |
insert(cmd, {'pdf', ' ' .. format("%f", greyboldb) .. ' g'}) |
| 126 |
insert(cmd, {'char',j}) |
| 127 |
insert(cmd, {'pdf', ' 0 g'}) |
| 128 |
insert(cmd, {'pop'}) |
| 129 |
end |
| 130 |
v.commands = cmd |
| 131 |
end |
| 132 |
end |
| 133 |
|
| 134 |
-- Now the main function |
| 135 |
|
| 136 |
local function define_tt_font(baseid, basename, name, csname, size, bold) |
| 137 |
local f = getfont(baseid) |
| 138 |
f.name = name |
| 139 |
f.type = 'virtual' |
| 140 |
f.fonts = {{ name = "file:"..basename, size = size}} |
| 141 |
if bold then |
| 142 |
define_bold_tt_font(f.characters) |
| 143 |
else |
| 144 |
define_normal_tt_font(f.characters) |
| 145 |
end |
| 146 |
local id = nextid(true) |
| 147 |
setfont(id, f) |
| 148 |
definefont(csname, id) |
| 149 |
end |
| 150 |
|
| 151 |
define_tt_font(base_id, ttbasefont, "cmtt10x", "myfont", ttfontsize, false) |
| 152 |
define_tt_font(base_math_id, ttmathfont, "cmtt10mx", "mymfont", ttfontsize, false) |
| 153 |
define_tt_font(base_math_id, ttmathfont, "cmtt10mx", "mymfonts", floor(0.75*ttfontsize), false) |
| 154 |
define_tt_font(base_id, ttbasefont, "cmtt10bx", "mybfont", ttfontsize, true) |
| 155 |
define_tt_font(base_math_id, ttmathfont, "cmtt10mbx", "mymbfont", ttfontsize, true) |
| 156 |
define_tt_font(base_math_id, ttmathfont, "cmtt10mbx", "mymbfonts", floor(0.75*ttfontsize), true) |
| 157 |
|