+;; FIXME: rename
+;; what is bla supposed to do? It breaks the default output terribly:
+
+;; \def\lilypondpaperbla$\backslash${$\backslash$}{bla$\backslash${$\backslash$}}%
+;; \lyitem{089.5557}{-15.3109}{\hbox{\magfontUGQLomTVo{}bla$\backslash${$\backslash$}}}%
+;; --jcn
+(define-public (sanitize-tex-string s)
+ (if (ly:get-option 'safe)
+ (regexp-substitute/global #f "\\\\"
+ (regexp-substitute/global #f "([{}])" "bla{}" 'pre "\\" 1 'post )
+ 'pre "$\\backslash$" 'post)
+
+ s))
+
+(define (symbol->tex-key sym)
+ (regexp-substitute/global
+ #f "_" (sanitize-tex-string (symbol->string sym)) 'pre "X" 'post) )
+
+(define (tex-number-def prefix key number)
+ (string-append
+ "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
+
+(define-public (tex-font-command font)
+ (string-append
+ "magfont"
+ (string-encode-integer
+ (hashq (ly:font-filename font) 1000000))
+ "m"
+ (string-encode-integer
+ (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
+
+(define (font-load-command bookpaper font)
+ (let* ((coding-alist (ly:font-encoding-alist font))
+ (font-encoding (assoc-get 'output-name coding-alist))
+ )
+ (string-append
+ "\\font\\lilypond" (tex-font-command font) "="
+ (ly:font-filename font)
+ " scaled "
+ (ly:number->string (inexact->exact
+ (round (* 1000
+ (ly:font-magnification font)
+ (ly:bookpaper-outputscale bookpaper)))))
+ "\n"
+ "\\def\\" (tex-font-command font) "{%\n"
+ ;; UGH. Should be handled via alist.
+ (if (equal? "Extended-TeX-Font-Encoding---Latin" font-encoding)
+ " \\lilypondfontencoding{T1}"
+ " ")
+ "\\lilypond" (tex-font-command font)
+ "}\n"
+ )))
+