]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature-engraver.cc
* flower
[lilypond.git] / lily / vaticana-ligature-engraver.cc
1 /*
2   vaticana-ligature-engraver.cc -- implement Vaticana_ligature_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2003--2005 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "gregorian-ligature-engraver.hh"
10 #include "gregorian-ligature.hh"
11 #include "vaticana-ligature.hh"
12 #include "spanner.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "font-interface.hh"
15 #include "warn.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
18
19 /*
20  * This class implements the notation specific aspects of Vaticana
21  * style ligatures for Gregorian chant notation.
22  */
23
24 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
25 {
26
27 private:
28   bool is_stacked_head (int prefix_set,
29                         int context_info);
30   Real align_heads (Array<Grob_info> primitives,
31                     Real flexa_width,
32                     Real thickness);
33
34 public:
35   TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
36
37 protected:
38   virtual Spanner *create_ligature_spanner ();
39   virtual void transform_heads (Spanner *ligature,
40                                 Array<Grob_info> primitives);
41 };
42
43 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
44 {
45 }
46
47 Spanner *
48 Vaticana_ligature_engraver::create_ligature_spanner ()
49 {
50   return make_spanner ("VaticanaLigature", SCM_EOL);
51 }
52
53 bool
54 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
55                                              int context_info)
56 {
57   bool is_stacked_b;
58
59   // upper head of pes is stacked upon lower head of pes ...
60   is_stacked_b = context_info & PES_UPPER;
61
62   // ... unless this note starts a flexa
63   if (context_info & FLEXA_LEFT)
64     is_stacked_b = false;
65
66   // ... or another pes
67   if (context_info & PES_LOWER)
68     is_stacked_b = false;
69
70   // ... or the previous note is a semivocalis or inclinatum
71   if (context_info & AFTER_DEMINUTUM)
72     is_stacked_b = false;
73
74   // auctum head is never stacked upon preceding note
75   if (prefix_set & AUCTUM)
76     is_stacked_b = false;
77
78   // virga is never stacked upon preceding note
79   if (prefix_set & VIRGA)
80     is_stacked_b = false;
81
82   // oriscus is never stacked upon preceding note
83   if (prefix_set & ORISCUS)
84     is_stacked_b = false;
85
86   if ((prefix_set & DEMINUTUM)
87       && ! (prefix_set & INCLINATUM)
88       && (context_info & FLEXA_RIGHT))
89     is_stacked_b = true; // semivocalis head of deminutus form
90
91   return is_stacked_b;
92 }
93
94 /*
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.
99  */
100 bool
101 need_extra_space (int prev_prefix_set, int prefix_set,
102                   int context_info, int delta_pitch)
103 {
104   if (prev_prefix_set & VIRGA)
105     /*
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
108      * following head.
109      */
110     return true;
111
112   if ((prefix_set & INCLINATUM)
113       && ! (prev_prefix_set & INCLINATUM))
114     /*
115      * Always start a series of inclinatum heads with an extra space.
116      */
117     return true;
118
119   if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
120     /*
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.
124      */
125     return true;
126
127   if (delta_pitch == 0)
128     /*
129      * If there are two adjacent noteheads with the same pitch, add
130      * additional small space between them, such that they do not
131      * touch each other.
132      */
133     return true;
134
135   return false;
136 }
137
138 Real
139 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
140                                          Real flexa_width,
141                                          Real thickness)
142 {
143   if (!primitives.size ())
144     {
145       programming_error ("Vaticana_ligature: "
146                          "empty ligature [ignored]");
147       return 0.0;
148     }
149
150   /*
151    * The paper column where we put the whole ligature into.
152    */
153   Paper_column *column
154     = dynamic_cast<Item *> (primitives[0].grob_)->get_column ();
155
156   Real join_thickness
157     = thickness * column->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
158
159   /*
160    * Amount of extra space two put between some particular
161    * configurations of adjacent heads.
162    *
163    * TODO: make this a property of primtive grobs.
164    */
165   Real extra_space = 4.0 * join_thickness;
166
167   /*
168    * Keep track of the total width of the ligature.
169    */
170   Real ligature_width = 0.0;
171
172   Item *prev_primitive = 0;
173   int prev_prefix_set = 0;
174   for (int i = 0; i < primitives.size (); i++)
175     {
176       Item *primitive = dynamic_cast<Item *> (primitives[i].grob_);
177       int prefix_set
178         = scm_to_int (primitive->get_property ("prefix-set"));
179       int context_info
180         = scm_to_int (primitive->get_property ("context-info"));
181
182       /*
183        * Get glyph_name, delta_pitch and context_info for this head.
184        */
185
186       SCM glyph_name_scm = primitive->get_property ("glyph-name");
187       if (glyph_name_scm == SCM_EOL)
188         {
189           primitive->programming_error ("Vaticana_ligature:"
190                                         "undefined glyph-name -> "
191                                         "ignoring grob");
192           continue;
193         }
194       String glyph_name = ly_scm2string (glyph_name_scm);
195
196       int delta_pitch = 0;
197       if (prev_primitive) /* urgh, need prev_primitive only here */
198         {
199           SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
200           if (delta_pitch_scm != SCM_EOL)
201             {
202               delta_pitch = scm_to_int (delta_pitch_scm);
203             }
204           else
205             {
206               primitive->programming_error ("Vaticana_ligature:"
207                                             "delta-pitch undefined -> "
208                                             "ignoring grob");
209               continue;
210             }
211         }
212
213       /*
214        * Now determine width and x-offset of head.
215        */
216
217       Real head_width;
218       Real x_offset;
219
220       if (context_info & STACKED_HEAD)
221         {
222           /*
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.
228            */
229           head_width = 0.0;
230           x_offset = join_thickness
231             - Font_interface::get_default_font (primitive)->
232             find_by_name ("noteheads." + glyph_name).extent (X_AXIS).length ();
233         }
234       else if (!String::compare (glyph_name, "flexa")
235                || !String::compare (glyph_name, ""))
236         {
237           /*
238            * This head represents either half of a flexa shape.
239            * Hence, it is assigned half the width of this shape.
240            */
241           head_width = 0.5 * flexa_width;
242           x_offset = 0.0;
243         }
244       else
245         {
246           /*
247            * This is a regular head, placed right to the previous one.
248            * Retrieve its width from corresponding font.
249            */
250           head_width
251             = Font_interface::get_default_font (primitive)->
252             find_by_name ("noteheads." + glyph_name).extent (X_AXIS).length ();
253           x_offset = 0.0;
254         }
255
256       /*
257        * Save the head's final x-offset.
258        */
259       primitive->set_property ("x-offset",
260                                scm_make_real (x_offset));
261
262       /*
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.
266        */
267       if ((context_info & PES_UPPER)
268           || ((context_info & FLEXA_RIGHT)
269               && ! (context_info & PES_LOWER)))
270         {
271           if (!prev_primitive)
272             {
273               primitive->programming_error ("vaticana ligature: add-join: "
274                                             "missing previous primitive");
275             }
276           else
277             {
278               prev_primitive->set_property ("add-join",
279                                             ly_bool2scm (true));
280
281               /*
282                * Create a small overlap of adjacent heads so that the join
283                * can be drawn perfectly between them.
284                */
285               ligature_width -= join_thickness;
286             }
287         }
288       else if (!String::compare (glyph_name, ""))
289         {
290           /*
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
294            * shape.
295            */
296         }
297
298       if (need_extra_space (prev_prefix_set, prefix_set,
299                             context_info, delta_pitch))
300         ligature_width += extra_space;
301
302       /*
303        * Horizontally line-up this head to form a ligature.
304        */
305       get_set_column (primitive, column);
306       primitive->translate_axis (ligature_width, X_AXIS);
307       ligature_width += head_width;
308
309       prev_primitive = primitive;
310       prev_prefix_set = prefix_set;
311     }
312
313   /*
314    * Add extra horizontal padding space after ligature, such that
315    * neighbouring ligatures do not touch each other.
316    */
317   ligature_width += extra_space;
318
319   return ligature_width;
320 }
321
322 /*
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.
328  *
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.
332  */
333 void
334 check_for_prefix_loss (Item *primitive)
335 {
336   int prefix_set
337     = scm_to_int (primitive->get_property ("prefix-set"));
338   if (prefix_set & ~PES_OR_FLEXA)
339     {
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",
343                               prefs.to_str0 ()));
344     }
345 }
346
347 void
348 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
349                                              Array<Grob_info> primitives)
350 {
351   Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
352
353   Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
354
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++)
361     {
362       Item *primitive = dynamic_cast<Item *> (primitives[i].grob_);
363
364       int delta_pitch;
365       SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
366       if (delta_pitch_scm != SCM_EOL)
367         {
368           delta_pitch = scm_to_int (delta_pitch_scm);
369         }
370       else
371         {
372           primitive->programming_error ("Vaticana_ligature:"
373                                         "delta-pitch undefined -> "
374                                         "ignoring grob");
375           continue;
376         }
377
378       /* retrieve & complete prefix_set and context_info */
379       int prefix_set
380         = scm_to_int (primitive->get_property ("prefix-set"));
381       int context_info
382         = scm_to_int (primitive->get_property ("context-info"));
383       if (is_stacked_head (prefix_set, context_info))
384         {
385           context_info |= STACKED_HEAD;
386           primitive->set_property ("context-info",
387                                    scm_int2num (context_info));
388         }
389
390       /*
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).
395        */
396       String glyph_name;
397       if (prefix_set & VIRGA)
398         {
399           glyph_name = "svaticana.punctum";
400           primitive->set_property ("add-stem", ly_bool2scm (true));
401         }
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";
415         else
416           glyph_name = "svaticana.inclinatum";
417       else if (prefix_set & DEMINUTUM)
418         if (i == 0)
419           {
420             // initio debilis
421             glyph_name = "svaticana.reverse.plica";
422           }
423         else if (prev_delta_pitch > 0)
424           {
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 = "svaticana.epiphonus";
430               else
431                 prev_glyph_name = "svaticana.vepiphonus";
432             glyph_name = "svaticana.plica";
433           }
434         else // (prev_delta_pitch <= 0)
435           {
436             // cephalicus
437             if (! (prev_context_info & FLEXA_RIGHT))
438               /* correct head of previous primitive */
439               {
440                 if (i > 1)
441                   {
442                     /* cephalicus head with fixed size cauda */
443                     prev_glyph_name = "svaticana.inner.cephalicus";
444                   }
445                 else
446                   {
447                     /* cephalicus head without cauda */
448                     prev_glyph_name = "svaticana.cephalicus";
449                   }
450
451                 /*
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.
455                  *
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.
460                  */
461                 prev_primitive->set_property ("add-cauda",
462                                               ly_bool2scm (false));
463               }
464             glyph_name = "svaticana.reverse.plica";
465           }
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";
471         else
472           glyph_name = "svaticana.linea.punctum";
473       else if (prefix_set & AUCTUM)
474         if (prefix_set & ASCENDENS)
475           glyph_name = "ssolesmes.auct.asc";
476         else
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";
482         else
483           glyph_name = "svaticana.vupes";
484       else
485         glyph_name = "svaticana.punctum";
486
487       /*
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.
490        */
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));
494
495       /*
496        * Execptional rule for porrectus:
497        *
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.
501        */
502       if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
503         {
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));
517         }
518
519       /*
520        * Exceptional rule for pes:
521        *
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.
526        */
527       if (prefix_set & PES_OR_FLEXA)
528         {
529           if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
530             {
531               if (!String::compare (prev_glyph_name, "svaticana.punctum"))
532                 if (prev_delta_pitch > 1)
533                   prev_glyph_name = "svaticana.lpes";
534                 else
535                   prev_glyph_name = "svaticana.vlpes";
536             }
537         }
538
539       if (prev_primitive)
540         prev_primitive->set_property ("glyph-name",
541                                       scm_makfrom0str (prev_glyph_name.to_str0 ()));
542
543       /*
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).
548        */
549       primitive->set_property ("thickness", scm_make_real (thickness));
550
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;
556     }
557
558   prev_primitive->set_property ("glyph-name",
559                                 scm_makfrom0str (prev_glyph_name.to_str0 ()));
560
561   align_heads (primitives, flexa_width, thickness);
562
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));
571   paper_column->
572     set_property ("forced-spacing", scm_make_real (ligature_width));
573 #endif
574 }
575
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",
581                 /* reads */ "",
582                 /* write */ "");