2 vaticana-ligature-engraver.cc -- implement Vaticana_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2003--2006 Juergen Reuter <reuter@ipd.uka.de>
9 #include "gregorian-ligature-engraver.hh"
11 #include "font-interface.hh"
12 #include "gregorian-ligature.hh"
13 #include "international.hh"
14 #include "output-def.hh"
15 #include "paper-column.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "vaticana-ligature.hh"
21 #include "translator.icc"
24 * This class implements the notation specific aspects of Vaticana
25 * style ligatures for Gregorian chant notation.
28 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
33 need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
34 int context_info, int delta_pitch);
35 bool is_stacked_head (int prefix_set,
37 Real align_heads (vector<Grob_info> primitives,
42 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
45 virtual Spanner *create_ligature_spanner ();
46 virtual void transform_heads (Spanner *ligature,
47 vector<Grob_info> primitives);
50 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
52 brew_ligature_primitive_proc =
53 Vaticana_ligature::brew_ligature_primitive_proc;
57 Vaticana_ligature_engraver::create_ligature_spanner ()
59 return make_spanner ("VaticanaLigature", SCM_EOL);
63 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
68 // upper head of pes is stacked upon lower head of pes ...
69 is_stacked_b = context_info & PES_UPPER;
71 // ... unless this note starts a flexa
72 if (context_info & FLEXA_LEFT)
76 if (context_info & PES_LOWER)
79 // ... or the previous note is a semivocalis or inclinatum
80 if (context_info & AFTER_DEMINUTUM)
83 // auctum head is never stacked upon preceding note
84 if (prefix_set & AUCTUM)
87 // virga is never stacked upon preceding note
88 if (prefix_set & VIRGA)
91 // oriscus is never stacked upon preceding note
92 if (prefix_set & ORISCUS)
95 if ((prefix_set & DEMINUTUM)
96 && ! (prefix_set & INCLINATUM)
97 && (context_info & FLEXA_RIGHT))
98 is_stacked_b = true; // semivocalis head of deminutus form
104 * When aligning the heads, sometimes extra space is needed, e.g. to
105 * avoid clashing with the appendix of an adjacent notehead or with an
106 * adjacent notehead itself if it has the same pitch. Extra space is
107 * added at most once between to heads.
110 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
111 int context_info, int delta_pitch)
113 if (prev_prefix_set & VIRGA)
115 * After a virga, make a an additional small space such that the
116 * appendix on the right side of the head does not touch the
121 if ((prefix_set & INCLINATUM)
122 && ! (prev_prefix_set & INCLINATUM))
124 * Always start a series of inclinatum heads with an extra space.
128 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
130 * Before a flexa (but not within a torculus), make a an
131 * additional small space such that the appendix on the left side
132 * of the flexa does not touch the this head.
136 if (delta_pitch == 0)
138 * If there are two adjacent noteheads with the same pitch, add
139 * additional small space between them, such that they do not
148 Vaticana_ligature_engraver::align_heads (vector<Grob_info> primitives,
152 if (!primitives.size ())
154 programming_error ("Vaticana_ligature: "
155 "empty ligature [ignored]");
160 * The paper column where we put the whole ligature into.
163 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
166 = thickness * column->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
169 * Amount of extra space two put between some particular
170 * configurations of adjacent heads.
172 * TODO: make this a property of primtive grobs.
174 Real extra_space = 4.0 * join_thickness;
177 * Keep track of the total width of the ligature.
179 Real ligature_width = 0.0;
181 Item *prev_primitive = 0;
182 int prev_prefix_set = 0;
183 for (vsize i = 0; i < primitives.size (); i++)
185 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
187 = scm_to_int (primitive->get_property ("prefix-set"));
189 = scm_to_int (primitive->get_property ("context-info"));
192 * Get glyph_name, delta_pitch and context_info for this head.
195 SCM glyph_name_scm = primitive->get_property ("glyph-name");
196 if (glyph_name_scm == SCM_EOL)
198 primitive->programming_error ("Vaticana_ligature:"
199 "undefined glyph-name -> "
203 string glyph_name = ly_scm2string (glyph_name_scm);
206 if (prev_primitive) /* urgh, need prev_primitive only here */
208 SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
209 if (delta_pitch_scm != SCM_EOL)
210 delta_pitch = scm_to_int (delta_pitch_scm);
213 primitive->programming_error ("Vaticana_ligature:"
214 "delta-pitch undefined -> "
221 * Now determine width and x-offset of head.
227 if (context_info & STACKED_HEAD)
230 * This head is stacked upon the previous one; hence, it
231 * does not contribute to the total width of the ligature,
232 * and its width is assumed to be 0.0. Moreover, it is
233 * shifted to the left by its width such that the right side
234 * of this and the other head are horizontally aligned.
237 x_offset = join_thickness
238 - Font_interface::get_default_font (primitive)->
239 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
241 else if (glyph_name == "flexa" || glyph_name == "")
244 * This head represents either half of a flexa shape.
245 * Hence, it is assigned half the width of this shape.
247 head_width = 0.5 * flexa_width;
253 * This is a regular head, placed right to the previous one.
254 * Retrieve its width from corresponding font.
257 = Font_interface::get_default_font (primitive)->
258 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
263 * Save the head's final x-offset.
265 primitive->set_property ("x-offset",
266 scm_from_double (x_offset));
269 * If the head is the 2nd head of a pes or flexa (but not a
270 * flexa shape), mark this head to be joined with the left-side
271 * neighbour head (i.e. the previous head) by a vertical beam.
273 if ((context_info & PES_UPPER)
274 || ((context_info & FLEXA_RIGHT)
275 && ! (context_info & PES_LOWER)))
279 primitive->programming_error ("vaticana ligature: add-join: "
280 "missing previous primitive");
284 prev_primitive->set_property ("add-join",
288 * Create a small overlap of adjacent heads so that the join
289 * can be drawn perfectly between them.
291 ligature_width -= join_thickness;
294 else if (glyph_name == "")
297 * This is the 2nd (virtual) head of flexa shape. Join it
298 * tightly with 1st head, i.e. do *not* add additional
299 * space, such that next head will not be off from the flexa
304 if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
305 context_info, delta_pitch))
306 ligature_width += extra_space;
309 * Horizontally line-up this head to form a ligature.
311 get_set_column (primitive, column);
312 primitive->translate_axis (ligature_width, X_AXIS);
313 ligature_width += head_width;
315 prev_primitive = primitive;
316 prev_prefix_set = prefix_set;
320 * Add extra horizontal padding space after ligature, such that
321 * neighbouring ligatures do not touch each other.
323 ligature_width += extra_space;
325 return ligature_width;
329 * Depending on the typographical features of a particular ligature
330 * style, some prefixes may be ignored. In particular, if a curved
331 * flexa shape is produced, any prefixes to either of the two
332 * contributing heads that would select a head other than punctum, is
333 * by definition ignored.
335 * This function prints a warning, if the given primitive is prefixed
336 * such that a head other than punctum would be chosen, if this
337 * primitive were engraved as a stand-alone head.
340 check_for_prefix_loss (Item *primitive)
343 = scm_to_int (primitive->get_property ("prefix-set"));
344 if (prefix_set & ~PES_OR_FLEXA)
346 string prefs = Gregorian_ligature::prefixes_to_str (primitive);
347 primitive->warning (_f ("ignored prefix (es) `%s' of this head according "
348 "to restrictions of the selected ligature style",
354 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
355 vector<Grob_info> primitives)
357 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
359 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
361 Item *prev_primitive = 0;
362 int prev_prefix_set = 0;
363 int prev_context_info = 0;
364 int prev_delta_pitch = 0;
365 string prev_glyph_name = "";
366 for (vsize i = 0; i < primitives.size (); i++)
368 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
371 SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
372 if (delta_pitch_scm != SCM_EOL)
373 delta_pitch = scm_to_int (delta_pitch_scm);
376 primitive->programming_error ("Vaticana_ligature:"
377 "delta-pitch undefined -> "
382 /* retrieve & complete prefix_set and context_info */
384 = scm_to_int (primitive->get_property ("prefix-set"));
386 = scm_to_int (primitive->get_property ("context-info"));
387 if (is_stacked_head (prefix_set, context_info))
389 context_info |= STACKED_HEAD;
390 primitive->set_property ("context-info",
391 scm_from_int (context_info));
395 * Now determine which head to typeset (this is context sensitive
396 * information, since it depends on neighbouring heads; therefore,
397 * this decision must be made here in the engraver rather than in
401 if (prefix_set & VIRGA)
403 glyph_name = "vaticana.punctum";
404 primitive->set_property ("add-stem", ly_bool2scm (true));
406 else if (prefix_set & QUILISMA)
407 glyph_name = "vaticana.quilisma";
408 else if (prefix_set & ORISCUS)
409 glyph_name = "solesmes.oriscus";
410 else if (prefix_set & STROPHA)
411 if (prefix_set & AUCTUM)
412 glyph_name = "solesmes.stropha.aucta";
413 else glyph_name = "solesmes.stropha";
414 else if (prefix_set & INCLINATUM)
415 if (prefix_set & AUCTUM)
416 glyph_name = "solesmes.incl.auctum";
417 else if (prefix_set & DEMINUTUM)
418 glyph_name = "solesmes.incl.parvum";
420 glyph_name = "vaticana.inclinatum";
421 else if (prefix_set & DEMINUTUM)
425 glyph_name = "vaticana.reverse.plica";
427 else if (prev_delta_pitch > 0)
430 if (! (prev_context_info & FLEXA_RIGHT))
431 /* correct head of previous primitive */
432 if (prev_delta_pitch > 1)
433 prev_glyph_name = "vaticana.epiphonus";
435 prev_glyph_name = "vaticana.vepiphonus";
436 if (prev_delta_pitch > 1)
437 glyph_name = "vaticana.plica";
439 glyph_name = "vaticana.vplica";
441 else if (prev_delta_pitch < 0)
444 if (! (prev_context_info & FLEXA_RIGHT))
445 /* correct head of previous primitive */
449 /* cephalicus head with fixed size cauda */
450 prev_glyph_name = "vaticana.inner.cephalicus";
454 /* cephalicus head without cauda */
455 prev_glyph_name = "vaticana.cephalicus";
459 * Flexa has no variable size cauda if its left head is
460 * stacked on the right head. This is true for
461 * cephalicus. Hence, remove the cauda.
463 * Urgh: for the current implementation, this rule only
464 * applies for cephalicus; but it is a fundamental rule.
465 * Therefore, the following line of code should be
466 * placed somewhere else.
468 prev_primitive->set_property ("add-cauda",
469 ly_bool2scm (false));
471 if (prev_delta_pitch < - 1)
472 glyph_name = "vaticana.reverse.plica";
474 glyph_name = "vaticana.reverse.vplica";
476 else // (prev_delta_pitch == 0)
478 primitive->programming_error ("Vaticana_ligature:"
479 "deminutum head must have different "
480 "pitch -> ignoring grob");
482 else if (prefix_set & (CAVUM | LINEA))
483 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
484 glyph_name = "vaticana.linea.punctum.cavum";
485 else if (prefix_set & CAVUM)
486 glyph_name = "vaticana.punctum.cavum";
488 glyph_name = "vaticana.linea.punctum";
489 else if (prefix_set & AUCTUM)
490 if (prefix_set & ASCENDENS)
491 glyph_name = "solesmes.auct.asc";
493 glyph_name = "solesmes.auct.desc";
494 else if ((context_info & STACKED_HEAD)
495 && (context_info & PES_UPPER))
496 if (prev_delta_pitch > 1)
497 glyph_name = "vaticana.upes";
499 glyph_name = "vaticana.vupes";
501 glyph_name = "vaticana.punctum";
504 * This head needs a cauda, if it starts a flexa, is not the upper
505 * head of a pes, and if it is a punctum.
507 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
508 if (glyph_name == "vaticana.punctum")
509 primitive->set_property ("add-cauda", ly_bool2scm (true));
512 * Execptional rule for porrectus:
514 * If the current head is preceded by a \flexa and succeded by a
515 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
516 * the previous head into a single curved flexa shape.
518 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
520 check_for_prefix_loss (prev_primitive);
521 prev_glyph_name = "flexa";
522 prev_primitive->set_property ("flexa-height",
523 scm_from_int (prev_delta_pitch));
524 prev_primitive->set_property ("flexa-width",
525 scm_from_double (flexa_width));
526 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
527 prev_primitive->set_property ("add-cauda",
528 ly_bool2scm (add_cauda));
529 check_for_prefix_loss (primitive);
531 primitive->set_property ("flexa-width",
532 scm_from_double (flexa_width));
536 * Exceptional rule for pes:
538 * If this head is stacked on the previous one due to a \pes, then
539 * set the glyph of the previous head to that for this special
540 * case, thereby avoiding potential vertical collision with the
543 if (prefix_set & PES_OR_FLEXA)
545 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
547 if (prev_glyph_name == "vaticana.punctum")
548 if (prev_delta_pitch > 1)
549 prev_glyph_name = "vaticana.lpes";
551 prev_glyph_name = "vaticana.vlpes";
556 prev_primitive->set_property ("glyph-name",
557 scm_makfrom0str (prev_glyph_name.c_str ()));
560 * In the backend, flexa shapes and joins need to know about line
561 * thickness. Hence, for simplicity, let's distribute the
562 * ligature grob's value for thickness to each ligature head (even
563 * if not all of them need to know).
565 primitive->set_property ("thickness", scm_from_double (thickness));
567 prev_primitive = primitive;
568 prev_prefix_set = prefix_set;
569 prev_context_info = context_info;
570 prev_delta_pitch = delta_pitch;
571 prev_glyph_name = glyph_name;
574 prev_primitive->set_property ("glyph-name",
575 scm_makfrom0str (prev_glyph_name.c_str ()));
577 align_heads (primitives, flexa_width, thickness);
579 #if 0 // experimental code to collapse spacing after ligature
580 /* TODO: set to max (old/new spacing-increment), since other
581 voices/staves also may want to set this property. */
582 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
583 Paper_column *paper_column = first_primitive->get_column ();
584 paper_column->warning (_f ("Vaticana_ligature_engraver: "
585 "setting `spacing-increment = %f': ptr =%ul",
586 ligature_width, paper_column));
588 set_property ("forced-spacing", scm_from_double (ligature_width));
592 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, rest);
593 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, note_head);
594 ADD_TRANSLATOR (Vaticana_ligature_engraver,
595 /* doc */ "Handles ligatures by glueing special ligature heads together.",
596 /* create */ "VaticanaLigature",
597 /* accept */ "ligature-event",