summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/tlgs/gswin32/ps2ps.tlu
blob: 063fc76d0cc84dbbe94294adbfed6038ca08dd75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env texlua
--*-Lua-*-
-- $Id$

-- Copyright (C) 2010 Reinhard Kotucha.
-- You may freely use, modify and/or distribute this file.

-- Replacement for ps2ps2.bat.
-- Converting PostScript 3 or PDF into PostScript 2 with the
-- Ghostscript 'ps2write' device.
-- This generates a PDF-style stream with an attached 
-- PostScript program to interpret it.


local function usage (progname)
   local message=
      'Usage: '..progname..' [options] <inputfile> <outputfile>\n\n'..
      '       <inputfile> can be either a PS, EPS, PDF file, or stdin.\n'..
      '       A single hyphen (-) denotes stdin.\n'
   return message
end


function fixwin(args_unix)
   if os.type == 'windows' then
      local args_win={}  -- new table
      args_win[0]=args_unix[1]
      for i=1, #args_unix do  
	 args_win[i]='"'..args_unix[i]..'"'
      end
      return args_win
   else
      return args_unix
   end
end


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


local function parse_cmdline ()
   local files={}
   local options={}

   local progname
   local basename=filename(arg[0])
   if basename:find('%.') then
      progname=basename:match('(.*)%..*')
   else
      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


local function check_files (progname, files) 
   if #files < 2 then
      io.stderr:write(usage(progname))
      os.exit(1)
   end
end

-- main --

local progname, options, file=parse_cmdline()

check_files (progname, file)

-- setup command

local command={}
if os.type == 'unix' then command={'gs'} else command={'gswin32c'} end

push(command, '-q', '-sDEVICE=ps2write')

if os.type=='unix' then push(command, '-sstdout=%stderr') end

push(command, '-sOutputFile='..file.output)
push(command, '-dNOPAUSE', '-dBATCH', '-P-', '-dSAFER')
push(command, options, file.input)

command=fixwin(command)

--[[ prepend an additional hyphen to activate this code
for i=0, #command do
   print (command[i])
end
os.exit(0)
--]]

ret=os.spawn(command)
os.exit(ret)