]> git.donarmstrong.com Git - lilypond.git/blob - scm/encoding.scm
* lily/performance.cc (output): remap modulo 16.
[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 as a vector of symbols."
15   (let* ((raw (ly:kpathsea-gulp-file filename))
16          (string (regexp-substitute/global #f "%[^\n]*" raw 'pre "" 'post))
17          (start (string-index string #\[))
18          (end (string-index string #\]))
19          (ps-lst (string-tokenize (substring string (+ start 1) end)))
20          (lst (map (lambda (x) (substring x 1)) ps-lst)))
21     (list->vector lst)))
22
23 (define (make-encoding-table encoding-vector)
24   "Return a hash table mapping names to chars. ENCODING-VECTOR is a
25 vector of symbols."
26
27   (let* ((h (make-hash-table 256)))
28     
29     (for-each
30      (lambda (i)
31        (hash-set! h (vector-ref encoding-vector i)
32                   (integer->char i)))
33      (iota 256))
34
35     h))
36
37 (define-public (reencode-string permutation str)
38   "Apply PERMUTATION, a vector of [0..256) -> char, to STR"
39   (string-map (lambda (chr)
40                 (vector-ref permutation (char->integer chr)))
41               str))
42
43 (define-public (make-encoding-permutation input-encoding
44                                           output-encoding)
45
46   "Contruct a permutation by applying output-encoding after input-encoding "
47   (list->vector
48    (map
49     (lambda (byte)
50       (let*
51           ((new-char (hash-ref output-encoding
52                                (vector-ref input-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   
64   (let*
65       ((vec (read-encoding-file filename))
66        (tab (make-encoding-table vec)))
67     (list vec tab)))
68
69 ;; coding-alist maps NAME -> (list VECTOR TAB)
70 (define coding-alist
71   
72   (map (lambda (x)
73          (cons (car x)
74                (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" . "f7b6d320.enc")
84          ("TeX text without f-ligatures" . "0ef0afca.enc")
85          ("Extended TeX Font Encoding - Latin" . "tex256.enc")
86          
87          ("T1" . "tex256.enc")
88
89          ;; FIXME: find full Adobe; for testing -- almost Adobe:
90          ("adobe" . "ad.enc")
91
92          ("latin1" . "cork.enc")
93          
94          ;; LilyPond.
95          ("feta braces" . "feta-braces-a.enc")
96          ("feta number" . "feta-nummer10.enc")
97          ("feta music" . "feta20.enc")
98          ("parmesan music" . "parmesan20.enc"))
99        ))
100
101 (define (get-coding coding-name)
102   (force (assoc-get coding-name coding-alist )))
103
104 (define-public (get-coding-vector coding-name)
105   (car (get-coding coding-name)))
106
107 (define-public (get-coding-table coding-name)
108   (cadr (get-coding coding-name)))
109
110
111 ;;; JUNKME
112 ;;; what's this for? --hwn
113 ;; (define-public (encoded-index font-coding input-coding code)
114 ;; This was used by simplistic first incarnation of reencode-string --jcn