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 "stream-event.hh"
19 #include "vaticana-ligature.hh"
22 #include "translator.icc"
25 * This class implements the notation specific aspects of Vaticana
26 * style ligatures for Gregorian chant notation.
29 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
34 need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
35 int context_info, int delta_pitch);
36 bool is_stacked_head (int prefix_set,
38 Real align_heads (vector<Grob_info> primitives,
43 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
46 virtual Spanner *create_ligature_spanner ();
47 virtual void transform_heads (Spanner *ligature,
48 vector<Grob_info> primitives);
49 DECLARE_TRANSLATOR_LISTENER (pes_or_flexa);
50 DECLARE_TRANSLATOR_LISTENER (ligature);
53 IMPLEMENT_TRANSLATOR_LISTENER (Vaticana_ligature_engraver, pes_or_flexa);
55 Vaticana_ligature_engraver::listen_pes_or_flexa (Stream_event *ev)
57 Gregorian_ligature_engraver::listen_pes_or_flexa (ev);
60 IMPLEMENT_TRANSLATOR_LISTENER (Vaticana_ligature_engraver, ligature);
62 Vaticana_ligature_engraver::listen_ligature (Stream_event *ev)
64 Ligature_engraver::listen_ligature (ev);
67 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
69 brew_ligature_primitive_proc =
70 Vaticana_ligature::brew_ligature_primitive_proc;
74 Vaticana_ligature_engraver::create_ligature_spanner ()
76 return make_spanner ("VaticanaLigature", SCM_EOL);
80 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
85 // upper head of pes is stacked upon lower head of pes ...
86 is_stacked_b = context_info & PES_UPPER;
88 // ... unless this note starts a flexa
89 if (context_info & FLEXA_LEFT)
93 if (context_info & PES_LOWER)
96 // ... or the previous note is a semivocalis or inclinatum
97 if (context_info & AFTER_DEMINUTUM)
100 // auctum head is never stacked upon preceding note
101 if (prefix_set & AUCTUM)
102 is_stacked_b = false;
104 // virga is never stacked upon preceding note
105 if (prefix_set & VIRGA)
106 is_stacked_b = false;
108 // oriscus is never stacked upon preceding note
109 if (prefix_set & ORISCUS)
110 is_stacked_b = false;
112 if ((prefix_set & DEMINUTUM)
113 && ! (prefix_set & INCLINATUM)
114 && (context_info & FLEXA_RIGHT))
115 is_stacked_b = true; // semivocalis head of deminutus form
121 * When aligning the heads, sometimes extra space is needed, e.g. to
122 * avoid clashing with the appendix of an adjacent notehead or with an
123 * adjacent notehead itself if it has the same pitch. Extra space is
124 * added at most once between to heads.
127 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
128 int context_info, int delta_pitch)
130 if (prev_prefix_set & VIRGA)
132 * After a virga, make a an additional small space such that the
133 * appendix on the right side of the head does not touch the
138 if ((prefix_set & INCLINATUM)
139 && ! (prev_prefix_set & INCLINATUM))
141 * Always start a series of inclinatum heads with an extra space.
145 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
147 * Before a flexa (but not within a torculus), make a an
148 * additional small space such that the appendix on the left side
149 * of the flexa does not touch the this head.
153 if (delta_pitch == 0)
155 * If there are two adjacent noteheads with the same pitch, add
156 * additional small space between them, such that they do not
165 Vaticana_ligature_engraver::align_heads (vector<Grob_info> primitives,
169 if (!primitives.size ())
171 programming_error ("Vaticana_ligature: "
172 "empty ligature [ignored]");
177 * The paper column where we put the whole ligature into.
180 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
183 = thickness * column->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
186 * Amount of extra space two put between some particular
187 * configurations of adjacent heads.
189 * TODO: make this a property of primtive grobs.
191 Real extra_space = 4.0 * join_thickness;
194 * Keep track of the total width of the ligature.
196 Real ligature_width = 0.0;
198 Item *prev_primitive = 0;
199 int prev_prefix_set = 0;
200 for (vsize i = 0; i < primitives.size (); i++)
202 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
204 = scm_to_int (primitive->get_property ("prefix-set"));
206 = scm_to_int (primitive->get_property ("context-info"));
209 * Get glyph_name, delta_pitch and context_info for this head.
212 SCM glyph_name_scm = primitive->get_property ("glyph-name");
213 if (glyph_name_scm == SCM_EOL)
215 primitive->programming_error ("Vaticana_ligature:"
216 "undefined glyph-name -> "
220 string glyph_name = ly_scm2string (glyph_name_scm);
223 if (prev_primitive) /* urgh, need prev_primitive only here */
225 SCM delta_pitch_scm = prev_primitive->get_property ("delta-position");
226 if (delta_pitch_scm != SCM_EOL)
227 delta_pitch = scm_to_int (delta_pitch_scm);
230 primitive->programming_error ("Vaticana_ligature:"
231 "delta-position undefined -> "
238 * Now determine width and x-offset of head.
244 if (context_info & STACKED_HEAD)
247 * This head is stacked upon the previous one; hence, it
248 * does not contribute to the total width of the ligature,
249 * and its width is assumed to be 0.0. Moreover, it is
250 * shifted to the left by its width such that the right side
251 * of this and the other head are horizontally aligned.
254 x_offset = join_thickness
255 - Font_interface::get_default_font (primitive)->
256 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
258 else if (glyph_name == "flexa" || glyph_name == "")
261 * This head represents either half of a flexa shape.
262 * Hence, it is assigned half the width of this shape.
264 head_width = 0.5 * flexa_width;
270 * This is a regular head, placed right to the previous one.
271 * Retrieve its width from corresponding font.
274 = Font_interface::get_default_font (primitive)->
275 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
280 * Save the head's final x-offset.
282 primitive->set_property ("x-offset",
283 scm_from_double (x_offset));
286 * If the head is the 2nd head of a pes or flexa (but not a
287 * flexa shape), mark this head to be joined with the left-side
288 * neighbour head (i.e. the previous head) by a vertical beam.
290 if ((context_info & PES_UPPER)
291 || ((context_info & FLEXA_RIGHT)
292 && ! (context_info & PES_LOWER)))
296 primitive->programming_error ("vaticana ligature: add-join: "
297 "missing previous primitive");
301 prev_primitive->set_property ("add-join",
305 * Create a small overlap of adjacent heads so that the join
306 * can be drawn perfectly between them.
308 ligature_width -= join_thickness;
311 else if (glyph_name == "")
314 * This is the 2nd (virtual) head of flexa shape. Join it
315 * tightly with 1st head, i.e. do *not* add additional
316 * space, such that next head will not be off from the flexa
321 if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
322 context_info, delta_pitch))
323 ligature_width += extra_space;
326 * Horizontally line-up this head to form a ligature.
328 get_set_column (primitive, column);
329 primitive->translate_axis (ligature_width, X_AXIS);
330 ligature_width += head_width;
332 prev_primitive = primitive;
333 prev_prefix_set = prefix_set;
337 * Add extra horizontal padding space after ligature, such that
338 * neighbouring ligatures do not touch each other.
340 ligature_width += extra_space;
342 return ligature_width;
346 * Depending on the typographical features of a particular ligature
347 * style, some prefixes may be ignored. In particular, if a curved
348 * flexa shape is produced, any prefixes to either of the two
349 * contributing heads that would select a head other than punctum, is
350 * by definition ignored.
352 * This function prints a warning, if the given primitive is prefixed
353 * such that a head other than punctum would be chosen, if this
354 * primitive were engraved as a stand-alone head.
357 check_for_prefix_loss (Item *primitive)
360 = scm_to_int (primitive->get_property ("prefix-set"));
361 if (prefix_set & ~PES_OR_FLEXA)
363 string prefs = Gregorian_ligature::prefixes_to_str (primitive);
364 primitive->warning (_f ("ignored prefix (es) `%s' of this head according "
365 "to restrictions of the selected ligature style",
371 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
372 vector<Grob_info> primitives)
374 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
376 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
378 Item *prev_primitive = 0;
379 int prev_prefix_set = 0;
380 int prev_context_info = 0;
381 int prev_delta_pitch = 0;
382 string prev_glyph_name = "";
383 for (vsize i = 0; i < primitives.size (); i++)
385 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
388 SCM delta_pitch_scm = primitive->get_property ("delta-position");
389 if (delta_pitch_scm != SCM_EOL)
390 delta_pitch = scm_to_int (delta_pitch_scm);
393 primitive->programming_error ("Vaticana_ligature:"
394 "delta-position undefined -> "
399 /* retrieve & complete prefix_set and context_info */
401 = scm_to_int (primitive->get_property ("prefix-set"));
403 = scm_to_int (primitive->get_property ("context-info"));
404 if (is_stacked_head (prefix_set, context_info))
406 context_info |= STACKED_HEAD;
407 primitive->set_property ("context-info",
408 scm_from_int (context_info));
412 * Now determine which head to typeset (this is context sensitive
413 * information, since it depends on neighbouring heads; therefore,
414 * this decision must be made here in the engraver rather than in
418 if (prefix_set & VIRGA)
420 glyph_name = "vaticana.punctum";
421 primitive->set_property ("add-stem", ly_bool2scm (true));
423 else if (prefix_set & QUILISMA)
424 glyph_name = "vaticana.quilisma";
425 else if (prefix_set & ORISCUS)
426 glyph_name = "solesmes.oriscus";
427 else if (prefix_set & STROPHA)
428 if (prefix_set & AUCTUM)
429 glyph_name = "solesmes.stropha.aucta";
430 else glyph_name = "solesmes.stropha";
431 else if (prefix_set & INCLINATUM)
432 if (prefix_set & AUCTUM)
433 glyph_name = "solesmes.incl.auctum";
434 else if (prefix_set & DEMINUTUM)
435 glyph_name = "solesmes.incl.parvum";
437 glyph_name = "vaticana.inclinatum";
438 else if (prefix_set & DEMINUTUM)
442 glyph_name = "vaticana.reverse.plica";
444 else if (prev_delta_pitch > 0)
447 if (! (prev_context_info & FLEXA_RIGHT))
448 /* correct head of previous primitive */
449 if (prev_delta_pitch > 1)
450 prev_glyph_name = "vaticana.epiphonus";
452 prev_glyph_name = "vaticana.vepiphonus";
453 if (prev_delta_pitch > 1)
454 glyph_name = "vaticana.plica";
456 glyph_name = "vaticana.vplica";
458 else if (prev_delta_pitch < 0)
461 if (! (prev_context_info & FLEXA_RIGHT))
462 /* correct head of previous primitive */
466 /* cephalicus head with fixed size cauda */
467 prev_glyph_name = "vaticana.inner.cephalicus";
471 /* cephalicus head without cauda */
472 prev_glyph_name = "vaticana.cephalicus";
476 * Flexa has no variable size cauda if its left head is
477 * stacked on the right head. This is true for
478 * cephalicus. Hence, remove the cauda.
480 * Urgh: for the current implementation, this rule only
481 * applies for cephalicus; but it is a fundamental rule.
482 * Therefore, the following line of code should be
483 * placed somewhere else.
485 prev_primitive->set_property ("add-cauda",
486 ly_bool2scm (false));
488 if (prev_delta_pitch < - 1)
489 glyph_name = "vaticana.reverse.plica";
491 glyph_name = "vaticana.reverse.vplica";
493 else // (prev_delta_pitch == 0)
495 primitive->programming_error ("Vaticana_ligature:"
496 "deminutum head must have different "
497 "pitch -> ignoring grob");
499 else if (prefix_set & (CAVUM | LINEA))
500 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
501 glyph_name = "vaticana.linea.punctum.cavum";
502 else if (prefix_set & CAVUM)
503 glyph_name = "vaticana.punctum.cavum";
505 glyph_name = "vaticana.linea.punctum";
506 else if (prefix_set & AUCTUM)
507 if (prefix_set & ASCENDENS)
508 glyph_name = "solesmes.auct.asc";
510 glyph_name = "solesmes.auct.desc";
511 else if ((context_info & STACKED_HEAD)
512 && (context_info & PES_UPPER))
513 if (prev_delta_pitch > 1)
514 glyph_name = "vaticana.upes";
516 glyph_name = "vaticana.vupes";
518 glyph_name = "vaticana.punctum";
521 * This head needs a cauda, if it starts a flexa, is not the upper
522 * head of a pes, and if it is a punctum.
524 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
525 if (glyph_name == "vaticana.punctum")
526 primitive->set_property ("add-cauda", ly_bool2scm (true));
529 * Execptional rule for porrectus:
531 * If the current head is preceded by a \flexa and succeded by a
532 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
533 * the previous head into a single curved flexa shape.
535 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
537 check_for_prefix_loss (prev_primitive);
538 prev_glyph_name = "flexa";
539 prev_primitive->set_property ("flexa-height",
540 scm_from_int (prev_delta_pitch));
541 prev_primitive->set_property ("flexa-width",
542 scm_from_double (flexa_width));
543 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
544 prev_primitive->set_property ("add-cauda",
545 ly_bool2scm (add_cauda));
546 check_for_prefix_loss (primitive);
548 primitive->set_property ("flexa-width",
549 scm_from_double (flexa_width));
553 * Exceptional rule for pes:
555 * If this head is stacked on the previous one due to a \pes, then
556 * set the glyph of the previous head to that for this special
557 * case, thereby avoiding potential vertical collision with the
560 if (prefix_set & PES_OR_FLEXA)
562 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
564 if (prev_glyph_name == "vaticana.punctum")
565 if (prev_delta_pitch > 1)
566 prev_glyph_name = "vaticana.lpes";
568 prev_glyph_name = "vaticana.vlpes";
573 prev_primitive->set_property ("glyph-name",
574 scm_makfrom0str (prev_glyph_name.c_str ()));
577 * In the backend, flexa shapes and joins need to know about line
578 * thickness. Hence, for simplicity, let's distribute the
579 * ligature grob's value for thickness to each ligature head (even
580 * if not all of them need to know).
582 primitive->set_property ("thickness", scm_from_double (thickness));
584 prev_primitive = primitive;
585 prev_prefix_set = prefix_set;
586 prev_context_info = context_info;
587 prev_delta_pitch = delta_pitch;
588 prev_glyph_name = glyph_name;
591 prev_primitive->set_property ("glyph-name",
592 scm_makfrom0str (prev_glyph_name.c_str ()));
594 align_heads (primitives, flexa_width, thickness);
596 #if 0 // experimental code to collapse spacing after ligature
597 /* TODO: set to max (old/new spacing-increment), since other
598 voices/staves also may want to set this property. */
599 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
600 Paper_column *paper_column = first_primitive->get_column ();
601 paper_column->warning (_f ("Vaticana_ligature_engraver: "
602 "setting `spacing-increment = %f': ptr =%ul",
603 ligature_width, paper_column));
605 set_property ("forced-spacing", scm_from_double (ligature_width));
609 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, rest);
610 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, note_head);
611 ADD_TRANSLATOR (Vaticana_ligature_engraver,
612 /* doc */ "Handles ligatures by glueing special ligature heads together.",
613 /* create */ "VaticanaLigature",
614 /* accept */ "ligature-event",