]> git.donarmstrong.com Git - lilypond.git/blob - lily/gregorian-ligature-engraver.cc
36cf898af86257bc4dd0c85d94ac1ba089c8b61d
[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--2007 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 /* ASSIGN_EVENT_ONCE */
21 #include "translator.icc"
22
23 /*
24  * This abstract class is the common superclass for all ligature
25  * engravers for Gregorian chant notation.  It cares for the musical
26  * handling of the neumes, such as checking for valid combinations of
27  * neumes and providing context information.  Notational aspects such
28  * as the glyphs to use or calculating the total width of a ligature,
29  * are left to the concrete subclass.  Currently, there is only a
30  * single subclass, Vaticana_ligature_engraver.  Other ligature
31  * engravers for Gregorian chant will be added in the future, such as
32  * Medicaea_ligature_engraver or Hufnagel_ligature_engraver.
33  */
34 Gregorian_ligature_engraver::Gregorian_ligature_engraver ()
35 {
36   pes_or_flexa_req_ = 0;
37 }
38
39 void
40 Gregorian_ligature_engraver::listen_pes_or_flexa (Stream_event *ev)
41 {
42   ASSIGN_EVENT_ONCE (pes_or_flexa_req_, ev);
43 }
44
45 void fix_prefix (char const *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 (vector<Grob_info> primitives)
86 {
87   /* Check for invalid head modifier combinations */
88   for (vsize 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_from_int (prefix_set));
189     }
190 }
191
192 /*
193  * Marks those heads that participate in a pes or flexa.
194  */
195 void
196 provide_context_info (vector<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 (vsize i = 0; i < primitives.size (); i++)
203     {
204       Grob *primitive = primitives[i].grob ();
205       Stream_event *event_cause = primitives[i].event_cause ();
206       int context_info = 0;
207       int pitch = unsmob_pitch (event_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         {
212           if (!i) // ligature may not start with 2nd head of pes or flexa
213             primitive->warning (_ ("cannot apply `\\~' on first head of ligature"));
214           else if (pitch > prev_pitch) // pes
215             {
216               prev_context_info |= PES_LOWER;
217               context_info |= PES_UPPER;
218             }
219           else if (pitch < prev_pitch) // flexa
220             {
221               prev_context_info |= FLEXA_LEFT;
222               context_info |= FLEXA_RIGHT;
223             }
224           else // (pitch == prev_pitch)
225             primitive->warning (_ ("cannot apply `\\~' on heads with identical pitch"));
226         }
227       if (prev_prefix_set & DEMINUTUM)
228         context_info |= AFTER_DEMINUTUM;
229
230       if (prev_primitive)
231         prev_primitive->set_property ("context-info",
232                                       scm_from_int (prev_context_info));
233       prev_primitive = primitive;
234       prev_prefix_set = prefix_set;
235       prev_context_info = context_info;
236       prev_pitch = pitch;
237     }
238   if (prev_primitive)
239     prev_primitive->set_property ("context-info",
240                                   scm_from_int (prev_context_info));
241 }
242
243 void
244 Gregorian_ligature_engraver::build_ligature (Spanner *ligature,
245                                              vector<Grob_info> primitives)
246 {
247   // apply style-independent checking and transformation
248   check_and_fix_all_prefixes (primitives);
249   provide_context_info (primitives);
250
251   // apply style-specific transformation (including line-up); to be
252   // implemented by subclass
253   transform_heads (ligature, primitives);
254 }
255
256 void
257 Gregorian_ligature_engraver::stop_translation_timestep ()
258 {
259   Ligature_engraver::stop_translation_timestep ();
260   pes_or_flexa_req_ = 0;
261 }
262
263 // no ADD_ACKNOWLEDGER / ADD_ACKNOWLEDGER / ADD_TRANSLATOR macro calls
264 // since this class is abstract