]> git.donarmstrong.com Git - lilypond.git/blob - scm/encoding.scm
* ly/book-paper-defaults.ly: set default encoding to ec.
[lilypond.git] / scm / encoding.scm
1 ;;;; encoding.scm -- font encoding
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;; WIP
8 ;; encoding.ly:
9 ;; #(display (reencode-string "adobe" "latin1" "hellö fóebär"))
10 ;;
11
12
13 (define-public (read-encoding-file filename)
14   "Read .enc file, return (COMMAND-NAME . VECTOR-OF-SYMBOLS)."
15   (let* ((raw (ly:kpathsea-gulp-file filename))
16          (string (regexp-substitute/global #f "%[^\n]*" raw 'pre "" 'post))
17          (command (match:substring
18                 (string-match "/([^ \t\n\r]*)[ \t\n\r]+[[]" string) 1))
19          (encoding (match:substring (string-match "[[](.*)[]]" string) 1))
20          (ps-lst (string-tokenize encoding))
21          (lst (map (lambda (x) (string->symbol (substring x 1))) ps-lst))
22          (vector (list->vector lst)))
23     (cons command vector)))
24
25 (define (make-encoding-table encoding-vector)
26   "Return a hash table mapping names to chars. ENCODING-VECTOR is a
27 vector of symbols."
28
29   (let* ((h (make-hash-table 256)))
30     
31     (for-each
32      (lambda (i)
33        (hash-set! h (vector-ref encoding-vector i)
34                   (integer->char i)))
35      (iota 256))
36
37     h))
38
39 (define-public (reencode-string mapping str)
40   "Apply MAPPING, a vector of [0..256) -> char, to STR"
41   (string-map (lambda (chr)
42                 (vector-ref mapping (char->integer chr)))
43               str))
44
45 (define-public (make-encoding-mapping input-encoding output-encoding)
46   "Contruct a mapping by applying output-encoding after input-encoding "
47   (list->vector
48    (map
49     (lambda (byte)
50       (let ((new-char (hash-ref
51                        output-encoding (vector-ref input-encoding byte) #f)))
52                        ;;input-encoding (vector-ref output-encoding byte) #f)))
53
54         ;; substitute space for unknown characters.
55         (if (char? new-char)
56             new-char
57             #\ )))
58     (iota 256))))
59
60
61 (define (get-coding-from-file filename)
62   "Read FILENAME, return a list containing encoding vector and table"
63    (let* ((coding (read-encoding-file filename))
64           (com (car coding))
65           (vec (cdr coding))
66           (tab (make-encoding-table vec)))
67     (list com vec tab)))
68
69 ;; coding-alist maps NAME -> (list FILENAME COMMAND VECTOR TAB)
70 (define coding-alist
71   
72   (map (lambda (x)
73          (cons (car x)
74                (cons (cdr x) (delay (get-coding-from-file (cdr x))))))
75        
76        '(
77          ;; teTeX
78          ("TeX-typewriter-text" . "09fbbfac.enc") ;; cmtt10
79          ("TeX-math-symbols" . "10037936.enc") ;; cmbsy
80          ("ASCII-caps-and-digits" . "1b6d048e.enc") ;; cminch
81          ("TeX-math-italic" . "aae443f0.enc")  ;; cmmi10
82          ("TeX-extended-ASCII" . "d9b29452.enc")
83          ("TeX-text" . "cmr.enc")
84          ("TeX-text-without-f-ligatures" . "0ef0afca.enc")
85          ("Extended-TeX-Font-Encoding---Latin" . "tex256.enc")
86          ("AdobeStandardEncoding" . "8a.enc")
87
88          ;; aliases
89          ("T1" . "tex256.enc")
90          ("adobe" . "8a.enc")
91          ("latin1" . "latin1.enc")
92          ("ec" . "EC.enc")
93          
94          ;; LilyPond.
95          ("fetaBraces" . "feta-braces-a.enc")
96          ("fetaNumber" . "feta-nummer10.enc")
97          ("fetaMusic" . "feta20.enc")
98          ("parmesanMusic" . "parmesan20.enc"))
99        ))
100
101 (define (get-coding coding-name)
102   (let ((entry (assoc-get coding-name coding-alist)))
103     (cons (car entry) (force (cdr entry)))))
104
105 (define-public (get-coding-filename coding-name)
106   (car (get-coding coding-name)))
107
108 (define-public (get-coding-command coding-name)
109   (cadr (get-coding coding-name)))
110
111 (define-public (get-coding-vector coding-name)
112   (caddr (get-coding coding-name)))
113
114 (define-public (get-coding-table coding-name)
115   (cadddr (get-coding coding-name)))