From acc7622eb6b2f6ac7229bb901c4cba484731ef50 Mon Sep 17 00:00:00 2001 From: hanwen Date: Tue, 14 May 2002 22:48:05 +0000 Subject: [PATCH] lpb and l2d fixes --- ChangeLog | 9 ++ Documentation/user/lilypond-book.itely | 8 +- Documentation/user/ly2dvi.itexi | 2 + flower/include/source-file.hh | 2 + flower/include/string.hh | 6 + lily/beam.cc | 28 +++-- scripts/lilypond-book.py | 59 +++++---- scripts/ly2dvi.py | 160 ++++++++++++++++++------- 8 files changed, 190 insertions(+), 84 deletions(-) diff --git a/ChangeLog b/ChangeLog index db78e92c1c..1b714ec0da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2002-05-15 Han-Wen + + * scripts/lilypond-book.py: add --no-music option: strip all blocks. + (completize_preamble): don't barf if no preamble present. + (do_file): allow extensions on --output + + * scripts/ly2dvi.py (make_preview): make automatic preview of + first system. Small cleanups. + 2002-05-13 Han-Wen Nienhuys * lily/parser.yy (My_lily_parser): uncomment code. (Causes diff --git a/Documentation/user/lilypond-book.itely b/Documentation/user/lilypond-book.itely index d3ad0259dd..43acf821a0 100644 --- a/Documentation/user/lilypond-book.itely +++ b/Documentation/user/lilypond-book.itely @@ -291,6 +291,8 @@ files, you should add the following to the top of your LaTeX prepend @code{PREF} before each @code{-M} dependency @item -n, --no-lily don't run lilypond, but do generate the @code{.ly} files +@item --no-music + strip all lilypond blocks from the file. @item --no-pictures don't generate pictures when processing texinfo. @item --read-lys @@ -315,12 +317,6 @@ files, you should add the following to the top of your LaTeX @end table - -@command{lilypond-book} is written in python 1.5, so you have to install -@uref{http://www.python.org,python}. - - - @section Bugs The La@TeX{} \includeonly@{...@} command is ignored. diff --git a/Documentation/user/ly2dvi.itexi b/Documentation/user/ly2dvi.itexi index 5b32295a53..6158570a0c 100644 --- a/Documentation/user/ly2dvi.itexi +++ b/Documentation/user/ly2dvi.itexi @@ -36,6 +36,8 @@ files. The temporary directory is created in the current directory as @code{ly2d Generate output to @var{FILE}. The extension of @var{FILE} is ignored. @item -P,--postscript Also generate PostScript output, using dvips. +@item --preview + Also generate a picture of the first system of the score. @item -s,--set=@var{KEY}=@var{VAL} Add @var{KEY}= @var{VAL} to the settings, overriding those specified in the files. Possible keys: @code{language}, @code{latexheaders}, diff --git a/flower/include/source-file.hh b/flower/include/source-file.hh index 7c039ca0ea..45985174ab 100644 --- a/flower/include/source-file.hh +++ b/flower/include/source-file.hh @@ -6,6 +6,8 @@ #ifndef SOURCE_FILE_HH #define SOURCE_FILE_HH +#include + #include "flower-proto.hh" #include "string.hh" #include "interval.hh" diff --git a/flower/include/string.hh b/flower/include/string.hh index d64c8403f3..334b0540a0 100644 --- a/flower/include/string.hh +++ b/flower/include/string.hh @@ -10,7 +10,13 @@ #ifndef STRING_HH #define STRING_HH + +#if ( __GNUC__ > 2 ) #include /* gcc 3.0 */ +#else +class ostream; +#endif + #include "arithmetic-operator.hh" #include "flower-proto.hh" #include "string-handle.hh" diff --git a/lily/beam.cc b/lily/beam.cc index 8d6c2e0935..da17fd4d0b 100644 --- a/lily/beam.cc +++ b/lily/beam.cc @@ -43,12 +43,17 @@ #include "item.hh" #include "spanner.hh" #include "warn.hh" -#include "text-item.hh" // debug output. -#include "font-interface.hh" // debug output. #define DEBUG_QUANTING 0 + +#if DEBUG_QUANTING +#include "text-item.hh" // debug output. +#include "font-interface.hh" // debug output. +#endif + + const int INTER_QUANT_PENALTY = 1000; const int SECONDARY_BEAM_DEMERIT = 15; const int STEM_LENGTH_DEMERIT_FACTOR = 5; @@ -528,13 +533,13 @@ Beam::quanting (SCM smob) gh_double2scm (qscores[best_idx].yr)) ); - if (DEBUG_QUANTING) - { - // debug quanting - me->set_grob_property ("quant-score", - gh_double2scm (qscores[best_idx].demerits)); - me->set_grob_property ("best-idx", gh_int2scm (best_idx)); - } +#if DEBUG_QUANTING + + // debug quanting + me->set_grob_property ("quant-score", + gh_double2scm (qscores[best_idx].demerits)); + me->set_grob_property ("best-idx", gh_int2scm (best_idx)); +#endif return SCM_UNSPECIFIED; } @@ -1236,7 +1241,7 @@ Beam::brew_molecule (SCM smob) ->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS); - if (DEBUG_QUANTING) +#if (DEBUG_QUANTING) { /* This code prints the demerits for each beam. Perhaps this @@ -1258,7 +1263,8 @@ Beam::brew_molecule (SCM smob) Molecule tm = Text_item::text2molecule (me, ly_str02scm (str.ch_C ()), properties); mol.add_at_edge (Y_AXIS, UP, tm, 5.0); } - +#endif + return mol.smobbed_copy (); } diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py index 3deb00b92b..f27abf5788 100644 --- a/scripts/lilypond-book.py +++ b/scripts/lilypond-book.py @@ -40,8 +40,6 @@ import os import stat import string - - import getopt import sys import __main__ @@ -72,6 +70,8 @@ except: errorport = sys.stderr verbose_p = 0 + + try: import gettext gettext.bindtextdomain ('lilypond', localedir) @@ -143,6 +143,7 @@ g_force_lilypond_fontsize = 0 g_read_lys = 0 g_do_pictures = 1 g_num_cols = 1 +g_do_music = 1 format = '' g_run_lilypond = 1 @@ -441,6 +442,7 @@ option_definitions = [ ('PREF', '', 'dep-prefix', 'prepend PREF before each -M dependency'), ('', 'n', 'no-lily', 'don\'t run lilypond'), ('', '', 'no-pictures', "don\'t generate pictures"), + ('', '', 'no-music', "strip all lilypond blocks from output"), ('', '', 'read-lys', "don't write ly files."), ('FILE', 'o', 'outname', 'filename main output file'), ('FILE', '', 'outdir', "where to place generated files"), @@ -825,7 +827,7 @@ def scan_latex_preamble(chunks): paperguru.m_fontsize = int(m.group(1)) break - while chunks[idx][0] != 'preamble-end': + while idx < len(chunks) and chunks[idx][0] != 'preamble-end': if chunks[idx] == 'ignore': idx = idx + 1 continue @@ -871,19 +873,25 @@ def completize_preamble (chunks): m = get_re('def-post-re').search(chunk[1]) if m: post_b = 1 + if chunk[0] == 'input': m = get_re('usepackage-graphics').search(chunk[1]) if m: graphics_b = 1 x = 0 - while chunks[x][0] != 'preamble-end': + while x < len (chunks) and chunks[x][0] != 'preamble-end': x = x + 1 + + if x == len(chunks): + return chunks + if not pre_b: chunks.insert(x, ('input', get_output ('output-default-pre'))) if not post_b: chunks.insert(x, ('input', get_output ('output-default-post'))) if not graphics_b: chunks.insert(x, ('input', get_output ('usepackage-graphics'))) + return chunks @@ -959,6 +967,9 @@ def make_lilypond_file(m): (options, content))] def make_lilypond_block(m): + if not g_do_music: + return [] + if m.group('options'): options = get_re('option-sep').split (m.group('options')) else: @@ -1131,7 +1142,7 @@ def schedule_lilypond_block (chunk): newbody = newbody + get_output (s) % {'fn': basename } return ('lilypond', newbody, opts, todo, basename) -def process_lilypond_blocks(outname, chunks):#ugh rename +def process_lilypond_blocks(chunks):#ugh rename newchunks = [] # Count sections/chapters. for c in chunks: @@ -1151,6 +1162,12 @@ def system (cmd): error ('Error command exited with value %d\n' % st) return st +def quiet_system (cmd, name): + if not verbose_p: + progress ( _("Running %s...") % name) + cmd = cmd + ' 1> /dev/null 2> /dev/null' + + return system (cmd) def get_bbox (filename): system ('gs -sDEVICE=bbox -q -sOutputFile=- -dNOPAUSE %s -c quit > %s.bbox 2>&1 ' % (filename, filename)) @@ -1178,10 +1195,8 @@ def make_pixmap (name): cmd = r'''gs -g%dx%d -sDEVICE=pgm -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=- -r%d -dNOPAUSE %s %s -c quit | pnmtopng > %s''' cmd = cmd % (x, y, res, name + '.trans.eps', name + '.eps',name + '.png') - if not verbose_p: - progress ( _("Running %s...") % 'gs') - cmd = cmd + ' 1> /dev/null 2> /dev/null' - + quiet_system (cmd, 'gs') + try: status = system (cmd) except: @@ -1227,10 +1242,7 @@ def compile_all_files (chunks): texfiles = string.join (tex, ' ') cmd = 'lilypond --header=texidoc %s %s %s' \ % (lilyopts, g_extra_opts, texfiles) - if not verbose_p: - progress ( _("Running %s...") % 'LilyPond') - cmd = cmd + ' 1> /dev/null 2> /dev/null' - system (cmd) + quiet_system (cmd, 'LilyPond') # # Ugh, fixing up dependencies for .tex generation @@ -1248,16 +1260,10 @@ def compile_all_files (chunks): for e in eps: cmd = r"tex '\nonstopmode \input %s'" % e - if not verbose_p: - progress ( _("Running %s...") % 'TeX') - cmd = cmd + ' 1> /dev/null 2> /dev/null' - system (cmd) + quiet_system (cmd, 'TeX') cmd = r"dvips -E -o %s %s" % (e + '.eps', e) - if not verbose_p: - progress ( _("Running %s...") % 'dvips') - cmd = cmd + ' 1> /dev/null 2> /dev/null' - system (cmd) + quiet_system (cmd, 'dvips') for g in png: make_pixmap (g) @@ -1439,6 +1445,7 @@ def fix_epswidth (chunks): return newchunks +##docme: why global? foutn="" def do_file(input_filename): global foutn @@ -1448,7 +1455,7 @@ def do_file(input_filename): elif input_filename == '-' or input_filename == "/dev/stdin": my_outname = '-' else: - my_outname = os.path.basename (os.path.splitext(input_filename)[0]) + my_outname = os.path.basename (os.path.splitext(input_filename)[0]) + '.' + format my_depname = my_outname + '.dep' chunks = read_doc_file(input_filename) @@ -1462,7 +1469,7 @@ def do_file(input_filename): #for c in chunks: print "c:", c; #sys.exit() scan_preamble(chunks) - chunks = process_lilypond_blocks(my_outname, chunks) + chunks = process_lilypond_blocks(chunks) # Do It. if __main__.g_run_lilypond: @@ -1479,9 +1486,7 @@ def do_file(input_filename): foutn = "" __main__.do_deps = 0 else: - ## ugh, ugh. - foutn = os.path.join (g_outdir, my_outname + '.' + format) - ## foutn = os.path.join (g_outdir, my_outname) + foutn = os.path.join (g_outdir, my_outname) sys.stderr.write ("Writing `%s'\n" % foutn) fout = open (foutn, 'w') for c in chunks: @@ -1543,6 +1548,8 @@ for opt in options: g_dep_prefix = a elif o == '--no-pictures': g_do_pictures = 0 + elif o == '--no-music': + g_do_music = 0 elif o == '--read-lys': g_read_lys = 1 elif o == '--outdir': diff --git a/scripts/ly2dvi.py b/scripts/ly2dvi.py index 0016ae6f8b..4e8a532253 100644 --- a/scripts/ly2dvi.py +++ b/scripts/ly2dvi.py @@ -233,6 +233,7 @@ def system (cmd, ignore_error = 0): if verbose_p: progress (_ ("Invoking `%s\'") % cmd) + st = os.system (cmd) if st: name = re.match ('[ \t]*([^ \t]*)', cmd).group (1) @@ -287,6 +288,7 @@ temp_dir = os.path.join (original_dir, '%s.dir' % program_name) errorport = sys.stderr keep_temp_dir_p = 0 verbose_p = 0 +preview_p = 0 try: import gettext @@ -318,6 +320,7 @@ option_definitions = [ (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")), (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")), # why capital P? + ('', '', 'preview', _("Make a picture of the first system.")), ('', 'P', 'postscript', _ ("generate PostScript output")), (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")), ('', 'V', 'verbose', _ ("verbose")), @@ -354,12 +357,9 @@ lily_p = 1 paper_p = 1 output_name = '' -targets = { - 'DVI' : 0, - 'LATEX' : 0, - 'MIDI' : 0, - 'TEX' : 0, - } + +## docme: what does this do? +targets = [ 'DVI', 'LATEX', 'MIDI', 'TEX'] track_dependencies_p = 0 dependency_files = [] @@ -419,6 +419,14 @@ def print_environment (): for (k,v) in os.environ.items (): sys.stderr.write ("%s=\"%s\"\n" % (k,v)) +def quiet_system (cmd, name): + if not verbose_p: + progress ( _("Running %s...") % name) + cmd = cmd + ' 1> /dev/null 2> /dev/null' + + return system (cmd) + + def run_lilypond (files, outbase, dep_prefix): opts = '' # opts = opts + '--output=%s.tex' % outbase @@ -446,7 +454,7 @@ def run_lilypond (files, outbase, dep_prefix): # for better debugging! print_environment () print opts, fs - system ('lilypond %s %s ' % (opts, fs)) + system ('lilypond %s %s ' % (opts, fs), 'lilypond') def analyse_lilypond_output (filename, extra): @@ -538,12 +546,8 @@ ly_paper_to_latexpaper = { } #TODO: should set textheight (enlarge) depending on papersize. - -def global_latex_definition (tfiles, extra): - '''construct preamble from EXTRA, dump Latex stuff for each -lily output file in TFILES after that, and return the Latex file constructed. ''' - - +def global_latex_preamble (extra): + '''construct preamble from EXTRA,''' s = "" s = s + '% generation tag\n' @@ -606,7 +610,16 @@ lily output file in TFILES after that, and return the Latex file constructed. ' else: s = s + '\\pagestyle{empty}\n' - s = s + '\\begin{document}\n' + + return s + + +def global_latex_definition (tfiles, extra): + '''construct preamble from EXTRA, dump Latex stuff for each +lily output file in TFILES after that, and return the Latex file constructed. ''' + + + s = global_latex_preamble (extra) + '\\begin{document}\n' s = s + '\\thispagestyle{firstpage}\n' first = 1 @@ -639,12 +652,26 @@ None f.close () cmd = 'latex \\\\nonstopmode \\\\input %s' % latex_fn - - if not verbose_p: - progress ( _("Running %s...") % 'LaTeX') - cmd = cmd + ' 1> /dev/null 2> /dev/null' - - system (cmd) + quiet_system (cmd, 'LaTeX') + + if preview_p: + # make a preview by rendering only the 1st line. + preview_fn = outbase + '.preview.tex' + f = open(preview_fn, 'w') + f.write (r''' +%s +\input lilyponddefs +\pagestyle{empty} +\begin{document} +\def\interscoreline{\endinput} +\input %s +\end{document} +''' % (global_latex_preamble (extra), outbase)) + + f.close() + cmd = 'latex \\\\nonstopmode \\\\input %s' % preview_fn + quiet_system (cmd, "LaTeX for preview") + def run_dvips (outbase, extra): @@ -664,12 +691,51 @@ None. opts = opts + ' -tlandscape' cmd = 'dvips %s -o%s %s' % (opts, outbase + '.ps', outbase + '.dvi') + quiet_system (cmd, 'dvips') + + if preview_p: + cmd = 'dvips -E -o%s %s' % ( outbase + '.preview.ps', outbase + '.preview.dvi') + quiet_system (cmd, 'dvips for preview') + +def get_bbox (filename): + # cut & paste + system ('gs -sDEVICE=bbox -q -sOutputFile=- -dNOPAUSE %s -c quit > %s.bbox 2>&1 ' % (filename, filename)) + + box = open (filename + '.bbox').read() + m = re.match ('^%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)', box) + gr = [] + if m: + gr = map (string.atoi, m.groups ()) - if not verbose_p: - progress ( _("Running %s...") % 'dvips') - cmd = cmd + ' 1> /dev/null 2> /dev/null' - - system (cmd) + return gr + +# +# cut & paste from lilypond-book. +# +def make_preview (name, extra): + bbox = get_bbox (name + '.preview.ps') + margin = 0 + fo = open (name + '.trans.eps' , 'w') + fo.write ('%d %d translate\n' % (-bbox[0]+margin, -bbox[1]+margin)) + fo.close () + + res = 90 + + x = (2* margin + bbox[2] - bbox[0]) * res / 72. + y = (2* margin + bbox[3] - bbox[1]) * res / 72. + + cmd = r'''gs -g%dx%d -sDEVICE=pgm -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=- -r%d -dNOPAUSE %s %s -c quit | pnmtopng > %s''' + + cmd = cmd % (x, y, res, name + '.trans.eps', name + '.preview.ps',name + '.png') + quiet_system (cmd, 'gs') + + try: + status = system (cmd) + except: + os.unlink (name + '.png') + error ("Removing output file") + + def generate_dependency_file (depfile, outname): df = open (depfile, 'w') @@ -743,14 +809,18 @@ for opt in options: elif o == '--include' or o == '-I': include_path.append (a) elif o == '--postscript' or o == '-P': - targets['PS'] = 0 + targets.append ('PS') elif o == '--keep' or o == '-k': keep_temp_dir_p = 1 elif o == '--no-lily': lily_p = 0 + elif o == '--preview': + preview_p = 1 + targets.append ('PNG') + elif o == '--no-paper' or o == '-m': - targets = {} - targets['MIDI'] = 0 + + targets = ['MIDI'] paper_p = 0 elif o == '--output' or o == '-o': output_name = a @@ -837,7 +907,7 @@ if files and files[0] != '-': dep_prefix = 0 reldir = os.path.dirname (output_name) - if outdir != '.' and (track_dependencies_p or targets.keys ()): + if outdir != '.' and (track_dependencies_p or targets): mkdir_p (outdir, 0777) setup_environment () @@ -860,32 +930,40 @@ if files and files[0] != '-': # - init.ly setup failure # - parse error in .ly # - unexpected: assert/core dump - targets = {} + targets = [] traceback.print_exc () - if targets.has_key ('DVI') or targets.has_key ('PS'): + if 'PNG' in targets and 'PS' not in targets: + targets.append ('PS') + if 'PS' in targets and 'DVI' not in targets: + targets.append('DVI') + + if 'DVI' in targets: try: run_latex (files, outbase, extra_init) # unless: add --tex, or --latex? - del targets['TEX'] - del targets['LATEX'] + targets.remove ('TEX') + targets.remove('LATEX') except: # TODO: friendly message about TeX/LaTeX setup, # trying to run tex/latex by hand - if targets.has_key ('DVI'): - del targets['DVI'] - if targets.has_key ('PS'): - del targets['PS'] + if 'DVI' in targets: + targets.remove ('DVI') + if 'PS' in targets: + targets.remove ('PS') traceback.print_exc () - if targets.has_key ('PS'): + if 'PS' in targets: try: run_dvips (outbase, extra_init) except: - if targets.has_key ('PS'): - del targets['PS'] + if 'PS' in targets: + targets.remove ('PS') traceback.print_exc () + if 'PNG' in targets: + make_preview (outbase, extra_init) + # add DEP to targets? if track_dependencies_p: depfile = os.path.join (outdir, outbase + '.dep') @@ -894,7 +972,7 @@ if files and files[0] != '-': progress (_ ("dependencies output to `%s'...") % depfile) # Hmm, if this were a function, we could call it the except: clauses - for i in targets.keys (): + for i in targets: ext = string.lower (i) cp_to_dir ('.*\.%s$' % ext, outdir) outname = outbase + '.' + string.lower (i) -- 2.39.5