X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=python%2Flilylib.py;h=8e38dc23e1511b2eb7dce60ffe14aae0bf6542eb;hb=77a1c4f700d51250717638dc8f55dd18220fc032;hp=e705cb1e15e77d994fb59521bfe11ce5acf3243c;hpb=841670d5de9e8b6cbcc44e1561d0863dc3f9ce30;p=lilypond.git diff --git a/python/lilylib.py b/python/lilylib.py index e705cb1e15..8e38dc23e1 100644 --- a/python/lilylib.py +++ b/python/lilylib.py @@ -3,7 +3,7 @@ # # source file of the GNU LilyPond music typesetter # -# (c) 1998--2003 Han-Wen Nienhuys +# (c) 1998--2005 Han-Wen Nienhuys # Jan Nieuwenhuizen ### subst:\(^\|[^._a-z]\)\(abspath\|identify\|warranty\|progress\|warning\|error\|exit\|getopt_args\|option_help_str\|options_help_str\|help\|setup_temp\|read_pipe\|system\|cleanup_temp\|strip_extension\|cp_to_dir\|mkdir_p\|init\) *( @@ -30,7 +30,9 @@ import glob # If set, LILYPONDPREFIX must take prevalence # if datadir is not set, we're doing a build and LILYPONDPREFIX -import getopt, os, sys +import getopt +import os +import sys datadir = '@local_lilypond_datadir@' if not os.path.isdir (datadir): datadir = '@lilypond_datadir@' @@ -107,7 +109,7 @@ def identify (port): def warranty (): identify (sys.stdout) sys.stdout.write ('\n') - sys.stdout.write (_ ('Copyright (c) %s by' % ' 1998--2003')) + sys.stdout.write (_ ("Copyright (c) %s by") % '1998--2005') sys.stdout.write ('\n') map (lambda x: sys.stdout.write (' %s\n' % x), __main__.copyright) sys.stdout.write ('\n') @@ -120,16 +122,16 @@ def progress (s): sys.stderr.write (s) def warning (s): - sys.stderr.write (__main__.program_name + ": " + _ ("warning: ") + s + '\n') + sys.stderr.write (__main__.program_name + ": " + _ ("warning: %s") % s + '\n') def error (s): - sys.stderr.write (__main__.program_name + ": " + _ ("error: ") + s + '\n') + sys.stderr.write (__main__.program_name + ": " + _ ("error: %s") % s + '\n') def exit (i): if __main__.verbose_p: raise _ ('Exiting (%d)...') % i else: - sys.exit (1) + sys.exit (i) def getopt_args (opts): '''Construct arguments (LONG, SHORT) for getopt from list of options.''' @@ -183,7 +185,15 @@ def options_help_str (opts): str = '' for s in strs: - str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0]) + 3), s[1]) + first = 1 + for ss in re.split ('\n\s*', s[1]): + if first: + str = str + '%s%s%s\n' \ + % (s[0], ' ' * (w - len (s[0]) + 3), ss) + first = 0 + else: + str = str + '%s%s\n' \ + % (' ' * (w + 3), ss) return str def help (): @@ -234,10 +244,13 @@ def setup_temp (): return __main__.temp_dir def command_name (cmd): - return re.match ('^[^a-z0-9_-]*([a-z0-9_-]*)', cmd).group (1) + + # deal with "((latex ) >& 1 ) .." too + cmd = re.match ("([\(\)]*)([^ ]*)", cmd).group(2) + return os.path.basename (cmd) def error_log (name): - name = re.sub('[^a-z]','x',name) + name = re.sub('[^a-z]','x', name) return tempfile.mktemp ('%s.errorlog' % name) def read_pipe (cmd, mode = 'r'): @@ -268,7 +281,12 @@ def read_pipe (cmd, mode = 'r'): if contents: error (_ ("The error log is as follows:")) sys.stderr.write (contents) - exit (status) + + # Ugh. code dup + if error_log_file: + os.unlink (error_log_file) + + exit (1) if __main__.verbose_p: progress ('\n') @@ -288,7 +306,8 @@ RETURN VALUE Exit status of CMD ''' name = command_name (cmd) - + error_log_file = '' + if __main__.verbose_p: progress_p = 1 progress (_ ("Invoking `%s\'") % cmd) @@ -297,10 +316,11 @@ Exit status of CMD ''' redirect = '' if not progress_p: - redirect = ' 1>/dev/null 2>' + error_log (name) + error_log_file = error_log (name) + redirect = ' 1>/dev/null 2>' + error_log_file elif __main__.pseudo_filter_p: redirect = ' 1>/dev/null' - + status = os.system (cmd + redirect) signal = 0x0f & status exit_status = status >> 8 @@ -317,11 +337,15 @@ Exit status of CMD ''' warning (msg + ' ' + _ ("(ignored)")) else: error (msg) - if not progress_p: + if not progress_p and error_log_file: error (_ ("The error log is as follows:")) - sys.stderr.write (open (error_log (name)).read ()) - exit (status) + sys.stderr.write (open (error_log_file).read ()) + if error_log_file: + os.unlink (error_log_file) + exit (1) + if error_log_file: + os.unlink (error_log_file) progress ('\n') return status @@ -397,8 +421,11 @@ def setup_environment (): # and thus may break tex run? 'TEXMF' : "{%s,%s}" % (datadir, texmf) , - 'GS_FONTPATH' : type1_paths, - 'GS_LIB' : datadir + '/ps', + + # 'GS_FONTPATH' : type1_paths, + # 'GS_LIB' : datadir + '/ps', + 'GS_FONTPATH' : "", + 'GS_LIB' : "", } # $TEXMF is special, previous value is already taken care of @@ -419,15 +446,15 @@ def print_environment (): for (k,v) in os.environ.items (): sys.stderr.write ("%s=\"%s\"\n" % (k, v)) +BOUNDING_BOX_RE = '^%%BoundingBox: (-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)' def get_bbox (filename): bbox = filename + '.bbox' ## -sOutputFile does not work with bbox? - cmd = 'gs -sDEVICE=bbox -q -dNOPAUSE %s -c quit 2>%s' % \ + cmd = 'gs -sDEVICE=bbox -q -dNOPAUSE %s -c showpage -c quit 2>%s' % \ (filename, bbox) system (cmd, progress_p = 1) box = open (bbox).read () - m = re.match ('^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', - box) + m = re.match (BOUNDING_BOX_RE, box) gr = [] if m: gr = map (string.atoi, m.groups ()) @@ -442,16 +469,27 @@ def make_ps_images (ps_name, resolution = 90): base = re.sub (r'\.e?ps', '', ps_name) header = open (ps_name).read (1024) + + match = re.search (BOUNDING_BOX_RE, header, re.MULTILINE) + bbox = [] + if match: + bbox = map (string.atoi, match.groups ()) + multi_page = re.search ('\n%%Pages: ', header) cmd = '' if multi_page == None: - bbox = get_bbox (ps_name) + + if bbox == []: + bbox = get_bbox (ps_name) + trans_ps = ps_name + '.trans.ps' output_file = re.sub (r'\.e?ps', '.png', ps_name) - - margin = 0 + # need to have margin, otherwise edges of letters will + # be cropped off. + + margin = 3 fo = open (trans_ps, 'w') fo.write ('%d %d translate\n' % (-bbox[0] + margin, -bbox[1] + margin)) @@ -466,20 +504,21 @@ def make_ps_images (ps_name, resolution = 90): if y == 0: y = 1 - cmd = r'''gs -g%dx%d -sDEVICE=pnggray -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -r%d -dNOPAUSE %s %s -c quit ''' % \ + cmd = r'''gs -g%dx%d -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -r%d -dNOPAUSE %s %s -c showpage -c quit ''' % \ (x, y, output_file, resolution, trans_ps, ps_name) rms = glob.glob (base + '-page*.png') map (os.unlink, rms) else: output_file = re.sub (r'\.e?ps', '-page%d.png', ps_name) + rmfile = base + '.png' if os.path.isfile (rmfile): os.unlink (rmfile) - cmd = r'''gs -s -sDEVICE=pnggray -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -dNOPAUSE -r%d %s -c quit''' % (output_file, + cmd = r'''gs -s -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -dNOPAUSE -r%d %s -c quit''' % (output_file, resolution, ps_name) - + status = system (cmd) signal = 0xf & status exit_status = status >> 8 @@ -489,3 +528,11 @@ def make_ps_images (ps_name, resolution = 90): os.unlink (png) error (_ ("Removing output file")) exit (1) + + + cmd = r'''gs -s -sDEVICE=pnggray -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -dNOPAUSE -r%d %s -c quit''' % (output_file, + resolution, ps_name) + + files = glob.glob (re.sub ('%d', '*', output_file)) + return files +