]> git.donarmstrong.com Git - lilypond.git/blob - scm/lily.scm
* scm/new-font.scm: new file. Tree based font lookup.
[lilypond.git] / scm / lily.scm
1 ;;;; lily.scm -- implement Scheme output routines for TeX and PostScript
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;; Library functions
9
10
11 (use-modules (ice-9 regex)
12              (ice-9 safe)
13              (oop goops)
14              (srfi srfi-1)  ; lists
15              (srfi srfi-13)) ; strings
16
17 (define-public safe-module (make-safe-module))
18
19 (define-public (myd k v) (display k) (display ": ") (display v) (display ", "))
20
21 ;;; General settings
22 ;;; debugging evaluator is slower.  This should
23 ;;; have a more sensible default.
24
25
26 (if (ly:get-option 'verbose)
27     (begin
28       (debug-enable 'debug)
29       (debug-enable 'backtrace)
30       (read-enable 'positions) ))
31
32
33 (define-public (line-column-location line col file)
34   "Print an input location, including column number ."
35   (string-append (number->string line) ":"
36                  (number->string col) " " file)
37   )
38
39 (define-public (line-location line col file)
40   "Print an input location, without column number ."
41   (string-append (number->string line) " " file)
42   )
43
44 (define-public point-and-click #f)
45
46 (define-public (lilypond-version)
47   (string-join
48    (map (lambda (x) (if (symbol? x)
49                         (symbol->string x)
50                         (number->string x)))
51                 (ly:version))
52    "."))
53
54
55
56 ;; cpp hack to get useful error message
57 (define ifdef "First run this through cpp.")
58 (define ifndef "First run this through cpp.")
59
60
61
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63
64 (define-public X 0)
65 (define-public Y 1)
66 (define-public START -1)
67 (define-public STOP 1)
68 (define-public LEFT -1)
69 (define-public RIGHT 1)
70 (define-public UP 1)
71 (define-public DOWN -1)
72 (define-public CENTER 0)
73
74 (define-public DOUBLE-FLAT -4)
75 (define-public THREE-Q-FLAT -3)
76 (define-public FLAT -2)
77 (define-public SEMI-FLAT -1)
78 (define-public NATURAL 0)
79 (define-public SEMI-SHARP 1)
80 (define-public SHARP 2)
81 (define-public THREE-Q-SHARP 3)
82 (define-public DOUBLE-SHARP 4)
83 (define-public SEMI-TONE 2)
84
85 (define-public ZERO-MOMENT (ly:make-moment 0 1)) 
86
87 (define-public (moment-min a b)
88   (if (ly:moment<? a b) a b))
89
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;; lily specific variables.
92 (define-public default-script-alist '())
93
94 (define-public safe-mode? #f)
95
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;;; Unassorted utility functions.
98
99
100 ;;;;;;;;;;;;;;;;
101 ; alist
102 (define (uniqued-alist  alist acc)
103   (if (null? alist) acc
104       (if (assoc (caar alist) acc)
105           (uniqued-alist (cdr alist) acc)
106           (uniqued-alist (cdr alist) (cons (car alist) acc)))))
107
108
109 (define-public (assoc-get key alist . default)
110   "Return value if KEY in ALIST, else DEFAULT (or #f if not specified)."
111   (let ((entry (assoc key alist)))
112     (if (pair? entry)
113         (cdr entry)
114         (if (pair? default) (car default) #f)
115         )))
116
117 (define-public (uniqued-alist  alist acc)
118   (if (null? alist) acc
119       (if (assoc (caar alist) acc)
120           (uniqued-alist (cdr alist) acc)
121           (uniqued-alist (cdr alist) (cons (car alist) acc)))))
122
123 (define-public (alist<? x y)
124   (string<? (symbol->string (car x))
125             (symbol->string (car y))))
126
127
128
129 (define (chain-assoc x alist-list)
130   (if (null? alist-list)
131       #f
132       (let* ((handle (assoc x (car alist-list))))
133         (if (pair? handle)
134             handle
135             (chain-assoc x (cdr alist-list))))))
136
137
138 (define (chain-assoc-get x alist-list . default)
139   "Return ALIST entry for X. Return DEFAULT (optional, else #f) if not
140 found."
141   (if (null? alist-list)
142       (if (pair? default) (car default) #f)
143       (let* ((handle (assoc x (car alist-list))))
144         (if (pair? handle)
145             (cdr handle)
146             (chain-assoc-get x (cdr alist-list) default)))))
147
148 (define (map-alist-vals func list)
149   "map FUNC over the vals of  LIST, leaving the keys."
150   (if (null?  list)
151       '()
152       (cons (cons  (caar list) (func (cdar list)))
153             (map-alist-vals func (cdr list)))
154       ))
155
156 (define (map-alist-keys func list)
157   "map FUNC over the keys of an alist LIST, leaving the vals. "
158   (if (null?  list)
159       '()
160       (cons (cons (func (caar list)) (cdar list))
161             (map-alist-keys func (cdr list)))
162       ))
163  
164 ;;;;;;;;;;;;;;;;
165 ;; hash
166
167
168
169 (if (not (defined? 'hash-table?))       ; guile 1.6 compat
170     (begin
171       (define hash-table? vector?)
172
173       (define-public (hash-table->alist t)
174         "Convert table t to list"
175         (apply append
176                (vector->list t)
177                )))
178
179     ;; native hashtabs.
180     (begin
181       (define-public (hash-table->alist t)
182
183         (hash-fold (lambda (k v acc) (acons  k v  acc))
184                    '() t)
185         )
186       ))
187
188 ;; todo: code dup with C++. 
189 (define-public (alist->hash-table l)
190   "Convert alist to table"
191   (let
192       ((m (make-hash-table (length l))))
193
194     (map (lambda (k-v)
195            (hashq-set! m (car k-v) (cdr k-v)))
196          l)
197
198     m))
199        
200
201
202 ;;;;;;;;;;;;;;;;
203 ; list
204
205 (define (flatten-list lst)
206   "Unnest LST" 
207   (if (null? lst)
208       '()
209       (if (pair? (car lst))
210           (append (flatten-list (car lst)) (flatten-list  (cdr lst)))
211           (cons (car lst) (flatten-list (cdr lst))))
212   ))
213
214 (define (list-minus a b)
215   "Return list of elements in A that are not in B."
216   (lset-difference eq? a b))
217
218
219 ;; TODO: use the srfi-1 partition function.
220 (define-public (uniq-list l)
221   
222   "Uniq LIST, assuming that it is sorted"
223   (define (helper acc l) 
224     (if (null? l)
225         acc
226         (if (null? (cdr l))
227             (cons (car l) acc)
228             (if (equal? (car l) (cadr l))
229                 (helper acc (cdr l))
230                 (helper (cons (car l) acc)  (cdr l)))
231             )))
232   (reverse! (helper '() l) '()))
233
234
235 (define (split-at-predicate predicate l)
236  "Split L = (a_1 a_2 ... a_k b_1 ... b_k)
237 into L1 = (a_1 ... a_k ) and L2 =(b_1 .. b_k) 
238 Such that (PREDICATE a_i a_{i+1}) and not (PREDICATE a_k b_1).
239 L1 is copied, L2 not.
240
241 (split-at-predicate (lambda (x y) (= (- y x) 2))  '(1 3 5 9 11) (cons '() '()))"
242 ;; "
243
244 ;; KUT EMACS MODE.
245
246   (define (inner-split predicate l acc)
247   (cond
248    ((null? l) acc)
249    ((null? (cdr l))
250     (set-car! acc (cons (car l) (car acc)))
251     acc)
252    ((predicate (car l) (cadr l))
253     (set-car! acc (cons (car l) (car acc)))
254     (inner-split predicate (cdr l) acc))
255    (else
256     (set-car! acc (cons (car l) (car acc)))
257     (set-cdr! acc (cdr l))
258     acc)
259
260   ))
261  (let*
262     ((c (cons '() '()))
263      )
264   (inner-split predicate l  c)
265   (set-car! c (reverse! (car c))) 
266   c)
267 )
268
269
270 (define-public (split-list l sep?)
271 "
272 (display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/))) )
273 =>
274 ((a b c) (d e f) (g))
275
276 "
277 ;; " KUT EMACS.
278
279 (define (split-one sep?  l acc)
280   "Split off the first parts before separator and return both parts."
281   (if (null? l)
282       (cons acc '())
283       (if (sep? (car l))
284           (cons acc (cdr l))
285           (split-one sep? (cdr l) (cons (car l) acc))
286           )
287       ))
288
289 (if (null? l)
290     '()
291     (let* ((c (split-one sep? l '())))
292       (cons (reverse! (car c) '()) (split-list (cdr c) sep?))
293       )))
294
295
296 (define-public (interval-length x)
297   "Length of the number-pair X, when an interval"
298   (max 0 (- (cdr x) (car x)))
299   )
300   
301
302 (define (other-axis a)
303   (remainder (+ a 1) 2))
304   
305
306 (define-public (interval-widen iv amount)
307    (cons (- (car iv) amount)
308          (+ (cdr iv) amount)))
309
310 (define-public (interval-union i1 i2)
311    (cons (min (car i1) (car i2))
312          (max (cdr i1) (cdr i2))))
313
314
315 (define-public (write-me message x)
316   "Return X.  Display MESSAGE and write X.  Handy for debugging, possibly turned off."
317   (display message) (write x) (newline) x)
318 ;;  x)
319
320 (define (index-cell cell dir)
321   (if (equal? dir 1)
322       (cdr cell)
323       (car cell)))
324
325 (define (cons-map f x)
326   "map F to contents of X"
327   (cons (f (car x)) (f (cdr x))))
328
329
330 (define-public (list-insert-separator lst between)
331   "Create new list, inserting BETWEEN between elements of LIST"
332   (define (conc x y )
333     (if (eq? y #f)
334         (list x)
335         (cons x  (cons between y))
336         ))
337   (fold-right conc #f lst))
338
339 ;;;;;;;;;;;;;;;;
340 ; other
341 (define (sign x)
342   (if (= x 0)
343       0
344       (if (< x 0) -1 1)))
345
346 (define-public (symbol<? l r)
347   (string<? (symbol->string l) (symbol->string r)))
348
349 (define-public (!= l r)
350   (not (= l r)))
351
352 (define-public (ly:load x)
353   (let* (
354          (fn (%search-load-path x))
355
356          )
357     (if (ly:get-option 'verbose)
358         (format (current-error-port) "[~A]" fn))
359     (primitive-load fn)))
360
361
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 ;;  output
364 (use-modules (scm output-tex)
365              (scm output-ps)
366              (scm output-sketch)
367              (scm output-sodipodi)
368              (scm output-pdftex)
369              )
370
371 (define output-alist
372   `(
373     ("tex" . ("TeX output. The default output form." ,tex-output-expression))
374     ("ps" . ("Direct postscript. Requires setting GS_LIB and GS_FONTPATH" ,ps-output-expression))
375     ("scm" . ("Scheme dump: debug scheme stencil expressions" ,write))
376     ("sketch" . ("Bare bones Sketch output." ,sketch-output-expression))
377     ("sodipodi" . ("Bare bones Sodipodi output." ,sodipodi-output-expression))
378     ("pdftex" . ("PDFTeX output. Was last seen nonfunctioning." ,pdftex-output-expression))
379     ))
380
381
382 (define (document-format-dumpers)
383   (map
384    (lambda (x)
385      (display (string-append  (pad-string-to 5 (car x)) (cadr x) "\n"))
386      output-alist)
387    ))
388
389 (define-public (find-dumper format )
390   (let* ((d (assoc format output-alist)))
391     
392     (if (pair? d)
393         (caddr d)
394         (scm-error "Could not find dumper for format ~s" format))))
395
396 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
397 ;; other files.
398
399 (for-each ly:load
400      ;; load-from-path
401      '("define-music-types.scm"
402        "output-lib.scm"
403        "c++.scm"
404        "chord-ignatzek-names.scm"
405        "chord-entry.scm"
406        "chord-generic-names.scm"
407        "stencil.scm"
408        "new-markup.scm"
409        "bass-figure.scm"
410        "music-functions.scm"
411        "part-combiner.scm"
412        "define-music-properties.scm"
413        "auto-beam.scm"
414        "chord-name.scm"
415        
416        "define-context-properties.scm"
417        "translation-functions.scm"
418        "script.scm"
419        "midi.scm"
420
421        "beam.scm"
422        "clef.scm"
423        "slur.scm"
424        "font.scm"
425        "new-font.scm"
426        
427        "define-markup-commands.scm"
428        "define-grob-properties.scm"
429        "define-grobs.scm"
430        "define-grob-interfaces.scm"
431
432        "page-layout.scm"
433        "paper.scm"
434        ))
435
436
437 (set! type-p-name-alist
438   `(
439    (,boolean-or-symbol? . "boolean or symbol")
440    (,boolean? . "boolean")
441    (,char? . "char")
442    (,grob-list? . "list of grobs")
443    (,hash-table? . "hash table")
444    (,input-port? . "input port")
445    (,integer? . "integer")
446    (,list? . "list")
447    (,ly:context? . "context")
448    (,ly:dimension? . "dimension, in staff space")
449    (,ly:dir? . "direction")
450    (,ly:duration? . "duration")
451    (,ly:grob? . "layout object")
452    (,ly:input-location? . "input location")
453    (,ly:moment? . "moment")
454    (,ly:music? . "music")
455    (,ly:pitch? . "pitch")
456    (,ly:translator? . "translator")
457    (,ly:font-metric? . "font metric")
458    (,markup-list? . "list of markups")
459    (,markup? . "markup")
460    (,ly:music-list? . "list of music")
461    (,number-or-grob? . "number or grob")
462    (,number-or-string? . "number or string")
463    (,number-pair? . "pair of numbers")
464    (,number? . "number")
465    (,output-port? . "output port")   
466    (,pair? . "pair")
467    (,procedure? . "procedure") 
468    (,scheme? . "any type")
469    (,string? . "string")
470    (,symbol? . "symbol")
471    (,vector? . "vector")
472    ))
473
474
475 ;; debug mem leaks
476
477 (define gc-protect-stat-count 0)
478 (define-public (dump-gc-protects)
479   (set! gc-protect-stat-count (1+ gc-protect-stat-count) )
480   (let*
481       ((protects (sort
482            (hash-table->alist (ly:protects))
483            (lambda (a b)
484              (< (object-address (car a))
485                 (object-address (car b))))))
486        (outfile    (open-file (string-append
487                "gcstat-" (number->string gc-protect-stat-count)
488                ".scm"
489                ) "w"))
490        )
491
492     (display
493      (filter
494       (lambda (x) (not (symbol? x))) 
495       (map (lambda (y)
496              (let
497                  ((x (car y))
498                   (c (cdr y)))
499
500                (string-append
501                 (string-join
502                  (map object->string (list (object-address x) c x))
503                  " ")
504                 "\n")))
505            protects))
506      outfile)
507
508     ))
509