]> git.donarmstrong.com Git - lilypond.git/blob - scm/parser-clef.scm
Run grand-replace (issue 3765)
[lilypond.git] / scm / parser-clef.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 ;; (name . (glyph clef-position transposition))
20 ;;
21 ;; -- the name clefTransposition is a bit misleading. Value 7 means
22 ;; a transposition of an octave, not a seventh.
23 (define-public supported-clefs
24   '(("treble" . ("clefs.G" -2 0))
25     ("violin" . ("clefs.G" -2 0))
26     ("G" . ("clefs.G" -2 0))
27     ("G2" . ("clefs.G" -2 0))
28     ("french" . ("clefs.G" -4 0))
29     ("soprano" . ("clefs.C" -4 0))
30     ("mezzosoprano" . ("clefs.C" -2 0))
31     ("alto" . ("clefs.C" 0 0))
32     ("C" . ("clefs.C" 0 0))
33     ("tenor" . ("clefs.C" 2 0))
34     ("baritone" . ("clefs.C" 4 0))
35     ("varbaritone" . ("clefs.F" 0 0))
36     ("bass" . ("clefs.F" 2 0))
37     ("F" . ("clefs.F" 2 0))
38     ("subbass" . ("clefs.F" 4 0))
39     ("percussion" . ("clefs.percussion" 0 0))
40     ("tab" . ("clefs.tab" 0 0))
41
42     ;; should move mensural stuff to separate file?
43     ("vaticana-do1" . ("clefs.vaticana.do" -1 0))
44     ("vaticana-do2" . ("clefs.vaticana.do" 1 0))
45     ("vaticana-do3" . ("clefs.vaticana.do" 3 0))
46     ("vaticana-fa1" . ("clefs.vaticana.fa" -1 0))
47
48     ("vaticana-fa2" . ("clefs.vaticana.fa" 1 0))
49     ("medicaea-do1" . ("clefs.medicaea.do" -1 0))
50     ("medicaea-do2" . ("clefs.medicaea.do" 1 0))
51     ("medicaea-do3" . ("clefs.medicaea.do" 3 0))
52     ("medicaea-fa1" . ("clefs.medicaea.fa" -1 0))
53     ("medicaea-fa2" . ("clefs.medicaea.fa" 1 0))
54     ("hufnagel-do1" . ("clefs.hufnagel.do" -1 0))
55     ("hufnagel-do2" . ("clefs.hufnagel.do" 1 0))
56     ("hufnagel-do3" . ("clefs.hufnagel.do" 3 0))
57     ("hufnagel-fa1" . ("clefs.hufnagel.fa" -1 0))
58     ("hufnagel-fa2" . ("clefs.hufnagel.fa" 1 0))
59     ("hufnagel-do-fa" . ("clefs.hufnagel.do.fa" 4 0))
60     ("mensural-c1" . ("clefs.mensural.c" -4 0))
61     ("mensural-c2" . ("clefs.mensural.c" -2 0))
62     ("mensural-c3" . ("clefs.mensural.c" 0 0))
63     ("mensural-c4" . ("clefs.mensural.c" 2 0))
64     ("mensural-c5" . ("clefs.mensural.c" 4 0))
65     ("blackmensural-c1" . ("clefs.blackmensural.c" -4 0))
66     ("blackmensural-c2" . ("clefs.blackmensural.c" -2 0))
67     ("blackmensural-c3" . ("clefs.blackmensural.c" 0 0))
68     ("blackmensural-c4" . ("clefs.blackmensural.c" 2 0))
69     ("blackmensural-c5" . ("clefs.blackmensural.c" 4 0))
70     ("mensural-f" . ("clefs.mensural.f" 2 0))
71     ("mensural-g" . ("clefs.mensural.g" -2 0))
72     ("neomensural-c1" . ("clefs.neomensural.c" -4 0))
73     ("neomensural-c2" . ("clefs.neomensural.c" -2 0))
74     ("neomensural-c3" . ("clefs.neomensural.c" 0 0))
75     ("neomensural-c4" . ("clefs.neomensural.c" 2 0))
76     ("neomensural-c5" . ("clefs.neomensural.c" 4 0))
77     ("petrucci-c1" . ("clefs.petrucci.c1" -4 0))
78     ("petrucci-c2" . ("clefs.petrucci.c2" -2 0))
79     ("petrucci-c3" . ("clefs.petrucci.c3" 0 0))
80     ("petrucci-c4" . ("clefs.petrucci.c4" 2 0))
81     ("petrucci-c5" . ("clefs.petrucci.c5" 4 0))
82     ("petrucci-f3" . ("clefs.petrucci.f" 0 0))
83     ("petrucci-f4" . ("clefs.petrucci.f" 2 0))
84     ("petrucci-f5" . ("clefs.petrucci.f" 4 0))
85     ("petrucci-f" . ("clefs.petrucci.f" 2 0))
86     ("petrucci-g" . ("clefs.petrucci.g" -2 0))
87     ("kievan-do" . ("clefs.kievan.do" 0 0))))
88
89 ;; "an alist mapping GLYPHNAME to the position of the middle C for
90 ;; that symbol"
91 (define c0-pitch-alist
92   '(("clefs.G" . -4)
93     ("clefs.C" . 0)
94     ("clefs.F" . 4)
95     ("clefs.percussion" . 0)
96     ("clefs.tab" . 0 )
97     ("clefs.vaticana.do" . 0)
98     ("clefs.vaticana.fa" . 4)
99     ("clefs.medicaea.do" . 0)
100     ("clefs.medicaea.fa" . 4)
101     ("clefs.hufnagel.do" . 0)
102     ("clefs.hufnagel.fa" . 4)
103     ("clefs.hufnagel.do.fa" . 0)
104     ("clefs.mensural.c" . 0)
105     ("clefs.mensural.f" . 4)
106     ("clefs.mensural.g" . -4)
107     ("clefs.blackmensural.c" . 0)
108     ("clefs.neomensural.c" . 0)
109     ("clefs.petrucci.c1" . 0)
110     ("clefs.petrucci.c2" . 0)
111     ("clefs.petrucci.c3" . 0)
112     ("clefs.petrucci.c4" . 0)
113     ("clefs.petrucci.c5" . 0)
114     ("clefs.petrucci.f" . 4)
115     ("clefs.petrucci.g" . -4)
116     ("clefs.kievan.do" . 0)))
117
118 (define-public (make-clef-set clef-name)
119   "Generate the clef setting commands for a clef with name @var{clef-name}."
120   (define (make-prop-set props)
121     (let ((m (make-music 'PropertySet)))
122       (for-each (lambda (x) (set! (ly:music-property m (car x)) (cdr x))) props)
123       m))
124   (let ((e '())
125         (c0 0)
126         (oct 0)
127         (style 'default)
128         (match (string-match "^(.*)([_^])([^0-9a-zA-Z]*)([1-9][0-9]*)([^0-9a-zA-Z]*)$" clef-name)))
129     (if match
130         (begin
131           (set! clef-name (match:substring match 1))
132           (set! oct
133                 (* (if (equal? (match:substring match 2) "^") -1 1)
134                    (- (string->number (match:substring match 4)) 1)))
135           (set! style
136                 (cond ((equal? (match:substring match 3) "(") 'parenthesized)
137                       ((equal? (match:substring match 3) "[") 'bracketed)
138                       (else style)))))
139     (set! e (assoc-get clef-name supported-clefs))
140     (if e
141         (let* ((prop-list `(((symbol . clefGlyph) (value . ,(car e)))
142                             ((symbol . middleCClefPosition)
143                              (value . ,(+ oct
144                                           (cadr e)
145                                           (assoc-get (car e) c0-pitch-alist))))
146                             ((symbol . clefPosition) (value . ,(cadr e)))
147                             ((symbol . clefTransposition) (value . ,(- oct)))))
148                ;; the clefTranspositionStyle property is set only when
149                ;; not 'default to calm display-lily-tests.scm
150                (prop-list (if (eq? style 'default)
151                               prop-list
152                               (append
153                                prop-list
154                                `(((symbol . clefTranspositionStyle)
155                                   (value . ,style))))))
156                (musics (map make-prop-set prop-list))
157                (recalc-mid-C (make-music 'ApplyContext))
158                (seq (make-music 'SequentialMusic
159                                 'elements (append musics (list recalc-mid-C))))
160                (csp (make-music 'ContextSpeccedMusic)))
161           (set! (ly:music-property recalc-mid-C 'procedure) ly:set-middle-C!)
162           (context-spec-music seq 'Staff))
163         (begin
164           (ly:warning (_ "unknown clef type `~a'") clef-name)
165           (ly:warning (_ "supported clefs: ~a")
166                       (string-join
167                        (sort (map car supported-clefs) string<?)))
168           (make-music 'Music)))))
169
170 (define-public (make-cue-clef-set clef-name)
171   "Generate the clef setting commands for a cue clef with name
172 @var{clef-name}."
173   (define cue-clef-map
174     '((clefGlyph . cueClefGlyph)
175       (middleCClefPosition . middleCCuePosition)
176       (clefPosition . cueClefPosition)
177       (clefTransposition . cueClefTransposition)
178       (clefTranspositionStyle . cueClefTranspositionStyle)))
179   (let ((clef (make-clef-set clef-name)))
180     (for-each
181      (lambda (m)
182        (let ((mapped (assq-ref cue-clef-map
183                                (ly:music-property m 'symbol))))
184          (if mapped
185              (set! (ly:music-property m 'symbol) mapped))))
186      (extract-named-music clef 'PropertySet))
187     clef))
188
189 (define-public (make-cue-clef-unset)
190   "Reset the clef settings for a cue clef."
191   (map-some-music
192    (lambda (m)
193      (and (eq? (ly:music-property m 'name) 'PropertySet)
194           (make-music 'PropertyUnset
195                       'symbol (ly:music-property m 'symbol))))
196    (make-cue-clef-set "treble_(8)")))
197
198 ;; a function to add new clefs at runtime
199 (define-public (add-new-clef clef-name clef-glyph clef-position transposition c0-position)
200   "Append the entries for a clef symbol to supported clefs and
201 @code{c0-pitch-alist}."
202   (set! supported-clefs
203         (acons clef-name (list clef-glyph clef-position transposition) supported-clefs))
204   (set! c0-pitch-alist
205         (acons clef-glyph c0-position c0-pitch-alist)))