]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/as2text.scm
fixes. polishing: add spaces and dots.
[lilypond.git] / scripts / as2text.scm
index ced2dcbb388bc53edd75af9cc97d280a4b910432..1079528e9cc3054b59990eb0224ccd5a1483f040 100644 (file)
@@ -1,18 +1,19 @@
-#!/usr/bin/guile \
+#!@GUILE@ \
 -e main -s
 !#
 ;;;; as2text.scm -- Translate AsciiScript to Text
 ;;;;
 ;;;; source file of the GNU LilyPond music typesetter
 ;;;; 
-;;;; (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
+;;;; (c)  2000--2003 Jan Nieuwenhuizen <janneke@gnu.org>
 
 ;;;; library funtions
 (use-modules
-   (ice-9 debug)
-  (ice-9 getopt-long)
-  (ice-9 string-fun)
-  (ice-9 regex))
+ (ice-9 debug)
+ (ice-9 getopt-long)
+ (ice-9 string-fun)
+ (ice-9 rdelim)
+ (ice-9 regex))
 
 ;;; Script stuff
 (define program-name "as2text")
       "unknown"
       subst-version))
 
-(define (show-version)
+(define (show-version port)
   (display (string-append 
            program-name " - LilyPond version " program-version "\n")
-          (current-error-port)))
+          port))
 
 (define (show-help)
   (display "Convert AsciiScript to text.
 
-
-Usage: as2text [OPTION]... AS-FILE
+Usage: as2text [OPTIONS]... AS-FILE
 
 Options:
-  -h,--help          this help
+  -h,--help          show this help
   -o,--output=FILE   set output file
   -v,--version       show version
-" (current-error-port)))
 
+"))
+
+;;
+;; FIXME: use (separate-fields-discarding-char) and (read-delimited "")
+;;
 (define (gulp-file name)
   (let ((port (if (equal? name "-")
                  (current-input-port)
@@ -86,7 +90,6 @@ Options:
   (set! lily-home (string-append 
                     (dirname (dirname (car args))) 
                     "/share/lilypond"))
-  (show-version)
   (let ((options (getopt-long args
                              `((output (single-char #\o)
                                           (value #t))
@@ -97,11 +100,12 @@ Options:
         (if pair (cdr pair) default)))
 
     (if (assq 'help options)
-       (begin (show-help) (exit 0)))
-           
+       (begin (show-version (current-output-port)) (show-help) (exit 0)))
+
     (if (assq 'version options)
-       (exit 0))
+       (begin (show-version (current-output-port)) (exit 0)))
 
+    (show-version (current-error-port))
     (let ((output-name (opt 'output "-"))
          (files (let ((foo (opt '() '())))
                      (if (null? foo) 
@@ -120,6 +124,8 @@ Options:
 
 (define first-line #t)
 
+(define scaling 1)
+
 ;; cursor
 (define cur-x 0)
 (define cur-y 0)
@@ -137,6 +143,9 @@ Options:
 ;; 
 (define line-char "-")
 
+;; 
+(define half-char-kludge #f)
+
 ;; the plotting fields
 (define canvas 0)
 ;; urg: 
@@ -159,7 +168,7 @@ Options:
   (let ((old-load-path %load-path))
        (set! %load-path 
             (cons (string-append 
-                   (or (getenv 'LILYPONDPREFIX) ".") "/mf")
+                   (or (getenv "LILYPONDPREFIX") ".") "/mf")
                   (cons (string-append lily-home "/mf") %load-path)))
        (let* ((path (%search-load-path name)) 
              (text (if path
@@ -183,32 +192,38 @@ Options:
   (cond ((equal? new " ") old)
        (else new)))
 
-(define (plot x y c)
-  (let ((ny (- (* -1 y) 1)))
-       (if (array-in-bounds? canvas ny x)
-          (array-set! canvas (merge-chars (array-ref canvas ny x) c) ny x)
-          (display (string-append "ouch: " (number->string x)  ","
-                                  (number->string ny) "\n")))))
+(define (plot-raw x y c)
+  (if (array-in-bounds? canvas y x)
+      (array-set! canvas (merge-chars (array-ref canvas y x) c) y x)
+      (display (string-append "ouch: " (number->string x)  ","
+                             (number->string y) "\n")
+              (current-error-port))))
+
+(define (plot x neg-y c)
+  (let ((y (- (* -1 neg-y) 1)))
+    (plot-raw (inexact->exact x) (inexact->exact y) c)))
 
 (define (plot-char c)
   (let ((bbox (car c))
-       (glyph (cadr c))) 
-       ;; BBox: (llx lly urx ury) * 1000
-       (let ((dx (inexact->exact (* .001 (car bbox))))
-            ;(dy (inexact->exact (* .001 (cadr bbox))))
-            (dy (inexact->exact (- (* .001 (cadddr bbox)) 1)))
-            (len (length glyph)))
-           ;;(display "Bbox: ") (display bbox) (newline)
-           ;;(display "dy: ") (display dy) (newline)
-           (do ((line glyph (cdr line))
-                (i 0 (+ i 1)))
-               ((= i len))
-               (plot-string (+ cur-x dx) (+ (- cur-y i) dy) (car line))))))
-
+       (glyph (cadr c))
+       (scur-x (* scaling cur-x))
+       (scur-y (* scaling cur-y)))
+    ;; BBox: (llx lly urx ury) * 1000
+    (let ((dx (inexact->exact (* .001 (car bbox))))
+         ;;(dy (inexact->exact (* .001 (cadr bbox))))
+         (dy (inexact->exact (- (* .001 (cadddr bbox)) 1)))
+         (len (length glyph)))
+      ;;(display "Bbox: ") (display bbox) (newline)
+      ;;(display "dy: ") (display dy) (newline)
+      (do ((line glyph (cdr line))
+          (i 0 (+ i 1)))
+         ((= i len))
+       (plot-string (+ scur-x dx) (+ (- scur-y i) dy) (car line))))))
+  
 (define (plot-string x y s)
   (do ((i 0 (+ i 1)))
       ((= i (string-length s)))
-      (plot (+ x i) y (substring s i (+ i 1)))))
+    (plot (+ x i) y (substring s i (+ i 1)))))
 
 (define (show-char char)
   (display char))
@@ -234,7 +249,7 @@ Options:
 
 (define (get-font name)
   ;; urg
-  (if (equal? name "as-dummy")
+  (if (equal? (substring name 0 (min (string-length name) 8)) "as-dummy")
       (get-font "default")
       (let ((entry (assoc name fonts)))
           (if entry
@@ -263,9 +278,13 @@ Options:
 
 (define (char n)
   (let* ((font (get-font cur-font))
-        (c (get-char font n)))
-       (if c
-           (plot-char c))))
+        (c
+         (if (and half-char-kludge
+                  (assoc (+ n 0.5) font))
+             (get-char font (+ n 0.5))
+             (get-char font n))))
+        (if (pair? c)
+            (plot-char c))))
 
 (define (end-output) 
   (display (string-append 
@@ -276,16 +295,24 @@ Options:
   (close cur-output-file)
   (set! cur-output-file '()))
 
+;; use plot-string
 (define (h-line len)
-  (let ((step (sign len)))
-       (do ((i 0 (+ i step)))
-          ((= i len))
-          (plot (+ cur-x i) cur-y line-char))))
+  (let ((scur-x (* scaling cur-x))
+       (scur-y (* scaling cur-y))
+       (slen (* scaling len)))
+    (let ((step (sign len)))
+         (do ((i 0 (+ i step)))
+             ((= i slen))
+           (plot (+ scur-x i) scur-y line-char)))))
 
 (define (v-line len)
-  (let ((step (sign len)))
-       (do ((i 0 (+ i step)))
-          ((= i len)) (plot cur-x (+ cur-y i) line-char))))
+  (let ((scur-x (* scaling cur-x))
+       (scur-y (* scaling cur-y))
+       (slen (* scaling len)))
+    (let ((step (sign len)))
+      (do ((i 0 (+ i step)))
+         ((= i len))
+       (plot scur-x (+ scur-y i) line-char)))))
 
 (define (header x y)
   ;(display (string-append x y "\n") (current-error-port))
@@ -293,6 +320,7 @@ Options:
 
 (define (header-end) "")
 
+;; FIXME: scale
 (define (rline-to dx dy)
   (plot (inexact->exact cur-x) (inexact->exact cur-y) line-char)
   (plot (inexact->exact (+ cur-x dx)) (inexact->exact (+ cur-y dy)) line-char)
@@ -327,7 +355,7 @@ Options:
 
 (define (load-font name mag)
   ;; urg: don't load dummy font
-  (if (not (equal? name "as-dummy"))
+  (if (not (equal? (substring name 0 (min (string-length name) 8)) "as-dummy"))
       (let ((text (af-gulp-file (string-append name ".af"))))
           (if (< 0 (string-length text))
               (let* ((char-list (cdr 
@@ -339,9 +367,16 @@ Options:
                     (if (< 0 (length font))
                         (set! fonts (cons (cons name font) fonts))))))))
 
+(define (number->rounded-exact x)
+  (* (sign x) (inexact->exact (abs x))))
+  
 (define (move-to x y)
   (set! cur-x x)
-  (set! cur-y y))
+  (let ((ey (number->rounded-exact y)))
+    (if (= 0.5 (- (abs ey) (abs y)))
+       (set! half-char-kludge #t)
+       (set! half-char-kludge #f))
+    (set! cur-y ey)))
 
 (define (put c)
   (plot cur-x cur-y c))
@@ -356,18 +391,27 @@ Options:
 (define (set-line-char c)
   (set! line-char c))
 
-(define (start-line height)
+(define (start-system width height)
   (if first-line 
       (begin
        (set! fonts (cons (cons "default" (generate-default-font)) fonts))
        (display "\n" (current-error-port))
-       (if (defined? 'mudelapaperlinewidth)
-          (set! canvas-width 
-                (inexact->exact (string->number mudelapaperlinewidth))))))
-  (set! canvas-height height)
+       (if (defined? 'lilypondpaperoutputscale)
+          (set! scaling (inexact->exact (string->number lilypondpaperoutputscale))))
+       (if (defined? 'lilypondpaperlinewidth)
+          (let ((width (inexact->exact
+                        (string->number lilypondpaperlinewidth))))
+            (if (> width 0)
+                (set! canvas-width width)
+                ;; need long line...
+                ;;(set! canvas-width 200)
+                (set! canvas-width 80)
+                )))
+       ))
+  (set! canvas-height (inexact->exact (* scaling height)))
   (set! canvas (make-array " " canvas-height canvas-width)))
 
-(define (stop-line)
+(define (stop-system)
   (if first-line
       (let ((output-file (if (equal? cur-output-name "-")
                              (current-output-port)
@@ -392,5 +436,6 @@ Options:
           ((= i n)) 
            (let* ((n (char->integer (string-ref s i)))
                   (c (get-char font n)))
-                 (plot-char c)
+             (if (pair? c) (plot-char c))
                  (rmove-to (char-width c) 0)))))
+