- for (k,v) in os.environ.items ():
- sys.stderr.write ("%s=\"%s\"\n" % (k, v))
-
-
-def ps_page_count (ps_name):
- header = open (ps_name).read (1024)
- m = re.search ('\n%%Pages: ([0-9]+)', header)
- if m:
- return string.atoi (m.group (1))
- return 0
-
-def make_ps_images (ps_name, resolution = 90, papersize = "a4",
- rename_page1_p = 0):
- base = os.path.basename (re.sub (r'\.e?ps', '', ps_name))
- header = open (ps_name).read (1024)
-
- png1 = base + '.png'
- pngn = base + '-page%d.png'
- output_file = pngn
- multi_page = re.search ('\n%%Pages: ', header)
-
- # png16m is because Lily produces color nowadays.
- if not multi_page:
-
- # GS can produce empty 2nd page if pngn is used.
- output_file = png1
- cmd = r'''gs\
- -dEPSCrop\
- -dGraphicsAlphaBits=4\
- -dNOPAUSE\
- -dTextAlphaBits=4\
- -sDEVICE=png16m\
- -sOutputFile='%(output_file)s'\
- -sPAPERSIZE=%(papersize)s\
- -q\
- -r%(resolution)d\
- '%(ps_name)s'\
- -c showpage\
- -c quit ''' % vars ()
- else:
- cmd = r'''gs\
- -s\
- -dGraphicsAlphaBits=4\
- -dNOPAUSE\
- -dTextAlphaBits=4\
- -sDEVICE=png16m\
- -sOutputFile='%(output_file)s'\
- -sPAPERSIZE=%(papersize)s\
- -q\
- -r%(resolution)d\
- '%(ps_name)s'\
- -c quit''' % vars ()
-
- remove = glob.glob (png1) + glob.glob (base + '-page*.png')
- map (os.unlink, remove)
-
- status = system (cmd)
- signal = 0xf & status
- exit_status = status >> 8
-
- if status:
- remove = glob.glob (png1) + glob.glob (base + '-page*.png')
- map (os.unlink, remove)
- error (_ ("%s exited with status: %d") % ('GS', status))
- exit (1)
-
- if rename_page1_p and multi_page:
- os.rename (pngn % 1, png1)
- files = glob.glob (png1) + glob.glob (re.sub ('%d', '*', pngn))
- return files