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