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 ()
53 Vaticana_ligature_engraver::create_ligature_spanner ()
55 return make_spanner ("VaticanaLigature", SCM_EOL);
59 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
64 // upper head of pes is stacked upon lower head of pes ...
65 is_stacked_b = context_info & PES_UPPER;
67 // ... unless this note starts a flexa
68 if (context_info & FLEXA_LEFT)
72 if (context_info & PES_LOWER)
75 // ... or the previous note is a semivocalis or inclinatum
76 if (context_info & AFTER_DEMINUTUM)
79 // auctum head is never stacked upon preceding note
80 if (prefix_set & AUCTUM)
83 // virga is never stacked upon preceding note
84 if (prefix_set & VIRGA)
87 // oriscus is never stacked upon preceding note
88 if (prefix_set & ORISCUS)
91 if ((prefix_set & DEMINUTUM)
92 && ! (prefix_set & INCLINATUM)
93 && (context_info & FLEXA_RIGHT))
94 is_stacked_b = true; // semivocalis head of deminutus form
100 * When aligning the heads, sometimes extra space is needed, e.g. to
101 * avoid clashing with the appendix of an adjacent notehead or with an
102 * adjacent notehead itself if it has the same pitch. Extra space is
103 * added at most once between to heads.
106 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
107 int context_info, int delta_pitch)
109 if (prev_prefix_set & VIRGA)
111 * After a virga, make a an additional small space such that the
112 * appendix on the right side of the head does not touch the
117 if ((prefix_set & INCLINATUM)
118 && ! (prev_prefix_set & INCLINATUM))
120 * Always start a series of inclinatum heads with an extra space.
124 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
126 * Before a flexa (but not within a torculus), make a an
127 * additional small space such that the appendix on the left side
128 * of the flexa does not touch the this head.
132 if (delta_pitch == 0)
134 * If there are two adjacent noteheads with the same pitch, add
135 * additional small space between them, such that they do not
144 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
148 if (!primitives.size ())
150 programming_error ("Vaticana_ligature: "
151 "empty ligature [ignored]");
156 * The paper column where we put the whole ligature into.
159 = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
162 = thickness * column->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
165 * Amount of extra space two put between some particular
166 * configurations of adjacent heads.
168 * TODO: make this a property of primtive grobs.
170 Real extra_space = 4.0 * join_thickness;
173 * Keep track of the total width of the ligature.
175 Real ligature_width = 0.0;
177 Item *prev_primitive = 0;
178 int prev_prefix_set = 0;
179 for (int i = 0; i < primitives.size (); i++)
181 Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
183 = scm_to_int (primitive->get_property ("prefix-set"));
185 = scm_to_int (primitive->get_property ("context-info"));
188 * Get glyph_name, delta_pitch and context_info for this head.
191 SCM glyph_name_scm = primitive->get_property ("glyph-name");
192 if (glyph_name_scm == SCM_EOL)
194 primitive->programming_error ("Vaticana_ligature:"
195 "undefined glyph-name -> "
199 String glyph_name = ly_scm2string (glyph_name_scm);
202 if (prev_primitive) /* urgh, need prev_primitive only here */
204 SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
205 if (delta_pitch_scm != SCM_EOL)
207 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." + 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." + glyph_name).extent (X_AXIS).length ();
262 * Save the head's final x-offset.
264 primitive->set_property ("x-offset",
265 scm_make_real (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)
373 delta_pitch = scm_to_int (delta_pitch_scm);
377 primitive->programming_error ("Vaticana_ligature:"
378 "delta-pitch undefined -> "
383 /* retrieve & complete prefix_set and context_info */
385 = scm_to_int (primitive->get_property ("prefix-set"));
387 = scm_to_int (primitive->get_property ("context-info"));
388 if (is_stacked_head (prefix_set, context_info))
390 context_info |= STACKED_HEAD;
391 primitive->set_property ("context-info",
392 scm_int2num (context_info));
396 * Now determine which head to typeset (this is context sensitive
397 * information, since it depends on neighbouring heads; therefore,
398 * this decision must be made here in the engraver rather than in
402 if (prefix_set & VIRGA)
404 glyph_name = "svaticana.punctum";
405 primitive->set_property ("add-stem", ly_bool2scm (true));
407 else if (prefix_set & QUILISMA)
408 glyph_name = "svaticana.quilisma";
409 else if (prefix_set & ORISCUS)
410 glyph_name = "ssolesmes.oriscus";
411 else if (prefix_set & STROPHA)
412 if (prefix_set & AUCTUM)
413 glyph_name = "ssolesmes.stropha.aucta";
414 else glyph_name = "ssolesmes.stropha";
415 else if (prefix_set & INCLINATUM)
416 if (prefix_set & AUCTUM)
417 glyph_name = "ssolesmes.incl.auctum";
418 else if (prefix_set & DEMINUTUM)
419 glyph_name = "ssolesmes.incl.parvum";
421 glyph_name = "svaticana.inclinatum";
422 else if (prefix_set & DEMINUTUM)
426 glyph_name = "svaticana.reverse.plica";
428 else if (prev_delta_pitch > 0)
431 if (! (prev_context_info & FLEXA_RIGHT))
432 /* correct head of previous primitive */
433 if (prev_delta_pitch > 1)
435 prev_glyph_name = "svaticana.epiphonus";
436 glyph_name = "svaticana.plica";
440 prev_glyph_name = "svaticana.vepiphonus";
441 glyph_name = "svaticana.vplica";
444 else if (prev_delta_pitch < 0)
447 if (! (prev_context_info & FLEXA_RIGHT))
448 /* correct head of previous primitive */
452 /* cephalicus head with fixed size cauda */
453 prev_glyph_name = "svaticana.inner.cephalicus";
457 /* cephalicus head without cauda */
458 prev_glyph_name = "svaticana.cephalicus";
462 * Flexa has no variable size cauda if its left head is
463 * stacked on the right head. This is true for
464 * cephalicus. Hence, remove the cauda.
466 * Urgh: for the current implementation, this rule only
467 * applies for cephalicus; but it is a fundamental rule.
468 * Therefore, the following line of code should be
469 * placed somewhere else.
471 prev_primitive->set_property ("add-cauda",
472 ly_bool2scm (false));
474 if (prev_delta_pitch < - 1)
476 glyph_name = "svaticana.reverse.plica";
480 glyph_name = "svaticana.reverse.vplica";
483 else // (prev_delta_pitch == 0)
485 primitive->programming_error ("Vaticana_ligature:"
486 "deminutum head must have different "
487 "pitch -> ignoring grob");
489 else if (prefix_set & (CAVUM | LINEA))
490 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
491 glyph_name = "svaticana.linea.punctum.cavum";
492 else if (prefix_set & CAVUM)
493 glyph_name = "svaticana.punctum.cavum";
495 glyph_name = "svaticana.linea.punctum";
496 else if (prefix_set & AUCTUM)
497 if (prefix_set & ASCENDENS)
498 glyph_name = "ssolesmes.auct.asc";
500 glyph_name = "ssolesmes.auct.desc";
501 else if ((context_info & STACKED_HEAD)
502 && (context_info & PES_UPPER))
503 if (prev_delta_pitch > 1)
504 glyph_name = "svaticana.upes";
506 glyph_name = "svaticana.vupes";
508 glyph_name = "svaticana.punctum";
511 * This head needs a cauda, if it starts a flexa, is not the upper
512 * head of a pes, and if it is a punctum.
514 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
515 if (!String::compare (glyph_name, "svaticana.punctum"))
516 primitive->set_property ("add-cauda", ly_bool2scm (true));
519 * Execptional rule for porrectus:
521 * If the current head is preceded by a \flexa and succeded by a
522 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
523 * the previous head into a single curved flexa shape.
525 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
527 check_for_prefix_loss (prev_primitive);
528 prev_glyph_name = "flexa";
529 prev_primitive->set_property ("flexa-height",
530 scm_int2num (prev_delta_pitch));
531 prev_primitive->set_property ("flexa-width",
532 scm_make_real (flexa_width));
533 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
534 prev_primitive->set_property ("add-cauda",
535 ly_bool2scm (add_cauda));
536 check_for_prefix_loss (primitive);
538 primitive->set_property ("flexa-width",
539 scm_make_real (flexa_width));
543 * Exceptional rule for pes:
545 * If this head is stacked on the previous one due to a \pes, then
546 * set the glyph of the previous head to that for this special
547 * case, thereby avoiding potential vertical collision with the
550 if (prefix_set & PES_OR_FLEXA)
552 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
554 if (!String::compare (prev_glyph_name, "svaticana.punctum"))
555 if (prev_delta_pitch > 1)
556 prev_glyph_name = "svaticana.lpes";
558 prev_glyph_name = "svaticana.vlpes";
563 prev_primitive->set_property ("glyph-name",
564 scm_makfrom0str (prev_glyph_name.to_str0 ()));
567 * In the backend, flexa shapes and joins need to know about line
568 * thickness. Hence, for simplicity, let's distribute the
569 * ligature grob's value for thickness to each ligature head (even
570 * if not all of them need to know).
572 primitive->set_property ("thickness", scm_make_real (thickness));
574 prev_primitive = primitive;
575 prev_prefix_set = prefix_set;
576 prev_context_info = context_info;
577 prev_delta_pitch = delta_pitch;
578 prev_glyph_name = glyph_name;
581 prev_primitive->set_property ("glyph-name",
582 scm_makfrom0str (prev_glyph_name.to_str0 ()));
584 align_heads (primitives, flexa_width, thickness);
586 #if 0 // experimental code to collapse spacing after ligature
587 /* TODO: set to max (old/new spacing-increment), since other
588 voices/staves also may want to set this property. */
589 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
590 Paper_column *paper_column = first_primitive->get_column ();
591 paper_column->warning (_f ("Vaticana_ligature_engraver: "
592 "setting `spacing-increment = %f': ptr =%ul",
593 ligature_width, paper_column));
595 set_property ("forced-spacing", scm_make_real (ligature_width));
599 ADD_ACKNOWLEDGER(Vaticana_ligature_engraver, rest);
600 ADD_ACKNOWLEDGER(Vaticana_ligature_engraver, note_head);
601 ADD_TRANSLATOR (Vaticana_ligature_engraver,
602 /* descr */ "Handles ligatures by glueing special ligature heads together.",
603 /* creats*/ "VaticanaLigature",
604 /* accepts */ "ligature-event",