2 vaticana-ligature-engraver.cc -- implement Vaticana_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2003--2005 Juergen Reuter <reuter@ipd.uka.de>
9 #include "gregorian-ligature-engraver.hh"
10 #include "gregorian-ligature.hh"
11 #include "vaticana-ligature.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "font-interface.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
19 #include "translator.icc"
22 * This class implements the notation specific aspects of Vaticana
23 * style ligatures for Gregorian chant notation.
26 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
31 need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
32 int context_info, int delta_pitch);
33 bool is_stacked_head (int prefix_set,
35 Real align_heads (Array<Grob_info> primitives,
40 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
43 virtual Spanner *create_ligature_spanner ();
44 virtual void transform_heads (Spanner *ligature,
45 Array<Grob_info> primitives);
48 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
50 brew_ligature_primitive_proc =
51 Vaticana_ligature::brew_ligature_primitive_proc;
55 Vaticana_ligature_engraver::create_ligature_spanner ()
57 return make_spanner ("VaticanaLigature", SCM_EOL);
61 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
66 // upper head of pes is stacked upon lower head of pes ...
67 is_stacked_b = context_info & PES_UPPER;
69 // ... unless this note starts a flexa
70 if (context_info & FLEXA_LEFT)
74 if (context_info & PES_LOWER)
77 // ... or the previous note is a semivocalis or inclinatum
78 if (context_info & AFTER_DEMINUTUM)
81 // auctum head is never stacked upon preceding note
82 if (prefix_set & AUCTUM)
85 // virga is never stacked upon preceding note
86 if (prefix_set & VIRGA)
89 // oriscus is never stacked upon preceding note
90 if (prefix_set & ORISCUS)
93 if ((prefix_set & DEMINUTUM)
94 && ! (prefix_set & INCLINATUM)
95 && (context_info & FLEXA_RIGHT))
96 is_stacked_b = true; // semivocalis head of deminutus form
102 * When aligning the heads, sometimes extra space is needed, e.g. to
103 * avoid clashing with the appendix of an adjacent notehead or with an
104 * adjacent notehead itself if it has the same pitch. Extra space is
105 * added at most once between to heads.
108 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
109 int context_info, int delta_pitch)
111 if (prev_prefix_set & VIRGA)
113 * After a virga, make a an additional small space such that the
114 * appendix on the right side of the head does not touch the
119 if ((prefix_set & INCLINATUM)
120 && ! (prev_prefix_set & INCLINATUM))
122 * Always start a series of inclinatum heads with an extra space.
126 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
128 * Before a flexa (but not within a torculus), make a an
129 * additional small space such that the appendix on the left side
130 * of the flexa does not touch the this head.
134 if (delta_pitch == 0)
136 * If there are two adjacent noteheads with the same pitch, add
137 * additional small space between them, such that they do not
146 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
150 if (!primitives.size ())
152 programming_error ("Vaticana_ligature: "
153 "empty ligature [ignored]");
158 * The paper column where we put the whole ligature into.
161 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
164 = thickness * column->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
167 * Amount of extra space two put between some particular
168 * configurations of adjacent heads.
170 * TODO: make this a property of primtive grobs.
172 Real extra_space = 4.0 * join_thickness;
175 * Keep track of the total width of the ligature.
177 Real ligature_width = 0.0;
179 Item *prev_primitive = 0;
180 int prev_prefix_set = 0;
181 for (int i = 0; i < primitives.size (); i++)
183 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
185 = scm_to_int (primitive->get_property ("prefix-set"));
187 = scm_to_int (primitive->get_property ("context-info"));
190 * Get glyph_name, delta_pitch and context_info for this head.
193 SCM glyph_name_scm = primitive->get_property ("glyph-name");
194 if (glyph_name_scm == SCM_EOL)
196 primitive->programming_error ("Vaticana_ligature:"
197 "undefined glyph-name -> "
201 String glyph_name = ly_scm2string (glyph_name_scm);
204 if (prev_primitive) /* urgh, need prev_primitive only here */
206 SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
207 if (delta_pitch_scm != SCM_EOL)
208 delta_pitch = scm_to_int (delta_pitch_scm);
211 primitive->programming_error ("Vaticana_ligature:"
212 "delta-pitch undefined -> "
219 * Now determine width and x-offset of head.
225 if (context_info & STACKED_HEAD)
228 * This head is stacked upon the previous one; hence, it
229 * does not contribute to the total width of the ligature,
230 * and its width is assumed to be 0.0. Moreover, it is
231 * shifted to the left by its width such that the right side
232 * of this and the other head are horizontally aligned.
235 x_offset = join_thickness
236 - Font_interface::get_default_font (primitive)->
237 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
239 else if (!String::compare (glyph_name, "flexa")
240 || !String::compare (glyph_name, ""))
243 * This head represents either half of a flexa shape.
244 * Hence, it is assigned half the width of this shape.
246 head_width = 0.5 * flexa_width;
252 * This is a regular head, placed right to the previous one.
253 * Retrieve its width from corresponding font.
256 = Font_interface::get_default_font (primitive)->
257 find_by_name ("noteheads.s" + glyph_name).extent (X_AXIS).length ();
262 * Save the head's final x-offset.
264 primitive->set_property ("x-offset",
265 scm_from_double (x_offset));
268 * If the head is the 2nd head of a pes or flexa (but not a
269 * flexa shape), mark this head to be joined with the left-side
270 * neighbour head (i.e. the previous head) by a vertical beam.
272 if ((context_info & PES_UPPER)
273 || ((context_info & FLEXA_RIGHT)
274 && ! (context_info & PES_LOWER)))
278 primitive->programming_error ("vaticana ligature: add-join: "
279 "missing previous primitive");
283 prev_primitive->set_property ("add-join",
287 * Create a small overlap of adjacent heads so that the join
288 * can be drawn perfectly between them.
290 ligature_width -= join_thickness;
293 else if (!String::compare (glyph_name, ""))
296 * This is the 2nd (virtual) head of flexa shape. Join it
297 * tightly with 1st head, i.e. do *not* add additional
298 * space, such that next head will not be off from the flexa
303 if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
304 context_info, delta_pitch))
305 ligature_width += extra_space;
308 * Horizontally line-up this head to form a ligature.
310 get_set_column (primitive, column);
311 primitive->translate_axis (ligature_width, X_AXIS);
312 ligature_width += head_width;
314 prev_primitive = primitive;
315 prev_prefix_set = prefix_set;
319 * Add extra horizontal padding space after ligature, such that
320 * neighbouring ligatures do not touch each other.
322 ligature_width += extra_space;
324 return ligature_width;
328 * Depending on the typographical features of a particular ligature
329 * style, some prefixes may be ignored. In particular, if a curved
330 * flexa shape is produced, any prefixes to either of the two
331 * contributing heads that would select a head other than punctum, is
332 * by definition ignored.
334 * This function prints a warning, if the given primitive is prefixed
335 * such that a head other than punctum would be chosen, if this
336 * primitive were engraved as a stand-alone head.
339 check_for_prefix_loss (Item *primitive)
342 = scm_to_int (primitive->get_property ("prefix-set"));
343 if (prefix_set & ~PES_OR_FLEXA)
345 String prefs = Gregorian_ligature::prefixes_to_str (primitive);
346 primitive->warning (_f ("ignored prefix (es) `%s' of this head according "
347 "to restrictions of the selected ligature style",
353 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
354 Array<Grob_info> primitives)
356 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
358 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
360 Item *prev_primitive = 0;
361 int prev_prefix_set = 0;
362 int prev_context_info = 0;
363 int prev_delta_pitch = 0;
364 String prev_glyph_name = "";
365 for (int i = 0; i < primitives.size (); i++)
367 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
370 SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
371 if (delta_pitch_scm != SCM_EOL)
372 delta_pitch = scm_to_int (delta_pitch_scm);
375 primitive->programming_error ("Vaticana_ligature:"
376 "delta-pitch undefined -> "
381 /* retrieve & complete prefix_set and context_info */
383 = scm_to_int (primitive->get_property ("prefix-set"));
385 = scm_to_int (primitive->get_property ("context-info"));
386 if (is_stacked_head (prefix_set, context_info))
388 context_info |= STACKED_HEAD;
389 primitive->set_property ("context-info",
390 scm_from_int (context_info));
394 * Now determine which head to typeset (this is context sensitive
395 * information, since it depends on neighbouring heads; therefore,
396 * this decision must be made here in the engraver rather than in
400 if (prefix_set & VIRGA)
402 glyph_name = "vaticana.punctum";
403 primitive->set_property ("add-stem", ly_bool2scm (true));
405 else if (prefix_set & QUILISMA)
406 glyph_name = "vaticana.quilisma";
407 else if (prefix_set & ORISCUS)
408 glyph_name = "solesmes.oriscus";
409 else if (prefix_set & STROPHA)
410 if (prefix_set & AUCTUM)
411 glyph_name = "solesmes.stropha.aucta";
412 else glyph_name = "solesmes.stropha";
413 else if (prefix_set & INCLINATUM)
414 if (prefix_set & AUCTUM)
415 glyph_name = "solesmes.incl.auctum";
416 else if (prefix_set & DEMINUTUM)
417 glyph_name = "solesmes.incl.parvum";
419 glyph_name = "vaticana.inclinatum";
420 else if (prefix_set & DEMINUTUM)
424 glyph_name = "vaticana.reverse.plica";
426 else if (prev_delta_pitch > 0)
429 if (! (prev_context_info & FLEXA_RIGHT))
430 /* correct head of previous primitive */
431 if (prev_delta_pitch > 1)
432 prev_glyph_name = "vaticana.epiphonus";
434 prev_glyph_name = "vaticana.vepiphonus";
435 if (prev_delta_pitch > 1)
436 glyph_name = "vaticana.plica";
438 glyph_name = "vaticana.vplica";
440 else if (prev_delta_pitch < 0)
443 if (! (prev_context_info & FLEXA_RIGHT))
444 /* correct head of previous primitive */
448 /* cephalicus head with fixed size cauda */
449 prev_glyph_name = "vaticana.inner.cephalicus";
453 /* cephalicus head without cauda */
454 prev_glyph_name = "vaticana.cephalicus";
458 * Flexa has no variable size cauda if its left head is
459 * stacked on the right head. This is true for
460 * cephalicus. Hence, remove the cauda.
462 * Urgh: for the current implementation, this rule only
463 * applies for cephalicus; but it is a fundamental rule.
464 * Therefore, the following line of code should be
465 * placed somewhere else.
467 prev_primitive->set_property ("add-cauda",
468 ly_bool2scm (false));
470 if (prev_delta_pitch < - 1)
471 glyph_name = "vaticana.reverse.plica";
473 glyph_name = "vaticana.reverse.vplica";
475 else // (prev_delta_pitch == 0)
477 primitive->programming_error ("Vaticana_ligature:"
478 "deminutum head must have different "
479 "pitch -> ignoring grob");
481 else if (prefix_set & (CAVUM | LINEA))
482 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
483 glyph_name = "vaticana.linea.punctum.cavum";
484 else if (prefix_set & CAVUM)
485 glyph_name = "vaticana.punctum.cavum";
487 glyph_name = "vaticana.linea.punctum";
488 else if (prefix_set & AUCTUM)
489 if (prefix_set & ASCENDENS)
490 glyph_name = "solesmes.auct.asc";
492 glyph_name = "solesmes.auct.desc";
493 else if ((context_info & STACKED_HEAD)
494 && (context_info & PES_UPPER))
495 if (prev_delta_pitch > 1)
496 glyph_name = "vaticana.upes";
498 glyph_name = "vaticana.vupes";
500 glyph_name = "vaticana.punctum";
503 * This head needs a cauda, if it starts a flexa, is not the upper
504 * head of a pes, and if it is a punctum.
506 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
507 if (!String::compare (glyph_name, "vaticana.punctum"))
508 primitive->set_property ("add-cauda", ly_bool2scm (true));
511 * Execptional rule for porrectus:
513 * If the current head is preceded by a \flexa and succeded by a
514 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
515 * the previous head into a single curved flexa shape.
517 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
519 check_for_prefix_loss (prev_primitive);
520 prev_glyph_name = "flexa";
521 prev_primitive->set_property ("flexa-height",
522 scm_from_int (prev_delta_pitch));
523 prev_primitive->set_property ("flexa-width",
524 scm_from_double (flexa_width));
525 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
526 prev_primitive->set_property ("add-cauda",
527 ly_bool2scm (add_cauda));
528 check_for_prefix_loss (primitive);
530 primitive->set_property ("flexa-width",
531 scm_from_double (flexa_width));
535 * Exceptional rule for pes:
537 * If this head is stacked on the previous one due to a \pes, then
538 * set the glyph of the previous head to that for this special
539 * case, thereby avoiding potential vertical collision with the
542 if (prefix_set & PES_OR_FLEXA)
544 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
546 if (!String::compare (prev_glyph_name, "vaticana.punctum"))
547 if (prev_delta_pitch > 1)
548 prev_glyph_name = "vaticana.lpes";
550 prev_glyph_name = "vaticana.vlpes";
555 prev_primitive->set_property ("glyph-name",
556 scm_makfrom0str (prev_glyph_name.to_str0 ()));
559 * In the backend, flexa shapes and joins need to know about line
560 * thickness. Hence, for simplicity, let's distribute the
561 * ligature grob's value for thickness to each ligature head (even
562 * if not all of them need to know).
564 primitive->set_property ("thickness", scm_from_double (thickness));
566 prev_primitive = primitive;
567 prev_prefix_set = prefix_set;
568 prev_context_info = context_info;
569 prev_delta_pitch = delta_pitch;
570 prev_glyph_name = glyph_name;
573 prev_primitive->set_property ("glyph-name",
574 scm_makfrom0str (prev_glyph_name.to_str0 ()));
576 align_heads (primitives, flexa_width, thickness);
578 #if 0 // experimental code to collapse spacing after ligature
579 /* TODO: set to max (old/new spacing-increment), since other
580 voices/staves also may want to set this property. */
581 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
582 Paper_column *paper_column = first_primitive->get_column ();
583 paper_column->warning (_f ("Vaticana_ligature_engraver: "
584 "setting `spacing-increment = %f': ptr =%ul",
585 ligature_width, paper_column));
587 set_property ("forced-spacing", scm_from_double (ligature_width));
591 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, rest);
592 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, note_head);
593 ADD_TRANSLATOR (Vaticana_ligature_engraver,
594 /* doc */ "Handles ligatures by glueing special ligature heads together.",
595 /* create */ "VaticanaLigature",
596 /* accept */ "ligature-event",