1 ;;;; framework-tex.scm -- structure for TeX output
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 (define-module (scm framework-tex)
8 #:export (output-framework-tex
9 output-classic-framework-tex))
11 (use-modules (ice-9 regex)
23 (define (output-formats)
24 (define formats (ly:output-formats))
25 (set! formats (completize-formats formats))
26 (if (member "ps" formats)
27 (set! formats (cons "dvi" formats)))
28 (if (member "dvi" formats)
29 (set! formats (cons "tex" formats)))
33 (define framework-tex-module (current-module))
34 (define-public (sanitize-tex-string s)
35 (if (ly:get-option 'safe)
36 (regexp-substitute/global
38 (regexp-substitute/global #f "([{}])" s 'pre "\\" 1 'post)
39 'pre "$\\backslash$" 'post)
42 (define (symbol->tex-key sym)
43 (regexp-substitute/global
44 #f "_" (sanitize-tex-string (symbol->string sym)) 'pre "X" 'post))
46 (define (tex-number-def prefix key number)
48 "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
50 (define-public (digits->letters str)
51 (regexp-substitute/global
53 (regexp-substitute/global
60 (+ (char->integer #\A)
61 (- (char->integer #\0))
62 (char->integer (string-ref (match:substring match 1) 0)))
68 (define-public (tex-font-command-raw name magnification)
71 (digits->letters (format "~a" name))
73 (string-encode-integer
74 (inexact->exact (round (* 1000 magnification))))))
76 (define-public (tex-font-command font)
78 (ly:font-file-name font) (ly:font-magnification font)))
80 (define (otf-font-load-command paper font)
81 (let* ((sub-fonts (ly:font-sub-fonts font)))
86 (format #f "\\font\\~a=~a scaled ~a%\n"
88 sub-name (ly:font-magnification font))
93 (ly:font-magnification font)
94 (ly:paper-output-scale paper)))))))
97 (define (simple-font-load-command paper font)
99 "\\font\\~a=~a scaled ~a%\n"
100 (tex-font-command font)
101 (ly:font-file-name font)
104 (ly:font-magnification font)
105 (ly:paper-output-scale paper))))))
107 (define (font-load-command paper font)
108 (if (pair? (ly:font-sub-fonts font))
109 (otf-font-load-command paper font)
110 (simple-font-load-command paper font)))
112 (define (define-fonts paper)
115 "\\def\\lilypondpaperunit{mm}%\n"
116 (tex-number-def "lilypondpaper" 'output-scale
117 (number->string (exact->inexact
118 (ly:paper-output-scale paper))))
119 (tex-string-def "lilypondpaper" 'papersize
120 (eval 'papersizename (ly:output-def-scope paper)))
122 (tex-string-def "lilypondpaper" 'input-encoding
123 (eval 'input-encoding (ly:output-def-scope paper)))
126 (map (lambda (x) (font-load-command paper x))
127 (ly:paper-fonts paper)))))
129 (define (tex-string-def prefix key str)
130 (if (equal? "" (sans-surrounding-whitespace (sanitize-tex-string str)))
131 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
132 (string-append "\\def\\" prefix (symbol->tex-key key)
133 "{" (sanitize-tex-string str) "}%\n")))
135 (define (header paper page-count classic?)
136 (let ((scale (ly:output-def-lookup paper 'output-scale))
137 (texpaper (string-append
138 (ly:output-def-lookup paper 'papersizename)
140 (landscape? (eq? #t (ly:output-def-lookup paper 'landscape))))
142 "% Generated by LilyPond "
143 (lilypond-version) "\n"
144 "% at " "time-stamp,FIXME" "\n"
146 (tex-string-def "lilypond" 'classic "1")
149 (if (ly:get-option 'safe)
154 "lilypondpaper" 'line-width
155 (ly:number->string (* scale (ly:output-def-lookup paper 'line-width))))
156 "\\def\\lilyponddocumentclassoptions{"
157 (sanitize-tex-string texpaper)
158 (if landscape? ",landscape" "")
164 "\\def\\scaletounit{ "
165 (number->string lily-unit->bigpoint-factor)
167 "\\ifx\\lilypondstart\\undefined\n"
168 " \\input lilyponddefs\n"
171 "\\lilypondspecial\n"
172 "\\lilypondpostscript\n"))
174 (define (dump-page putter page last? with-extents?)
175 (ly:outputter-dump-string
177 (format "\\lybox{~a}{~a}{%\n"
179 (interval-start (ly:stencil-extent page X))
182 (- (interval-start (ly:stencil-extent page Y)))
184 (ly:outputter-dump-stencil putter page)
185 (ly:outputter-dump-string
189 "}%\n\\vfill\n\\lilypondpagebreak\n")))
191 (define-public (output-framework basename book scopes fields)
192 (let* ((filename (format "~a.tex" basename))
193 (outputter (ly:make-paper-outputter (open-file filename "wb") "tex"))
194 (paper (ly:paper-book-paper book))
195 (page-stencils (map page-stencil (ly:paper-book-pages book)))
196 (last-page (car (last-pair pages)))
198 (eq? #t (ly:output-def-lookup paper 'dump-extents))))
201 (ly:outputter-dump-string outputter x))
203 (header paper (length page-stencils) #f)
206 (ly:outputter-dump-string outputter "\\lilypondnopagebreak\n")
209 (dump-page outputter page (eq? last-page page) with-extents))
211 (ly:outputter-dump-string outputter "\\lilypondend\n")
212 (ly:outputter-close outputter)
213 (postprocess-output book framework-tex-module filename
216 (define (dump-line putter line last?)
217 (ly:outputter-dump-string
219 (format "\\lybox{~a}{~a}{%\n"
221 (max 0 (interval-end (paper-system-extent line X))))
223 (interval-length (paper-system-extent line Y)))))
225 (ly:outputter-dump-stencil putter (paper-system-stencil line))
226 (ly:outputter-dump-string
230 "}\\interscoreline\n")))
232 (define-public (output-classic-framework
233 basename book scopes fields)
234 (let* ((filename (format "~a.tex" basename))
235 (outputter (ly:make-paper-outputter
236 (open-file filename "w") "tex"))
237 (paper (ly:paper-book-paper book))
238 (lines (ly:paper-book-systems book))
239 (last-line (car (last-pair lines))))
242 (ly:outputter-dump-string outputter x))
245 (header paper (length lines) #f)
246 "\\def\\lilypondclassic{1}%\n"
247 (output-scopes scopes fields basename)
252 (lambda (line) (dump-line outputter line (eq? line last-line))) lines)
253 (ly:outputter-dump-string outputter "\\lilypondend\n")
254 (ly:outputter-close outputter)
255 (postprocess-output book framework-tex-module filename
259 (define-public (output-preview-framework
260 basename book scopes fields)
261 (let* ((filename (format "~a.tex" basename))
262 (outputter (ly:make-paper-outputter (open-file filename "wb")
264 (paper (ly:paper-book-paper book))
265 (lines (ly:paper-book-systems book))
266 (first-notes-index (list-index
267 (lambda (s) (not (ly:paper-system-title? s)))
272 (ly:outputter-dump-string outputter x))
276 (header paper (length lines) #f)
277 "\\def\\lilypondclassic{1}%\n"
278 (output-scopes scopes fields basename)
284 (dump-line outputter lst (not (ly:paper-system-title? lst))))
285 (take lines (1+ first-notes-index)))
286 (ly:outputter-dump-string outputter "\\lilypondend\n")
287 (ly:outputter-close outputter)
288 (postprocess-output book framework-tex-module filename
291 (define-public (convert-to-pdf book name)
292 (let* ((defs (ly:paper-book-paper book))
293 (paper-width (ly:output-def-lookup defs 'paper-width))
294 (paper-height (ly:output-def-lookup defs 'paper-height))
295 (output-scale (ly:output-def-lookup defs 'output-scale)))
296 (postscript->pdf (* paper-width output-scale (/ (ly:bp 1)))
297 (* paper-height output-scale (/ (ly:bp 1)))
298 (string-append (basename name ".tex") ".ps"))))
300 (define-public (convert-to-png book name)
301 (let* ((defs (ly:paper-book-paper book))
302 (resolution (ly:output-def-lookup defs 'pngresolution))
303 (paper-width (ly:output-def-lookup defs 'paper-width))
304 (paper-height (ly:output-def-lookup defs 'paper-height))
305 (output-scale (ly:output-def-lookup defs 'output-scale)))
307 (if (number? resolution)
309 (ly:get-option 'resolution))
311 (* paper-width output-scale (/ (ly:bp 1)))
312 (* paper-height output-scale (/ (ly:bp 1)))
314 (string-append (basename name ".tex") ".ps"))))
316 (define-public (convert-to-ps book name)
317 (let* ((paper (ly:paper-book-paper book))
318 (preview? (string-contains name ".preview"))
319 (papersizename (ly:output-def-lookup paper 'papersizename))
320 (landscape? (eq? #t (ly:output-def-lookup paper 'landscape)))
321 (base (basename name ".tex"))
322 (ps-name (format "~a.ps" base ".ps"))
323 (cmd (string-append "dvips"
328 ;; careful: papersizename is user-set.
329 (sanitize-command-option papersizename)
331 (if landscape? " -tlandscape" "")
332 (if (ly:kpathsea-find-file "lm.map")
334 (if (ly:kpathsea-find-file "ecrm10.pfa")
335 " -u+ec-mftrace.map" "")
336 " -u+lilypond.map -Ppdf" ""
339 (if (access? ps-name W_OK)
340 (delete-file ps-name))
341 (if (not (ly:get-option 'verbose))
343 (ly:message (_ "Converting to `~a'...") (string-append base ".ps"))
347 (define-public (convert-to-dvi book name)
348 (let* ((curr-extra-mem
350 (regexp-substitute/global
352 (ly:kpathsea-expand-variable "extra_mem_top")
354 (base (basename name ".tex"))
356 #f "latex \\\\nonstopmode \\\\input '~a'" name)))
358 ;; FIXME: latex 'foo bar' works, but \input 'foe bar' does not?
359 (if (string-index name (char-set #\space #\ht #\newline #\cr))
360 (ly:error (_"TeX file name must not contain whitespace: `~a'") name))
362 (setenv "extra_mem_top" (number->string (max curr-extra-mem 1024000)))
363 (let ((dvi-name (string-append base ".dvi")))
364 (if (access? dvi-name W_OK)
365 (delete-file dvi-name)))
366 (if (not (ly:get-option 'verbose))
368 (ly:message (_ "Converting to `~a'...") (string-append base ".dvi"))
371 ;; FIXME: set in environment?
372 (if (ly:get-option 'safe)
373 (set! cmd (string-append "openout_any=p " cmd)))
377 (define-public (convert-to-tex book name)