| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
local options = { |
| 4 |
pdfoutput=true, |
| 5 |
redoall=false, |
| 6 |
multipicfile=false, |
| 7 |
noborder=false, |
| 8 |
dotypesetting=true, |
| 9 |
nopostprocess=false, |
| 10 |
} |
| 11 |
|
| 12 |
local usagetext = 'Usage: cachepic [options] latexfile' |
| 13 |
local tryhelp = "Try `cachepic --help' for more information" |
| 14 |
local helptext = [[ |
| 15 |
Options start with '-' or '--' and can be any of: |
| 16 |
-pdf output in (e)pdf format (default) |
| 17 |
-eps output in eps format |
| 18 |
-all regenerate all graphics |
| 19 |
-multi all graphics in one file (implies -pdf) |
| 20 |
-tight no 0.5bp margin around the graphic |
| 21 |
-notex no typesetting, only graphic postprocessing |
| 22 |
-nopic generate no graphics, only .cachepic file |
| 23 |
-usage display usage message |
| 24 |
-help,-h,-? display help message]] |
| 25 |
|
| 26 |
local msg = { |
| 27 |
MultipleInputFiles = "cannot process multiple files:\t%s\t%s", |
| 28 |
NoInputFile = "no input file given\n" .. tryhelp, |
| 29 |
UnknownOption = "unknown option: %s\n" .. tryhelp, |
| 30 |
FileOpenFailed = "could not open file: %s", |
| 31 |
NoGhostScript = "could not locate ghostscript interpreter", |
| 32 |
WrongOutputMode = "expected %s output but got %s", |
| 33 |
NoPreviews = "no cachepic previews found", |
| 34 |
MultiPicOnlyInPDF = "option '-multi' for all graphics in one file supported only in pdf mode" |
| 35 |
} |
| 36 |
|
| 37 |
function die(msgfmt, ...) |
| 38 |
error(string.format(msgfmt, ...)) |
| 39 |
end |
| 40 |
|
| 41 |
function warn(msgfmt, ...) |
| 42 |
print('warning: ' .. string.format(msgfmt, ...)) |
| 43 |
end |
| 44 |
|
| 45 |
local function qfile(filename) |
| 46 |
filename = string.gsub(filename, '"', '') |
| 47 |
if string.find(filename, "[%s%&%(%)%[%]%{%}%^%=%;%!%'%+%,%`%~]") then |
| 48 |
filename = '"' .. filename .. '"' |
| 49 |
end |
| 50 |
return filename |
| 51 |
end |
| 52 |
|
| 53 |
local function syscall(command) |
| 54 |
print('system: ' .. command) |
| 55 |
local ret = os.execute(command) |
| 56 |
if ret > 0 then os.exit(ret) |
| 57 |
else return ret end |
| 58 |
end |
| 59 |
|
| 60 |
local function copyfile(src, dst) |
| 61 |
print('copy ' .. qfile(src) .. ' ' .. qfile(dst)) |
| 62 |
local buffer, bufsize = '', 1024 |
| 63 |
local fin = io.open(src, 'rb') |
| 64 |
if not fin then die(msg.FileOpenFailed, src) end |
| 65 |
local fout = io.open(dst, 'wb') |
| 66 |
if not fout then die(msg.FileOpenFailed, dst) end |
| 67 |
while buffer do |
| 68 |
fout:write(buffer) |
| 69 |
buffer = fin:read(bufsize) |
| 70 |
end |
| 71 |
fin:close() |
| 72 |
fout:close() |
| 73 |
end |
| 74 |
|
| 75 |
local isfile = lfs and lfs.isfile or function (fname) |
| 76 |
-- if we are not running under texlua, then do it the hard way |
| 77 |
local fh = io.open(fname) |
| 78 |
return (fh and (fh:close() or true) or false) |
| 79 |
end |
| 80 |
|
| 81 |
local function whichfile(fname, path) |
| 82 |
if isfile(fname) then return fname end |
| 83 |
-- make the best guess if it is Windows or Unix |
| 84 |
local dirsep, pathsep = '/', ':' |
| 85 |
if (os.type == 'windows') or string.find(path, '\\') or string.find(path, ';') then |
| 86 |
dirsep, pathsep = '\\', ';' -- Windows |
| 87 |
end |
| 88 |
-- iterate path |
| 89 |
for dir in string.gmatch(path, '[^' .. pathsep .. ']+') do |
| 90 |
local f = dir .. dirsep .. fname |
| 91 |
-- print(f) |
| 92 |
if isfile(f) then return f end |
| 93 |
end |
| 94 |
return nil |
| 95 |
end |
| 96 |
|
| 97 |
local function findgs() |
| 98 |
local path = os.getenv('PATH') |
| 99 |
for _, fname in ipairs{'gs', 'gswin32c.exe', 'mgs.exe'} do |
| 100 |
if whichfile(fname, path) then return fname end |
| 101 |
end |
| 102 |
die(msg.NoGhostScript) |
| 103 |
end |
| 104 |
|
| 105 |
-- Process options -- |
| 106 |
if #arg == 0 then |
| 107 |
print(usagetext) |
| 108 |
print(tryhelp) |
| 109 |
os.exit(0) |
| 110 |
end |
| 111 |
local filename = nil |
| 112 |
for _, argn in ipairs(arg) do |
| 113 |
local opt, num = string.gsub(argn, '^%-%-?', '') |
| 114 |
if num == 0 then -- not an option |
| 115 |
if filename then die(msg.MultipleInputFiles, filename, opt) |
| 116 |
else filename = opt end |
| 117 |
elseif opt == 'eps' then options.pdfoutput = false |
| 118 |
elseif opt == 'pdf' then options.pdfoutput = true |
| 119 |
elseif opt == 'all' then options.redoall = true |
| 120 |
elseif opt == 'multi' then options.multipicfile = true |
| 121 |
elseif opt == 'tight' then options.noborder = true |
| 122 |
elseif opt == 'notex' then options.dotypesetting = false |
| 123 |
elseif opt == 'nopic' then options.nopostprocess = true |
| 124 |
elseif (opt == 'usage') or (opt == 'help') or (opt == 'h') or (opt == '?') then |
| 125 |
print(usagetext) |
| 126 |
if opt == 'usage' then os.exit(0) end |
| 127 |
print(helptext) |
| 128 |
os.exit(0) |
| 129 |
else die(msg.UnknownOption, argn) end |
| 130 |
end |
| 131 |
if not filename then |
| 132 |
die(msg.NoInputFile) |
| 133 |
end |
| 134 |
if options.multipicfile and not options.pdfoutput then |
| 135 |
warn(msg.MultiPicOnlyInPDF) |
| 136 |
options.multipicfile = false |
| 137 |
end |
| 138 |
filename = string.gsub(filename, '\\', '/') |
| 139 |
local noextname = string.gsub(filename, '%.[^./]*$', '') |
| 140 |
local jobname = string.match(noextname, '[^/]+$') |
| 141 |
local jobsuffix = (options.dotypesetting and '_cptmp' or '') |
| 142 |
local multipicfilename = noextname .. '-cachepic.pdf' |
| 143 |
|
| 144 |
-- Typeset document -- |
| 145 |
if options.dotypesetting then |
| 146 |
local engine = (options.pdfoutput and 'pdflatex' or 'latex') |
| 147 |
local psfixbb = (options.pdfoutput and '' or ',psfixbb') |
| 148 |
local noborder = (options.noborder and ' \\PreviewBorder=0pt' or '') |
| 149 |
local redoall = (options.redoall and ' \\makeatletter\\def\\cachepic@tryincludepic#1#2{\\xdef\\cachepic@name{#1}#2}\\makeatother' or '') |
| 150 |
local command = engine .. ' -interaction=nonstopmode -jobname=' .. jobname .. jobsuffix .. |
| 151 |
' \\def\\jobname{' .. jobname .. '}' .. |
| 152 |
' \\RequirePackage[active,tightpage,delayed,cachepic' .. psfixbb .. ']{preview}' .. |
| 153 |
noborder .. redoall .. ' \\input{' .. qfile(filename) .. '}' |
| 154 |
syscall(command) |
| 155 |
end |
| 156 |
|
| 157 |
-- Process log file -- |
| 158 |
local iLines = io.lines(noextname .. jobsuffix .. '.log') |
| 159 |
local pics = {} |
| 160 |
local pdfmode = options.pdfoutput |
| 161 |
local trim = '0 0 0 0' |
| 162 |
for line in iLines do |
| 163 |
local head, c1, c2, c3, c4, c5 = string.match(line, |
| 164 |
'^Preview:%s*([A-Za-z]+)%s*(%-?%d*)%s*(%-?%d*)%s*(%-?%d*)%s*(%-?%d*)%s*(.*)') |
| 165 |
if head == 'PDFoutput' then |
| 166 |
pdfmode = (tonumber(c1) ~= 0) |
| 167 |
elseif (head == 'Tightpage') and c1 and c2 and c3 and c4 then |
| 168 |
trim = string.format('%dsp %dsp %dsp %dsp', |
| 169 |
-tonumber(c1), -tonumber(c2), tonumber(c3), tonumber(c4)) |
| 170 |
elseif (head == 'CachePic') and c1 and c2 and c3 and c4 and c5 then |
| 171 |
local picname = c5 |
| 172 |
local picpage = tonumber(c1) |
| 173 |
local baseline = tonumber(c4) or 0 |
| 174 |
local texmacro = string.format('\\CachePicDefMacro{%s}{%d}{trim=%s}{%dsp}', |
| 175 |
picname, picpage, trim, -baseline) |
| 176 |
pics[#pics+1] = {name=picname, page=picpage, macro=texmacro} |
| 177 |
end |
| 178 |
end |
| 179 |
if #pics == 0 then |
| 180 |
warn(msg.NoPreviews) |
| 181 |
return |
| 182 |
end |
| 183 |
if pdfmode ~= options.pdfoutput then |
| 184 |
local expectedmode = options.pdfoutput and 'pdf' or 'dvi' |
| 185 |
local actualmode = pdfmode and 'pdf' or 'dvi' |
| 186 |
die(msg.WrongOutputMode, expectedmode, actualmode) |
| 187 |
end |
| 188 |
|
| 189 |
-- Write out cachepic macros -- |
| 190 |
io.output(noextname .. '.cachepic') |
| 191 |
for i = 1, #pics do |
| 192 |
io.write(pics[i].macro, '\n') |
| 193 |
end |
| 194 |
io.close() |
| 195 |
if options.nopostprocess then return end |
| 196 |
|
| 197 |
-- Postprocess pics -- |
| 198 |
if options.multipicfile then |
| 199 |
if options.pdfoutput then |
| 200 |
-- without option -all the file size will slowly grow |
| 201 |
-- extra run through GhostScript seems to prevent that |
| 202 |
-- but it degrades raster graphics or makes them much larger |
| 203 |
-- no perfect solution it seems |
| 204 |
copyfile(noextname .. jobsuffix .. '.pdf', multipicfilename) |
| 205 |
else die(msg.MultiPicOnlyInPDF) end -- this should never happen |
| 206 |
else |
| 207 |
local ext, cmdtmpl |
| 208 |
if options.pdfoutput then |
| 209 |
ext = '.pdf' |
| 210 |
filename = qfile(noextname .. jobsuffix .. '.pdf') |
| 211 |
cmdtmpl = findgs() .. |
| 212 |
' -sDEVICE=pdfwrite -dQUIET -dBATCH -dNOPAUSE -dCompatibilityLevel=1.4' .. |
| 213 |
' -dFirstPage=%s -dLastPage=%s -sOutputFile=%s %s' |
| 214 |
else |
| 215 |
ext = '.eps' |
| 216 |
filename = qfile(noextname .. jobsuffix .. '.dvi') |
| 217 |
cmdtmpl = 'dvips -E -p %s -l %s -o %s %s' |
| 218 |
end |
| 219 |
for i = 1, #pics do |
| 220 |
local outfile = string.gsub(pics[i].name, '"', '') .. ext |
| 221 |
if not isfile(outfile) or options.redoall then |
| 222 |
outfile = qfile(outfile) |
| 223 |
syscall(string.format(cmdtmpl, pics[i].page, pics[i].page, outfile, filename)) |
| 224 |
end |
| 225 |
end |
| 226 |
end |
| 227 |
--[[-- GS options preventing lossy image recompression |
| 228 |
' -dMonoImageFilter=/CCITTFaxEncode' .. |
| 229 |
' -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode' .. |
| 230 |
' -dAutoFilterGrayImages=false -dGrayImageFilter=/FlateEncode' .. |
| 231 |
]] |