2 ;; framework-tex.scm -- structure for TeX output
4 ;; source file of the GNU LilyPond music typesetter
6 ;; (c) 2004--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 (define-module (scm framework-tex)
9 #:export (output-framework-tex
10 output-classic-framework-tex))
12 (use-modules (ice-9 regex)
21 (define (output-formats)
22 (define formats (ly:output-formats))
23 (set! formats (completize-formats formats))
24 (if (member "ps" formats)
25 (set! formats (cons "dvi" formats)))
26 (if (member "dvi" formats)
27 (set! formats (cons "tex" formats)))
31 (define framework-tex-module (current-module))
32 (define-public (sanitize-tex-string s)
33 (if (ly:get-option 'safe)
34 (regexp-substitute/global
36 (regexp-substitute/global #f "([{}])" s 'pre "\\" 1 'post)
37 'pre "$\\backslash$" 'post)
40 (define (symbol->tex-key sym)
41 (regexp-substitute/global
42 #f "_" (sanitize-tex-string (symbol->string sym)) 'pre "X" 'post))
44 (define (tex-number-def prefix key number)
46 "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
48 (define-public (digits->letters str)
49 (regexp-substitute/global
51 (regexp-substitute/global
58 (+ (char->integer #\A)
59 (- (char->integer #\0))
60 (char->integer (string-ref (match:substring match 1) 0)))
66 (define-public (tex-font-command-raw name magnification)
69 (digits->letters (format "~a" name))
71 (string-encode-integer
72 (inexact->exact (round (* 1000 magnification))))))
74 (define-public (tex-font-command font)
76 (ly:font-file-name font) (ly:font-magnification font)))
78 (define (otf-font-load-command paper font)
79 (let* ((sub-fonts (ly:font-sub-fonts font)))
84 (format #f "\\font\\~a=~a scaled ~a%\n"
86 sub-name (ly:font-magnification font))
91 (ly:font-magnification font)
92 (ly:paper-outputscale paper)))))))
95 (define (simple-font-load-command paper font)
97 "\\font\\~a=~a scaled ~a%\n"
98 (tex-font-command font)
99 (ly:font-file-name font)
102 (ly:font-magnification font)
103 (ly:paper-outputscale paper))))))
105 (define (font-load-command paper font)
106 (if (pair? (ly:font-sub-fonts font))
107 (otf-font-load-command paper font)
108 (simple-font-load-command paper font)))
110 (define (define-fonts paper)
113 "\\def\\lilypondpaperunit{mm}%\n"
114 (tex-number-def "lilypondpaper" 'outputscale
115 (number->string (exact->inexact
116 (ly:paper-outputscale paper))))
117 (tex-string-def "lilypondpaper" 'papersize
118 (eval 'papersizename (ly:output-def-scope paper)))
120 (tex-string-def "lilypondpaper" 'inputencoding
121 (eval 'inputencoding (ly:output-def-scope paper)))
124 (map (lambda (x) (font-load-command paper x))
125 (ly:paper-fonts paper)))))
127 (define (tex-string-def prefix key str)
128 (if (equal? "" (sans-surrounding-whitespace (sanitize-tex-string str)))
129 (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
130 (string-append "\\def\\" prefix (symbol->tex-key key)
131 "{" (sanitize-tex-string str) "}%\n")))
133 (define (header paper page-count classic?)
134 (let ((scale (ly:output-def-lookup paper 'outputscale))
135 (texpaper (string-append
136 (ly:output-def-lookup paper 'papersizename)
138 (landscape? (eq? #t (ly:output-def-lookup paper 'landscape))))
140 "% Generated by LilyPond "
141 (lilypond-version) "\n"
142 "% at " "time-stamp,FIXME" "\n"
144 (tex-string-def "lilypond" 'classic "1")
147 (if (ly:get-option 'safe)
152 "lilypondpaper" 'linewidth
153 (ly:number->string (* scale (ly:output-def-lookup paper 'linewidth))))
154 "\\def\\lilyponddocumentclassoptions{"
155 (sanitize-tex-string texpaper)
156 (if landscape? ",landscape" "")
159 "lilypondpaper" 'interscoreline
161 (* scale (ly:output-def-lookup paper 'interscoreline)))))))
165 "\\def\\scaletounit{ "
166 (number->string lily-unit->bigpoint-factor)
168 "\\ifx\\lilypondstart\\undefined\n"
169 " \\input lilyponddefs\n"
172 "\\lilypondspecial\n"
173 "\\lilypondpostscript\n"))
175 (define (dump-page putter page last? with-extents?)
176 (ly:outputter-dump-string
178 (format "\\lybox{~a}{~a}{%\n"
180 (interval-start (ly:stencil-extent page X))
183 (- (interval-start (ly:stencil-extent page Y)))
185 (ly:outputter-dump-stencil putter page)
186 (ly:outputter-dump-string
190 "}%\n\\vfill\n\\lilypondpagebreak\n")))
192 (define-public (output-framework basename book scopes fields)
193 (let* ((filename (format "~a.tex" basename))
194 (outputter (ly:make-paper-outputter filename "tex"))
195 (paper (ly:paper-book-paper book))
196 (pages (ly:paper-book-pages book))
197 (last-page (car (last-pair pages)))
199 (eq? #t (ly:output-def-lookup paper 'dump-extents))))
202 (ly:outputter-dump-string outputter x))
204 (header paper (length pages) #f)
207 (ly:outputter-dump-string outputter "\\lilypondnopagebreak\n")
210 (dump-page outputter page (eq? last-page page) with-extents))
212 (ly:outputter-dump-string outputter "\\lilypondend\n")
213 (ly:outputter-close outputter)
214 (postprocess-output book framework-tex-module filename
217 (define (dump-line putter line last?)
218 (ly:outputter-dump-string
220 (format "\\lybox{~a}{~a}{%\n"
222 (max 0 (interval-end (ly:paper-system-extent line X))))
224 (interval-length (ly:paper-system-extent line Y)))))
226 (ly:outputter-dump-stencil putter (ly:paper-system-stencil line))
227 (ly:outputter-dump-string
231 "}\\interscoreline\n")))
233 (define-public (output-classic-framework
234 basename book scopes fields)
235 (let* ((filename (format "~a.tex" basename))
236 (outputter (ly:make-paper-outputter filename "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 filename
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 (papersizename (ly:output-def-lookup defs 'papersizename)))
294 (postscript->pdf (if (string? papersizename) papersizename "a4")
295 (string-append (basename name ".tex") ".ps"))))
297 (define-public (convert-to-png book name)
298 (let* ((defs (ly:paper-book-paper book))
299 (resolution (ly:output-def-lookup defs 'pngresolution))
300 (papersizename (ly:output-def-lookup defs 'papersizename)))
302 (if (number? resolution)
304 (ly:get-option 'resolution))
306 (if (string? papersizename)
310 (string-append (basename name ".tex") ".ps"))))
312 (define-public (convert-to-ps book name)
313 (let* ((paper (ly:paper-book-paper book))
314 (preview? (string-contains name ".preview"))
315 (papersizename (ly:output-def-lookup paper 'papersizename))
316 (landscape? (eq? #t (ly:output-def-lookup paper 'landscape)))
317 (base (basename name ".tex"))
318 (ps-name (format "~a.ps" base ".ps"))
319 (cmd (string-append "dvips"
324 ;; careful: papersizename is user-set.
325 (sanitize-command-option papersizename)
327 (if landscape? " -tlandscape" "")
328 (if (ly:kpathsea-find-file "lm.map")
330 (if (ly:kpathsea-find-file "ecrm10.pfa")
331 " -u+ec-mftrace.map" "")
332 " -u+lilypond.map -Ppdf" ""
335 (if (access? ps-name W_OK)
336 (delete-file ps-name))
337 (if (not (ly:get-option 'verbose))
339 (ly:message (_ "Converting to `~a'...") (string-append base ".ps"))
343 (define-public (convert-to-dvi book name)
344 (let* ((curr-extra-mem
346 (regexp-substitute/global
348 (ly:kpathsea-expand-variable "$extra_mem_top")
350 (base (basename name ".tex"))
352 #f "latex \\\\nonstopmode \\\\input '~a'" name)))
354 ;; FIXME: latex 'foo bar' works, but \input 'foe bar' does not?
355 (if (string-index name (char-set #\space #\ht #\newline #\cr))
356 (ly:error (_"TeX file name must not contain whitespace: `~a'") name))
358 (setenv "extra_mem_top" (number->string (max curr-extra-mem 1024000)))
359 (let ((dvi-name (string-append base ".dvi")))
360 (if (access? dvi-name W_OK)
361 (delete-file dvi-name)))
362 (if (not (ly:get-option 'verbose))
364 (ly:message (_ "Converting to `~a'...") (string-append base ".dvi"))
367 ;; FIXME: set in environment?
368 (if (ly:get-option 'safe)
369 (set! cmd (string-append "openout_any=p " cmd)))
373 (define-public (convert-to-tex book name)