summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu
diff options
context:
space:
mode:
authorReinhard Kotucha <reinhard.kotucha@web.de>2015-05-05 21:26:28 +0000
committerReinhard Kotucha <reinhard.kotucha@web.de>2015-05-05 21:26:28 +0000
commitc51296e843e35a3d133557000409674baae01b54 (patch)
tree571fdfb2f2660b3c7d6bd5d522a1268a776f8ab4 /Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu
parentfe576e3d01b31dbc35e1bc4008052c06c47d3cf3 (diff)
texlua scripts revised
pdfopt.tlu is using -dFastWebView now instead of pdfopt.ps git-svn-id: svn://tug.org/texlive/trunk@37204 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu')
-rwxr-xr-xMaster/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu147
1 files changed, 124 insertions, 23 deletions
diff --git a/Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu b/Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu
index d267d55f057..d694047cfff 100755
--- a/Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu
+++ b/Master/texmf-dist/scripts/tlgs/gswin32/pdfopt.tlu
@@ -2,13 +2,24 @@
--*-Lua-*-
-- $Id$
--- Copyright (C) 2008 Reinhard Kotucha.
+-- Copyright (C) 2008-2015 Reinhard Kotucha.
-- You may freely use, modify and/or distribute this file.
--- Replacement for pdfopt.bat.
--- Linearize PDF
+-- Generic wrapper for ps2pdf{,12,13,14} and pdfopt.
-function fixwin (args_unix)
+
+local function usage (progname)
+ local message=
+ 'Usage: '..progname..' [options] <inputfile> <outputfile>\n\n'..
+ ' <inputfile> can be either a PS, EPS, or PDF file.\n'..
+ ' A single hyphen (-) denotes stdin.\n\n'..
+ ' <outputfile> is required if <inputfile> is a PDF file\n'..
+ ' or input is read from stdin.\n'
+ return message
+end
+
+
+local function fixwin (args_unix)
if os.type == 'windows' then
local args_win={} -- new table
args_win[0]=args_unix[1]
@@ -21,43 +32,133 @@ function fixwin (args_unix)
end
end
-if os.type == 'windows' then
- gs='gswin32c'
-else
- gs='gs'
+
+local function push (t, ...)
+ local args={...}
+ for _,v in ipairs(args) do
+ if type(v) == 'table' then
+ for _,x in ipairs(v) do
+ t[#t+1]=x
+ end
+ else
+ t[#t+1]=v
+ end
+ end
+end
+
+
+local function filename (file)
+ -- strip path
+ if string.find(file, '[/\\]') then
+ return string.match(file, '.*[/\\](.*)$')
+ else
+ return file
+ end
+end
+
+
+local function basename (file)
+ -- strip extension
+ return string.match(filename(file), '(.*)%..*')
+end
+
+
+local function extension (file)
+ -- return extension if available, false otherwise.
+ local fn=filename(file)
+ if fn:find('%.') then
+ return string.match(fn, '.*%.(.*)')
+ end
end
-command={gs, '-q', '-dNODISPLAY', '-dSAFER', '-dDELAYSAFER'}
-files={}
+local function parse_cmdline ()
+ local files={}
+ local options={}
-for i=1, #arg do
- if string.find(arg[i], '^%-') then
- command[#command+1]=arg[i]
+ local progname
+ local basename=filename(arg[0])
+ if basename:find('%.') then
+ progname=basename:match('(.*)%..*')
else
- files[#files+1]=arg[i]
+ progname=basename
end
+
+ for i=1, #arg do
+ if string.find(arg[i], '^%-.+') then
+ push(options, arg[i])
+ else
+ push(files, arg[i])
+ end
+ end
+ files.input =files[1]
+ files.output=files[2]
+ return progname, options, files
end
-command[#command+1]='--'
-command[#command+1]='pdfopt.ps'
-if #files ~= 2 then
- io.stderr:write('Usage: pdfopt [options] input.pdf output.pdf\n')
- os.exit(1)
-else
- for i=1, #files do
- command[#command+1]=files[i]
+local function check_files (progname, files, default_output_ext)
+ if #files < 1 then
+ io.stderr:write(usage(progname))
+ os.exit(1)
+ end
+
+ if #files < 2 then
+ if extension(files.input) then
+ local ext=string.lower(extension(files.input))
+ if ext:match(default_output_ext..'$') then
+ io.stderr:write('!Error: no output file specified.\n\n')
+ io.stderr:write(usage(progname))
+ os.exit(1)
+ else
+ files.output=basename(files.input)..'.'..default_output_ext
+ end
+ else -- the input file has no extension
+ files.output=files.input..'.'..default_output_ext
+ end
end
end
+-- main --
+
+-- parse command line
+local progname, options, file=parse_cmdline()
+
+check_files(progname, file, 'pdf')
+
+-- determine PDF level
+local PDF_level='1.4' -- default, may be changed in the future
+if progname == 'ps2pdf12' then PDF_level='1.2'
+elseif progname == 'ps2pdf13' then PDF_level='1.3'
+elseif progname == 'ps2pdf14' then PDF_level='1.4'
+end
+
+-- setup command
+
+local command={}
+if os.type == 'unix' then command={'gs'} else command={'gswin32c'} end
+
+push(command, '-P-', '-dSAFER', options, '-dCompatibilityLevel='..PDF_level)
+
+if progname == 'pdfopt' then push(command, '-dFastWebView=true') end
+
+push(command, '-q', '-P-', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite')
+
+if os.type == 'unix' then push(command, '-sstdout=%stderr') end
+
+push(command, '-sOutputFile='..file.output, '-P-', '-dSAFER')
+
+-- We have to include the options twice because -I only takes effect
+-- if it appears before other options [see ps2pdfwr].
+push(command, options, '-c', '.setpdfwrite', '-f', file.input)
+
command=fixwin(command)
--[[ prepend an additional hyphen to activate this code
for i=0, #command do
print (command[i])
end
-os.exit(ret)
+os.exit(0)
--]]
ret=os.spawn(command)