]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
*** empty log message ***
[lilypond.git] / scm / output-lib.scm
1 ;;;; output-lib.scm -- implement Scheme output helper functions
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
9 ;;; Tablature functions, by Jiba (jiba@tuxfamily.org)
10
11 ;; The TabNoteHead stem attachment function.
12 (define (tablature-stem-attachment-function style duration)
13   (cons 0.0 0.5))
14
15 ;; The TabNoteHead tablatureFormat callback.
16 ;; Compute the text grob-property
17 (define-public (fret-number-tablature-format string tuning pitch)
18   (number->string
19    (- (ly:pitch-semitones pitch)
20       (list-ref tuning
21                 ;; remove 1 because list index starts at 0 and guitar string at 1. 
22                 (- string 1)))))
23
24 (define-public (hammer-print-function grob)
25   (let* ((note-collums (ly:grob-property grob 'note-columns))
26          (note-column1 (cadr note-collums))
27          (note-column2 (car  note-collums))
28          (note1 (car (ly:grob-property note-column1 'note-heads)))
29          (note2 (car (ly:grob-property note-column2 'note-heads)))
30          (text1 (ly:grob-property note1 'text))
31          (text2 (ly:grob-property note2 'text))
32          (fret1 (if (string? text1) (string->number text1) 0))
33          (fret2 (if (string? text2) (string->number text2) 0))
34          (letter (cond
35                   ((< fret1 fret2) "H")
36                   ((> fret1 fret2) "P")
37                   (else ""))))
38     (let* ((slur
39             ;; (Slur::print grob)
40
41             ;; 
42             ;; FIXME: a hammer is not a slur.
43             ;; 
44             (ly:make-stencil '() '(0 . 0) '(0 . 0)))
45            (layout (ly:grob-layout grob))
46            (text (interpret-markup
47                   layout
48                   (ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))
49                   letter)))
50       
51       (let ((x (/ (- (cdr (ly:stencil-extent slur 0)) 
52                      (/ (cdr (ly:stencil-extent text 0)) 2.0))
53                   -2.0)))
54         
55         (ly:stencil-set-extent! text 0 (cons x x))
56         (ly:stencil-align-to! text 0 1)))))
57
58 ;; (ly:stencil-combine-at-edge slur 1 1 text -0.6)
59
60
61
62 (define-public guitar-tunings '(4 -1 -5 -10 -15 -20))
63
64 ;;; end of tablature functions
65
66
67 (define-public (make-stencil-boxer line-thick x-padding y-padding callback)
68   "Makes a routine that adds a box around the grob parsed as argument"
69   (define (stencil-boxer grob)
70     (let* ((mol (callback grob))
71            (x-ext (interval-widen (ly:stencil-extent mol 0) x-padding))
72            (y-ext (interval-widen (ly:stencil-extent mol 1) y-padding))
73            (x-rule (make-filled-box-stencil (interval-widen x-ext line-thick)
74                                             (cons 0 line-thick)))
75            (y-rule (make-filled-box-stencil (cons 0 line-thick) y-ext)))
76       
77       (set! mol (ly:stencil-combine-at-edge mol 0 1 y-rule x-padding))
78       (set! mol (ly:stencil-combine-at-edge mol 0 -1 y-rule x-padding))
79       (set! mol (ly:stencil-combine-at-edge mol 1 1 x-rule 0))  
80       (set! mol (ly:stencil-combine-at-edge mol 1 -1 x-rule 0))
81       mol))
82   stencil-boxer)
83
84 (define-public (arg->string arg)
85   (cond ((number? arg) (ly:inexact->string arg 10))
86         ((string? arg) (string-append "\"" arg "\""))
87         ((symbol? arg) (string-append "\"" (symbol->string arg) "\""))))
88
89 (define-public (func name . args)
90   (string-append
91    "(" name
92    (if (null? args)
93        ""
94        (apply string-append
95               (map (lambda (x) (string-append " " (arg->string x))) args)))
96    ")\n"))
97
98 ;;(define (mm-to-pt x)
99 ;;  (* (/ 72.27 25.40) x))
100
101 ;; do nothing in .scm output
102
103 (define-public (ly:numbers->string lst)
104   (string-join (map ly:number->string lst) " "))
105
106 (define (number->octal-string x)
107   (let* ((n (inexact->exact x))
108          (n64 (quotient n 64))
109          (n8 (quotient (- n (* n64 64)) 8)))
110     (string-append
111      (number->string n64)
112      (number->string n8)
113      (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
114
115 (define-public (ly:inexact->string x radix)
116   (let ((n (inexact->exact x)))
117     (number->string n radix)))
118
119 (define-public (ly:number-pair->string c)
120   (string-append (ly:number->string (car c)) " "
121                  (ly:number->string (cdr c))))
122
123 (define (font i)
124   (string-append
125    "font"
126    (make-string 1 (integer->char (+ (char->integer #\A) i)))))
127
128 (define (scm-scm action-name)
129   1)
130
131 ;; silly, use alist? 
132 (define-public (find-notehead-symbol duration style)
133   (case style
134     ((xcircle) "2xcircle")
135     ((harmonic) "0harmonic")
136     ((baroque) 
137      ;; Oops, I actually would not call this "baroque", but, for
138      ;; backwards compatibility to 1.4, this is supposed to take
139      ;; brevis, longa and maxima from the neo-mensural font and all
140      ;; other note heads from the default font.  -- jr
141      (if (< duration 0)
142          (string-append (number->string duration) "neomensural")
143          (number->string duration)))
144     ((mensural)
145      (string-append (number->string duration) (symbol->string style)))
146     ((neomensural)
147      (string-append (number->string duration) (symbol->string style)))
148     ((default)
149      ;; The default font in mf/feta-bolletjes.mf defines a brevis, but
150      ;; neither a longa nor a maxima.  Hence let us, for the moment,
151      ;; take these from the neo-mensural font.  TODO: mf/feta-bolletjes
152      ;; should define at least a longa for the default font.  The longa
153      ;; should look exactly like the brevis of the default font, but
154      ;; with a stem exactly like that of the quarter note. -- jr
155      (if (< duration -1)
156          (string-append (number->string duration) "neomensural")
157          (number->string duration)))
158     (else
159      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
160          (symbol->string style)
161          (string-append (number->string (max 0 duration))
162                         (symbol->string style))))))
163
164 ;; TODO junk completely?
165 (define (note-head-style->attachment-coordinates grob axis)
166   "Return pair (X . Y), containing multipliers for the note head
167 bounding box, where to attach the stem. e.g.: X==0 means horizontally
168 centered, X==1 is at the right, X == -1 is at the left."
169
170   '(1.0 . 0.0))
171
172 (define-public (string-encode-integer i)
173   (cond
174    ((= i  0) "o")
175    ((< i 0)   (string-append "n" (string-encode-integer (- i))))
176    (else (string-append
177           (make-string 1 (integer->char (+ 65 (modulo i 26))))
178           (string-encode-integer (quotient i 26))))))
179
180 (define-public ((every-nth-bar-number-visible n) barnum) (= 0 (modulo barnum n)))
181
182 (define-public ((modulo-bar-number-visible n m) barnum) (and (> barnum 1) (= m (modulo barnum n))))
183
184 (define-public ((set-bar-number-visibility n) tr)
185   (let* ((bn (ly:context-property tr 'currentBarNumber)))
186     (ly:context-set-property! tr 'barNumberVisibility (modulo-bar-number-visible n (modulo bn n)))))
187
188 (define-public (default-bar-number-visibility barnum) (> barnum 1))
189
190 ;; See documentation of Item::visibility_lambda_
191 (define-public (begin-of-line-visible d) (if (= d 1) '(#f . #f) '(#t . #t)))
192 (define-public (end-of-line-visible d) (if (= d -1) '(#f . #f) '(#t . #t)))
193 (define-public (spanbar-begin-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
194
195 (define-public (all-visible d) '(#f . #f))
196 (define-public (all-invisible d) '(#t . #t))
197 (define-public (begin-of-line-invisible d) (if (= d 1) '(#t . #t) '(#f . #f)))
198 (define-public (end-of-line-invisible d) (if (= d -1) '(#t . #t) '(#f . #f)))
199
200
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;; Bar lines.
203
204 ;;
205 ;; How should a  bar line behave at a break? 
206 ;;
207 ;; Why prepend `default-' to every scm identifier?
208 (define-public (default-break-barline glyph dir)
209   (let ((result (assoc glyph 
210                        '((":|:" . (":|" . "|:"))
211                          ("||:" . ("||" . "|:"))
212                          ("|" . ("|" . ()))
213                          ("||:" . ("||" . "|:"))
214                          ("|s" . (() . "|"))
215                          ("|:" . ("|" . "|:"))
216                          ("|." . ("|." . ()))
217
218                          ;; hmm... should we end with a bar line here?
219                          (".|" . ("|" . ".|"))
220                          (":|" . (":|" . ()))
221                          ("||" . ("||" . ()))
222                          (".|." . (".|." . ()))
223                          ("" . ("" . ""))
224                          (":" . (":" . ""))
225                          ("empty" . (() . ()))
226                          ("brace" . (() . "brace"))
227                          ("bracket" . (() . "bracket"))  ))))
228
229     (if (equal? result #f)
230         (ly:warn "Unknown bar glyph: `~S'" glyph)
231         (index-cell (cdr result) dir))))
232
233 (define-public (shift-right-at-line-begin g)
234   "Shift an item to the right, but only at the start of the line."
235   (if (and (ly:item? g)  (equal? (ly:item-break-dir g) RIGHT))
236       (ly:grob-translate-axis! g 3.5 X)))