]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/lilylib.py
(LyricsVoiceContext): add padding for
[lilypond.git] / python / lilylib.py
index f929cd3bada9817e31bb0c837208306fbcc0f13c..dcb7c797fa8e1f3e8135d7c6d9c5052935c1b816 100644 (file)
@@ -3,7 +3,7 @@
 # 
 # source file of the GNU LilyPond music typesetter
 #
-# (c) 1998--2002  Han-Wen Nienhuys <hanwen@cs.uu.nl>
+# (c)  1998--2003  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 #                 Jan Nieuwenhuizen <janneke@gnu.org>
 
 ###  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\) *(
@@ -16,6 +16,7 @@ import shutil
 import string
 import sys
 import tempfile
+import glob
 
 ################################################################
 # Users of python modules should include this snippet
@@ -28,6 +29,7 @@ import tempfile
 
 # If set, LILYPONDPREFIX must take prevalence
 # if datadir is not set, we're doing a build and LILYPONDPREFIX
+
 import getopt, os, sys
 datadir = '@local_lilypond_datadir@'
 if not os.path.isdir (datadir):
@@ -39,6 +41,8 @@ if os.environ.has_key ('LILYPONDPREFIX') :
 
 sys.path.insert (0, os.path.join (datadir, 'python'))
 
+
+
 # Customize these
 if __name__ == '__main__':
        import lilylib as ly
@@ -86,6 +90,7 @@ try:
 except:
        pass
 
+localedir = '@localedir@'
 try:
        import gettext
        gettext.bindtextdomain ('lilypond', localedir)
@@ -98,7 +103,7 @@ underscore = _
 
 program_version = '@TOPLEVEL_VERSION@'
 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
-       program_version = '1.7.5'
+       program_version = '1.7.14'
 
 def identify (port):
        port.write ('%s (GNU LilyPond) %s\n' % (__main__.program_name, program_version))
@@ -106,7 +111,7 @@ def identify (port):
 def warranty ():
        identify (sys.stdout)
        sys.stdout.write ('\n')
-       sys.stdout.write (_ ('Copyright (c) %s by' % ' 1998--2002'))
+       sys.stdout.write (_ ('Copyright (c) %s by' % ' 1998--2003'))
        sys.stdout.write ('\n')
        map (lambda x: sys.stdout.write ('  %s\n' % x), __main__.copyright)
        sys.stdout.write ('\n')
@@ -119,10 +124,10 @@ 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 + '\n')
 
 def error (s):
-       sys.stderr.write (__main__.program_name + ":" + _ ("error: ") + s + '\n')
+       sys.stderr.write (__main__.program_name + ": " + _ ("error: ") + s + '\n')
        
 def exit (i):
        if __main__.verbose_p:
@@ -205,7 +210,7 @@ def setup_temp ():
        if not __main__.keep_temp_dir_p:
                __main__.temp_dir = tempfile.mktemp (__main__.program_name)
        try:
-               os.mkdir (__main__.temp_dir, 0777)
+               os.mkdir (__main__.temp_dir, 0700)
        except OSError:
                pass
 
@@ -241,10 +246,10 @@ def read_pipe (cmd, mode = 'r'):
                progress ('\n')
        return output
 
-def system (cmd, ignore_error = 0):
+def system (cmd, ignore_error = 0, progress_p = 0):
        
-       ''' Run CMD.  If IGNORE_ERROR is set, do not complain when CMD
-returns non zero.
+       '''System CMD.  If IGNORE_ERROR, do not complain when CMD
+returns non zero.  If PROGRESS_P, always show progress.
 
 RETURN VALUE
 
