2 vaticana-ligature-engraver.cc -- implement Vaticana_ligature_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2003 Juergen Reuter <reuter@ipd.uka.de>
9 #include "gregorian-ligature-engraver.hh"
10 #include "gregorian-ligature.hh"
11 #include "vaticana-ligature.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "font-interface.hh"
17 #include "paper-def.hh"
18 #include "paper-column.hh"
21 * This class implements the notation specific aspects of Vaticana
22 * style ligatures for Gregorian chant notation.
25 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
29 bool is_stacked_head (int prefix_set,
31 Real align_heads (Array<Grob_info> primitives,
36 TRANSLATOR_DECLARATIONS(Vaticana_ligature_engraver);
39 virtual Spanner *create_ligature_spanner ();
40 virtual void transform_heads (Spanner *ligature,
41 Array<Grob_info> primitives);
44 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
49 Vaticana_ligature_engraver::create_ligature_spanner ()
51 return new Spanner (get_property ("VaticanaLigature"));
55 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
60 // upper head of pes is stacked upon lower head of pes ...
61 is_stacked_b = context_info & PES_UPPER;
63 // ... unless this note starts a flexa
64 if (context_info & FLEXA_LEFT)
68 if (context_info & PES_LOWER)
71 // ... or the previous note is a semivocalis or inclinatum
72 if (context_info & AFTER_DEMINUTUM)
75 // auctum head is never stacked upon preceding note
76 if (prefix_set & AUCTUM)
79 // virga is never stacked upon preceding note
80 if (prefix_set & VIRGA)
83 // oriscus is never stacked upon preceding note
84 if (prefix_set & ORISCUS)
87 if ((prefix_set & DEMINUTUM) &&
88 !(prefix_set & INCLINATUM) &&
89 (context_info & FLEXA_RIGHT))
90 is_stacked_b = true; // semivocalis head of deminutus form
96 * When aligning the heads, sometimes extra space is needed, e.g. to
97 * avoid clashing with the appendix of an adjacent notehead or with an
98 * adjacent notehead itself if it has the same pitch. Extra space is
99 * added at most once between to heads.
102 need_extra_space (int prev_prefix_set, int prefix_set,
103 int context_info, int delta_pitch)
105 if (prev_prefix_set & VIRGA)
107 * After a virga, make a an additional small space such that the
108 * appendix on the right side of the head does not touch the
113 if ((prefix_set & INCLINATUM) &&
114 !(prev_prefix_set & INCLINATUM))
116 * Always start a series of inclinatum heads with an extra space.
120 if ((context_info & FLEXA_LEFT) && !(context_info & PES_UPPER))
122 * Before a flexa (but not within a torculus), make a an
123 * additional small space such that the appendix on the left side
124 * of the flexa does not touch the this head.
128 if (delta_pitch == 0)
130 * If there are two adjacent noteheads with the same pitch, add
131 * additional small space between them, such that they do not
140 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
144 if (!primitives.size ())
146 programming_error ("Vaticana_ligature: "
147 "empty ligature [ignored]");
152 * The paper column where we put the whole ligature into.
154 Paper_column *column =
155 dynamic_cast<Item*> (primitives[0].grob_)->get_column ();
157 Real join_thickness =
158 thickness * column->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));
161 * Amount of extra space two put between some particular
162 * configurations of adjacent heads.
164 * TODO: make this a property of primtive grobs.
166 Real extra_space = 4.0 * join_thickness;
169 * Keep track of the total width of the ligature.
171 Real ligature_width = 0.0;
173 Item *prev_primitive = 0;
174 int prev_prefix_set = 0;
175 for (int i = 0; i < primitives.size (); i++)
177 Item *primitive = dynamic_cast<Item*> (primitives[i].grob_);
179 gh_scm2int (primitive->get_grob_property ("prefix-set"));
181 gh_scm2int (primitive->get_grob_property ("context-info"));
184 * Get glyph_name, delta_pitch and context_info for this head.
187 SCM glyph_name_scm = primitive->get_grob_property ("glyph-name");
188 if (glyph_name_scm == SCM_EOL)
190 primitive->programming_error ("Vaticana_ligature:"
191 "undefined glyph-name -> "
195 String glyph_name = ly_scm2string (glyph_name_scm);
198 if (prev_primitive) /* urgh, need prev_primitive only here */
200 SCM delta_pitch_scm = prev_primitive->get_grob_property ("delta-pitch");
201 if (delta_pitch_scm != SCM_EOL)
203 delta_pitch = gh_scm2int (delta_pitch_scm);
207 primitive->programming_error ("Vaticana_ligature:"
208 "delta-pitch undefined -> "
215 * Now determine width and x-offset of head.
221 if (context_info & STACKED_HEAD)
224 * This head is stacked upon the previous one; hence, it
225 * does not contribute to the total width of the ligature,
226 * and its width is assumed to be 0.0. Moreover, it is
227 * shifted to the left by its width such that the right side
228 * of this and the other head are horizontally aligned.
231 x_offset = join_thickness -
232 Font_interface::get_default_font (primitive)->
233 find_by_name ("noteheads-" + glyph_name).extent (X_AXIS).length ();
235 else if (!String::compare (glyph_name, "flexa") ||
236 !String::compare (glyph_name, ""))
239 * This head represents either half of a flexa shape.
240 * Hence, it is assigned half the width of this shape.
242 head_width = 0.5 * flexa_width;
248 * This is a regular head, placed right to the previous one.
249 * Retrieve its width from corresponding font.
252 Font_interface::get_default_font (primitive)->
253 find_by_name ("noteheads-" + glyph_name).extent (X_AXIS).length ();
258 * Save the head's final x-offset.
260 primitive->set_grob_property ("x-offset",
261 gh_double2scm (x_offset));
264 * If the head is the 2nd head of a pes or flexa (but not a
265 * flexa shape), mark this head to be joined with the left-side
266 * neighbour head (i.e. the previous head) by a vertical beam.
268 if ((context_info & PES_UPPER) ||
269 ((context_info & FLEXA_RIGHT) &&
270 !(context_info & PES_LOWER)))
274 primitive->programming_error ("vaticana ligature: add-join: "
275 "missing previous primitive");
279 prev_primitive->set_grob_property ("add-join",
283 * Create a small overlap of adjacent heads so that the join
284 * can be drawn perfectly between them.
286 ligature_width -= join_thickness;
289 else if (!String::compare (glyph_name, ""))
292 * This is the 2nd (virtual) head of flexa shape. Join it
293 * tightly with 1st head, i.e. do *not* add additional
294 * space, such that next head will not be off from the flexa
299 if (need_extra_space (prev_prefix_set, prefix_set,
300 context_info, delta_pitch))
301 ligature_width += extra_space;
304 * Horizontally line-up this head to form a ligature.
306 get_set_column (primitive, column);
307 primitive->translate_axis (ligature_width, X_AXIS);
308 ligature_width += head_width;
310 prev_primitive = primitive;
311 prev_prefix_set = prefix_set;
315 * Add extra horizontal padding space after ligature, such that
316 * neighbouring ligatures do not touch each other.
318 ligature_width += extra_space;
320 return ligature_width;
324 * Depending on the typographical features of a particular ligature
325 * style, some prefixes may be ignored. In particular, if a curved
326 * flexa shape is produced, any prefixes to either of the two
327 * contributing heads that would select a head other than punctum, is
328 * by definition ignored.
330 * This function prints a warning, if the given primitive is prefixed
331 * such that a head other than punctum would be chosen, if this
332 * primitive were engraved as a stand-alone head.
335 check_for_prefix_loss (Item *primitive)
338 gh_scm2int (primitive->get_grob_property ("prefix-set"));
339 if (prefix_set & ~PES_OR_FLEXA)
341 String prefs = Gregorian_ligature::prefixes_to_str (primitive);
342 primitive->warning (_f ("ignored prefix(es) `%s' of this head according "
343 "to restrictions of the selected ligature style",
349 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
350 Array<Grob_info> primitives)
353 SCM flexa_width_scm = ligature->get_grob_property ("flexa-width");
354 if (flexa_width_scm != SCM_EOL)
356 flexa_width = gh_scm2double (flexa_width_scm);
360 ligature->programming_error ("Vaticana_ligature_engraver:"
361 "flexa-width undefined; "
362 "assuming 2.0 staff space");
364 2.0 * Staff_symbol_referencer::staff_space (ligature);
368 SCM thickness_scm = ligature->get_grob_property ("thickness");
369 if (thickness_scm != SCM_EOL)
371 thickness = gh_scm2double (thickness_scm);
375 ligature->programming_error ("Vaticana_ligature_engraver:"
376 "thickness undefined; assuming 1.0");
380 Item *prev_primitive = 0;
381 int prev_prefix_set = 0;
382 int prev_context_info = 0;
383 int prev_delta_pitch = 0;
384 String prev_glyph_name = "";
385 for (int i = 0; i < primitives.size(); i++) {
386 Item *primitive = dynamic_cast<Item*> (primitives[i].grob_);
389 SCM delta_pitch_scm = primitive->get_grob_property ("delta-pitch");
390 if (delta_pitch_scm != SCM_EOL)
392 delta_pitch = gh_scm2int (delta_pitch_scm);
396 primitive->programming_error ("Vaticana_ligature:"
397 "delta-pitch undefined -> "
402 /* retrieve & complete prefix_set and context_info */
404 gh_scm2int (primitive->get_grob_property ("prefix-set"));
406 gh_scm2int (primitive->get_grob_property ("context-info"));
407 if (is_stacked_head (prefix_set, context_info))
409 context_info |= STACKED_HEAD;
410 primitive->set_grob_property ("context-info",
411 gh_int2scm (context_info));
415 * Now determine which head to typeset (this is context sensitive
416 * information, since it depends on neighbouring heads; therefore,
417 * this decision must be made here in the engraver rather than in
421 if (prefix_set & VIRGA)
423 glyph_name = "vaticana_punctum";
424 primitive->set_grob_property ("add-stem", gh_bool2scm(true));
426 else if (prefix_set & QUILISMA)
427 glyph_name = "vaticana_quilisma";
428 else if (prefix_set & ORISCUS)
429 glyph_name = "solesmes_oriscus";
430 else if (prefix_set & STROPHA)
431 if (prefix_set & AUCTUM)
432 glyph_name = "solesmes_stropha_aucta";
433 else glyph_name = "solesmes_stropha";
434 else if (prefix_set & INCLINATUM)
435 if (prefix_set & AUCTUM)
436 glyph_name = "solesmes_incl_auctum";
437 else if (prefix_set & DEMINUTUM)
438 glyph_name = "solesmes_incl_parvum";
440 glyph_name = "vaticana_inclinatum";
441 else if (prefix_set & DEMINUTUM)
445 glyph_name = "vaticana_reverse_plica";
447 else if (prev_delta_pitch > 0)
450 if (!(prev_context_info & FLEXA_RIGHT))
451 /* correct head of previous primitive */
452 if (prev_delta_pitch > 1)
453 prev_glyph_name = "vaticana_epiphonus";
455 prev_glyph_name = "vaticana_vepiphonus";
456 glyph_name = "vaticana_plica";
458 else // (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_grob_property ("add-cauda",
488 glyph_name = "vaticana_reverse_plica";
490 else if (prefix_set & (CAVUM | LINEA))
491 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
492 glyph_name = "vaticana_linea_punctum_cavum";
493 else if (prefix_set & CAVUM)
494 glyph_name = "vaticana_punctum_cavum";
496 glyph_name = "vaticana_linea_punctum";
497 else if (prefix_set & AUCTUM)
498 if (prefix_set & ASCENDENS)
499 glyph_name = "solesmes_auct_asc";
501 glyph_name = "solesmes_auct_desc";
502 else if ((context_info & STACKED_HEAD) &&
503 (context_info & PES_UPPER))
504 if (prev_delta_pitch > 1)
505 glyph_name = "vaticana_upes";
507 glyph_name = "vaticana_vupes";
509 glyph_name = "vaticana_punctum";
512 * This head needs a cauda, if it starts a flexa, is not the upper
513 * head of a pes, and if it is a punctum.
515 if ((context_info & FLEXA_LEFT) && !(context_info & PES_UPPER))
516 if (!String::compare (glyph_name, "vaticana_punctum"))
517 primitive->set_grob_property ("add-cauda", gh_bool2scm(true));
520 * Execptional rule for porrectus:
522 * If the current head is preceded by a \flexa and succeded by a
523 * \pes (e.g. "a \flexa g \pes a"), then join the current head and
524 * the previous head into a single curved flexa shape.
526 if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
528 check_for_prefix_loss (prev_primitive);
529 prev_glyph_name = "flexa";
530 prev_primitive->set_grob_property ("flexa-height",
531 gh_int2scm (prev_delta_pitch));
532 prev_primitive->set_grob_property ("flexa-width",
533 gh_double2scm (flexa_width));
534 bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
535 prev_primitive->set_grob_property ("add-cauda",
536 gh_bool2scm (add_cauda));
537 check_for_prefix_loss (primitive);
539 primitive->set_grob_property ("flexa-width",
540 gh_double2scm (flexa_width));
544 * Exceptional rule for pes:
546 * If this head is stacked on the previous one due to a \pes, then
547 * set the glyph of the previous head to that for this special
548 * case, thereby avoiding potential vertical collision with the
551 if (prefix_set & PES_OR_FLEXA)
553 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
555 if (!String::compare (prev_glyph_name, "vaticana_punctum"))
556 if (prev_delta_pitch > 1)
557 prev_glyph_name = "vaticana_lpes";
559 prev_glyph_name = "vaticana_vlpes";
564 prev_primitive->set_grob_property ("glyph-name",
565 scm_makfrom0str (prev_glyph_name.to_str0 ()));
568 * In the backend, flexa shapes and joins need to know about line
569 * thickness. Hence, for simplicity, let's distribute the
570 * ligature grob's value for thickness to each ligature head (even
571 * if not all of them need to know).
573 primitive->set_grob_property ("thickness", gh_double2scm (thickness));
575 prev_primitive = primitive;
576 prev_prefix_set = prefix_set;
577 prev_context_info = context_info;
578 prev_delta_pitch = delta_pitch;
579 prev_glyph_name = glyph_name;
582 prev_primitive->set_grob_property ("glyph-name",
583 scm_makfrom0str (prev_glyph_name.to_str0 ()));
586 Real ligature_width =
589 align_heads (primitives, flexa_width, thickness);
591 #if 0 // experimental code to collapse spacing after ligature
592 /* TODO: set to max(old/new spacing-increment), since other
593 voices/staves also may want to set this property. */
594 Item *first_primitive = dynamic_cast<Item*> (primitives[0].grob_);
595 Paper_column *paper_column = first_primitive->get_column();
596 paper_column->warning (_f ("Vaticana_ligature_engraver: "
597 "setting `spacing-increment = %f': ptr=%ul",
598 ligature_width, paper_column));
600 set_grob_property("forced-spacing", gh_double2scm (ligature_width));
605 ENTER_DESCRIPTION (Vaticana_ligature_engraver,
606 /* descr */ "Handles ligatures by glueing special ligature heads together.",
607 /* creats*/ "VaticanaLigature",
608 /* accepts */ "ligature-event abort-event",
609 /* acks */ "note-head-interface rest-interface",