1 ;;;; framework-tex.scm --
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 (define-module (scm framework-tex)
8 #:export (output-framework-tex
9 output-classic-framework-tex
12 (use-modules (ice-9 regex)
20 ;; what is bla supposed to do? It breaks the default output terribly:
22 ;; \def\lilypondpaperbla$\backslash${$\backslash$}{bla$\backslash${$\backslash$}}%
23 ;; \lyitem{089.5557}{-15.3109}{\hbox{\magfontUGQLomTVo{}bla$\backslash${$\backslash$}}}%
25 (define-public (sanitize-tex-string s)
26 (if (ly:get-option 'safe)
27 (regexp-substitute/global #f "\\\\"
28 (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
29 'pre "$\\backslash$" 'post)
33 (define (symbol->tex-key sym)
34 (regexp-substitute/global
35 #f "_" (sanitize-tex-string (symbol->string sym)) 'pre "X" 'post) )
37 (define (tex-number-def prefix key number)
39 "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
41 (define-public (tex-font-command font)
44 (string-encode-integer
45 (hashq (ly:font-filename font) 1000000))
47 (string-encode-integer
48 (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
50 (define (font-load-command bookpaper font)
52 "\\font\\" (tex-font-command font) "="
53 (ly:font-filename font)
55 (ly:number->string (inexact->exact
57 (ly:font-magnification font)
58 (ly:bookpaper-outputscale bookpaper)))))
62 (define (define-fonts bookpaper)
65 "\\def\\lilypondpaperunit{mm}\n"
66 (tex-number-def "lilypondpaper" 'outputscale
67 (number->string (exact->inexact
68 (ly:bookpaper-outputscale bookpaper))))
69 (tex-string-def "lilypondpaper" 'papersize
70 (eval 'papersize (ly:output-def-scope bookpaper)))
73 (map (lambda (x) (font-load-command bookpaper x))
74 (ly:bookpaper-fonts bookpaper)))))
76 (define (header-to-file fn key val)
77 (set! key (symbol->string key))
78 (if (not (equal? "-" fn))
79 (set! fn (string-append fn "." key)))
81 (format (_ "Writing header field `~a' to `~a'...")
83 (if (equal? "-" fn) "<stdout>" fn))
87 (display val (open-file fn "w")))
88 (newline (current-error-port))
91 (define (output-scopes scopes fields basename)
92 (define (output-scope scope)
97 (let ((val (if (variable-bound? var) (variable-ref var) ""))
100 (if (and (memq sym fields) (string? val))
101 (header-to-file basename sym val))
104 (apply string-append (map output-scope scopes)))
106 (define (tex-string-def prefix key str)
107 (if (equal? "" (sans-surrounding-whitespace (sanitize-tex-string str)))
108 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
109 (string-append "\\def\\" prefix (symbol->tex-key key)
110 "{" (sanitize-tex-string str) "}%\n")))
112 (define (header bookpaper page-count classic?)
113 (let ((scale (ly:output-def-lookup bookpaper 'outputscale)))
116 "% Generated by LilyPond "
117 (lilypond-version) "\n"
118 "% at " "time-stamp,FIXME" "\n"
120 (tex-string-def "lilypond" 'classic "1")
123 (if (ly:get-option 'safe)
128 "lilypondpaper" 'linewidth
129 (ly:number->string (* scale (ly:output-def-lookup bookpaper 'linewidth))))
132 "lilypondpaper" 'interscoreline
134 (* scale (ly:output-def-lookup bookpaper 'interscoreline)))))))
138 "\\def\\scaletounit{ "
139 (number->string (cond
140 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
141 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
142 (else (error "unknown unit" (ly:unit)))))
144 "\\ifx\\lilypondstart\\undefined\n"
145 " \\input lilyponddefs\n"
147 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
149 "\\lilypondspecial\n"
150 "\\lilypondpostscript\n"))
152 (define (dump-page putter page last?)
153 (ly:outputter-dump-string
155 "\\vbox to 0pt{%\n\\leavevmode\n\\lybox{0}{0}{0}{0}{%\n")
156 (ly:outputter-dump-stencil putter page)
157 (ly:outputter-dump-string
160 "}\\vss\n}\n\\vfill\n"
161 "}\\vss\n}\n\\vfill\\lilypondpagebreak\n")))
163 (define-public (output-framework outputter book scopes fields basename )
164 (let* ((bookpaper (ly:paper-book-book-paper book))
165 (pages (ly:paper-book-pages book))
166 (last-page (car (last-pair pages)))
170 (ly:outputter-dump-string outputter x))
172 (header bookpaper (length pages) #f)
173 (define-fonts bookpaper)
177 (lambda (page) (dump-page outputter page (eq? last-page page)))
179 (ly:outputter-dump-string outputter "\\lilypondend\n")))
181 (define (dump-line putter line last?)
182 (ly:outputter-dump-string
184 (string-append "\\leavevmode\n\\lybox{0}{0}{0}{"
185 (ly:number->string (interval-length (ly:paper-system-extent line Y)))
188 (ly:outputter-dump-stencil putter (ly:paper-system-stencil line))
189 (ly:outputter-dump-string
193 "}\\interscoreline\n")))
195 (define-public (output-classic-framework
196 outputter book scopes fields basename)
197 (let* ((bookpaper (ly:paper-book-book-paper book))
198 (lines (ly:paper-book-systems book))
199 (last-line (car (last-pair lines))))
202 (ly:outputter-dump-string outputter x))
205 (header bookpaper (length lines) #f)
206 "\\def\\lilypondclassic{1}%\n"
207 (output-scopes scopes fields basename)
208 (define-fonts bookpaper)
212 (lambda (line) (dump-line outputter line (eq? line last-line))) lines)
213 (ly:outputter-dump-string outputter "\\lilypondend\n")))
216 (define-public (output-preview-framework
217 outputter book scopes fields basename )
218 (let* ((bookpaper (ly:paper-book-book-paper book))
219 (lines (ly:paper-book-systems book)))
222 (ly:outputter-dump-string outputter x))
225 (header "creator" "timestamp" bookpaper (length lines) #f)
226 "\\def\\lilypondclassic{1}%\n"
227 (output-scopes scopes fields basename)
228 (define-fonts bookpaper)
231 (dump-line outputter (car lines) #t)
232 (ly:outputter-dump-string outputter "\\lilypondend\n")))
235 (define-public (convert-to-pdf book name)
237 ((defs (ly:paper-book-book-paper book))
238 (size (ly:output-def-lookup defs 'papersize)))
240 (postscript->pdf (if (string? size) size "a4")
242 (basename name ".tex")
246 (define-public (convert-to-png book name)
248 ((defs (ly:paper-book-book-paper book))
249 (resolution (ly:output-def-lookup defs 'pngresolution)))
252 (if (number? resolution) resolution 90)
253 (string-append (basename name ".tex") ".ps")
256 (define-public (convert-to-ps book name)
258 ((cmd (string-append "dvips -u+ec-mftrace.map -u+lilypond.map -Ppdf "
259 (basename name ".tex"))))
261 (display (format #f (_ "Invoking ~S") cmd) (current-error-port))
262 (newline (current-error-port))
265 (define-public (convert-to-dvi book name)
267 ((cmd (string-append "latex \\\\nonstopmode \\\\input " name)))
269 (display (format #f (_ "Invoking ~S") cmd) (current-error-port))
270 (newline (current-error-port))
272 ;; fixme: set in environment?
273 (if (ly:get-option 'safe)
274 (set! cmd (string-append "openout_any=p " cmd)))
278 (define-public (convert-to-tex book name)