@@ -252,27 +257,35 @@ Exit status of CMD '''
 
        name = command_name (cmd)
 
-       redirect = ''
        if __main__.verbose_p:
+               progress_p = 1
                progress (_ ("Invoking `%s\'") % cmd)
-               if __main__.pseudo_filter_p:
-                       redirect = ' 1> /dev/null'
        else:
                progress ( _("Running %s...") % name)
-               redirect = ' 1> /dev/null 2>%s' % error_log (name)
 
+       redirect = ''
+       if not progress_p:
+               redirect = ' 1>/dev/null 2>' + error_log (name)
+       elif __main__.pseudo_filter_p:
+               redirect = ' 1>/dev/null'
+                       
        status = os.system (cmd + redirect)
        signal = 0x0f & status
        exit_status = status >> 8
        
        if status:
-               msg = _ ("`%s\' failed (%d)") % (name, status / exit_status)
+               
+               exit_type =  'status %d' % exit_status
+               if signal:
+                       exit_type = 'signal %d' % signal 
+               
+               msg = _ ("`%s\' failed (%s)") % (name, exit_type)
                if ignore_error:
                        if __main__.verbose_p:
                                warning (msg + ' ' + _ ("(ignored)"))
                else:
                        error (msg)
-                       if not __main__.verbose_p:
+                       if not progress_p:
                                error (_ ("The error log is as follows:"))
                                sys.stderr.write (open (error_log (name)).read ())
                        exit (status)
@@ -298,8 +311,10 @@ def strip_extension (f, ext):
 
 def cp_to_dir (pattern, dir):
        "Copy files matching re PATTERN from cwd to DIR"
+       
        # Duh.  Python style portable: cp *.EXT OUTDIR
        # system ('cp *.%s %s' % (ext, outdir), 1)
+
        files = filter (lambda x, p=pattern: re.match (p, x), os.listdir ('.'))
        map (lambda x, d=dir: shutil.copy2 (x, os.path.join (d, x)), files)
 
@@ -308,6 +323,7 @@ def cp_to_dir (pattern, dir):
 #
 # On most platforms, this is equivalent to
 #`normpath(join(os.getcwd()), PATH)'.  *Added in Python version 1.5.2*
+
 if os.path.__dict__.has_key ('abspath'):
        abspath = os.path.abspath
 else:
@@ -347,6 +363,7 @@ def setup_environment ():
                #       * clean TEXINPUTS, MFINPUTS, TFMFONTS,
                #         as these take prevalence over $TEXMF
                #         and thus may break tex run?
+               
                'TEXMF' : "{%s,%s}" % (datadir, texmf) ,
                'GS_FONTPATH' : type1_paths,
                'GS_LIB' : datadir + '/ps',
@@ -371,15 +388,11 @@ def print_environment ():
                sys.stderr.write ("%s=\"%s\"\n" % (k, v)) 
 
 def get_bbox (filename):
-       ####system ('gs -sDEVICE=bbox -q  -sOutputFile=- -dNOPAUSE %s -c quit > %s.bbox 2>&1 ' % (filename, filename))
-       #### FIXME: 2>&1 ? --jcn
        bbox = filename + '.bbox'
        ## -sOutputFile does not work with bbox?
-       ##cmd = 'gs -sDEVICE=bbox -q -sOutputFile=%s -dNOPAUSE %s -c quit' % \
-       ##      (bbox, filename)
        cmd = 'gs -sDEVICE=bbox -q -dNOPAUSE %s -c quit 2>%s' % \
              (filename, bbox)
-       system (cmd)
+       system (cmd, progress_p = 1)
        box = open (bbox).read ()
        m = re.match ('^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)',
                      box)
@@ -389,39 +402,56 @@ def get_bbox (filename):
        
        return gr
 
-def make_preview (name):
-       ## ly2dvi/lilypond-book discrepancy
-       preview_ps = name + '.preview.ps'
-       if not os.path.isfile (preview_ps):
-               preview_ps = name + '.eps'
-       bbox = get_bbox (preview_ps)
-       print 'bbox:' + `bbox`
-       trans_ps = name + '.trans.ps'
-       png = name + '.png'
-       
-       margin = 0
-       fo = open (trans_ps, 'w')
-       fo.write ('%d %d translate\n' % (-bbox[0] + margin,
-                                        -bbox[1] + margin))
-       fo.close ()
+
+def make_ps_images (ps_name, resolution = 90):
+       ## todo:
+       ## have better algorithm for deciding when to crop page,
+       ## and when to show full page
+       base = re.sub (r'\.e?ps', '', ps_name)
        
-       x = (2* margin + bbox[2] - bbox[0]) \
-           * __main__.preview_resolution / 72.0
-       y = (2* margin + bbox[3] - bbox[1]) \
-           * __main__.preview_resolution / 72.0
-       if x == 0:
-               x = 1
-       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 ''' % \
-             (x, y, png, __main__.preview_resolution, trans_ps, preview_ps)
+       header = open (ps_name).read (1024)
+       multi_page = re.search ('\n%%Pages: ', header)
+       cmd = ''
+
+       if multi_page == None:
+               bbox = get_bbox (ps_name)
+               trans_ps = ps_name + '.trans.ps'
+               output_file = re.sub (r'\.e?ps', '.png', ps_name)
        
-       system (cmd)
 
+               margin = 0
+               fo = open (trans_ps, 'w')
+               fo.write ('%d %d translate\n' % (-bbox[0] + margin,
+                                                -bbox[1] + margin))
+               fo.close ()
+
+               x = (2* margin + bbox[2] - bbox[0]) \
+                   * resolution / 72.0
+               y = (2* margin + bbox[3] - bbox[1]) \
+                   * resolution / 72.0
+               if x == 0:
+                       x = 1
+               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 ''' % \
+                     (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,
+                                                                                                                                     resolution, ps_name)
+       
        status = system (cmd)
        signal = 0xf & status
        exit_status = status >> 8
+
        
        if status:
                os.unlink (png)