]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/lilylib.py
* tex/lilyponddefs.tex (\lilypondECencoding): New command to handle
[lilypond.git] / python / lilylib.py
index e221817bd12b9791d1301548fa87f62311787379..0f48d4dac05465d0e1bc156d0fa938a0106ab7a5 100644 (file)
@@ -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--2004')
        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.'''
@@ -234,10 +236,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'):
@@ -260,7 +265,6 @@ def read_pipe (cmd, mode = 'r'):
        signal = 0x0f & status
        exit_status = status >> 8
 
-       print 'errorlog', error_log_file 
        if status:
                error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
                
@@ -274,15 +278,13 @@ def read_pipe (cmd, mode = 'r'):
                if error_log_file:
                        os.unlink (error_log_file)
 
-               exit (status)
+               exit (1)
                
        if __main__.verbose_p:
                progress ('\n')
 
        if error_log_file:
-               print 'unlinking errorlog', error_log_file 
                os.unlink (error_log_file)
-       print 'reachin end', error_log_file 
                
        return output
 
@@ -329,10 +331,10 @@ Exit status of CMD '''
                        error (msg)
                        if not progress_p and error_log_file:
                                error (_ ("The error log is as follows:"))
-                               sys.stderr.write (error_log_file).read ()
+                               sys.stderr.write (open (error_log_file).read ())
                        if error_log_file:
                                os.unlink (error_log_file)
-                       exit (status)
+                       exit (1)
 
        if error_log_file:
                os.unlink (error_log_file)
@@ -433,6 +435,7 @@ 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?
@@ -440,8 +443,7 @@ def get_bbox (filename):
              (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 ())
@@ -456,16 +458,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.match (BOUNDING_BOX_RE, header)
+       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))
@@ -487,13 +500,14 @@ def make_ps_images (ps_name, resolution = 90):
                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,
                                                                                                                                      resolution, ps_name)
-       
+
        status = system (cmd)
        signal = 0xf & status
        exit_status = status >> 8
@@ -503,3 +517,10 @@ 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)
+
+       return output_file
+