* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / vaticana-ligature-engraver.cc
blobe8197fc1fa5e302ae6bdb16363ee0a2ac2bf2aee
1 /*
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>
7 */
9 #include "gregorian-ligature-engraver.hh"
10 #include "gregorian-ligature.hh"
11 #include "vaticana-ligature.hh"
12 #include "item.hh"
13 #include "spanner.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "font-interface.hh"
16 #include "warn.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
28 private:
29 bool is_stacked_head (int prefix_set,
30 int context_info);
31 Real align_heads (Array<Grob_info> primitives,
32 Real flexa_width,
33 Real thickness);
35 public:
36 TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
38 protected:
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 ()
48 Spanner *
49 Vaticana_ligature_engraver::create_ligature_spanner ()
51 return make_spanner ("VaticanaLigature");
54 bool
55 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
56 int context_info)
58 bool is_stacked_b;
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)
65 is_stacked_b = false;
67 // ... or another pes
68 if (context_info & PES_LOWER)
69 is_stacked_b = false;
71 // ... or the previous note is a semivocalis or inclinatum
72 if (context_info & AFTER_DEMINUTUM)
73 is_stacked_b = false;
75 // auctum head is never stacked upon preceding note
76 if (prefix_set & AUCTUM)
77 is_stacked_b = false;
79 // virga is never stacked upon preceding note
80 if (prefix_set & VIRGA)
81 is_stacked_b = false;
83 // oriscus is never stacked upon preceding note
84 if (prefix_set & ORISCUS)
85 is_stacked_b = false;
87 if ((prefix_set & DEMINUTUM) &&
88 !(prefix_set & INCLINATUM) &&
89 (context_info & FLEXA_RIGHT))
90 is_stacked_b = true; // semivocalis head of deminutus form
92 return is_stacked_b;
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.
101 bool
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
109 * following head.
111 return true;
113 if ((prefix_set & INCLINATUM) &&
114 !(prev_prefix_set & INCLINATUM))
116 * Always start a series of inclinatum heads with an extra space.
118 return true;
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.
126 return true;
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
132 * touch each other.
134 return true;
136 return false;
139 Real
140 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
141 Real flexa_width,
142 Real thickness)
144 if (!primitives.size ())
146 programming_error ("Vaticana_ligature: "
147 "empty ligature [ignored]");
148 return 0.0;
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_dimension (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_);
178 int prefix_set =
179 ly_scm2int (primitive->get_property ("prefix-set"));
180 int context_info =
181 ly_scm2int (primitive->get_property ("context-info"));
184 * Get glyph_name, delta_pitch and context_info for this head.
187 SCM glyph_name_scm = primitive->get_property ("glyph-name");
188 if (glyph_name_scm == SCM_EOL)
190 primitive->programming_error ("Vaticana_ligature:"
191 "undefined glyph-name -> "
192 "ignoring grob");
193 continue;
195 String glyph_name = ly_scm2string (glyph_name_scm);
197 int delta_pitch = 0;
198 if (prev_primitive) /* urgh, need prev_primitive only here */
200 SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
201 if (delta_pitch_scm != SCM_EOL)
203 delta_pitch = ly_scm2int (delta_pitch_scm);
205 else
207 primitive->programming_error ("Vaticana_ligature:"
208 "delta-pitch undefined -> "
209 "ignoring grob");
210 continue;
215 * Now determine width and x-offset of head.
218 Real head_width;
219 Real x_offset;
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.
230 head_width = 0.0;
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;
243 x_offset = 0.0;
245 else
248 * This is a regular head, placed right to the previous one.
249 * Retrieve its width from corresponding font.
251 head_width =
252 Font_interface::get_default_font (primitive)->
253 find_by_name ("noteheads-" + glyph_name).extent (X_AXIS).length ();
254 x_offset = 0.0;
258 * Save the head's final x-offset.
260 primitive->set_property ("x-offset",
261 scm_make_real (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)))
272 if (!prev_primitive)
274 primitive->programming_error ("vaticana ligature: add-join: "
275 "missing previous primitive");
277 else
279 prev_primitive->set_property ("add-join",
280 ly_bool2scm (true));
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
295 * shape.
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.
334 void
335 check_for_prefix_loss (Item *primitive)
337 int prefix_set =
338 ly_scm2int (primitive->get_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",
344 prefs.to_str0 ()));
348 void
349 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
350 Array<Grob_info> primitives)
352 Real flexa_width= robust_scm2double ( ligature->get_property ("flexa-width"), 2);
354 Real thickness= robust_scm2double ( ligature->get_property ("thickness"), 1);
356 Item *prev_primitive = 0;
357 int prev_prefix_set = 0;
358 int prev_context_info = 0;
359 int prev_delta_pitch = 0;
360 String prev_glyph_name = "";
361 for (int i = 0; i < primitives.size (); i++) {
362 Item *primitive = dynamic_cast<Item*> (primitives[i].grob_);
364 int delta_pitch;
365 SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
366 if (delta_pitch_scm != SCM_EOL)
368 delta_pitch = ly_scm2int (delta_pitch_scm);
370 else
372 primitive->programming_error ("Vaticana_ligature:"
373 "delta-pitch undefined -> "
374 "ignoring grob");
375 continue;
378 /* retrieve & complete prefix_set and context_info */
379 int prefix_set =
380 ly_scm2int (primitive->get_property ("prefix-set"));
381 int context_info =
382 ly_scm2int (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
394 * the backend).
396 String glyph_name;
397 if (prefix_set & VIRGA)
399 glyph_name = "vaticana_punctum";
400 primitive->set_property ("add-stem", ly_bool2scm (true));
402 else if (prefix_set & QUILISMA)
403 glyph_name = "vaticana_quilisma";
404 else if (prefix_set & ORISCUS)
405 glyph_name = "solesmes_oriscus";
406 else if (prefix_set & STROPHA)
407 if (prefix_set & AUCTUM)
408 glyph_name = "solesmes_stropha_aucta";
409 else glyph_name = "solesmes_stropha";
410 else if (prefix_set & INCLINATUM)
411 if (prefix_set & AUCTUM)
412 glyph_name = "solesmes_incl_auctum";
413 else if (prefix_set & DEMINUTUM)
414 glyph_name = "solesmes_incl_parvum";
415 else
416 glyph_name = "vaticana_inclinatum";
417 else if (prefix_set & DEMINUTUM)
418 if (i == 0)
420 // initio debilis
421 glyph_name = "vaticana_reverse_plica";
423 else if (prev_delta_pitch > 0)
425 // epiphonus
426 if (!(prev_context_info & FLEXA_RIGHT))
427 /* correct head of previous primitive */
428 if (prev_delta_pitch > 1)
429 prev_glyph_name = "vaticana_epiphonus";
430 else
431 prev_glyph_name = "vaticana_vepiphonus";
432 glyph_name = "vaticana_plica";
434 else // (prev_delta_pitch <= 0)
436 // cephalicus
437 if (!(prev_context_info & FLEXA_RIGHT))
438 /* correct head of previous primitive */
440 if (i > 1)
442 /* cephalicus head with fixed size cauda */
443 prev_glyph_name = "vaticana_inner_cephalicus";
445 else
447 /* cephalicus head without cauda */
448 prev_glyph_name = "vaticana_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 = "vaticana_reverse_plica";
466 else if (prefix_set & (CAVUM | LINEA))
467 if ((prefix_set & CAVUM) && (prefix_set & LINEA))
468 glyph_name = "vaticana_linea_punctum_cavum";
469 else if (prefix_set & CAVUM)
470 glyph_name = "vaticana_punctum_cavum";
471 else
472 glyph_name = "vaticana_linea_punctum";
473 else if (prefix_set & AUCTUM)
474 if (prefix_set & ASCENDENS)
475 glyph_name = "solesmes_auct_asc";
476 else
477 glyph_name = "solesmes_auct_desc";
478 else if ((context_info & STACKED_HEAD) &&
479 (context_info & PES_UPPER))
480 if (prev_delta_pitch > 1)
481 glyph_name = "vaticana_upes";
482 else
483 glyph_name = "vaticana_vupes";
484 else
485 glyph_name = "vaticana_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, "vaticana_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);
514 glyph_name = "";
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
525 * current head.
527 if (prefix_set & PES_OR_FLEXA)
529 if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
531 if (!String::compare (prev_glyph_name, "vaticana_punctum"))
532 if (prev_delta_pitch > 1)
533 prev_glyph_name = "vaticana_lpes";
534 else
535 prev_glyph_name = "vaticana_vlpes";
539 if (prev_primitive)
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 #if 0
562 Real ligature_width =
563 #endif
565 align_heads (primitives, flexa_width, thickness);
567 #if 0 // experimental code to collapse spacing after ligature
568 /* TODO: set to max (old/new spacing-increment), since other
569 voices/staves also may want to set this property. */
570 Item *first_primitive = dynamic_cast<Item*> (primitives[0].grob_);
571 Paper_column *paper_column = first_primitive->get_column ();
572 paper_column->warning (_f ("Vaticana_ligature_engraver: "
573 "setting `spacing-increment = %f': ptr=%ul",
574 ligature_width, paper_column));
575 paper_column->
576 set_property ("forced-spacing", scm_make_real (ligature_width));
577 #endif
581 ENTER_DESCRIPTION (Vaticana_ligature_engraver,
582 /* descr */ "Handles ligatures by glueing special ligature heads together.",
583 /* creats*/ "VaticanaLigature",
584 /* accepts */ "ligature-event",
585 /* acks */ "note-head-interface rest-interface",
586 /* reads */ "",
587 /* write */ "");