]> git.donarmstrong.com Git - lilypond.git/blob - scm/translation-functions.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / translation-functions.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; (c) 1998--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
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 ;; clefs
22
23 (define-public (clef-transposition-markup oct style)
24   "The transposition sign formatting function.  @var{oct} is supposed to be
25 a string holding the transposition number, @var{style} determines the
26 way the transposition number is displayed."
27   (let* ((delim (if (symbol? style)
28                     (case style
29                       ((parenthesized) (cons "(" ")"))
30                       ((bracketed) (cons "[" "]"))
31                       (else (cons "" "")))
32                     (cons "" "")))
33          (text (string-concatenate (list (car delim) oct (cdr delim)))))
34
35     (make-vcenter-markup text)))
36
37
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; metronome marks
40
41 (define-public (format-metronome-markup event context)
42    (let ((hide-note (ly:context-property context 'tempoHideNote #f))
43          (text (ly:event-property event 'text))
44          (dur (ly:event-property event 'tempo-unit))
45          (count (ly:event-property event 'metronome-count)))
46    (metronome-markup text dur count hide-note)))
47 (export format-metronome-markup)
48
49 (define (metronome-markup text dur count hide-note)
50   (let* ((note-mark
51             (if (and (not hide-note) (ly:duration? dur))
52                 (make-smaller-markup
53                   (make-note-by-number-markup
54                     (ly:duration-log dur)
55                     (ly:duration-dot-count dur)
56                     UP))
57                 #f))
58          (count-markup (cond ((number? count)
59                               (if (> count 0)
60                                   (make-simple-markup
61                                           (number->string count))
62                                   #f))
63                              ((pair? count)
64                               (make-concat-markup
65                                (list
66                                 (make-simple-markup
67                                         (number->string (car count)))
68                                 (make-simple-markup " ")
69                                 (make-simple-markup "–")
70                                 (make-simple-markup " ")
71                                 (make-simple-markup
72                                         (number->string (cdr count))))))
73                              (else #f)))
74          (note-markup (if (and (not hide-note) count-markup)
75                           (make-concat-markup
76                            (list
77                             (make-general-align-markup Y DOWN note-mark)
78                             (make-simple-markup " ")
79                             (make-simple-markup "=")
80                             (make-simple-markup " ")
81                             count-markup))
82                           #f))
83          (text-markup (if (not (null? text))
84                           (make-bold-markup text)
85                           #f)))
86     (if text-markup
87         (if (and note-markup (not hide-note))
88             (make-line-markup (list text-markup
89                                     (make-concat-markup
90                                      (list (make-simple-markup "(")
91                                            note-markup
92                                            (make-simple-markup ")")))))
93             (make-line-markup (list text-markup)))
94         (if note-markup
95             (make-line-markup (list note-markup))
96             (make-null-markup)))))
97
98 (define-public (format-mark-alphabet mark context)
99   (make-bold-markup (make-markalphabet-markup (1- mark))))
100
101 (define-public (format-mark-box-alphabet mark context)
102   (make-bold-markup (make-box-markup (make-markalphabet-markup (1- mark)))))
103
104 (define-public (format-mark-circle-alphabet mark context)
105   (make-bold-markup (make-circle-markup (make-markalphabet-markup (1- mark)))))
106
107 (define-public (format-mark-letters mark context)
108   (make-bold-markup (make-markletter-markup (1- mark))))
109
110 (define-public (format-mark-numbers mark context)
111   (make-bold-markup (number->string mark)))
112
113 (define-public (format-mark-barnumbers mark context)
114   (make-bold-markup (number->string (ly:context-property context
115                                                          'currentBarNumber))))
116
117 (define-public (format-mark-box-letters mark context)
118   (make-bold-markup (make-box-markup (make-markletter-markup (1- mark)))))
119
120 (define-public (format-mark-circle-letters mark context)
121   (make-bold-markup (make-circle-markup (make-markletter-markup (1- mark)))))
122
123 (define-public (format-mark-box-numbers mark context)
124   (make-bold-markup (make-box-markup (number->string mark))))
125
126 (define-public (format-mark-circle-numbers mark context)
127   (make-bold-markup (make-circle-markup (number->string mark))))
128
129 (define-public (format-mark-box-barnumbers mark context)
130   (make-bold-markup (make-box-markup
131                      (number->string (ly:context-property context
132                                                           'currentBarNumber)))))
133
134 (define-public (format-mark-circle-barnumbers mark context)
135   (make-bold-markup (make-circle-markup
136                      (number->string (ly:context-property context
137                                                           'currentBarNumber)))))
138
139
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; Bass figures.
142
143 (define-public (format-bass-figure figure event context)
144   (let* ((fig (ly:event-property event 'figure))
145          (fig-markup (if (number? figure)
146
147                          ;; this is not very elegant, but center-aligning
148                          ;; all digits is problematic with other markups,
149                          ;; and shows problems in the (lack of) overshoot
150                          ;; of feta-alphabet glyphs.
151                          ((if (<= 10 figure)
152                               (lambda (y) (make-translate-scaled-markup
153                                            (cons -0.7 0) y))
154                               identity)
155
156                           (cond
157                            ((eq? #t (ly:event-property event 'diminished))
158                             (markup #:slashed-digit figure))
159                            ((eq? #t (ly:event-property event 'augmented-slash))
160                             (markup #:backslashed-digit figure))
161                            (else (markup #:number (number->string figure 10)))))
162                          #f))
163
164          (alt (ly:event-property event 'alteration))
165          (alt-markup
166           (if (number? alt)
167               (markup
168                #:general-align Y DOWN #:fontsize
169                (if (not (= alt DOUBLE-SHARP))
170                    -2 2)
171                (alteration->text-accidental-markup alt))
172               #f))
173
174          (plus-markup (if (eq? #t (ly:event-property event 'augmented))
175                           (markup #:number "+")
176                           #f))
177
178          (alt-dir (ly:context-property context 'figuredBassAlterationDirection))
179          (plus-dir (ly:context-property context 'figuredBassPlusDirection)))
180
181     (if (and (not fig-markup) alt-markup)
182         (begin
183           (set! fig-markup (markup #:left-align #:pad-around 0.3 alt-markup))
184           (set! alt-markup #f)))
185
186
187     ;; hmm, how to get figures centered between note, and
188     ;; lone accidentals too?
189
190     ;;    (if (markup? fig-markup)
191     ;;  (set!
192     ;;   fig-markup (markup #:translate (cons 1.0 0)
193     ;;                      #:center-align fig-markup)))
194
195     (if alt-markup
196         (set! fig-markup
197               (markup #:put-adjacent
198                       X (if (number? alt-dir)
199                             alt-dir
200                             LEFT)
201                       fig-markup
202                       #:pad-x 0.2 alt-markup)))
203
204     (if plus-markup
205         (set! fig-markup
206               (if fig-markup
207                   (markup #:put-adjacent
208                           X (if (number? plus-dir)
209                                 plus-dir
210                                 LEFT)
211                           fig-markup
212                           #:pad-x 0.2 plus-markup)
213                   plus-markup)))
214
215     (if (markup? fig-markup)
216         (markup #:fontsize -2 fig-markup)
217         empty-markup)))
218
219
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;; fret diagrams
222
223 (define (create-fretboard context grob placement-list)
224   "Convert @var{placement-list} into a fretboard @var{grob}."
225
226   (let* ((tunings (ly:context-property context 'stringTunings))
227          (my-string-count (length tunings))
228          (details (ly:grob-property grob 'fret-diagram-details)))
229
230     ;; Add string-count from string-tunings to fret-diagram-details.
231     (set! (ly:grob-property grob 'fret-diagram-details)
232           (acons 'string-count my-string-count details))
233     ;; Create the dot-placement list for the grob
234     (set! (ly:grob-property grob 'dot-placement-list) placement-list)))
235
236 (define-public
237   (determine-frets context notes specified-info . rest)
238   "Determine string numbers and frets for playing @var{notes}
239 as a chord, given specified information @var{specified-info}.
240 @var{specified-info} is a list with two list elements,
241 specified strings @code{defined-strings} and
242 specified fingerings @code{defined-fingers}.  Only a fingering of@tie{}0
243 will affect the fret selection, as it specifies an open string.
244 If @code{defined-strings} is @code{'()}, the context property
245 @code{defaultStrings} will be used as a list of defined strings.
246 Will look for predefined fretboards if @code{predefinedFretboardTable}
247 is not @code {#f}.  If @var{rest} is present, it contains the
248 @code{FretBoard} grob, and a fretboard will be
249 created.  Otherwise, a list of @code{(string fret finger)} lists will
250 be returned.
251 If the context-property @code{supportNonIntegerFret} is set @code{#t},
252 micro-tones are supported for TabStaff, but not not for FretBoards."
253
254   ;;  helper functions
255
256   (define (string-frets->placement-list string-frets string-count)
257     "Convert @var{string-frets} to @code{fret-diagram-verbose}
258 dot placement entries."
259     (let* ((placements (list->vector
260                         (map (lambda (x) (list 'mute  x))
261                              (iota string-count 1)))))
262
263       (for-each (lambda (sf)
264                   (let* ((string (car sf))
265                          (fret (cadr sf))
266                          (finger (caddr sf)))
267                     (vector-set!
268                      placements
269                      (1- string)
270                      (if (= 0 fret)
271                          (list 'open string)
272                          (if finger
273                              (list 'place-fret string fret finger)
274                              (list 'place-fret string fret))))))
275                 string-frets)
276       (vector->list placements)))
277
278   (define (placement-list->string-frets placement-list)
279     "Convert @var{placement-list} to string-fret list."
280     (map (lambda (x) (if (eq? (car x) 'place-fret)
281                          (cdr x)
282                          (list (cadr x) 0)))
283          (filter (lambda (l) (or (eq? (car l) 'place-fret)
284                                  (eq? (car l) 'open)))
285                  placement-list)))
286
287   (define (entry-count art-list)
288     "Count the number of entries in a list of articulations."
289     (length (filter (lambda (x) (not (null? x)))
290                     art-list)))
291
292   (define (string-number event)
293     "Get the string-number from @var{event}.  Return @var{#f}
294 if no string-number is present."
295     (let ((num (ly:event-property event 'string-number)))
296       (and (integer? num) (positive? num) num)))
297
298   (define (determine-frets-and-strings
299            notes
300            defined-strings
301            defined-fingers
302            minimum-fret
303            maximum-stretch
304            tuning)
305     "Determine the frets and strings used to play the notes in
306 @var{notes}, given @var{defined-strings} and @var{defined-fingers}
307 along with @var{minimum-fret}, @var{maximum-stretch}, and
308 @var{tuning}.  Returns a list of @code{(string fret finger) lists."
309
310
311     (define restrain-open-strings (ly:context-property context
312                                                        'restrainOpenStrings
313                                                        #f))
314     (define specified-frets '())
315     (define free-strings (iota (length tuning) 1))
316
317     (define (calc-fret pitch string tuning)
318       "Calculate the fret to play @var{pitch} on @var{string} with
319 @var{tuning}."
320       (* 2  (- (ly:pitch-tones pitch) (ly:pitch-tones (list-ref tuning (1- string))))))
321
322     (define (note-pitch note)
323       "Get the pitch (in semitones) from @var{note}."
324       (ly:event-property note 'pitch))
325
326     (define (note-finger ev)
327       "Get the fingering from @var{ev}.  Return @var{#f}
328 if no fingering is present."
329       (let* ((articulations (ly:event-property ev 'articulations))
330              (finger-found #f))
331         (for-each (lambda (art)
332                     (let* ((num (ly:event-property art 'digit)))
333
334                       (if (and (ly:in-event-class? art 'fingering-event)
335                                (number? num)
336                                (> num 0))
337                           (set! finger-found num))))
338                   articulations)
339         finger-found))
340
341     (define (delete-free-string string)
342       (if (number? string)
343           (set! free-strings
344                 (delete string free-strings))))
345
346     (define (close-enough fret)
347       "Decide if @var{fret} is acceptable, given the already used frets."
348       (every (lambda (specced-fret)
349                (or (zero? specced-fret)
350                    (zero? fret)
351                    (>= maximum-stretch (abs (- fret specced-fret)))))
352              specified-frets))
353
354     (define (string-qualifies string pitch)
355       "Can @var{pitch} be played on @var{string}, given already placed
356 notes?"
357       (let* ((fret (calc-fret pitch string tuning)))
358         (and (or (and (not restrain-open-strings)
359                       (zero? fret))
360                  (>= fret minimum-fret))
361              (if (and
362                    (ly:context-property context 'supportNonIntegerFret #f)
363                    (null? rest))
364                  (integer? (truncate fret))
365                  (integer? fret))
366              (close-enough fret))))
367
368     (define (open-string string pitch)
369       "Is @var{pitch} and open-string note on @var{string}, given
370 the current tuning?"
371       (let* ((fret (calc-fret pitch string tuning)))
372         (zero? fret)))
373
374     (define (set-fret! pitch-entry string finger)
375       (let ((this-fret (calc-fret (car pitch-entry)
376                                   string
377                                   tuning)))
378         (if (< this-fret 0)
379             (ly:warning (_ "Negative fret for pitch ~a on string ~a")
380                         (car pitch-entry) string)
381             (if (and
382                   (not (integer? this-fret))
383                   (not (ly:context-property context 'supportNonIntegerFret #f)))
384                 (ly:warning (_ "Missing fret for pitch ~a on string ~a")
385                             (car pitch-entry) string)))
386         (delete-free-string string)
387         (set! specified-frets (cons this-fret specified-frets))
388         (list-set! string-fret-fingers
389                    (cdr pitch-entry)
390                    (list string this-fret finger))))
391
392     (define (kill-note! string-fret-fingers note-index)
393       (list-set! string-fret-fingers note-index (list #f #t)))
394
395     (define string-fret-fingers
396       (map (lambda (string finger)
397              (if (null? finger)
398                  (list string #f)
399                  (list string #f finger)))
400            defined-strings defined-fingers))
401
402     ;;; body of determine-frets-and-strings
403     (let* ((pitches (map note-pitch notes))
404            (pitch-alist (map cons pitches (iota (length pitches)))))
405
406       ;; handle notes with strings assigned and fingering of 0
407       (for-each
408        (lambda (pitch-entry string-fret-finger)
409          (let* ((string (list-ref string-fret-finger 0))
410                 (finger (if (= (length string-fret-finger) 3)
411                             (list-ref string-fret-finger 2)
412                             '()))
413                 (pitch (car pitch-entry))
414                 (digit (if (null? finger)
415                            #f
416                            finger)))
417            (if (or (not (null? string))
418                    (eqv? digit 0))
419                (if (eqv? digit 0)
420                    ;; here we handle fingers of 0 -- open strings
421                    (let ((fit-string
422                           (find (lambda (string)
423                                   (open-string string pitch))
424                                 free-strings)))
425                      (if fit-string
426                          (set-fret! pitch-entry fit-string #f)
427                          (ly:warning (_ "No open string for pitch ~a")
428                                      pitch)))
429                    ;; here we handle assigned strings
430                    (let* ((this-fret
431                            (calc-fret pitch string tuning))
432                           (possible-fret?
433                            (and (>= this-fret 0)
434                                 (if (and
435                                       (ly:context-property
436                                         context 'supportNonIntegerFret #f)
437                                       (null? rest))
438                                     (integer? (truncate this-fret))
439                                     (integer? this-fret))))
440                           (handle-negative
441                            (ly:context-property context
442                                                 'handleNegativeFrets
443                                                 'recalculate)))
444                      (cond ((or possible-fret?
445                                 (eq? handle-negative 'include))
446                             (set-fret! pitch-entry string finger))
447                            ((eq? handle-negative 'recalculate)
448                             (begin
449                               (ly:warning
450                                (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
451                                string
452                                pitch)
453                               (ly:warning (_ "Ignoring string request and recalculating."))
454                               (list-set! string-fret-fingers
455                                          (cdr pitch-entry)
456                                          (if (null? finger)
457                                              (list '() #f)
458                                              (list '() #f finger)))))
459                            ((eq? handle-negative 'ignore)
460                             (begin
461                               (ly:warning
462                                (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
463                                string
464                                pitch)
465                               (ly:warning (_ "Ignoring note in tablature."))
466                               (kill-note! string-fret-fingers
467                                           (cdr pitch-entry))))))))))
468        pitch-alist string-fret-fingers)
469       ;; handle notes without strings assigned -- sorted by pitch, so
470       ;; we need to use the alist to have the note number available
471       (for-each
472        (lambda (pitch-entry)
473          (let* ((string-fret-finger (list-ref string-fret-fingers
474                                               (cdr pitch-entry)))
475                 (string (list-ref string-fret-finger 0))
476                 (finger (if (= (length string-fret-finger) 3)
477                             (list-ref string-fret-finger 2)
478                             '()))
479                 (pitch (car pitch-entry))
480                 (fit-string
481                  (find (lambda (string)
482                          (string-qualifies string pitch))
483                        free-strings)))
484            (if (not (list-ref string-fret-finger 1))
485                (if fit-string
486                    (set-fret! pitch-entry fit-string finger)
487                    (begin
488                      (ly:event-warning
489                       (list-ref notes (cdr pitch-entry))
490                       (_ "No string for pitch ~a (given frets ~a)")
491                       pitch
492                       specified-frets)
493                      (kill-note! string-fret-fingers
494                                  (cdr pitch-entry)))))))
495        (sort pitch-alist (lambda (pitch-entry-a pitch-entry-b)
496                            (ly:pitch<? (car pitch-entry-b)
497                                        (car pitch-entry-a)))))
498       string-fret-fingers)) ;; end of determine-frets-and-strings
499
500   (define (get-predefined-fretboard predefined-fret-table tuning pitches)
501     "Search through @var{predefined-fret-table} looking for a predefined
502 fretboard with a key of @var{(tuning . pitches)}.  The search will check
503 both up and down an octave in order to accomodate transposition of the
504 chords.  Returns a placement-list."
505
506     (define (get-fretboard key)
507       (let ((hash-handle
508              (hash-get-handle predefined-fret-table key)))
509         (if hash-handle
510             (cdr hash-handle)  ; return table entry
511             '())))
512
513     ;; body of get-predefined-fretboard
514     (let ((test-fretboard (get-fretboard (cons tuning pitches))))
515       (if (not (null? test-fretboard))
516           test-fretboard
517           (let ((test-fretboard
518                  (get-fretboard
519                   (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
520             (if (not (null? test-fretboard))
521                 test-fretboard
522                 (get-fretboard
523                  (cons tuning (map (lambda (x) (shift-octave x -1))
524                                    pitches))))))))
525
526   ;; TODO: Does it make sense to have additional bass strings in a fret-diagram?
527   (if (and (not (null? rest))
528            (not (null? (ly:context-property context 'additionalBassStrings))))
529       (ly:warning "additional bass strings are not supported by FretBoards"))
530
531   ;; body of determine-frets
532   (let* ((predefined-fret-table
533           (ly:context-property context 'predefinedDiagramTable))
534          (tunings
535            (append
536              (ly:context-property context 'stringTunings)
537              (ly:context-property context 'additionalBassStrings '())))
538          (string-count (length tunings))
539          (grob (if (null? rest) '() (car rest)))
540          (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
541          (defined-strings (map (lambda (x)
542                                  (if (null? x)
543                                      x
544                                      (or (string-number x) '())))
545                                (car specified-info)))
546          (defined-fingers (map (lambda (x)
547                                  (if (null? x)
548                                      x
549                                      (ly:event-property x 'digit)))
550                                (cadr specified-info)))
551          (default-strings (ly:context-property context 'defaultStrings '()))
552          (strings-used (if (and (zero? (entry-count defined-strings))
553                                 (not (zero? (entry-count default-strings))))
554                            default-strings
555                            defined-strings))
556          (predefined-fretboard
557           (if predefined-fret-table
558               (get-predefined-fretboard
559                predefined-fret-table
560                tunings
561                pitches)
562               '())))
563     (if (null? predefined-fretboard)
564         (let ((string-frets
565                (determine-frets-and-strings
566                 notes
567                 strings-used
568                 defined-fingers
569                 (ly:context-property context 'minimumFret 0)
570                 (ly:context-property context 'maximumFretStretch 4)
571                 tunings)))
572           (if (null? grob)
573               string-frets
574               (create-fretboard
575                context grob (string-frets->placement-list
576                              (filter (lambda (entry)
577                                        (car entry))
578                                      string-frets)
579                              string-count))))
580         (if (null? grob)
581             (placement-list->string-frets predefined-fretboard)
582             (create-fretboard context grob predefined-fretboard)))))
583
584
585
586 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
587 ;; tablature
588
589 ;; The TabNoteHead tablatureFormat callbacks.
590
591 ;; Calculate the fret from pitch and string number as letter
592 ;; The fret letter is taken from 'fretLabels if present
593 (define-public (fret-letter-tablature-format
594                 context string-number fret-number)
595   (let* ((labels (ly:context-property context 'fretLabels))
596          (string-tunings (ly:context-property context 'stringTunings))
597          (string-count (length string-tunings))
598          (letter
599            (cond
600             ((= 0 (length labels))
601              (string (integer->char (+ fret-number (char->integer #\a)))))
602             ((and (<= 0 fret-number) (< fret-number (length labels)))
603              (list-ref labels fret-number))
604             (else
605              (ly:warning
606                (_ "No label for fret ~a (on string ~a);
607 only ~a fret labels provided")
608                fret-number string-number (length labels))
609              ".")))
610          (add-bass-string-nr ;; starting at zero
611            (- string-number string-count 1)))
612     (make-translate-scaled-markup '(0 . -0.5)
613       ;; For additional bass strings, we add zero up to three "/"-signs before
614       ;; the letter, even more bass strings will get numbers, starting with "4".
615       ;; In the rare case such a string isn't played open, we put out, eg."4b"
616       (make-concat-markup
617         (if (> string-number (+ string-count 4))
618             (list (number->string add-bass-string-nr)
619                   (if (zero? fret-number) "" letter))
620             (list (make-string (max 0 add-bass-string-nr) #\/)
621                   letter))))))
622
623 ;; Display the fret number as a number
624 (define-public (fret-number-tablature-format
625                 context string-number fret-number)
626   (if (integer? fret-number)
627       (make-vcenter-markup
628         (format #f "~a" fret-number))
629       ;; for non-integer fret-number print p.e. "2½"
630       (let* ((whole-part (truncate fret-number))
631              (remaining (- fret-number whole-part))
632              (fret
633                (if (and (zero? whole-part) (not (zero? remaining)))
634                    ""
635                    (format #f "~a" whole-part)))
636              (frac
637                (if (zero? remaining)
638                    ""
639                    (format #f "~a" remaining))))
640         (make-concat-markup
641           (list (make-vcenter-markup fret)
642                 (make-vcenter-markup
643                   ;; the value `-2.5' is my choice
644                   (make-fontsize-markup -2.5 frac)))))))
645
646 ;; The 5-string banjo has got an extra string, the fifth (duh), which
647 ;; starts at the fifth fret on the neck.  Frets on the fifth string
648 ;; are referred to relative to the other frets:
649 ;;   the "first fret" on the fifth string is really the sixth fret
650 ;;   on the banjo neck.
651 ;; We solve this by defining a new fret-number-tablature function:
652 (define-public (fret-number-tablature-format-banjo
653                 context string-number fret-number)
654   (make-vcenter-markup
655    (number->string (cond
656                     ((and (> fret-number 0) (= string-number 5))
657                      (+ fret-number 5))
658                     (else fret-number)))))
659
660 ;;  Tab note head staff position functions
661 ;;
662 ;;  Define where in the staff to display a given string.  Some forms of
663 ;;  tablature put the tab note heads in the spaces, rather than on the
664 ;;  lines
665
666 (define-public (tablature-position-on-lines context string-number)
667   (let* ((string-tunings (ly:context-property context 'stringTunings))
668          (string-count (length string-tunings))
669          (string-nr
670            (if (> string-number (length string-tunings))
671                (1+ (length string-tunings))
672                string-number))
673          (string-one-topmost (ly:context-property context 'stringOneTopmost))
674          (staff-line (- (* 2 string-nr) string-count 1)))
675     (if string-one-topmost
676         (- staff-line)
677         staff-line)))
678
679 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
680 ;; bar numbers
681
682 (define ((every-nth-bar-number-visible n) barnum mp)
683   (= 0 (modulo barnum n)))
684 (export every-nth-bar-number-visible)
685
686 (define ((modulo-bar-number-visible n m) barnum mp)
687   (and (> barnum 1) (= m (modulo barnum n))))
688 (export modulo-bar-number-visible)
689
690 (define ((set-bar-number-visibility n) tr)
691   (let ((bn (ly:context-property tr 'currentBarNumber)))
692     (ly:context-set-property! tr 'barNumberVisibility
693                               (modulo-bar-number-visible n (modulo bn n)))))
694 (export set-bar-number-visibility)
695
696 (define-public (first-bar-number-invisible barnum mp)
697   (> barnum 1))
698
699 (define-public (first-bar-number-invisible-save-broken-bars barnum mp)
700   (or (> barnum 1)
701       (> (ly:moment-main-numerator mp) 0)))
702
703 (define-public (first-bar-number-invisible-and-no-parenthesized-bar-numbers barnum mp)
704   (and (> barnum 1)
705        (= (ly:moment-main-numerator mp) 0)))
706
707 (define-public (robust-bar-number-function barnum measure-pos alt-number context)
708   (define (get-number-and-power an pow)
709     (if (<= an alt-number)
710         (get-number-and-power (+ an (expt 26 (1+ pow))) (1+ pow))
711         (cons (+ alt-number (- (expt 26 pow) an)) (1- pow))))
712   (define (make-letter so-far an pow)
713     (if (< pow 0)
714         so-far
715         (let ((pos (modulo (quotient an (expt 26 pow)) 26)))
716           (make-letter (string-append so-far
717                                       (substring "abcdefghijklmnopqrstuvwxyz"
718                                                  pos
719                                                  (1+ pos)))
720                        an
721                        (1- pow)))))
722   (let* ((number-and-power (get-number-and-power 0 0))
723          (begin-measure (= 0 (ly:moment-main-numerator measure-pos)))
724          (maybe-open-parenthesis (if begin-measure "" "("))
725          (maybe-close-parenthesis (if begin-measure "" ")")))
726     (markup (string-append maybe-open-parenthesis
727                            (number->string barnum)
728                            (make-letter ""
729                                         (car number-and-power)
730                                         (cdr number-and-power))
731                            maybe-close-parenthesis))))
732
733 (define-public (all-bar-numbers-visible barnum mp) #t)
734
735
736 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
737 ;; percent repeat counters
738
739 (define ((every-nth-repeat-count-visible n) count context)
740   (= 0 (modulo count n)))
741 (export every-nth-repeat-count-visible)
742
743 (define-public (all-repeat-counts-visible count context) #t)
744
745 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
746 ;; pitch recognition
747
748 (define-public (make-semitone->pitch pitches)
749   "Convert @var{pitches}, an unordered list of note values
750 covering (after disregarding octaves) all absolute pitches in need of
751 conversion, into a function converting semitone numbers (absolute
752 pitch missing enharmonic information) back into note values.
753
754 For a key signature without accidentals
755 @example
756 c cis d es e f fis g gis a bes b
757 @end example
758 might be a good choice, covering Bb major to A major and their
759 parallel keys, and melodic/harmonic C minor to A minor."
760   ;; TODO: short-circuit lcm calculation once we know it will be large
761   (let* ((size (apply lcm (map (lambda (pitch)
762                                  (denominator (/ (ly:pitch-tones pitch) 6)))
763                                pitches)))
764          ;; Normal tunings need 12 steps per octave, quartertone
765          ;; tunings 24, Makam needs 108.  But microtunings might cause
766          ;; trouble.
767          (lookup (if (> size 400)
768                      (make-hash-table)
769                      (make-vector size #f))))
770     (for-each
771      (lambda (pitch)
772        (let* ((rawoct (/ (ly:pitch-tones pitch) 6))
773               (oct (floor rawoct))
774               (ref (- rawoct oct))
775               (val (ly:pitch-transpose pitch
776                                        (ly:make-pitch (- oct) 0))))
777          (if (hash-table? lookup)
778              (hashv-set! lookup ref val)
779              (vector-set! lookup (* size ref) val))))
780      pitches)
781     (lambda (semitone)
782       "Convert @var{semitone} numbers into note values.  If the
783 originally specified list of pitches does not contain a note
784 corresponding to @var{semitone} (disregarding octaves), @code{#f} is
785 returned."
786       (let* ((rawoct (/ semitone 12))
787              (oct (floor rawoct))
788              (ref (- rawoct oct))
789              (val (if (hash-table? lookup)
790                       (hashv-ref lookup ref)
791                       (let ((ref (* (vector-length lookup) ref)))
792                         (and (integer? ref)
793                              (vector-ref lookup ref))))))
794         (and val
795              (ly:pitch-transpose val (ly:make-pitch oct 0)))))))
796
797 (define ((shift-semitone->pitch key semitone->pitch) semitone)
798   "Given a function @var{semitone->pitch} converting a semitone number
799 into a note value for a lookup table created in relation to@tie{}C,
800 returns a corresponding function in relation to @var{key}.  The note
801 values returned by this function differ only enharmonically from the
802 original @var{semitone->pitch} function."
803   (ly:pitch-transpose (semitone->pitch (- semitone (* 2 (ly:pitch-tones key))))
804                       key))
805
806 (export shift-semitone->pitch)