X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scm%2Foutput-tex.scm;h=3a3e023ebe77e302b0c65341a7763cc7ce91cb65;hb=08b2a48bdd102e2e53b4eafaf8bdc4726e4f1119;hp=759173122eb31b840e58e388f98b0b3702aa835b;hpb=c90a265bbcf3158a0252b15f5f70083b3fdae1b3;p=lilypond.git diff --git a/scm/output-tex.scm b/scm/output-tex.scm index 759173122e..3a3e023ebe 100644 --- a/scm/output-tex.scm +++ b/scm/output-tex.scm @@ -1,289 +1,185 @@ -;;; tex.scm -- implement Scheme output routines for TeX -;;; -;;; source file of the GNU LilyPond music typesetter -;;; -;;; (c) 1998--2003 Jan Nieuwenhuizen -;;; Han-Wen Nienhuys - - -(define-module (scm output-tex) ) -; (debug-enable 'backtrace) -(use-modules (scm output-ps) - (ice-9 regex) +;;;; tex.scm -- implement Scheme output routines for TeX +;;;; +;;;; source file of the GNU LilyPond music typesetter +;;;; +;;;; (c) 1998--2006 Jan Nieuwenhuizen +;;;; Han-Wen Nienhuys + + +;; The public interface is tight. +;; It has to be, because user-code is evalled with this module. + +;; ***It should also be clean, well defined, documented and reviewed*** + +;; To be reasonably safe, you probably do not want to use the TeX +;; backend anyway, but rather the PostScript backend. You may want +;; to run gs in a uml sandbox too. + + +(define-module (scm output-tex) + #:re-export (quote) + + ;; JUNK this -- see lily.scm: ly:all-output-backend-commands + #:export (unknown + blank + circle + dot + dashed-slur + named-glyph + dashed-line + zigzag-line + comment + repeat-slash + placebox + bezier-sandwich + round-filled-box + text + setcolor + resetcolor + polygon + draw-line + no-origin + grob-cause)) + +(use-modules (ice-9 regex) (ice-9 string-fun) (ice-9 format) (guile) - (lily) - ) + (srfi srfi-13) + (scm framework-tex) + (lily)) + -(define this-module (current-module)) ;;;;;;;; ;;;;;;;; DOCUMENT ME! ;;;;;;;; -(define font-name-alist '()) - -(define (tex-encoded-fontswitch name-mag) - (let* ((iname-mag (car name-mag)) - (ename-mag (cdr name-mag))) - - (cons iname-mag - (cons ename-mag - (string-append "magfont" - (string-encode-integer - (hashq (car ename-mag) 1000000)) - "m" - (string-encode-integer - (inexact->exact (round (* 1000 (cdr ename-mag)))))))))) - -(define (define-fonts internal-external-name-mag-pairs) - (set! font-name-alist (map tex-encoded-fontswitch - internal-external-name-mag-pairs)) - (apply string-append - (map (lambda (x) - (font-load-command (car x) (cdr x))) - (map cdr font-name-alist)))) - - - -;; urg, how can exp be #unspecified? -- in sketch output -;; -;; set! returns # --hwn -(define (fontify name-mag-pair exp) - (string-append (select-font name-mag-pair) - exp)) +(define (char font i) + (string-append "\\" (tex-font-command font) + "\\char" (ly:inexact->string i 10) " ")) (define (unknown) "%\n\\unknown\n") -(define (select-font name-mag-pair) - (let* - ( - (c (assoc name-mag-pair font-name-alist)) - ) - - (if (eq? c #f) - (begin - (ly:warn (string-append - "Programming error: No such font known " - (car name-mag-pair) " " - (ly:number->string (cdr name-mag-pair)))) - - (display "FAILED\n" (current-error-port)) - (if #f ;(pair? name-mag-pair)) - (display (object-type (car name-mag-pair)) (current-error-port)) - (write name-mag-pair (current-error-port))) - (if #f ; (pair? font-name-alist) - (display - (object-type (caaar font-name-alist)) (current-error-port)) - (write font-name-alist (current-error-port))) - - ;; (format #f "\n%FAILED: (select-font ~S)\n" name-mag-pair)) - "") - - (string-append "\\" (cddr c))))) +(define (url-link url x y) + "") (define (blank) "") +(define (circle radius thick) + (embedded-ps (list 'circle radius thick))) + (define (dot x y radius) (embedded-ps (list 'dot x y radius))) -(define (beam width slope thick blot) - (embedded-ps (list 'beam width slope thick blot))) - -(define (bracket arch_angle arch_width arch_height height arch_thick thick) - (embedded-ps (list 'bracket arch_angle arch_width arch_height height arch_thick thick))) +(define (embedded-ps string) + (embedded-ps (list 'embedded-ps string))) + +(define (dashed-slur thick on off lst) + (embedded-ps (list 'dashed-slur thick on off `(quote ,lst)))) + +(define (named-glyph font name) + (let* ((info (ly:otf-font-glyph-info font name)) + (subfont (assoc-get 'subfont info)) + (subidx (assoc-get 'subfont-index info))) + + ;;(stderr "INFO: ~S\n" info) + ;;(stderr "FONT: ~S\n" font) + (if (and subfont subidx) + (string-append "\\" (tex-font-command-raw + subfont + (ly:font-magnification font)) + "\\char" (number->string subidx)) -(define (dashed-slur thick dash l) - (embedded-ps (list 'dashed-slur thick dash `(quote ,l)))) - -(define (char i) - (string-append "\\char" (inexact->string i 10) " ")) + (begin + (ly:warning (_ "can't find ~a in ~a" name font)) + "")))) -(define (dashed-line thick on off dx dy) - (embedded-ps (list 'dashed-line thick on off dx dy))) +(define (dashed-line thick on off dx dy phase) + (embedded-ps (list 'dashed-line thick on off dx dy phase))) (define (zigzag-line centre? zzw zzh thick dx dy) (embedded-ps (list 'zigzag-line centre? zzw zzh thick dx dy))) -(define (symmetric-x-triangle t w h) - (embedded-ps (list 'symmetric-x-triangle t w h))) - -(define (font-load-command name-mag command) - (string-append - "\\font\\" command "=" - (car name-mag) - " scaled " - (ly:number->string (inexact->exact (round (* 1000 (cdr name-mag))))) - "\n")) - -(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) "" 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 (with-output-to-string (lambda () (ps-output-expression expr (current-output-port)))))) (string-append "\\embeddedps{" ps-string "}"))) - -(define (comment s) - (string-append "% " s "\n")) - -(define (end-output) - (begin - ; uncomment for some stats about lily memory - ; (display (gc-stats)) - (string-append - "\\lilypondend\n" - ; Put GC stats here. - ))) - -(define (experimental-on) - "") (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")) - -;; Note: this string must match the string in lilypond.py!!! -(define (header creator generate) - (string-append - "% Generated automatically by: " creator generate "\n")) - -(define (invoke-char s i) - (string-append - "\n\\" s "{" (inexact->string i 10) "}" )) - -;; -;; need to do something to make this really safe. -;; -(define-public (output-tex-string s) - (if security-paranoia - (regexp-substitute/global #f "\\\\" s 'pre "$\\backslash$" 'post) - s)) - -(define (lily-def key val) - (let ((tex-key - (regexp-substitute/global - #f "_" (output-tex-string key) 'pre "X" 'post)) - - (tex-val (output-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 (number->dim x) (string-append ;;ugh ly:* in backend needs compatibility func for standalone output - (ly:number->string x) " \\outputscale ")) + (ly:number->string x) " \\output-scale ")) (define (placebox x y s) - (string-append "\\lyitem{" - (ly:number->string y) "}{" - (ly:number->string x) "}{" - s "}%\n")) - -(define (bezier-sandwich l thick) - (embedded-ps (list 'bezier-sandwich `(quote ,l) thick))) - -(define (start-system wd ht) - (string-append "\\leavevmode\n" - "\\scoreshift = " (number->dim (* ht 0.5)) "\n" - "\\lilypondifundefined{lilypondscoreshift}%\n" - " {}%\n" - " {\\advance\\scoreshift by -\\lilypondscoreshift}%\n" - "\\lybox{" - (ly:number->string wd) "}{" - (ly:number->string ht) "}{%\n")) - -(define (stop-system) - "}%\n%\n\\interscoreline\n%\n") -(define (stop-last-system) - "}%\n") - -(define (horizontal-line x1 x2 th) - (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th ))) - -(define (filledbox breapth width depth height) - (if (and #f (defined? 'ps-testing)) - (embedded-ps - (string-append (numbers->string (list breapth width depth height)) - " draw_box" )) - (string-append "\\lyvrule{" - (ly:number->string (- breapth)) "}{" - (ly:number->string (+ breapth width)) "}{" - (ly:number->string depth) "}{" - (ly:number->string height) "}"))) + (string-append + "\\lyitem{" (ly:number->string x) "}{" (ly:number->string y) "}{" s "}%\n")) + +(define (bezier-sandwich lst thick) + (embedded-ps (list 'bezier-sandwich `(quote ,lst) thick))) + (define (round-filled-box x y width height blotdiam) (embedded-ps (list 'round-filled-box x y width height blotdiam))) -(define (text s) - (string-append "\\hbox{" (output-tex-string s) "}")) +(define (text font s) + (format + "\\hbox{\\~a{}~a}" (tex-font-command font) + (sanitize-tex-string s))) -(define (tuplet ht gapx dx dy thick dir) - (embedded-ps (list 'tuplet ht gapx dx dy thick dir))) +(define (setcolor r g b) + (string-append "\\color[rgb]{" + (number->string r) ", " + (number->string g) ", " + (number->string b) "}")) -(define (polygon points blotdiameter) - (embedded-ps (list 'polygon `(quote ,points) blotdiameter))) +;; FIXME +;; The PostScript backend saves the current color +;; during setcolor and restores it during resetcolor. +;; We don't do that here. +(define (resetcolor) + (string-append "\\color[rgb]{0,0,0}\n")) + +(define (polygon points blot-diameter fill) + (embedded-ps (list 'polygon `(quote ,points) blot-diameter fill))) (define (draw-line thick fx fy tx ty) (embedded-ps (list 'draw-line thick fx fy tx ty))) -;; TODO: this should be a default, which is overriden in PS -(define (between-system-string string) - string - ) -(define (define-origin file line col) - (if (procedure? point-and-click) - (string-append "\\special{src:" ;;; \\string ? - (point-and-click line col file) - "}" ) - "") - ) - ;; no-origin not yet supported by Xdvi (define (no-origin) "") -(define-public (tex-output-expression expr port) - (display (eval expr this-module) port )) + +(define-public (line-location file line col) + "Print an input location, without column number ." + (string-append (number->string line) " " file)) + +(define-public point-and-click #f) + +(define (grob-cause offset grob) + (define (line-column-location file line col) + "Print an input location, including column number ." + (string-append (number->string line) ":" + (number->string col) " " file)) + + (if (procedure? point-and-click) + (let* ((cause (ly:grob-property grob 'cause)) + (music-origin (if (ly:stream-event? cause) + (ly:event-property cause 'origin))) + (location (if (ly:input-location? music-origin) + (ly:input-file-line-column music-origin)))) + (if (pair? location) + ;;; \\string ? + (string-append "\\special{src:" + (line-column-location location) "}") + "")) + ""))