]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-lib.scm
Merge branch 'Paco' into staging
[lilypond.git] / scm / output-lib.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; general
22
23 (define-public (grob::has-interface grob iface)
24   (memq iface (ly:grob-interfaces grob)))
25
26 (define-public (grob::is-live? grob)
27   (pair? (ly:grob-basic-properties grob)))
28
29 (define-public (grob::x-parent-width grob)
30   (ly:grob-property (ly:grob-parent grob X) 'X-extent))
31
32 (define-public (make-stencil-boxer thickness padding callback)
33   "Return function that adds a box around the grob passed as argument."
34   (lambda (grob)
35     (box-stencil (callback grob) thickness padding)))
36
37 (define-public (make-stencil-circler thickness padding callback)
38   "Return function that adds a circle around the grob passed as argument."
39   (lambda (grob)
40     (circle-stencil (callback grob) thickness padding)))
41
42 (define-public (print-circled-text-callback grob)
43   (grob-interpret-markup grob (make-circle-markup
44                                (ly:grob-property grob 'text))))
45
46 (define-public (event-cause grob)
47   (let ((cause (ly:grob-property  grob 'cause)))
48
49     (cond
50      ((ly:stream-event? cause) cause)
51      ((ly:grob? cause) (event-cause cause))
52      (else #f))))
53
54 (define-public (grob-interpret-markup grob text)
55   (let* ((layout (ly:grob-layout grob))
56          (defs (ly:output-def-lookup layout 'text-font-defaults))
57          (props (ly:grob-alist-chain grob defs)))
58
59     (ly:text-interface::interpret-markup layout props text)))
60
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 ;; beam slope
63
64 ;; calculates each slope of a broken beam individually
65 (define-public (beam::place-broken-parts-individually grob)
66   (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
67
68 ;; calculates the slope of a beam as a single unit,
69 ;; even if it is broken.  this assures that the beam
70 ;; will pick up where it left off after a line break
71 (define-public (beam::align-with-broken-parts grob)
72   (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
73
74 ;; uses the broken beam style from edition peters combines the
75 ;; values of place-broken-parts-individually and align-with-broken-parts above,
76 ;; favoring place-broken-parts-individually when the beam naturally has a steeper
77 ;; incline and align-with-broken-parts when the beam is flat
78 (define-public (beam::slope-like-broken-parts grob)
79   (define (slope y x)
80     (/ (- (cdr y) (car y)) (- (cdr x) (car x))))
81   (let* ((quant1 (ly:beam::quanting grob '(+inf.0 . -inf.0) #t))
82          (original (ly:grob-original grob))
83          (siblings (if (ly:grob? original)
84                        (ly:spanner-broken-into original)
85                        '())))
86     (if (null? siblings)
87         quant1
88         (let* ((quant2 (ly:beam::quanting grob '(+inf.0 . -inf.0) #f))
89                (x-span (ly:grob-property grob 'X-positions))
90                (slope1 (slope quant1 x-span))
91                (slope2 (slope quant2 x-span))
92                (quant2 (if (not (= (sign slope1) (sign slope2)))
93                            '(0 . 0)
94                            quant2))
95                (factor (/ (atan (abs slope1)) PI-OVER-TWO))
96                (base (cons-map
97                        (lambda (x)
98                          (+ (* (x quant1) (- 1 factor))
99                             (* (x quant2) factor)))
100                        (cons car cdr))))
101           (ly:beam::quanting grob base #f)))))
102
103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 ;; cross-staff stuff
105
106 (define-public (script-or-side-position-cross-staff g)
107   (or
108    (ly:script-interface::calc-cross-staff g)
109    (ly:side-position-interface::calc-cross-staff g)))
110
111
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113 ;; note heads
114
115 (define-public (stem::calc-duration-log grob)
116   (ly:duration-log
117    (ly:event-property (event-cause grob) 'duration)))
118
119 (define-public (stem::length grob)
120   (let* ((ss (ly:staff-symbol-staff-space grob))
121          (beg (ly:grob-property grob 'stem-begin-position))
122          (beam (ly:grob-object grob 'beam)))
123     (if (null? beam)
124         (abs (- (ly:stem::calc-stem-end-position grob) beg))
125         (begin
126           (ly:programming-error
127             "stem::length called but will not be used for beamed stem.")
128           0.0))))
129
130 (define-public (stem::pure-length grob beg end)
131   (let* ((ss (ly:staff-symbol-staff-space grob))
132          (beg (ly:grob-pure-property grob 'stem-begin-position 0 1000)))
133     (abs (- (ly:stem::pure-calc-stem-end-position grob 0 2147483646) beg))))
134
135 (define (stem-stub::do-calculations grob)
136   (and (ly:grob-property (ly:grob-parent grob X) 'cross-staff)
137        (not (ly:grob-property (ly:grob-parent grob X) 'transparent))))
138
139 (define-public (stem-stub::pure-height grob beg end)
140   (if (stem-stub::do-calculations grob)
141       '(0 . 0)
142       '(+inf.0 . -inf.0)))
143
144 (define-public (stem-stub::width grob)
145   (if (stem-stub::do-calculations grob)
146       (grob::x-parent-width grob)
147       '(+inf.0 . -inf.0)))
148
149 (define-public (stem-stub::extra-spacing-height grob)
150   (if (stem-stub::do-calculations grob)
151       (let* ((dad (ly:grob-parent grob X))
152              (refp (ly:grob-common-refpoint grob dad Y))
153              (stem_ph (ly:grob-pure-height dad refp 0 1000000))
154              (my_ph (ly:grob-pure-height grob refp 0 1000000))
155              ;; only account for distance if stem is on different staff than stub
156              (dist (if (grob::has-interface refp 'hara-kiri-group-spanner-interface)
157                        0
158                        (- (car my_ph) (car stem_ph)))))
159         (if (interval-empty? (interval-intersection stem_ph my_ph)) #f (coord-translate stem_ph dist)))
160       #f))
161
162 ;; FIXME: NEED TO FIND A BETTER WAY TO HANDLE KIEVAN NOTATION
163 (define-public (note-head::calc-duration-log grob)
164   (let ((style (ly:grob-property grob 'style)))
165     (if (and (symbol? style) (string-match "kievan*" (symbol->string style)))
166       (min 3
167         (ly:duration-log
168         (ly:event-property (event-cause grob) 'duration)))
169       (min 2
170         (ly:duration-log
171         (ly:event-property (event-cause grob) 'duration))))))
172
173 (define-public (dots::calc-dot-count grob)
174   (ly:duration-dot-count
175    (ly:event-property (event-cause grob) 'duration)))
176
177 (define-public (dots::calc-staff-position grob)
178   (let* ((head (ly:grob-parent grob Y))
179          (log (ly:grob-property head 'duration-log)))
180
181     (cond
182      ((or (not (grob::has-interface head 'rest-interface))
183           (not (integer? log))) 0)
184      ((= log 7) 4)
185      ((> log 4) 3)
186      ((= log 0) -1)
187      ((= log 1) 1)
188      ((= log -1) 1)
189      (else 0))))
190
191 ;; Kept separate from note-head::calc-glyph-name to allow use by
192 ;; markup commands \note and \note-by-number
193 (define-public (select-head-glyph style log)
194   "Select a note head glyph string based on note head style @var{style}
195 and duration-log @var{log}."
196   (case style
197     ;; "default" style is directly handled in note-head.cc as a
198     ;; special case (HW says, mainly for performance reasons).
199     ;; Therefore, style "default" does not appear in this case
200     ;; statement.  -- jr
201     ((xcircle) "2xcircle")
202     ((harmonic) "0harmonic")
203     ((harmonic-black) "2harmonic")
204     ((harmonic-mixed) (if (<= log 1) "0harmonic"
205                           "2harmonic"))
206     ((baroque)
207      ;; Oops, I actually would not call this "baroque", but, for
208      ;; backwards compatibility to 1.4, this is supposed to take
209      ;; brevis, longa and maxima from the neo-mensural font and all
210      ;; other note heads from the default font.  -- jr
211      (if (< log 0)
212          (string-append (number->string log) "neomensural")
213          (number->string log)))
214     ((altdefault)
215      ;; Like default, but brevis is drawn with double vertical lines
216      (if (= log -1)
217          (string-append (number->string log) "double")
218          (number->string log)))
219     ((mensural)
220      (string-append (number->string log) (symbol->string style)))
221     ((petrucci)
222      (if (< log 0)
223          (string-append (number->string log) "mensural")
224          (string-append (number->string log) (symbol->string style))))
225     ((blackpetrucci)
226      (if (< log 0)
227          (string-append (number->string log) "blackmensural")
228          (string-append (number->string log) (symbol->string style))))
229     ((semipetrucci)
230      (if (< log 0)
231          (string-append (number->string log) "semimensural")
232          (string-append (number->string log) "petrucci")))
233     ((neomensural)
234      (string-append (number->string log) (symbol->string style)))
235     ((kievan)
236      (string-append (number->string log) "kievan"))
237     (else
238      (if (string-match "vaticana*|hufnagel*|medicaea*" (symbol->string style))
239          (symbol->string style)
240          (string-append (number->string (max 0 log))
241                         (symbol->string style))))))
242
243 (define-public (note-head::calc-glyph-name grob)
244   (let* ((style (ly:grob-property grob 'style))
245          (log (if (string-match "kievan*" (symbol->string style))
246                   (min 3 (ly:grob-property grob 'duration-log))
247                   (min 2 (ly:grob-property grob 'duration-log)))))
248     (select-head-glyph style log)))
249
250 (define-public (note-head::brew-ez-stencil grob)
251   (let* ((log (ly:grob-property grob 'duration-log))
252          (pitch (ly:event-property (event-cause grob) 'pitch))
253          (pitch-index (ly:pitch-notename pitch))
254          (note-names (ly:grob-property grob 'note-names))
255          (pitch-string (if (and (vector? note-names)
256                                 (> (vector-length note-names) pitch-index))
257                            (vector-ref note-names pitch-index)
258                            (string
259                             (integer->char
260                              (+ (modulo (+ pitch-index 2) 7)
261                                 (char->integer #\A))))))
262          (staff-space (ly:staff-symbol-staff-space grob))
263          (line-thickness (ly:staff-symbol-line-thickness grob))
264          (stem (ly:grob-object grob 'stem))
265          (stem-thickness (* (if (ly:grob? stem)
266                                 (ly:grob-property stem 'thickness)
267                                 1.3)
268                             line-thickness))
269          (radius (/ (+ staff-space line-thickness) 2))
270          (letter (markup #:center-align #:vcenter pitch-string))
271          (filled-circle (markup #:draw-circle radius 0 #t)))
272
273     (ly:stencil-translate-axis
274      (grob-interpret-markup
275       grob
276       (if (>= log 2)
277           (make-combine-markup
278            filled-circle
279            (make-with-color-markup white letter))
280           (make-combine-markup
281            (make-combine-markup
282             filled-circle
283             (make-with-color-markup white (make-draw-circle-markup
284                                            (- radius stem-thickness) 0 #t)))
285            letter)))
286      radius X)))
287
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
289 ;; clipping
290
291 (define-public (make-rhythmic-location bar-num num den)
292   (cons
293    bar-num (ly:make-moment num den)))
294
295 (define-public (rhythmic-location? a)
296   (and (pair? a)
297        (integer? (car a))
298        (ly:moment? (cdr a))))
299
300 (define-public (make-graceless-rhythmic-location loc)
301   (make-rhythmic-location
302    (car loc)
303    (ly:moment-main-numerator (rhythmic-location-measure-position loc))
304    (ly:moment-main-denominator (rhythmic-location-measure-position loc))))
305
306 (define-public rhythmic-location-measure-position cdr)
307 (define-public rhythmic-location-bar-number car)
308
309 (define-public (rhythmic-location<? a b)
310   (cond
311    ((< (car a) (car b)) #t)
312    ((> (car a) (car b)) #f)
313    (else
314     (ly:moment<? (cdr a) (cdr b)))))
315
316 (define-public (rhythmic-location<=? a b)
317   (not (rhythmic-location<? b a)))
318 (define-public (rhythmic-location>=? a b)
319   (rhythmic-location<? a b))
320 (define-public (rhythmic-location>? a b)
321   (rhythmic-location<? b a))
322
323 (define-public (rhythmic-location=? a b)
324   (and (rhythmic-location<=? a b)
325        (rhythmic-location<=? b a)))
326
327 (define-public (rhythmic-location->file-string a)
328   (ly:format "~a.~a.~a"
329              (car a)
330              (ly:moment-main-numerator (cdr a))
331              (ly:moment-main-denominator (cdr a))))
332
333 (define-public (rhythmic-location->string a)
334   (ly:format "bar ~a ~a"
335              (car a)
336              (ly:moment->string (cdr a))))
337
338 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
339 ;; break visibility
340
341 (define-public all-visible             #(#t #t #t))
342 (define-public begin-of-line-invisible #(#t #t #f))
343 (define-public center-invisible        #(#t #f #t))
344 (define-public end-of-line-invisible   #(#f #t #t))
345 (define-public begin-of-line-visible   #(#f #f #t))
346 (define-public center-visible          #(#f #t #f))
347 (define-public end-of-line-visible     #(#t #f #f))
348 (define-public all-invisible           #(#f #f #f))
349 (define-public (inherit-x-parent-visibility grob)
350   (let ((parent (ly:grob-parent grob X)))
351     (ly:grob-property parent 'break-visibility all-invisible)))
352 (define-public (inherit-y-parent-visibility grob)
353   (let ((parent (ly:grob-parent grob X)))
354     (ly:grob-property parent 'break-visibility)))
355
356
357 (define-public spanbar-begin-of-line-invisible #(#t #f #f))
358
359
360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 ;; Bar lines.
362
363 ;;
364 ;; How should a  bar line behave at a break?
365 (define bar-glyph-alist
366   '((":|:" . (":|" . "|:"))
367     (":|.|:" . (":|" . "|:"))
368     (":|.:" . (":|" . "|:"))
369     ("||:" . ("||" . "|:"))
370     ("dashed" . ("dashed" . '()))
371     ("|" . ("|" . ()))
372     ("||:" . ("||" . "|:"))
373     ("|s" . (() . "|"))
374     ("|:" . ("|" . "|:"))
375     ("|." . ("|." . ()))
376
377     ;; hmm... should we end with a bar line here?
378     (".|" . ("|" . ".|"))
379     (":|" . (":|" . ()))
380     ("||" . ("||" . ()))
381     (".|." . (".|." . ()))
382     ("|.|" . ("|.|" . ()))
383     ("" . ("" . ""))
384     (":" . (":" . ""))
385     ("." . ("." . ()))
386     ("'" . ("'" . ()))
387     ("empty" . (() . ()))
388     ("brace" . (() . "brace"))
389     ("bracket" . (() . "bracket"))
390
391     ;; segno bar lines
392     ("S" . ("||" . "S"))
393     ("|S" . ("|" . "S"))
394     ("S|" . ("S" . ()))
395     (":|S" . (":|" . "S"))
396     (":|S." . (":|S" . ()))
397     ("S|:" . ("S" . "|:"))
398     (".S|:" . ("|" . "S|:"))
399     (":|S|:" . (":|" . "S|:"))
400     (":|S.|:" . (":|S" . "|:"))
401
402     ;; ancient bar lines
403     ("kievan" . ("kievan" . ""))))
404
405 (define-public (bar-line::calc-glyph-name grob)
406   (let* ((glyph (ly:grob-property grob 'glyph))
407          (dir (ly:item-break-dir grob))
408          (result (assoc-get glyph bar-glyph-alist))
409          (glyph-name (if (= dir CENTER)
410                          glyph
411                          (if (and result
412                                   (string? (index-cell result dir)))
413                              (index-cell result dir)
414                              #f))))
415     glyph-name))
416
417 (define-public (bar-line::calc-break-visibility grob)
418   (let* ((glyph (ly:grob-property grob 'glyph))
419          (result (assoc-get glyph bar-glyph-alist)))
420
421     (if result
422         (vector (string? (car result)) #t (string? (cdr result)))
423         all-invisible)))
424
425 (define-public (shift-right-at-line-begin g)
426   "Shift an item to the right, but only at the start of the line."
427   (if (and (ly:item? g)
428            (equal? (ly:item-break-dir g) RIGHT))
429       (ly:grob-translate-axis! g 3.5 X)))
430
431 (define-public (pure-from-neighbor-interface::extra-spacing-height-at-beginning-of-line grob)
432   (if (= 1 (ly:item-break-dir grob))
433       (pure-from-neighbor-interface::extra-spacing-height grob)
434       (cons -0.1 0.1)))
435
436 (define-public (pure-from-neighbor-interface::extra-spacing-height grob)
437   (let* ((height (ly:grob-pure-height grob grob 0 10000000))
438          (from-neighbors (interval-union
439                             height
440                             (ly:axis-group-interface::pure-height
441                               grob
442                               0
443                               10000000))))
444     (coord-operation - from-neighbors height)))
445
446 (define-public (pure-from-neighbor-interface::account-for-span-bar grob)
447   (let* ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
448          (hsb (ly:grob-property grob 'has-span-bar))
449          (ii (interval-intersection esh (cons -1.01 1.01))))
450     (if (pair? hsb)
451         (cons (car (if (and (car hsb)
452                        (ly:grob-property grob 'allow-span-bar))
453                        esh ii))
454               (cdr (if (cdr hsb) esh ii)))
455         ii)))
456
457 (define-public (pure-from-neighbor-interface::extra-spacing-height-including-staff grob)
458   (let ((esh (pure-from-neighbor-interface::extra-spacing-height grob))
459         (to-staff (coord-operation -
460                                    (interval-widen
461                                      '(0 . 0)
462                                      (ly:staff-symbol-staff-radius grob))
463                                    (ly:grob::stencil-height grob))))
464     (interval-union esh to-staff)))
465
466
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
468 ;; Tuplets
469
470 (define-public (tuplet-number::calc-direction grob)
471   (ly:tuplet-bracket::calc-direction (ly:grob-object grob 'bracket)))
472
473 (define-public (tuplet-number::calc-denominator-text grob)
474   (number->string (ly:event-property (event-cause grob) 'denominator)))
475
476 (define-public (tuplet-number::calc-fraction-text grob)
477   (let ((ev (event-cause grob)))
478
479     (format #f "~a:~a"
480             (ly:event-property ev 'denominator)
481             (ly:event-property ev 'numerator))))
482
483 ;; a formatter function, which is simply a wrapper around an existing
484 ;; tuplet formatter function. It takes the value returned by the given
485 ;; function and appends a note of given length.
486 (define-public ((tuplet-number::append-note-wrapper function note) grob)
487   (let ((txt (if function (function grob) #f)))
488
489     (if txt
490         (markup txt #:fontsize -5 #:note note UP)
491         (markup #:fontsize -5 #:note note UP))))
492
493 ;; Print a tuplet denominator with a different number than the one derived from
494 ;; the actual tuplet fraction
495 (define-public ((tuplet-number::non-default-tuplet-denominator-text denominator)
496                 grob)
497   (number->string (if denominator
498                       denominator
499                       (ly:event-property (event-cause grob) 'denominator))))
500
501 ;; Print a tuplet fraction with different numbers than the ones derived from
502 ;; the actual tuplet fraction
503 (define-public ((tuplet-number::non-default-tuplet-fraction-text
504                  denominator numerator) grob)
505   (let* ((ev (event-cause grob))
506          (den (if denominator denominator (ly:event-property ev 'denominator)))
507          (num (if numerator numerator (ly:event-property ev 'numerator))))
508
509     (format #f "~a:~a" den num)))
510
511 ;; Print a tuplet fraction with note durations appended to the numerator and the
512 ;; denominator
513 (define-public ((tuplet-number::fraction-with-notes
514                  denominatornote numeratornote) grob)
515   (let* ((ev (event-cause grob))
516          (denominator (ly:event-property ev 'denominator))
517          (numerator (ly:event-property ev 'numerator)))
518
519     ((tuplet-number::non-default-fraction-with-notes
520       denominator denominatornote numerator numeratornote) grob)))
521
522 ;; Print a tuplet fraction with note durations appended to the numerator and the
523 ;; denominator
524 (define-public ((tuplet-number::non-default-fraction-with-notes
525                  denominator denominatornote numerator numeratornote) grob)
526   (let* ((ev (event-cause grob))
527          (den (if denominator denominator (ly:event-property ev 'denominator)))
528          (num (if numerator numerator (ly:event-property ev 'numerator))))
529
530     (make-concat-markup (list
531                          (make-simple-markup (format #f "~a" den))
532                          (markup #:fontsize -5 #:note denominatornote UP)
533                          (make-simple-markup " : ")
534                          (make-simple-markup (format #f "~a" num))
535                          (markup #:fontsize -5 #:note numeratornote UP)))))
536
537
538 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
539 ;; Color
540
541 (define-public (color? x)
542   (and (list? x)
543        (= 3 (length x))
544        (apply eq? #t (map number? x))
545        (apply eq? #t (map (lambda (y) (<= 0 y 1)) x))))
546
547 (define-public (rgb-color r g b) (list r g b))
548
549 ; predefined colors
550 (define-public black       '(0.0 0.0 0.0))
551 (define-public white       '(1.0 1.0 1.0))
552 (define-public red         '(1.0 0.0 0.0))
553 (define-public green       '(0.0 1.0 0.0))
554 (define-public blue        '(0.0 0.0 1.0))
555 (define-public cyan        '(0.0 1.0 1.0))
556 (define-public magenta     '(1.0 0.0 1.0))
557 (define-public yellow      '(1.0 1.0 0.0))
558
559 (define-public grey        '(0.5 0.5 0.5))
560 (define-public darkred     '(0.5 0.0 0.0))
561 (define-public darkgreen   '(0.0 0.5 0.0))
562 (define-public darkblue    '(0.0 0.0 0.5))
563 (define-public darkcyan    '(0.0 0.5 0.5))
564 (define-public darkmagenta '(0.5 0.0 0.5))
565 (define-public darkyellow  '(0.5 0.5 0.0))
566
567
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
569 ;; key signature
570
571 (define-public (key-signature-interface::alteration-position step alter
572                                                              c0-position)
573   ;; TODO: memoize - this is mostly constant.
574
575   ;; fes, ges, as and bes typeset in lower octave
576   (define FLAT_TOP_PITCH 2)
577
578   ;; ais and bis typeset in lower octave
579   (define SHARP_TOP_PITCH 4)
580
581   (if (pair? step)
582       (+ (cdr step) (* (car step) 7) c0-position)
583       (let* ((from-bottom-pos (modulo (+ 4 49 c0-position) 7))
584              (p step)
585              (c0 (- from-bottom-pos 4)))
586
587         (if
588          (or (and (< alter 0)
589                   (or (> p FLAT_TOP_PITCH) (> (+ p c0) 4)) (> (+ p c0) 1))
590              (and (> alter 0)
591                   (or (> p SHARP_TOP_PITCH) (> (+ p c0) 5)) (> (+ p c0) 2)))
592
593          ;; Typeset below c_position
594          (set! p (- p 7)))
595
596         ;; Provide for the four cases in which there's a glitch
597         ;; it's a hack, but probably not worth
598         ;; the effort of finding a nicer solution.
599         ;; --dl.
600         (cond
601          ((and (= c0 2) (= p 3) (> alter 0))
602           (set! p (- p 7)))
603          ((and (= c0 -3) (= p -1) (> alter 0))
604           (set! p (+ p 7)))
605          ((and (= c0 -4) (= p -1) (< alter 0))
606           (set! p (+ p 7)))
607          ((and (= c0 -2) (= p -3) (< alter 0))
608           (set! p (+ p 7))))
609
610         (+ c0 p))))
611
612
613 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
614 ;; annotations
615
616 (define-public (numbered-footnotes int)
617   (markup #:tiny (number->string (+ 1 int))))
618
619 (define-public (symbol-footnotes int)
620   (define (helper symbols out idx n)
621     (if (< n 1)
622         out
623         (helper symbols
624                 (string-append out (list-ref symbols idx))
625                 idx
626                 (- n 1))))
627   (markup #:tiny (helper '("*" "†" "‡" "§" "¶")
628                           ""
629                           (remainder int 5)
630                           (+ 1 (quotient int 5)))))
631
632 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
633 ;; accidentals
634
635 (define-public (accidental-interface::calc-alteration grob)
636   (ly:pitch-alteration (ly:event-property (event-cause grob) 'pitch)))
637
638 (define-public cancellation-glyph-name-alist
639   '((0 . "accidentals.natural")))
640
641 (define-public standard-alteration-glyph-name-alist
642   '(
643     ;; ordered for optimal performance.
644     (0 . "accidentals.natural")
645     (-1/2 . "accidentals.flat")
646     (1/2 . "accidentals.sharp")
647
648     (1 . "accidentals.doublesharp")
649     (-1 . "accidentals.flatflat")
650
651     (3/4 . "accidentals.sharp.slashslash.stemstemstem")
652     (1/4 . "accidentals.sharp.slashslash.stem")
653     (-1/4 . "accidentals.mirroredflat")
654     (-3/4 . "accidentals.mirroredflat.flat")))
655
656 ;; FIXME: standard vs default, alteration-FOO vs FOO-alteration
657 (define-public alteration-default-glyph-name-alist
658   standard-alteration-glyph-name-alist)
659
660 (define-public makam-alteration-glyph-name-alist
661   '((1 . "accidentals.doublesharp")
662     (8/9 . "accidentals.sharp.slashslashslash.stemstem")
663     (5/9 . "accidentals.sharp.slashslashslash.stem")
664     (4/9 . "accidentals.sharp")
665     (1/9 . "accidentals.sharp.slashslash.stem")
666     (0 . "accidentals.natural")
667     (-1/9 . "accidentals.mirroredflat")
668     (-4/9 . "accidentals.flat.slash")
669     (-5/9 . "accidentals.flat")
670     (-8/9 . "accidentals.flat.slashslash")
671     (-1 . "accidentals.flatflat")))
672
673 (define-public alteration-hufnagel-glyph-name-alist
674   '((-1/2 . "accidentals.hufnagelM1")
675     (0 . "accidentals.vaticana0")
676     (1/2 . "accidentals.mensural1")))
677
678 (define-public alteration-medicaea-glyph-name-alist
679   '((-1/2 . "accidentals.medicaeaM1")
680     (0 . "accidentals.vaticana0")
681     (1/2 . "accidentals.mensural1")))
682
683 (define-public alteration-vaticana-glyph-name-alist
684   '((-1/2 . "accidentals.vaticanaM1")
685     (0 . "accidentals.vaticana0")
686     (1/2 . "accidentals.mensural1")))
687
688 (define-public alteration-mensural-glyph-name-alist
689   '((-1/2 . "accidentals.mensuralM1")
690     (0 . "accidentals.vaticana0")
691     (1/2 . "accidentals.mensural1")))
692
693 (define-public alteration-kievan-glyph-name-alist
694  '((-1/2 . "accidentals.kievanM1")
695    (1/2 . "accidentals.kievan1")))
696
697 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
698 ;; * Pitch Trill Heads
699 ;; * Parentheses
700
701 (define-public (parentheses-item::calc-parenthesis-stencils grob)
702   (let* ((font (ly:grob-default-font grob))
703          (lp (ly:font-get-glyph font "accidentals.leftparen"))
704          (rp (ly:font-get-glyph font "accidentals.rightparen")))
705
706     (list lp rp)))
707
708 (define-public (parentheses-item::calc-angled-bracket-stencils grob)
709   (let* ((parent (ly:grob-parent grob Y))
710          (y-extent (ly:grob-extent parent parent Y))
711          (half-thickness 0.05) ; should it be a property?
712          (width 0.5) ; should it be a property?
713          (angularity 1.5)  ; makes angle brackets
714          (white-padding 0.1) ; should it be a property?
715          (lp (ly:stencil-aligned-to
716                  (ly:stencil-aligned-to
717                    (make-parenthesis-stencil y-extent
718                                              half-thickness
719                                              (- width)
720                                              angularity)
721                    Y CENTER)
722                  X RIGHT))
723          (lp-x-extent
724            (interval-widen (ly:stencil-extent lp X) white-padding))
725          (rp (ly:stencil-aligned-to
726                  (ly:stencil-aligned-to
727                    (make-parenthesis-stencil y-extent
728                                              half-thickness
729                                              width
730                                              angularity)
731                    Y CENTER)
732                  X LEFT))
733           (rp-x-extent
734             (interval-widen (ly:stencil-extent rp X) white-padding)))
735     (set! lp (ly:make-stencil (ly:stencil-expr lp)
736                               lp-x-extent
737                               (ly:stencil-extent lp Y)))
738     (set! rp (ly:make-stencil (ly:stencil-expr rp)
739                               rp-x-extent
740                               (ly:stencil-extent rp Y)))
741     (list (stencil-whiteout lp)
742           (stencil-whiteout rp))))
743
744 (define (parenthesize-elements grob . rest)
745   (let* ((refp (if (null? rest)
746                    grob
747                    (car rest)))
748          (elts (ly:grob-object grob 'elements))
749          (x-ext (ly:relative-group-extent elts refp X))
750          (stencils (ly:grob-property grob 'stencils))
751          (lp (car stencils))
752          (rp (cadr stencils))
753          (padding (ly:grob-property grob 'padding 0.1)))
754
755     (ly:stencil-add
756      (ly:stencil-translate-axis lp (- (car x-ext) padding) X)
757      (ly:stencil-translate-axis rp (+ (cdr x-ext) padding) X))))
758
759
760 (define-public (parentheses-item::print me)
761   (let* ((elts (ly:grob-object me 'elements))
762          (y-ref (ly:grob-common-refpoint-of-array me elts Y))
763          (x-ref (ly:grob-common-refpoint-of-array me elts X))
764          (stencil (parenthesize-elements me x-ref))
765          (elt-y-ext (ly:relative-group-extent elts y-ref Y))
766          (y-center (interval-center elt-y-ext)))
767
768     (ly:stencil-translate
769      stencil
770      (cons
771       (- (ly:grob-relative-coordinate me x-ref X))
772       (- y-center (ly:grob-relative-coordinate me y-ref Y))))))
773
774
775
776 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
777 ;;
778
779 (define-public (chain-grob-member-functions grob value . funcs)
780   (for-each
781    (lambda (func)
782      (set! value (func grob value)))
783    funcs)
784
785   value)
786
787
788 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
789 ;; falls/doits
790
791 (define-public (bend::print spanner)
792   (define (close  a b)
793     (< (abs (- a b)) 0.01))
794
795   (let* ((delta-y (* 0.5 (ly:grob-property spanner 'delta-position)))
796          (left-span (ly:spanner-bound spanner LEFT))
797          (dots (if (and (grob::has-interface left-span 'note-head-interface)
798                         (ly:grob? (ly:grob-object left-span 'dot)))
799                    (ly:grob-object left-span 'dot) #f))
800
801          (right-span (ly:spanner-bound spanner RIGHT))
802          (thickness (* (ly:grob-property spanner 'thickness)
803                        (ly:output-def-lookup (ly:grob-layout spanner)
804                                              'line-thickness)))
805          (padding (ly:grob-property spanner 'padding 0.5))
806          (common (ly:grob-common-refpoint right-span
807                                           (ly:grob-common-refpoint spanner
808                                                                    left-span X)
809                                           X))
810          (common-y (ly:grob-common-refpoint spanner left-span Y))
811          (minimum-length (ly:grob-property spanner 'minimum-length 0.5))
812
813          (left-x (+ padding
814                     (max
815                      (interval-end (ly:grob-robust-relative-extent
816                                     left-span common X))
817                      (if
818                       (and dots
819                            (close
820                             (ly:grob-relative-coordinate dots common-y Y)
821                             (ly:grob-relative-coordinate spanner common-y Y)))
822                       (interval-end
823                        (ly:grob-robust-relative-extent dots common X))
824                       ;; TODO: use real infinity constant.
825                       -10000))))
826          (right-x (max (- (interval-start
827                            (ly:grob-robust-relative-extent right-span common X))
828                           padding)
829                        (+ left-x minimum-length)))
830          (self-x (ly:grob-relative-coordinate spanner common X))
831          (dx (- right-x left-x))
832          (exp (list 'path thickness
833                     `(quote
834                       (rmoveto
835                        ,(- left-x self-x) 0
836
837                        rcurveto
838                        ,(/ dx 3)
839                        0
840                        ,dx ,(* 0.66 delta-y)
841                        ,dx ,delta-y)))))
842
843     (ly:make-stencil
844      exp
845      (cons (- left-x self-x) (- right-x self-x))
846      (cons (min 0 delta-y)
847            (max 0 delta-y)))))
848
849
850 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
851 ;; grace spacing
852
853 (define-public (grace-spacing::calc-shortest-duration grob)
854   (let* ((cols (ly:grob-object grob 'columns))
855          (get-difference
856           (lambda (idx)
857             (ly:moment-sub (ly:grob-property
858                             (ly:grob-array-ref cols (1+ idx)) 'when)
859                            (ly:grob-property
860                             (ly:grob-array-ref cols idx) 'when))))
861
862          (moment-min (lambda (x y)
863                        (cond
864                         ((and x y)
865                          (if (ly:moment<? x y)
866                              x
867                              y))
868                         (x x)
869                         (y y)))))
870
871     (fold moment-min #f (map get-difference
872                              (iota (1- (ly:grob-array-length cols)))))))
873
874
875 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
876 ;; fingering
877
878 (define-public (fingering::calc-text grob)
879   (let* ((event (event-cause grob))
880          (digit (ly:event-property event 'digit)))
881
882     (number->string digit 10)))
883
884 (define-public (string-number::calc-text grob)
885   (let ((digit (ly:event-property (event-cause grob) 'string-number)))
886
887     (number->string digit 10)))
888
889 (define-public (stroke-finger::calc-text grob)
890   (let* ((digit (ly:event-property (event-cause grob) 'digit))
891          (text (ly:event-property (event-cause grob) 'text)))
892
893     (if (string? text)
894         text
895         (vector-ref (ly:grob-property grob 'digit-names)
896                     (1- (max (min 5 digit) 1))))))
897
898
899 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
900 ;; dynamics
901
902 (define-public (hairpin::calc-grow-direction grob)
903   (if (ly:in-event-class? (event-cause grob) 'decrescendo-event)
904       START
905       STOP))
906
907 (define-public (dynamic-text-spanner::before-line-breaking grob)
908   "Monitor left bound of @code{DynamicTextSpanner} for absolute dynamics.
909 If found, ensure @code{DynamicText} does not collide with spanner text by
910 changing @code{'attach-dir} and @code{'padding}.  Reads the
911 @code{'right-padding} property of @code{DynamicText} to fine tune space
912 between the two text elements."
913   (let ((left-bound (ly:spanner-bound grob LEFT)))
914     (if (grob::has-interface left-bound 'dynamic-text-interface)
915         (let* ((details (ly:grob-property grob 'bound-details))
916                (left-details (ly:assoc-get 'left details))
917                (my-padding (ly:assoc-get 'padding left-details))
918                (script-padding (ly:grob-property left-bound 'right-padding 0)))
919
920           (and (number? my-padding)
921                (ly:grob-set-nested-property! grob
922                                              '(bound-details left attach-dir)
923                                              RIGHT)
924                (ly:grob-set-nested-property! grob
925                                              '(bound-details left padding)
926                                              (+ my-padding script-padding)))))))
927
928
929 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
930 ;; lyrics
931
932 (define-public (lyric-text::print grob)
933   "Allow interpretation of tildes as lyric tieing marks."
934
935   (let ((text (ly:grob-property grob 'text)))
936
937     (grob-interpret-markup grob (if (string? text)
938                                     (make-tied-lyric-markup text)
939                                     text))))
940
941 (define-public ((grob::calc-property-by-copy prop) grob)
942   (ly:event-property (event-cause grob) prop))
943
944
945 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
946 ;; fret boards
947
948 (define-public (fret-board::calc-stencil grob)
949   (grob-interpret-markup
950    grob
951    (make-fret-diagram-verbose-markup
952     (ly:grob-property grob 'dot-placement-list))))
953
954
955 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
956 ;; scripts
957
958 (define-public (script-interface::calc-x-offset grob)
959   (ly:grob-property grob 'positioning-done)
960   (let* ((shift (ly:grob-property grob 'toward-stem-shift 0.0))
961          (note-head-location
962           (ly:self-alignment-interface::centered-on-x-parent grob))
963          (note-head-grob (ly:grob-parent grob X))
964          (stem-grob (ly:grob-object note-head-grob 'stem)))
965
966     (+ note-head-location
967        ;; If the property 'toward-stem-shift is defined and the script
968        ;; has the same direction as the stem, move the script accordingly.
969        ;; Since scripts can also be over skips, we need to check whether
970        ;; the grob has a stem at all.
971        (if (ly:grob? stem-grob)
972            (let ((dir1 (ly:grob-property grob 'direction))
973                  (dir2 (ly:grob-property stem-grob 'direction)))
974              (if (equal? dir1 dir2)
975                  (let* ((common-refp (ly:grob-common-refpoint grob stem-grob X))
976                         (stem-location
977                          (ly:grob-relative-coordinate stem-grob common-refp X)))
978                    (* shift (- stem-location note-head-location)))
979                  0.0))
980            0.0))))
981
982
983 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
984 ;; instrument names
985
986 (define-public (system-start-text::print grob)
987   (let* ((left-bound (ly:spanner-bound grob LEFT))
988          (left-mom (ly:grob-property left-bound 'when))
989          (name (if (moment<=? left-mom ZERO-MOMENT)
990                    (ly:grob-property grob 'long-text)
991                    (ly:grob-property grob 'text))))
992
993     (if (and (markup? name)
994              (!= (ly:item-break-dir left-bound) CENTER))
995
996         (grob-interpret-markup grob name)
997         (ly:grob-suicide! grob))))
998
999 (define-public (system-start-text::calc-x-offset grob)
1000   (let* ((left-bound (ly:spanner-bound grob LEFT))
1001          (left-mom (ly:grob-property left-bound 'when))
1002          (layout (ly:grob-layout grob))
1003          (indent (ly:output-def-lookup layout
1004                                        (if (moment<=? left-mom ZERO-MOMENT)
1005                                            'indent
1006                                            'short-indent)
1007                                        0.0))
1008          (system (ly:grob-system grob))
1009          (my-extent (ly:grob-extent grob system X))
1010          (elements (ly:grob-object system 'elements))
1011          (common (ly:grob-common-refpoint-of-array system elements X))
1012          (total-ext empty-interval)
1013          (align-x (ly:grob-property grob 'self-alignment-X 0))
1014          (padding (min 0 (- (interval-length my-extent) indent)))
1015          (right-padding (- padding
1016                            (/ (* padding (1+ align-x)) 2))))
1017
1018     ;; compensate for the variation in delimiter extents by
1019     ;; calculating an X-offset correction based on united extents
1020     ;; of all delimiters in this system
1021     (let unite-delims ((l (ly:grob-array-length elements)))
1022       (if (> l 0)
1023           (let ((elt (ly:grob-array-ref elements (1- l))))
1024
1025             (if (grob::has-interface elt 'system-start-delimiter-interface)
1026                 (let ((dims (ly:grob-extent elt common X)))
1027                   (if (interval-sane? dims)
1028                       (set! total-ext (interval-union total-ext dims)))))
1029             (unite-delims (1- l)))))
1030
1031     (+
1032      (ly:side-position-interface::x-aligned-side grob)
1033      right-padding
1034      (- (interval-length total-ext)))))
1035
1036 (define-public (system-start-text::calc-y-offset grob)
1037
1038   (define (live-elements-list me)
1039     (let ((elements (ly:grob-object me 'elements)))
1040
1041       (filter! grob::is-live?
1042                (ly:grob-array->list elements))))
1043
1044   (let* ((left-bound (ly:spanner-bound grob LEFT))
1045          (live-elts (live-elements-list grob))
1046          (system (ly:grob-system grob))
1047          (extent empty-interval))
1048
1049     (if (and (pair? live-elts)
1050              (interval-sane? (ly:grob-extent grob system Y)))
1051         (let get-extent ((lst live-elts))
1052           (if (pair? lst)
1053               (let ((axis-group (car lst)))
1054
1055                 (if (and (ly:spanner? axis-group)
1056                          (equal? (ly:spanner-bound axis-group LEFT)
1057                                  left-bound))
1058                     (set! extent (add-point extent
1059                                             (ly:grob-relative-coordinate
1060                                              axis-group system Y))))
1061                 (get-extent (cdr lst)))))
1062         ;; no live axis group(s) for this instrument name -> remove from system
1063         (ly:grob-suicide! grob))
1064
1065     (+
1066      (ly:self-alignment-interface::y-aligned-on-self grob)
1067      (interval-center extent))))
1068
1069
1070 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1071 ;; ambitus
1072
1073 (define-public (ambitus::print grob)
1074   (let ((heads (ly:grob-object grob 'note-heads)))
1075
1076     (if (and (ly:grob-array? heads)
1077              (= (ly:grob-array-length heads) 2))
1078         (let* ((common (ly:grob-common-refpoint-of-array grob heads Y))
1079                (head-down (ly:grob-array-ref heads 0))
1080                (head-up (ly:grob-array-ref heads 1))
1081                (gap (ly:grob-property grob 'gap 0.35))
1082                (point-min (+ (interval-end (ly:grob-extent head-down common Y))
1083                              gap))
1084                (point-max (- (interval-start (ly:grob-extent head-up common Y))
1085                              gap)))
1086
1087           (if (< point-min point-max)
1088               (let* ((layout (ly:grob-layout grob))
1089                      (line-thick (ly:output-def-lookup layout 'line-thickness))
1090                      (blot (ly:output-def-lookup layout 'blot-diameter))
1091                      (grob-thick (ly:grob-property grob 'thickness 2))
1092                      (width (* line-thick grob-thick))
1093                      (x-ext (symmetric-interval (/ width 2)))
1094                      (y-ext (cons point-min point-max))
1095                      (line (ly:round-filled-box x-ext y-ext blot))
1096                      (y-coord (ly:grob-relative-coordinate grob common Y)))
1097
1098                 (ly:stencil-translate-axis line (- y-coord) Y))
1099               empty-stencil))
1100         (begin
1101           (ly:grob-suicide! grob)
1102           (list)))))
1103
1104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1105 ;;  laissez-vibrer tie
1106 ;;
1107 ;;  needed so we can make laissez-vibrer a pure print
1108 ;;
1109 (define-public (laissez-vibrer::print grob)
1110  (ly:tie::print grob))
1111