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