]> git.donarmstrong.com Git - lilypond.git/blob - lily/gregorian-ligature-engraver.cc
*** empty log message ***
[lilypond.git] / lily / gregorian-ligature-engraver.cc
1 /*
2   gregorian-ligature-engraver.cc -- implement Gregorian_ligature_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2003--2005 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "gregorian-ligature-engraver.hh"
10
11 #include "gregorian-ligature.hh"
12 #include "warn.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "spanner.hh"
15 #include "paper-column.hh"
16
17 /*
18  * This abstract class is the common superclass for all ligature
19  * engravers for Gregorian chant notation.  It cares for the musical
20  * handling of the neumes, such as checking for valid combinations of
21  * neumes and providing context information.  Notational aspects such
22  * as the glyphs to use or calculating the total width of a ligature,
23  * are left to the concrete subclass.  Currently, there is only a
24  * single subclass, Vaticana_ligature_engraver.  Other ligature
25  * engravers for Gregorian chant will be added in the future, such as
26  * Medicaea_ligature_engraver or Hufnagel_ligature_engraver.
27  */
28 Gregorian_ligature_engraver::Gregorian_ligature_engraver ()
29 {
30   pes_or_flexa_req_ = 0;
31 }
32
33 bool
34 Gregorian_ligature_engraver::try_music (Music *m)
35 {
36   if (m->is_mus_type ("pes-or-flexa-event"))
37     {
38       pes_or_flexa_req_ = m;
39       return true;
40     }
41   else
42     return Ligature_engraver::try_music (m);
43 }
44
45 void fix_prefix (char *name, int mask,
46                  int *current_set, int min_set, int max_set,
47                  Grob *primitive)
48 {
49   bool current = *current_set & mask;
50   bool min = min_set & mask;
51   bool max = max_set & mask;
52   if (max < min)
53     {
54       programming_error ("min_set > max_set");
55       return;
56     }
57   if (min && !current)
58     {
59       primitive->warning (_f ("\\%s ignored", name));
60       *current_set &= ~mask;
61     }
62   if (!max && current)
63     {
64       primitive->warning (_f ("implied \\%s added", name));
65       *current_set |= mask;
66     }
67 }
68
69 void fix_prefix_set (int *current_set, int min_set, int max_set, Grob *primitive)
70 {
71   fix_prefix ("virga", VIRGA, current_set, min_set, max_set, primitive);
72   fix_prefix ("stropha", STROPHA, current_set, min_set, max_set, primitive);
73   fix_prefix ("inclinatum", INCLINATUM, current_set, min_set, max_set, primitive);
74   fix_prefix ("auctum", AUCTUM, current_set, min_set, max_set, primitive);
75   fix_prefix ("descendens", DESCENDENS, current_set, min_set, max_set, primitive);
76   fix_prefix ("ascendens", ASCENDENS, current_set, min_set, max_set, primitive);
77   fix_prefix ("oriscus", ORISCUS, current_set, min_set, max_set, primitive);
78   fix_prefix ("quilisma", QUILISMA, current_set, min_set, max_set, primitive);
79   fix_prefix ("deminutum", DEMINUTUM, current_set, min_set, max_set, primitive);
80   fix_prefix ("cavum", CAVUM, current_set, min_set, max_set, primitive);
81   fix_prefix ("linea", LINEA, current_set, min_set, max_set, primitive);
82   fix_prefix ("pes_or_flexa", LINEA, current_set, min_set, max_set, primitive);
83 }
84
85 void check_and_fix_all_prefixes (Array<Grob_info> primitives)
86 {
87   /* Check for illegal head modifier combinations */
88   for (int i = 0; i < primitives.size (); i++)
89     {
90       Grob *primitive = primitives[i].grob_;
91
92       /* compute head prefix set by inspecting primitive grob properties */
93       int prefix_set
94         = (VIRGA *to_boolean (primitive->get_property ("virga")))
95         | (STROPHA *to_boolean (primitive->get_property ("stropha")))
96         | (INCLINATUM *to_boolean (primitive->get_property ("inclinatum")))
97         | (AUCTUM *to_boolean (primitive->get_property ("auctum")))
98         | (DESCENDENS *to_boolean (primitive->get_property ("descendens")))
99         | (ASCENDENS *to_boolean (primitive->get_property ("ascendens")))
100         | (ORISCUS *to_boolean (primitive->get_property ("oriscus")))
101         | (QUILISMA *to_boolean (primitive->get_property ("quilisma")))
102         | (DEMINUTUM *to_boolean (primitive->get_property ("deminutum")))
103         | (CAVUM *to_boolean (primitive->get_property ("cavum")))
104         | (LINEA *to_boolean (primitive->get_property ("linea")))
105         | (PES_OR_FLEXA *to_boolean (primitive->get_property ("pes-or-flexa")));
106
107       /* check: ascendens and descendens exclude each other; same with
108          auctum and deminutum */
109       if (prefix_set & DESCENDENS)
110         {
111           fix_prefix_set (&prefix_set,
112                           prefix_set & ~ASCENDENS,
113                           prefix_set & ~ASCENDENS,
114                           primitive);
115         }
116       if (prefix_set & AUCTUM)
117         {
118           fix_prefix_set (&prefix_set,
119                           prefix_set & ~DEMINUTUM,
120                           prefix_set & ~DEMINUTUM,
121                           primitive);
122         }
123
124       /* check: virga, quilisma and oriscus cannot be combined with any
125          other prefix, but may be part of a pes or flexa */
126       if (prefix_set & VIRGA)
127         {
128           fix_prefix_set (&prefix_set,
129                           VIRGA,
130                           VIRGA | PES_OR_FLEXA,
131                           primitive);
132         }
133       if (prefix_set & QUILISMA)
134         {
135           fix_prefix_set (&prefix_set,
136                           QUILISMA,
137                           QUILISMA | PES_OR_FLEXA,
138                           primitive);
139         }
140       if (prefix_set & ORISCUS)
141         {
142           fix_prefix_set (&prefix_set,
143                           ORISCUS,
144                           ORISCUS | PES_OR_FLEXA,
145                           primitive);
146         }
147
148       /* check: auctum is the only valid optional prefix for stropha */
149       if (prefix_set & STROPHA)
150         {
151           fix_prefix_set (&prefix_set,
152                           STROPHA,
153                           STROPHA | AUCTUM,
154                           primitive);
155         }
156
157       /* check: inclinatum may be prefixed with auctum or deminutum only */
158       if (prefix_set & INCLINATUM)
159         {
160           fix_prefix_set (&prefix_set,
161                           INCLINATUM,
162                           INCLINATUM | AUCTUM | DEMINUTUM,
163                           primitive);
164         }
165       /* check: semivocalis (deminutum but not inclinatum) must occur in
166          combination with and only with pes or flexa */
167       else if (prefix_set & DEMINUTUM)
168         {
169           fix_prefix_set (&prefix_set,
170                           DEMINUTUM | PES_OR_FLEXA,
171                           DEMINUTUM | PES_OR_FLEXA,
172                           primitive);
173         }
174
175       /* check: cavum and linea (either or both) may be applied only
176          upon core punctum */
177       if (prefix_set & (CAVUM | LINEA))
178         {
179           fix_prefix_set (&prefix_set,
180                           0,
181                           CAVUM | LINEA,
182                           primitive);
183         }
184
185       /* all other combinations should be valid (unless I made a
186          mistake) */
187
188       primitive->set_property ("prefix-set", scm_int2num (prefix_set));
189     }
190 }
191
192 /*
193  * Marks those heads that participate in a pes or flexa.
194  */
195 void
196 provide_context_info (Array<Grob_info> primitives)
197 {
198   Grob *prev_primitive = 0;
199   int prev_prefix_set = 0;
200   int prev_context_info = 0;
201   int prev_pitch = 0;
202   for (int i = 0; i < primitives.size (); i++)
203     {
204       Grob *primitive = primitives[i].grob_;
205       Music *music_cause = primitives[i].music_cause ();
206       int context_info = 0;
207       int pitch = unsmob_pitch (music_cause->get_property ("pitch"))->steps ();
208       int prefix_set = scm_to_int (primitive->get_property ("prefix-set"));
209
210       if (prefix_set & PES_OR_FLEXA)
211         if (!i) // ligature may not start with 2nd head of pes or flexa
212           {
213             primitive->warning (_ ( "Cannot apply `\\~' on first head of ligature; ignoring `\\~'"));
214           }
215         else if (pitch > prev_pitch) // pes
216           {
217             prev_context_info |= PES_LOWER;
218             context_info |= PES_UPPER;
219           }
220         else if (pitch < prev_pitch) // flexa
221           {
222             prev_context_info |= FLEXA_LEFT;
223             context_info |= FLEXA_RIGHT;
224           }
225         else // (pitch == prev_pitch)
226           {
227             primitive->warning (_ ("can't apply `\\~' on heads with identical pitch; ignoring `\\~'"));
228           }
229       if (prev_prefix_set & DEMINUTUM)
230         {
231           context_info |= AFTER_DEMINUTUM;
232         }
233
234       if (prev_primitive)
235         prev_primitive->set_property ("context-info",
236                                       scm_int2num (prev_context_info));
237       prev_primitive = primitive;
238       prev_prefix_set = prefix_set;
239       prev_context_info = context_info;
240       prev_pitch = pitch;
241     }
242   if (prev_primitive)
243     prev_primitive->set_property ("context-info",
244                                   scm_int2num (prev_context_info));
245 }
246
247 void
248 Gregorian_ligature_engraver::transform_heads (Spanner *, Array<Grob_info>)
249 {
250   programming_error ("Gregorian_ligature_engraver::transform_heads (): "
251                      "this is an abstract method that should not be called, "
252                      "but overridden by a subclass");
253 }
254
255 void
256 Gregorian_ligature_engraver::build_ligature (Spanner *ligature,
257                                              Array<Grob_info> primitives)
258 {
259   // apply style-independent checking and transformation
260   check_and_fix_all_prefixes (primitives);
261   provide_context_info (primitives);
262
263   // apply style-specific transformation (including line-up); to be
264   // implemented by subclass
265   transform_heads (ligature, primitives);
266 }
267
268 void
269 Gregorian_ligature_engraver::stop_translation_timestep ()
270 {
271   Ligature_engraver::stop_translation_timestep ();
272   pes_or_flexa_req_ = 0;
273 }
274
275 ADD_TRANSLATOR (Gregorian_ligature_engraver,
276                 /* descr */ "This is an abstract class.  Subclasses such as Vaticana_ligature_engraver handle ligatures by glueing special ligature heads together.",
277                 /* creats*/ "",
278                 /* accepts */ "ligature-event",
279                 /* acks  */ "note-head-interface rest-interface",
280                 /* reads */ "",
281                 /* write */ "");