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