]> git.donarmstrong.com Git - lilypond.git/blobdiff - scm/framework-tex.scm
*** empty log message ***
[lilypond.git] / scm / framework-tex.scm
index 20a04f5a1611eeed6f8d83feb639b74a6cf86b2a..c4b4ad8207f71de91ffde46de318867ca48c57b8 100644 (file)
             (ice-9 string-fun)
             (ice-9 format)
             (guile)
+            (srfi srfi-1)
             (srfi srfi-13)
             (lily))
 
-;; 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 )
+       (regexp-substitute/global #f "([{}])" s 'pre  "\\" 1 'post )
        'pre "$\\backslash$" 'post)
       s))
 
   (string-append
    "\\def\\" prefix (symbol->tex-key key) "{" number "}%\n"))
 
-(define-public (tex-font-command font)
+(define-public (digits->letters str)
+  (regexp-substitute/global
+   #f "[-\\._]"
+   (regexp-substitute/global
+    #f "([0-9])" str
+    'pre
+    (lambda (match)
+      (make-string
+       1
+       (integer->char
+       (+ (char->integer #\A)
+          (- (char->integer #\0))
+          (char->integer (string-ref (match:substring match 1) 0)))
+       )))
+    'post)
+   'pre ""
+   'post))
+
+(define-public (tex-font-command-raw name magnification)
   (string-append
    "magfont"
-   (string-encode-integer
-    (hashq (ly:font-filename font) 1000000))
+   (digits->letters (format "~a" name))
    "m"
    (string-encode-integer
-    (inexact->exact (round (* 1000 (ly:font-magnification font)))))))
+    (inexact->exact (round (* 1000 magnification))))))
 
-(define (font-load-command paper font)
+(define-public (tex-font-command font)
+  (tex-font-command-raw
+   (ly:font-file-name font) (ly:font-magnification font)))
+
+(define (otf-font-load-command paper font)
+  (let* ((sub-fonts (ly:font-sub-fonts font)))
+    (string-append
+     (apply string-append
+           (map
+            (lambda (sub-name)
+              (format #f "\\font\\~a=~a scaled ~a%\n"
+                      (tex-font-command-raw
+                       sub-name (ly:font-magnification font))
+                      sub-name
+                      (ly:number->string
+                       (inexact->exact
+                        (round (* 1000
+                                  (ly:font-magnification font)
+                                  (ly:paper-outputscale paper)))))))
+            sub-fonts)))))
+
+(define (simple-font-load-command paper 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)
+     (if (or (equal? (ly:font-encoding font) "cork-lm")
+            ;; FIXME: encoding: FontSpecific for cork-lm
+            (string-prefix? "lm" (ly:font-file-name font)))
+        "cork-" "")
+     (ly:font-file-name font)
      " scaled "
      (ly:number->string (inexact->exact
                         (round (* 1000
      "\n"
      "\\def\\" (tex-font-command font) "{%\n"
      ;; UGH.  Should be handled via alist.
-     (if (equal? "Extended-TeX-Font-Encoding---Latin" font-encoding)
+     (if (or (equal? "Extended-TeX-Font-Encoding---Latin" font-encoding)
+            (not font-encoding))
         "  \\lilypondfontencoding{T1}"
         "  ")
      "\\lilypond" (tex-font-command font)
      "}%\n")))
 
+(define (font-load-command paper font)
+  (if (pair? (ly:font-sub-fonts font))
+      (otf-font-load-command paper font)
+      (simple-font-load-command paper font)))
+
 (define (define-fonts paper)
   (string-append
    ;; UGH. FIXME.
       "lilypondpaper" 'linewidth
       (ly:number->string (* scale (ly:output-def-lookup paper 'linewidth))))
      "\\def\\lilyponddocumentclassoptions{"
-     texpaper
+     (sanitize-tex-string texpaper)
      (if landscape? ",landscape" "")
      "}%\n"
      (tex-string-def
 (define-public (output-preview-framework
                outputter book scopes fields basename )
   (let* ((paper (ly:paper-book-paper book))
-        (lines (ly:paper-book-systems book)))
+        (lines (ly:paper-book-systems book))
+        (first-notes-index (list-index
+                            (lambda (s) (not (ly:paper-system-title? s)))
+                            lines)))
+
     (for-each
      (lambda (x)
        (ly:outputter-dump-string outputter x))
      (list
+      
       ;;FIXME
       (header paper (length lines) #f)
       "\\def\\lilypondclassic{1}%\n"
       (define-fonts paper)
       (header-end)))
 
-    (dump-line outputter (car lines) #t)
+    (for-each
+     (lambda (l)
+       (dump-line outputter l (not (ly:paper-system-title? l))))
+     (take lines (1+ first-notes-index)))
     (ly:outputter-dump-string outputter "\\lilypondend\n")))
 
 (define-public (convert-to-pdf book name)
 (define-public (convert-to-ps book name)
   (let* ((paper (ly:paper-book-paper book))
         (preview? (string-contains name ".preview"))
+
         (papersizename (ly:output-def-lookup paper 'papersizename))
         (landscape? (eq? #t (ly:output-def-lookup paper 'landscape)))
         (base (basename name ".tex"))
         (cmd (string-append "dvips "
                             (if preview?
-                                " -E "
-                                (string-append " -t " papersizename))
-                            (if landscape?
-                                " -t landscape "
-                                " ")
-                            "  -u+ec-mftrace.map -u+lilypond.map -Ppdf "
-                            base
-
-                            (if (ly:get-option 'verbose)
-                                " "
-                                " 2>&1 1>& /dev/null ")
-                            )))
-
-    (if (ly:get-option 'verbose)
-       (begin 
-         (newline (current-error-port))
-
-         (display (format #f (_ "Invoking ~S") cmd) (current-error-port))
-         (newline (current-error-port)))
-       (display (format #f "Converting to `~a.ps'...\n" base) (current-error-port))
-       )
-    (system cmd)))
+                                "-E "
+                                (string-append
+                                 "-t"
+                                 ;; careful: papersizename is user-set.
+                                 (sanitize-command-option papersizename)
+                                 " "))
+                            (if landscape? "-t landscape " "")
+                            (if (ly:kpathsea-find-file "lm.map")
+                                "-u+lm.map " "")
+                            (if (ly:kpathsea-find-file "ecrm10.pfa")
+                                "-u+ec-mftrace.map " "")
+                            "-u+lilypond.map -Ppdf " ""
+                            base)))
+    (let ((ps-name (string-append base ".ps")))
+      (if (access? ps-name W_OK)
+         (delete-file ps-name)))
+    (if (not (ly:get-option 'verbose))
+       (begin
+         (format (current-error-port) (_ "Converting to `~a.ps'...") base)
+         (newline (current-error-port))))
+    (ly:system cmd)))
 
 (define-public (convert-to-dvi book name)
   (let* ((curr-extra-mem
            'pre "" 'post)))
         (base (basename name ".tex"))
         (cmd (string-append
-              "latex \\\\nonstopmode \\\\input " name
-              (if (ly:get-option 'verbose)
-                  " "
-                  " 2>&1 1>& /dev/null ")
-
-              )))
+              "latex \\\\nonstopmode \\\\input " name)))
     (setenv "extra_mem_top" (number->string (max curr-extra-mem 1024000)))
-    (if (ly:get-option 'verbose)
-       (begin 
-         (newline (current-error-port))
-         (display (format #f (_ "Invoking ~S") cmd) (current-error-port))
-         (newline (current-error-port)))
-       (format (current-error-port) "Converting to `~a.dvi'...\n" base))
+    (let ((dvi-name (string-append base ".dvi")))
+      (if (access? dvi-name W_OK)
+         (delete-file dvi-name)))
+    (if (not (ly:get-option 'verbose))
+       (begin
+         (format (current-error-port) (_ "Converting to `~a.dvi'...") base)
+         (newline (current-error-port))))
 
     ;; fixme: set in environment?
     (if (ly:get-option 'safe)
        (set! cmd (string-append "openout_any=p " cmd)))
 
-    (system cmd)))
+    (ly:system cmd)))
 
 (define-public (convert-to-tex book name)
   #t)