Skip to content

Instantly share code, notes, and snippets.

@phi-gamma
Created October 21, 2016 22:41
Show Gist options
  • Save phi-gamma/9cc23b3fb414c797ee8a65276601bda1 to your computer and use it in GitHub Desktop.
Save phi-gamma/9cc23b3fb414c797ee8a65276601bda1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env texlua
local stringformat = string.format
local texprint = tex.print
local stringgsub = string.gsub
local stringrep = string.rep
package.path = "/usr/share/lua/5.2/?.lua;/usr/share/lua/5.2/?/init.lua"
package.cpath = "/usr/lib/lua/5.2/?.so"
local stats = {
t_total = { tv_sec = 0, tv_nsec = 0 },
n_fonts = 0
}
local tm = require "posix.time"
local time_get = function ()
return tm.clock_gettime (tm.CLOCK_MONOTONIC)
end
local nano = 10^9
local time_diff = function (a, b)
local sec = a.tv_sec - b.tv_sec
local nsec = a.tv_nsec - b.tv_nsec
if nsec < 0 then
nsec = nano + nsec
sec = sec - 1
end
return { tv_sec = sec, tv_nsec = nsec }
end
local time_add = function (a, b)
local sec = a.tv_sec + b.tv_sec
local nsec = a.tv_nsec + b.tv_nsec
if nsec > nano then
nsec = nsec - nano
sec = sec + 1
end
return { tv_sec = sec, tv_nsec = nsec }
end
local time_fmt = function (t, full)
if full then
return stringformat ("%d.%0.9d", t.tv_sec, t.tv_nsec)
end
return stringformat ("%d.%0.3d", t.tv_sec, t.tv_nsec / 10^6)
end
local noise = function (fmt, ...)
if not fmt or fmt == true then
print ""
print (stringrep ("-", 80))
texprint [[\hbox to \hsize {\hrulefill}\par]]
return
end
local res = stringformat ("[timings] "..fmt, ...)
print (res)
res = stringgsub (res, "_", [[\char`_]])
texprint (res)
texprint [[\par]]
end
local die = function (fmt, ...)
local n = select ("#", ...)
local msg = fmt
if n > 1 then
msg = stringformat (msg, ...)
end
error (msg)
os.exit (-1)
end
local load_cached = function (fname)
local luc = dofile (fname)
print(luc)
if not luc then die ("error loading %s", fname) end
return luc
end
local load_font = function (fname)
local spec = stringformat ("file:%s:script=latn;language=dflt", fname)
local t_0 = time_get ()
local tfmdata = fonts.definers.read (spec, 42*2^16)
local t_1 = time_get ()
if not tfmdata then die "fonts.definers.read" end
local id = font.define (tfmdata)
noise ("loaded “%s” → %d", spec, id)
local t_2 = time_get ()
return { t_0, t_1, t_2 }
end
local analyze_timings = function (a, b, c)
local d_cached = time_diff (b, a)
local d_define = time_diff (c, b)
noise ("load from cache : %s s", time_fmt (d_cached))
noise ("define loaded : %s s", time_fmt (d_define))
end
local get_target_file = function (argc, argv)
if type (argc) == "number" then
return arg [1] or die "filename required"
end
if type (argc) == "string" then
return argc
end
die "no filename given"
end
local measure = function (...)
local fname = get_target_file (...)
local t_0 = time_get ()
noise (true)
local timings = load_font (fname)
local _ = analyze_timings (unpack (timings))
local t_1 = time_get ()
local d_t = time_diff (t_1, t_0)
noise ("total: %s s", time_fmt (d_t))
stats.t_total = time_add (stats.t_total, d_t)
stats.n_fonts = stats.n_fonts + 1
end
local total = function ()
noise (true)
noise ("total fonts: %d", stats.n_fonts)
noise ("total time: %s s", time_fmt (stats.t_total))
end
if _G.tex then
return { measure = measure, total = total }
else
return measure (#arg, arg)
end
\input luaotfload.sty
\directlua {
userdata = userdata or { }
userdata.fonttiming = dofile "\jobname.lua"
}
\def \timings #1{\directlua {userdata.fonttiming.measure "\luaescapestring{#1}"}}
%\timings {kozminpr6n_regular.otf}
\protected \def \timelist #1#2{%
\def\proc##1,##2\stopthat{%
\ifx##1\empty\else#1{##1}\fi
\ifx##2\empty\else\proc##2\stopthat\fi%
}%
\proc#2,\stopthat\relax%
}
\timelist \timings {%
lmroman5-regular.otf,%
lmroman6-regular.otf,%
lmroman7-regular.otf,%
lmroman8-regular.otf,%
lmroman9-regular.otf,%
lmroman10-regular.otf,%
lmroman12-regular.otf,%
lmroman17-regular.otf,%
%
lmroman5-bold.otf,%
lmroman6-bold.otf,%
lmroman7-bold.otf,%
lmroman8-bold.otf,%
lmroman9-bold.otf,%
lmroman10-bold.otf,%
lmroman12-bold.otf,%
%
lmsans8-regular.otf,%
lmsans9-regular.otf,%
lmsans10-regular.otf,%
lmsans12-regular.otf,%
lmsans17-regular.otf,%
%
lmsans8-oblique.otf,%
lmsans9-oblique.otf,%
lmsans10-oblique.otf,%
lmsans12-oblique.otf,%
lmsans17-oblique.otf,%
%
kozminpr6n_regular.otf,%
%
iwona-regular.otf,%
iwona-italic.otf,%
iwona-bold.otf,%
iwona-bolditalic.otf,%
%
garamondpremrpro.otf,%
garamondpremrpro-it.otf,%
garamondpremrpro-bd.otf,%
garamondpremrpro-bdit.otf,%
garamondpremrpro-med.otf,%
garamondpremrpro-medit.otf,%
garamondpremrpro-smbd.otf,%
garamondpremrpro-smbdit.otf,%
}
\directlua {userdata.fonttiming.total ()}
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment