]> git.donarmstrong.com Git - lilypond.git/blob - scm/parser-clef.scm
Merge remote-tracking branch 'origin/translation' into staging
[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     ("GG" . ("clefs.GG" -2 0))
29     ("tenorG" . ("clefs.tenorG" -2 0))
30     ("french" . ("clefs.G" -4 0))
31     ("soprano" . ("clefs.C" -4 0))
32     ("mezzosoprano" . ("clefs.C" -2 0))
33     ("alto" . ("clefs.C" 0 0))
34     ("C" . ("clefs.C" 0 0))
35     ("varC" . ("clefs.varC" 0 0))
36     ("altovarC" . ("clefs.varC" 0 0))
37     ("tenor" . ("clefs.C" 2 0))
38     ("tenorvarC" . ("clefs.varC" 2 0))
39     ("baritone" . ("clefs.C" 4 0))
40     ("baritonevarC" . ("clefs.varC" 4 0))
41     ("varbaritone" . ("clefs.F" 0 0))
42     ("baritonevarF" . ("clefs.F" 0 0))
43     ("bass" . ("clefs.F" 2 0))
44     ("F" . ("clefs.F" 2 0))
45     ("subbass" . ("clefs.F" 4 0))
46     ("percussion" . ("clefs.percussion" 0 0))
47     ("varpercussion" . ("clefs.varpercussion" 0 0))
48     ("tab" . ("clefs.tab" 0 0))
49
50     ;; should move mensural stuff to separate file?
51     ("vaticana-do1" . ("clefs.vaticana.do" -1 0))
52     ("vaticana-do2" . ("clefs.vaticana.do" 1 0))
53     ("vaticana-do3" . ("clefs.vaticana.do" 3 0))
54     ("vaticana-fa1" . ("clefs.vaticana.fa" -1 0))
55
56     ("vaticana-fa2" . ("clefs.vaticana.fa" 1 0))
57     ("medicaea-do1" . ("clefs.medicaea.do" -1 0))
58     ("medicaea-do2" . ("clefs.medicaea.do" 1 0))
59     ("medicaea-do3" . ("clefs.medicaea.do" 3 0))
60     ("medicaea-fa1" . ("clefs.medicaea.fa" -1 0))
61     ("medicaea-fa2" . ("clefs.medicaea.fa" 1 0))
62     ("hufnagel-do1" . ("clefs.hufnagel.do" -1 0))
63     ("hufnagel-do2" . ("clefs.hufnagel.do" 1 0))
64     ("hufnagel-do3" . ("clefs.hufnagel.do" 3 0))
65     ("hufnagel-fa1" . ("clefs.hufnagel.fa" -1 0))
66     ("hufnagel-fa2" . ("clefs.hufnagel.fa" 1 0))
67     ("hufnagel-do-fa" . ("clefs.hufnagel.do.fa" 4 0))
68     ("mensural-c1" . ("clefs.mensural.c" -4 0))
69     ("mensural-c2" . ("clefs.mensural.c" -2 0))
70     ("mensural-c3" . ("clefs.mensural.c" 0 0))
71     ("mensural-c4" . ("clefs.mensural.c" 2 0))
72     ("mensural-c5" . ("clefs.mensural.c" 4 0))
73     ("blackmensural-c1" . ("clefs.blackmensural.c" -4 0))
74     ("blackmensural-c2" . ("clefs.blackmensural.c" -2 0))
75     ("blackmensural-c3" . ("clefs.blackmensural.c" 0 0))
76     ("blackmensural-c4" . ("clefs.blackmensural.c" 2 0))
77     ("blackmensural-c5" . ("clefs.blackmensural.c" 4 0))
78     ("mensural-f" . ("clefs.mensural.f" 2 0))
79     ("mensural-g" . ("clefs.mensural.g" -2 0))
80     ("neomensural-c1" . ("clefs.neomensural.c" -4 0))
81     ("neomensural-c2" . ("clefs.neomensural.c" -2 0))
82     ("neomensural-c3" . ("clefs.neomensural.c" 0 0))
83     ("neomensural-c4" . ("clefs.neomensural.c" 2 0))
84     ("neomensural-c5" . ("clefs.neomensural.c" 4 0))
85     ("petrucci-c1" . ("clefs.petrucci.c1" -4 0))
86     ("petrucci-c2" . ("clefs.petrucci.c2" -2 0))
87     ("petrucci-c3" . ("clefs.petrucci.c3" 0 0))
88     ("petrucci-c4" . ("clefs.petrucci.c4" 2 0))
89     ("petrucci-c5" . ("clefs.petrucci.c5" 4 0))
90     ("petrucci-f3" . ("clefs.petrucci.f" 0 0))
91     ("petrucci-f4" . ("clefs.petrucci.f" 2 0))
92     ("petrucci-f5" . ("clefs.petrucci.f" 4 0))
93     ("petrucci-f" . ("clefs.petrucci.f" 2 0))
94     ("petrucci-g" . ("clefs.petrucci.g" -2 0))
95     ("kievan-do" . ("clefs.kievan.do" 0 0))))
96
97 ;; "an alist mapping GLYPHNAME to the position of the middle C for
98 ;; that symbol"
99 (define c0-pitch-alist
100   '(("clefs.G" . -4)
101     ("clefs.GG" . 3)
102     ("clefs.tenorG" . 3)
103     ("clefs.C" . 0)
104     ("clefs.varC" . 0)
105     ("clefs.F" . 4)
106     ("clefs.percussion" . 0)
107     ("clefs.varpercussion" . 0)
108     ("clefs.tab" . 0 )
109     ("clefs.vaticana.do" . 0)
110     ("clefs.vaticana.fa" . 4)
111     ("clefs.medicaea.do" . 0)
112     ("clefs.medicaea.fa" . 4)
113     ("clefs.hufnagel.do" . 0)
114     ("clefs.hufnagel.fa" . 4)
115     ("clefs.hufnagel.do.fa" . 0)
116     ("clefs.mensural.c" . 0)
117     ("clefs.mensural.f" . 4)
118     ("clefs.mensural.g" . -4)
119     ("clefs.blackmensural.c" . 0)
120     ("clefs.neomensural.c" . 0)
121     ("clefs.petrucci.c1" . 0)
122     ("clefs.petrucci.c2" . 0)
123     ("clefs.petrucci.c3" . 0)
124     ("clefs.petrucci.c4" . 0)
125     ("clefs.petrucci.c5" . 0)
126     ("clefs.petrucci.f" . 4)
127     ("clefs.petrucci.g" . -4)
128     ("clefs.kievan.do" . 0)))
129
130 (define-public (make-clef-set clef-name)
131   "Generate the clef setting commands for a clef with name @var{clef-name}."
132   (define (make-prop-set props)
133     (let ((m (make-music 'PropertySet)))
134       (for-each (lambda (x) (set! (ly:music-property m (car x)) (cdr x))) props)
135       m))
136   (let ((e '())
137         (c0 0)
138         (oct 0)
139         (style 'default)
140         (match (string-match "^(.*)([_^])([^0-9a-zA-Z]*)([1-9][0-9]*)([^0-9a-zA-Z]*)$" clef-name)))
141     (if match
142         (begin
143           (set! clef-name (match:substring match 1))
144           (set! oct
145                 (* (if (equal? (match:substring match 2) "^") -1 1)
146                    (- (string->number (match:substring match 4)) 1)))
147           (set! style
148                 (cond ((equal? (match:substring match 3) "(") 'parenthesized)
149                       ((equal? (match:substring match 3) "[") 'bracketed)
150                       (else style)))))
151     (set! e (assoc-get clef-name supported-clefs))
152     (if e
153         (let* ((prop-list `(((symbol . clefGlyph) (value . ,(car e)))
154                             ((symbol . middleCClefPosition)
155                              (value . ,(+ oct
156                                           (cadr e)
157                                           (assoc-get (car e) c0-pitch-alist))))
158                             ((symbol . clefPosition) (value . ,(cadr e)))
159                             ((symbol . clefTransposition) (value . ,(- oct)))))
160                ;; the clefTranspositionStyle property is set only when
161                ;; not 'default to calm display-lily-tests.scm
162                (prop-list (if (eq? style 'default)
163                               prop-list
164                               (append
165                                prop-list
166                                `(((symbol . clefTranspositionStyle)
167                                   (value . ,style))))))
168                (musics (map make-prop-set prop-list))
169                (recalc-mid-C (make-music 'ApplyContext))
170                (seq (make-music 'SequentialMusic
171                                 'elements (append musics (list recalc-mid-C))))
172                (csp (make-music 'ContextSpeccedMusic)))
173           (set! (ly:music-property recalc-mid-C 'procedure) ly:set-middle-C!)
174           (context-spec-music seq 'Staff))
175         (begin
176           (ly:warning (_ "unknown clef type `~a'") clef-name)
177           (ly:warning (_ "supported clefs: ~a")
178                       (string-join
179                        (sort (map car supported-clefs) string<?)))
180           (make-music 'Music)))))
181
182 (define-public (make-cue-clef-set clef-name)
183   "Generate the clef setting commands for a cue clef with name
184 @var{clef-name}."
185   (define cue-clef-map
186     '((clefGlyph . cueClefGlyph)
187       (middleCClefPosition . middleCCuePosition)
188       (clefPosition . cueClefPosition)
189       (clefTransposition . cueClefTransposition)
190       (clefTranspositionStyle . cueClefTranspositionStyle)))
191   (let ((clef (make-clef-set clef-name)))
192     (for-each
193      (lambda (m)
194        (let ((mapped (assq-ref cue-clef-map
195                                (ly:music-property m 'symbol))))
196          (if mapped
197              (set! (ly:music-property m 'symbol) mapped))))
198      (extract-named-music clef 'PropertySet))
199     clef))
200
201 (define-public (make-cue-clef-unset)
202   "Reset the clef settings for a cue clef."
203   (map-some-music
204    (lambda (m)
205      (and (eq? (ly:music-property m 'name) 'PropertySet)
206           (make-music 'PropertyUnset
207                       'symbol (ly:music-property m 'symbol))))
208    (make-cue-clef-set "treble_(8)")))
209
210 ;; a function to add new clefs at runtime
211 (define-public (add-new-clef clef-name clef-glyph clef-position transposition c0-position)
212   "Append the entries for a clef symbol to supported clefs and
213 @code{c0-pitch-alist}."
214   (set! supported-clefs
215         (acons clef-name (list clef-glyph clef-position transposition) supported-clefs))
216   (set! c0-pitch-alist
217         (acons clef-glyph c0-position c0-pitch-alist)))