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"
20 * This class implements the notation specific aspects of Vaticana
21 * style ligatures for Gregorian chant notation.
24 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
28 bool is_stacked_head (int prefix_set,
30 Real align_heads (Array<Grob_info> primitives,
35 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
38 virtual Spanner *create_ligature_spanner ();
39 virtual void transform_heads (Spanner *ligature,
40 Array<Grob_info> primitives);
43 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
48 Vaticana_ligature_engraver::create_ligature_spanner ()
50 return make_spanner ("VaticanaLigature", SCM_EOL);
54 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
59 // upper head of pes is stacked upon lower head of pes ...
60 is_stacked_b = context_info & PES_UPPER;
62 // ... unless this note starts a flexa
63 if (context_info & FLEXA_LEFT)
67 if (context_info & PES_LOWER)
70 // ... or the previous note is a semivocalis or inclinatum
71 if (context_info & AFTER_DEMINUTUM)
74 // auctum head is never stacked upon preceding note
75 if (prefix_set & AUCTUM)
78 // virga is never stacked upon preceding note
79 if (prefix_set & VIRGA)
82 // oriscus is never stacked upon preceding note
83 if (prefix_set & ORISCUS)
86 if ((prefix_set & DEMINUTUM)
87 && ! (prefix_set & INCLINATUM)
88 && (context_info & FLEXA_RIGHT))
89 is_stacked_b = true; // semivocalis head of deminutus form
95 * When aligning the heads, sometimes extra space is needed, e.g. to
96 * avoid clashing with the appendix of an adjacent notehead or with an
97 * adjacent notehead itself if it has the same pitch. Extra space is
98 * added at most once between to heads.
101 need_extra_space (int prev_prefix_set, int prefix_set,
102 int context_info, int delta_pitch)
104 if (prev_prefix_set & VIRGA)
106 * After a virga, make a an additional small space such that the
107 * appendix on the right side of the head does not touch the
112 if ((prefix_set & INCLINATUM)
113 && ! (prev_prefix_set & INCLINATUM))
115 * Always start a series of inclinatum heads with an extra space.
119 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
121 * Before a flexa (but not within a torculus), make a an
122 * additional small space such that the appendix on the left side
123 * of the flexa does not touch the this head.
127 if (delta_pitch == 0)
129 * If there are two adjacent noteheads with the same pitch, add
130 * additional small space between them, such that they do not
139 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
143 if (!primitives.size ())
145 programming_error ("Vaticana_ligature: "
146 "empty ligature [ignored]");
151 * The paper column where we put the whole ligature into.
154 = dynamic_cast<Item *> (primitives[0].grob_)->get_column ();
157 = thickness * column->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
160 * Amount of extra space two put between some particular
161 * configurations of adjacent heads.
163 * TODO: make this a property of primtive grobs.
165 Real extra_space = 4.0 * join_thickness;
168 * Keep track of the total width of the ligature.
170 Real ligature_width = 0.0;
172 Item *prev_primitive = 0;
173 int prev_prefix_set = 0;
174 for (int i = 0; i < primitives.size (); i++)
176 Item *primitive = dynamic_cast<Item *> (primitives[i].grob_);
178 = scm_to_int (primitive->get_property ("prefix-set"));
180 = scm_to_int (primitive->get_property ("context-info"));
183 * Get glyph_name, delta_pitch and context_info for this head.
186 SCM glyph_name_scm = primitive->get_property ("glyph-name");
187 if (glyph_name_scm == SCM_EOL)
189 primitive->programming_error ("Vaticana_ligature:"
190 "undefined glyph-name -> "
194 String glyph_name = ly_scm2string (glyph_name_scm);
197 if (prev_primitive) /* urgh, need prev_primitive only here */
199 SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
200 if (delta_pitch_scm != SCM_EOL)
202 delta_pitch = scm_to_int (delta_pitch_scm);
206 primitive->programming_error ("Vaticana_ligature:"
207 "delta-pitch undefined -> "
214 * Now determine width and x-offset of head.
220 if (context_info & STACKED_HEAD)
223 * This head is stacked upon the previous one; hence, it
224 * does not contribute to the total width of the ligature,
225 * and its width is assumed to be 0.0. Moreover, it is
226 * shifted to the left by its width such that the right side
227 * of this and the other head are horizontally aligned.
230 x_offset = join_thickness
231 - Font_interface::get_default_font (primitive)->
232 find_by_name ("noteheads." + glyph_name).extent (X_AXIS).length ();
234 else if (!String::compare (glyph_name, "flexa")
235 || !String::compare (glyph_name, ""))
238 * This head represents either half of a flexa shape.
239 * Hence, it is assigned half the width of this shape.
241 head_width = 0.5 * flexa_width;
247 * This is a regular head, placed right to the previous one.
248 * Retrieve its width from corresponding font.
251 = Font_interface::get_default_font (primitive)->
252 find_by_name ("noteheads." + glyph_name).extent (X_AXIS).length ();
257 * Save the head's final x-offset.
259 primitive->set_property ("x-offset",
260 scm_make_real (x_offset));
263 * If the head is the 2nd head of a pes or flexa (but not a
264 * flexa shape), mark this head to be joined with the left-side
265 * neighbour head (i.e. the previous head) by a vertical beam.
267 if ((context_info & PES_UPPER)
268 || ((context_info & FLEXA_RIGHT)
269 && ! (context_info & PES_LOWER)))
273 primitive->programming_error ("vaticana ligature: add-join: "
274 "missing previous primitive");
278 prev_primitive->set_property ("add-join",
282 * Create a small overlap of adjacent heads so that the join
283 * can be drawn perfectly between them.
285 ligature_width -= join_thickness;
288 else if (!String::compare (glyph_name, ""))
291 * This is the 2nd (virtual) head of flexa shape. Join it
292 * tightly with 1st head, i.e. do *not* add additional
293 * space, such that next head will not be off from the flexa
298 if (need_extra_space (prev_prefix_set, prefix_set,
299 context_info, delta_pitch))
300 ligature_width += extra_space;
303 * Horizontally line-up this head to form a ligature.
305 get_set_column (primitive, column);
306 primitive->translate_axis (ligature_width, X_AXIS);
307 ligature_width += head_width;
309 prev_primitive = primitive;
310 prev_prefix_set = prefix_set;
314 * Add extra horizontal padding space after ligature, such that
315 * neighbouring ligatures do not touch each other.
317 ligature_width += extra_space;
319 return ligature_width;
323 * Depending on the typographical features of a particular ligature
324 * style, some prefixes may be ignored. In particular, if a curved
325 * flexa shape is produced, any prefixes to either of the two
326 * contributing heads that would select a head other than punctum, is
327 * by definition ignored.
329 * This function prints a warning, if the given primitive is prefixed
330 * such that a head other than punctum would be chosen, if this
331 * primitive were engraved as a stand-alone head.
334 check_for_prefix_loss (Item *primitive)
337 = scm_to_int (primitive->get_property ("prefix-set"));
338 if (prefix_set & ~PES_OR_FLEXA)
340 String prefs = Gregorian_ligature::prefixes_to_str (primitive);
341 primitive->warning (_f ("ignored prefix (es) `%s' of this head according "
342 "to restrictions of the selected ligature style",
348 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
349 Array<Grob_info> primitives)
351 Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
353 Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
355 Item *prev_primitive = 0;
356 int prev_prefix_set = 0;
357 int prev_context_info = 0;
358 int prev_delta_pitch = 0;
359 String prev_glyph_name = "";
360 for (int i = 0; i < primitives.size (); i++)
362 Item *primitive = dynamic_cast<Item *> (primitives[i].grob_);
365 SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
366 if (delta_pitch_scm != SCM_EOL)
368 delta_pitch = scm_to_int (delta_pitch_scm);
372 primitive->programming_error ("Vaticana_ligature:"
373 "delta-pitch undefined -> "
378 /* retrieve & complete prefix_set and context_info */
380 = scm_to_int (primitive->get_property ("prefix-set"));
382 = scm_to_int (primitive->get_property ("context-info"));
383 if (is_stacked_head (prefix_set, context_info))
385 context_info |= STACKED_HEAD;
386 primitive->set_property ("context-info",
387 scm_int2num (context_info));
391 * Now determine which head to typeset (this is context sensitive
392 * information, since it depends on neighbouring heads; therefore,
393 * this decision must be made here in the engraver rather than in
397 if (prefix_set & VIRGA)
399 glyph_name = "svaticana.punctum";
400 primitive->set_property ("add-stem", ly_bool2scm (true));
402 else if (prefix_set & QUILISMA)
403 glyph_name = "svaticana.quilisma";
404 else if (prefix_set & ORISCUS)
405 glyph_name = "ssolesmes.oriscus";
406 else if (prefix_set & STROPHA)
407 if (prefix_set & AUCTUM)
408 glyph_name = "ssolesmes.stropha.aucta";
409 else glyph_name = "ssolesmes.stropha";
410 else if (prefix_set & INCLINATUM)
411 if (prefix_set & AUCTUM)
412 glyph_name = "ssolesmes.incl.auctum";
413 else if (prefix_set & DEMINUTUM)
414 glyph_name = "ssolesmes.incl.parvum";
416 glyph_name = "svaticana.inclinatum";
417 else if (prefix_set & DEMINUTUM)
421 glyph_name = "svaticana.reverse.plica";
423 else if (prev_delta_pitch > 0)
426 if (! (prev_context_info & FLEXA_RIGHT))
427 /* correct head of previous primitive */
428 if (prev_delta_pitch > 1)
429 prev_glyph_name = "svaticana.epiphonus";
431 prev_glyph_name = "svaticana.vepiphonus";
432 glyph_name = "svaticana.plica";
434 else // (prev_delta_pitch <= 0)
437 if (! (prev_context_info & FLEXA_RIGHT))
438 /* correct head of previous primitive */
442 /* cephalicus head with fixed size cauda */
443 prev_glyph_name = "svaticana.inner.cephalicus";
447 /* cephalicus head without cauda */
448 prev_glyph_name = "svaticana.cephalicus";
452 * Flexa has no variable size cauda if its left head is
453 * stacked on the right head. This is true for
454 * cephalicus. Hence, remove the cauda.
456 * Urgh: for the current implementation, this rule only
457 * applies for cephalicus; but it is a fundamental rule.
458 * Therefore, the following line of code should be
459 * placed somewhere else.
461 prev_primitive->set_property ("add-cauda",
462 ly_bool2scm (false));
464 glyph_name = "svaticana.reverse.plica";
466 else if (prefix_set & (CAVUM | LINEA))
467 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
468 glyph_name = "svaticana.linea.punctum.cavum";
469 else if (prefix_set & CAVUM)
470 glyph_name = "svaticana.punctum.cavum";
472 glyph_name = "svaticana.linea.punctum";
473 else if (prefix_set & AUCTUM)
474 if (prefix_set & ASCENDENS)
475 glyph_name = "ssolesmes.auct.asc";
477 glyph_name = "ssolesmes.auct.desc";
478 else if ((context_info & STACKED_HEAD)
479 && (context_info & PES_UPPER))
480 if (prev_delta_pitch > 1)
481 glyph_name = "svaticana.upes";
483 glyph_name = "svaticana.vupes";
485 glyph_name = "svaticana.punctum";
488 * This head needs a cauda, if it starts a flexa, is not the upper
489 * head of a pes, and if it is a punctum.
491 if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
492 if (!String::compare (glyph_name, "svaticana.punctum"))
493 primitive->set_property ("add-cauda", ly_bool2scm (true));
496 * Execptional rule for porrectus:
498 * If the current head is preceded by a \flexa and succeded by a
499 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
500 * the previous head into a single curved flexa shape.
502 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
504 check_for_prefix_loss (prev_primitive);
505 prev_glyph_name = "flexa";
506 prev_primitive->set_property ("flexa-height",
507 scm_int2num (prev_delta_pitch));
508 prev_primitive->set_property ("flexa-width",
509 scm_make_real (flexa_width));
510 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
511 prev_primitive->set_property ("add-cauda",
512 ly_bool2scm (add_cauda));
513 check_for_prefix_loss (primitive);
515 primitive->set_property ("flexa-width",
516 scm_make_real (flexa_width));
520 * Exceptional rule for pes:
522 * If this head is stacked on the previous one due to a \pes, then
523 * set the glyph of the previous head to that for this special
524 * case, thereby avoiding potential vertical collision with the
527 if (prefix_set & PES_OR_FLEXA)
529 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
531 if (!String::compare (prev_glyph_name, "svaticana.punctum"))
532 if (prev_delta_pitch > 1)
533 prev_glyph_name = "svaticana.lpes";
535 prev_glyph_name = "svaticana.vlpes";
540 prev_primitive->set_property ("glyph-name",
541 scm_makfrom0str (prev_glyph_name.to_str0 ()));
544 * In the backend, flexa shapes and joins need to know about line
545 * thickness. Hence, for simplicity, let's distribute the
546 * ligature grob's value for thickness to each ligature head (even
547 * if not all of them need to know).
549 primitive->set_property ("thickness", scm_make_real (thickness));
551 prev_primitive = primitive;
552 prev_prefix_set = prefix_set;
553 prev_context_info = context_info;
554 prev_delta_pitch = delta_pitch;
555 prev_glyph_name = glyph_name;
558 prev_primitive->set_property ("glyph-name",
559 scm_makfrom0str (prev_glyph_name.to_str0 ()));
561 align_heads (primitives, flexa_width, thickness);
563 #if 0 // experimental code to collapse spacing after ligature
564 /* TODO: set to max (old/new spacing-increment), since other
565 voices/staves also may want to set this property. */
566 Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob_);
567 Paper_column *paper_column = first_primitive->get_column ();
568 paper_column->warning (_f ("Vaticana_ligature_engraver: "
569 "setting `spacing-increment = %f': ptr =%ul",
570 ligature_width, paper_column));
572 set_property ("forced-spacing", scm_make_real (ligature_width));
576 ADD_TRANSLATOR (Vaticana_ligature_engraver,
577 /* descr */ "Handles ligatures by glueing special ligature heads together.",
578 /* creats*/ "VaticanaLigature",
579 /* accepts */ "ligature-event",
580 /* acks */ "note-head-interface rest-interface",