;; (debug-enable 'backtrace)
+
+;; the public interface is tight.
+;; It has to be, because user-code is evalled with this module.
+
(define-module (scm output-tex)
#:re-export (quote)
- #:export (define-fonts
- font-command
+ #:export (font-command
unknown
- output-paper-def
- output-scopes
blank
dot
beam
symmetric-x-triangle
ez-ball
comment
- end-output
- experimental-on
repeat-slash
- header-end
- header
placebox
bezier-sandwich
- start-system
- stop-system
- stop-last-system
horizontal-line
filledbox
round-filled-box
draw-line
define-origin
no-origin
- start-page
- stop-page
))
(use-modules (ice-9 regex)
(string-encode-integer
(inexact->exact (round (* 1000 (ly:font-magnification font)))))))
-(define (define-fonts bookpaper)
- (string-append
- "\\def\\lilypondpaperunit{mm}" ;; UGH. FIXME.
- (tex-number-def "lilypondpaper" 'outputscale
- (number->string (exact->inexact
- (ly:bookpaper-outputscale bookpaper))))
- (tex-string-def "lilypondpapersize" 'papersize
- (eval 'papersize (ly:bookpaper-def-scope bookpaper)))
- (apply string-append
- (map (lambda (x) (font-load-command bookpaper x))
- (ly:bookpaper-fonts bookpaper)
- )))
- )
+
(define (unknown)
"%\n\\unknown\n")
-(define (symbol->tex-key sym)
+(define-public (symbol->tex-key sym)
(regexp-substitute/global
- #f "_" (output-tex-string (symbol->string sym)) 'pre "X" 'post) )
+ #f "_" (sanitize-tex-string (symbol->string sym)) 'pre "X" 'post) )
(define (string->param string)
(string-append "{" string "}"))
(define (number-pair->param o)
(string-append (number->param (car o)) (number->param (cdr o))))
-(define (tex-string-def prefix key str)
- (if (equal? "" (sans-surrounding-whitespace (output-tex-string str)))
- (string-append "\\let\\" prefix (symbol->tex-key key) "\\undefined%\n")
- (string-append "\\def\\" prefix (symbol->tex-key key)
- "{" (output-tex-string str) "}%\n")))
-
-(define (tex-number-def prefix key number)
+(define-public (tex-number-def prefix key number)
(string-append
"\\def\\" prefix (symbol->tex-key key) (string->param number) "%\n"))
-(define (output-paper-def paper)
- (apply
- string-append
- (module-map
- (lambda (sym var)
- (let ((val (variable-ref var))
- (key (symbol->tex-key sym)))
-
- (cond
- ((string? val)
- (tex-string-def "lilypondpaper" sym val))
- ((number? val)
- (tex-number-def "lilypondpaper" sym
- (if (integer? val)
- (number->string val)
- (number->string (exact->inexact val)))))
- (else ""))))
-
- (ly:output-def-scope pd))))
-
-(define (output-scopes paper scopes fields basename)
- (define (output-scope scope)
- (apply
- string-append
- (module-map
- (lambda (sym var)
- (let (;;(val (variable-ref var))
- (val (if (variable-bound? var) (variable-ref var) '""))
- (tex-key (symbol->string sym)))
-
- (if (and (memq sym fields) (string? val))
- (header-to-file basename sym val))
-
- (cond
- ((string? val)
- (tex-string-def "lilypond" sym val))
- ((number? val)
- (tex-number-def "lilypond" sym
- (if (integer? val)
- (number->string val)
- (number->string (exact->inexact val)))))
- (else ""))))
- scope)))
-
- (apply string-append
- (map output-scope scopes)))
(define (blank)
"")
(define (symmetric-x-triangle t w h)
(embedded-ps (list 'symmetric-x-triangle t w h)))
-(define (font-load-command bookpaper font)
+(define-public (font-load-command bookpaper font)
(string-append
"\\font\\" (font-command font) "="
(ly:font-filename font)
(define (ez-ball c l b)
(embedded-ps (list 'ez-ball c l b)))
-(define (header-to-file fn key val)
- (set! key (symbol->string key))
- (if (not (equal? "-" fn))
- (set! fn (string-append fn "." key))
- )
- (display
- (format "writing header field `~a' to `~a'..."
- key
- (if (equal? "-" fn) "<stdout>" fn)
- )
- (current-error-port))
- (if (equal? fn "-")
- (display val)
- (display val (open-file fn "w"))
- )
- (display "\n" (current-error-port))
- ""
- )
+
(define (embedded-ps expr)
(let ((ps-string
(define (repeat-slash w a t)
(embedded-ps (list 'repeat-slash w a t)))
-(define (header-end)
- (string-append
- "\\def\\scaletounit{ "
- (number->string (cond
- ((equal? (ly:unit) "mm") (/ 72.0 25.4))
- ((equal? (ly:unit) "pt") (/ 72.0 72.27))
- (else (error "unknown unit" (ly:unit)))
- ))
- " mul }%\n"
- "\\ifx\\lilypondstart\\undefined\n"
- " \\input lilyponddefs\n"
- "\\fi\n"
- "\\outputscale = \\lilypondpaperoutputscale\\lilypondpaperunit\n"
- "\\lilypondstart\n"
- "\\lilypondspecial\n"
- "\\lilypondpostscript\n"))
-
-(define (header creator time-stamp bookpaper page-count classic?)
- (string-append
- "% Generated by " creator "\n"
- "% at " time-stamp "\n"
- (if classic?
- (tex-string-def "lilypond" 'classic "1")
- "")
- ;; FIXME: duplicated in every backend
- "\\def\\lilypondtagline{Engraved by LilyPond (version "
- (lilypond-version)")}\n"
-
- ;; FIXME
- ;; this is -of course- severely broken, (--hwn)
- (tex-string-def "lilypondpaper" 'linewidth
- (ly:number->string (/ 18 0.175))) ; 18 cm.
- (tex-string-def "lilypondpaper" 'interscoreline
- (ly:number->string 0.0))
- ))
-
-(define (output-tex-string s)
+
+(define-public (sanitize-tex-string s) ;; todo: rename
(if (ly:get-option 'safe)
(regexp-substitute/global #f "\\\\"
(regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
(define (lily-def key val)
(let ((tex-key
(regexp-substitute/global
- #f "_" (output-tex-string key) 'pre "X" 'post))
+ #f "_" (sanitize-tex-string key) 'pre "X" 'post))
- (tex-val (output-tex-string val)))
+ (tex-val (sanitize-tex-string val)))
(if (equal? (sans-surrounding-whitespace tex-val) "")
(string-append "\\let\\" tex-key "\\undefined\n")
(string-append "\\def\\" tex-key "{" tex-val "}%\n"))))
(define (bezier-sandwich l thick)
(embedded-ps (list 'bezier-sandwich `(quote ,l) thick)))
-(define (start-system origin dim)
- (string-append
- "\\leavevmode\n"
- "\\lybox" (number-pair->param origin) (number-pair->param dim)
- "{%\n"))
-
-(define (stop-system last?)
- (if last?
- "}%\n"
- ;; FIXME: still used by lilypond.py for --preview
- "}%\n%\n\\interscoreline\n%\n"))
-
;; WTF is this in every backend?
(define (horizontal-line x1 x2 th)
(filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))
;; LaTeX gets in the way, and we need to remap
;; nonprintable chars.
-
(input-enc-name #f) ;; (assoc-get 'input-name (ly:font-encoding-alist font) ))
)
(if (string? input-enc-name)
(string-append "\\inputencoding{" input-enc-name "}")
"{}")
- (output-tex-string
+ (sanitize-tex-string
(if (vector? mapping)
(reencode-string mapping s)
s))
;; no-origin not yet supported by Xdvi
(define (no-origin) "")
-(define (start-page)
- "\n\\vbox to 0pt{\n")
-
-(define (stop-page last?)
- (if last?
- "\\vss\n}\n\\vfill\n"
- "\\vss\n}\n\\vfill\\lilypondpagebreak\n"))