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)
51 (let* ((coding-alist (ly:font-encoding-alist font))
52 (font-encoding (assoc-get 'output-name coding-alist))
55 "\\font\\lilypond" (tex-font-command font) "="
56 (ly:font-filename font)
58 (ly:number->string (inexact->exact
60 (ly:font-magnification font)
61 (ly:bookpaper-outputscale bookpaper)))))
63 "\\def\\" (tex-font-command font) "{%\n"
64 ;; UGH. Should be handled via alist.
65 (if (equal? "Extended-TeX-Font-Encoding---Latin" font-encoding)
66 " \\fontencoding{T1}\\selectfont"
68 "\\lilypond" (tex-font-command font)
73 (define (define-fonts bookpaper)
76 "\\def\\lilypondpaperunit{mm}\n"
77 (tex-number-def "lilypondpaper" 'outputscale
78 (number->string (exact->inexact
79 (ly:bookpaper-outputscale bookpaper))))
80 (tex-string-def "lilypondpaper" 'papersize
81 (eval 'papersize (ly:output-def-scope bookpaper)))
82 (tex-string-def "lilypondpaper" 'inputencoding
83 (eval 'inputencoding (ly:output-def-scope bookpaper)))
86 (map (lambda (x) (font-load-command bookpaper x))
87 (ly:bookpaper-fonts bookpaper)))))
89 (define (header-to-file fn key val)
90 (set! key (symbol->string key))
91 (if (not (equal? "-" fn))
92 (set! fn (string-append fn "." key)))
94 (format (_ "Writing header field `~a' to `~a'...")
96 (if (equal? "-" fn) "<stdout>" fn))
100 (display val (open-file fn "w")))
101 (newline (current-error-port))
104 (define (output-scopes scopes fields basename)
105 (define (output-scope scope)
110 (let ((val (if (variable-bound? var) (variable-ref var) ""))
113 (if (and (memq sym fields) (string? val))
114 (header-to-file basename sym val))
117 (apply string-append (map output-scope scopes)))
119 (define (tex-string-def prefix key str)
120 (if (equal? "" (sans-surrounding-whitespace (sanitize-tex-string str)))
121 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
122 (string-append "\\def\\" prefix (symbol->tex-key key)
123 "{" (sanitize-tex-string str) "}%\n")))
125 (define (header bookpaper page-count classic?)
126 (let ((scale (ly:output-def-lookup bookpaper 'outputscale)))
129 "% Generated by LilyPond "
130 (lilypond-version) "\n"
131 "% at " "time-stamp,FIXME" "\n"
133 (tex-string-def "lilypond" 'classic "1")
136 (if (ly:get-option 'safe)
141 "lilypondpaper" 'linewidth
142 (ly:number->string (* scale (ly:output-def-lookup bookpaper 'linewidth))))
145 "lilypondpaper" 'interscoreline
147 (* scale (ly:output-def-lookup bookpaper 'interscoreline)))))))
151 "\\def\\scaletounit{ "
152 (number->string (cond
153 ((equal? (ly:unit) "mm") (/ 72.0 25.4))
154 ((equal? (ly:unit) "pt") (/ 72.0 72.27))
155 (else (error "unknown unit" (ly:unit)))))
157 "\\ifx\\lilypondstart\\undefined\n"
158 " \\input lilyponddefs\n"
160 "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
162 "\\lilypondspecial\n"
163 "\\lilypondpostscript\n"))
165 (define (dump-page putter page last?)
166 (ly:outputter-dump-string
168 (format "\\vbox to ~a\\outputscale{%\n\\leavevmode\n\\lybox{0}{0}{0}{0}{%\n"
169 (interval-length (ly:stencil-extent page Y))
171 (ly:outputter-dump-stencil putter page)
172 (ly:outputter-dump-string
175 "}\\vss\n}\n\\vfill\n"
176 "}\\vss\n}\n\\vfill\\lilypondpagebreak\n")))
178 (define-public (output-framework outputter book scopes fields basename )
179 (let* ((bookpaper (ly:paper-book-book-paper book))
180 (pages (ly:paper-book-pages book))
181 (last-page (car (last-pair pages)))
185 (ly:outputter-dump-string outputter x))
187 (header bookpaper (length pages) #f)
188 (define-fonts bookpaper)
192 (lambda (page) (dump-page outputter page (eq? last-page page)))
194 (ly:outputter-dump-string outputter "\\lilypondend\n")))
196 (define (dump-line putter line last?)
197 (ly:outputter-dump-string
199 (string-append "\\leavevmode\n\\lybox{0}{0}{0}{"
200 (ly:number->string (interval-length (ly:paper-system-extent line Y)))
203 (ly:outputter-dump-stencil putter (ly:paper-system-stencil line))
204 (ly:outputter-dump-string
208 "}\\interscoreline\n")))
210 (define-public (output-classic-framework
211 outputter book scopes fields basename)
212 (let* ((bookpaper (ly:paper-book-book-paper book))
213 (lines (ly:paper-book-systems book))
214 (last-line (car (last-pair lines))))
217 (ly:outputter-dump-string outputter x))
220 (header bookpaper (length lines) #f)
221 "\\def\\lilypondclassic{1}%\n"
222 (output-scopes scopes fields basename)
223 (define-fonts bookpaper)
227 (lambda (line) (dump-line outputter line (eq? line last-line))) lines)
228 (ly:outputter-dump-string outputter "\\lilypondend\n")))
231 (define-public (output-preview-framework
232 outputter book scopes fields basename )
233 (let* ((bookpaper (ly:paper-book-book-paper book))
234 (lines (ly:paper-book-systems book)))
237 (ly:outputter-dump-string outputter x))
240 (header bookpaper (length lines) #f)
241 "\\def\\lilypondclassic{1}%\n"
242 (output-scopes scopes fields basename)
243 (define-fonts bookpaper)
246 (dump-line outputter (car lines) #t)
247 (ly:outputter-dump-string outputter "\\lilypondend\n")))
250 (define-public (convert-to-pdf book name)
252 ((defs (ly:paper-book-book-paper book))
253 (size (ly:output-def-lookup defs 'papersize)))
255 (postscript->pdf (if (string? size) size "a4")
257 (basename name ".tex")
261 (define-public (convert-to-png book name)
263 ((defs (ly:paper-book-book-paper book))
264 (resolution (ly:output-def-lookup defs 'pngresolution)))
267 (if (number? resolution) resolution 90)
268 (string-append (basename name ".tex") ".ps")
271 (define-public (convert-to-ps book name)
273 ((cmd (string-append "dvips -u+ec-mftrace.map -u+lilypond.map -Ppdf "
274 (basename name ".tex"))))
276 (display (format #f (_ "Invoking ~S") cmd) (current-error-port))
277 (newline (current-error-port))
280 (define-public (convert-to-dvi book name)
282 ((cmd (string-append "latex \\\\nonstopmode \\\\input " name)))
284 (newline (current-error-port))
285 (display (format #f (_ "Invoking ~S") cmd) (current-error-port))
286 (newline (current-error-port))
288 ;; fixme: set in environment?
289 (if (ly:get-option 'safe)
290 (set! cmd (string-append "openout_any=p " cmd)))
294 (define-public (convert-to-tex book name)