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