]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature-engraver.cc
(parse_symbol_list): Bugfix.
[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 #include "translator.icc"
20
21 /*
22  * This class implements the notation specific aspects of Vaticana
23  * style ligatures for Gregorian chant notation.
24  */
25
26 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
27 {
28
29 private:
30   static bool
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,
34                         int context_info);
35   Real align_heads (Array<Grob_info> primitives,
36                     Real flexa_width,
37                     Real thickness);
38
39 public:
40   TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
41
42 protected:
43   virtual Spanner *create_ligature_spanner ();
44   virtual void transform_heads (Spanner *ligature,
45                                 Array<Grob_info> primitives);
46 };
47
48 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
49 {
50 }
51
52 Spanner *
53 Vaticana_ligature_engraver::create_ligature_spanner ()
54 {
55   return make_spanner ("VaticanaLigature", SCM_EOL);
56 }
57
58 bool
59 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
60                                              int context_info)
61 {
62   bool is_stacked_b;
63
64   // upper head of pes is stacked upon lower head of pes ...
65   is_stacked_b = context_info & PES_UPPER;
66
67   // ... unless this note starts a flexa
68   if (context_info & FLEXA_LEFT)
69     is_stacked_b = false;
70
71   // ... or another pes
72   if (context_info & PES_LOWER)
73     is_stacked_b = false;
74
75   // ... or the previous note is a semivocalis or inclinatum
76   if (context_info & AFTER_DEMINUTUM)
77     is_stacked_b = false;
78
79   // auctum head is never stacked upon preceding note
80   if (prefix_set & AUCTUM)
81     is_stacked_b = false;
82
83   // virga is never stacked upon preceding note
84   if (prefix_set & VIRGA)
85     is_stacked_b = false;
86
87   // oriscus is never stacked upon preceding note
88   if (prefix_set & ORISCUS)
89     is_stacked_b = false;
90
91   if ((prefix_set & DEMINUTUM)
92       && ! (prefix_set & INCLINATUM)
93       && (context_info & FLEXA_RIGHT))
94     is_stacked_b = true; // semivocalis head of deminutus form
95
96   return is_stacked_b;
97 }
98
99 /*
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.
104  */
105 bool
106 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
107                                                          int context_info, int delta_pitch)
108 {
109   if (prev_prefix_set & VIRGA)
110     /*
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
113      * following head.
114      */
115     return true;
116
117   if ((prefix_set & INCLINATUM)
118       && ! (prev_prefix_set & INCLINATUM))
119     /*
120      * Always start a series of inclinatum heads with an extra space.
121      */
122     return true;
123
124   if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
125     /*
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.
129      */
130     return true;
131
132   if (delta_pitch == 0)
133     /*
134      * If there are two adjacent noteheads with the same pitch, add
135      * additional small space between them, such that they do not
136      * touch each other.
137      */
138     return true;
139
140   return false;
141 }
142
143 Real
144 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
145                                          Real flexa_width,
146                                          Real thickness)
147 {
148   if (!primitives.size ())
149     {
150       programming_error ("Vaticana_ligature: "
151                          "empty ligature [ignored]");
152       return 0.0;
153     }
154
155   /*
156    * The paper column where we put the whole ligature into.
157    */
158   Paper_column *column
159     = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
160
161   Real join_thickness
162     = thickness * column->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
163
164   /*
165    * Amount of extra space two put between some particular
166    * configurations of adjacent heads.
167    *
168    * TODO: make this a property of primtive grobs.
169    */
170   Real extra_space = 4.0 * join_thickness;
171
172   /*
173    * Keep track of the total width of the ligature.
174    */
175   Real ligature_width = 0.0;
176
177   Item *prev_primitive = 0;
178   int prev_prefix_set = 0;
179   for (int i = 0; i < primitives.size (); i++)
180     {
181       Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
182       int prefix_set
183         = scm_to_int (primitive->get_property ("prefix-set"));
184       int context_info
185         = scm_to_int (primitive->get_property ("context-info"));
186
187       /*
188        * Get glyph_name, delta_pitch and context_info for this head.
189        */
190
191       SCM glyph_name_scm = primitive->get_property ("glyph-name");
192       if (glyph_name_scm == SCM_EOL)
193         {
194           primitive->programming_error ("Vaticana_ligature:"
195                                         "undefined glyph-name -> "
196                                         "ignoring grob");
197           continue;
198         }
199       String glyph_name = ly_scm2string (glyph_name_scm);
200
201       int delta_pitch = 0;
202       if (prev_primitive) /* urgh, need prev_primitive only here */
203         {
204           SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
205           if (delta_pitch_scm != SCM_EOL)
206             delta_pitch = scm_to_int (delta_pitch_scm);
207           else
208             {
209               primitive->programming_error ("Vaticana_ligature:"
210                                             "delta-pitch undefined -> "
211                                             "ignoring grob");
212               continue;
213             }
214         }
215
216       /*
217        * Now determine width and x-offset of head.
218        */
219
220       Real head_width;
221       Real x_offset;
222
223       if (context_info & STACKED_HEAD)
224         {
225           /*
226            * This head is stacked upon the previous one; hence, it
227            * does not contribute to the total width of the ligature,
228            * and its width is assumed to be 0.0.  Moreover, it is
229            * shifted to the left by its width such that the right side
230            * of this and the other head are horizontally aligned.
231            */
232           head_width = 0.0;
233           x_offset = join_thickness
234             - Font_interface::get_default_font (primitive)->
235             find_by_name ("noteheads." + glyph_name).extent (X_AXIS).length ();
236         }
237       else if (!String::compare (glyph_name, "flexa")
238                || !String::compare (glyph_name, ""))
239         {
240           /*
241            * This head represents either half of a flexa shape.
242            * Hence, it is assigned half the width of this shape.
243            */
244           head_width = 0.5 * flexa_width;
245           x_offset = 0.0;
246         }
247       else
248         {
249           /*
250            * This is a regular head, placed right to the previous one.
251            * Retrieve its width from corresponding font.
252            */
253           head_width
254             = Font_interface::get_default_font (primitive)->
255             find_by_name ("noteheads." + glyph_name).extent (X_AXIS).length ();
256           x_offset = 0.0;
257         }
258
259       /*
260        * Save the head's final x-offset.
261        */
262       primitive->set_property ("x-offset",
263                                scm_from_double (x_offset));
264
265       /*
266        * If the head is the 2nd head of a pes or flexa (but not a
267        * flexa shape), mark this head to be joined with the left-side
268        * neighbour head (i.e. the previous head) by a vertical beam.
269        */
270       if ((context_info & PES_UPPER)
271           || ((context_info & FLEXA_RIGHT)
272               && ! (context_info & PES_LOWER)))
273         {
274           if (!prev_primitive)
275             {
276               primitive->programming_error ("vaticana ligature: add-join: "
277                                             "missing previous primitive");
278             }
279           else
280             {
281               prev_primitive->set_property ("add-join",
282                                             ly_bool2scm (true));
283
284               /*
285                * Create a small overlap of adjacent heads so that the join
286                * can be drawn perfectly between them.
287                */
288               ligature_width -= join_thickness;
289             }
290         }
291       else if (!String::compare (glyph_name, ""))
292         {
293           /*
294            * This is the 2nd (virtual) head of flexa shape.  Join it
295            * tightly with 1st head, i.e. do *not* add additional
296            * space, such that next head will not be off from the flexa
297            * shape.
298            */
299         }
300
301       if (need_extra_horizontal_space (prev_prefix_set, prefix_set,
302                                        context_info, delta_pitch))
303         ligature_width += extra_space;
304
305       /*
306        * Horizontally line-up this head to form a ligature.
307        */
308       get_set_column (primitive, column);
309       primitive->translate_axis (ligature_width, X_AXIS);
310       ligature_width += head_width;
311
312       prev_primitive = primitive;
313       prev_prefix_set = prefix_set;
314     }
315
316   /*
317    * Add extra horizontal padding space after ligature, such that
318    * neighbouring ligatures do not touch each other.
319    */
320   ligature_width += extra_space;
321
322   return ligature_width;
323 }
324
325 /*
326  * Depending on the typographical features of a particular ligature
327  * style, some prefixes may be ignored.  In particular, if a curved
328  * flexa shape is produced, any prefixes to either of the two
329  * contributing heads that would select a head other than punctum, is
330  * by definition ignored.
331  *
332  * This function prints a warning, if the given primitive is prefixed
333  * such that a head other than punctum would be chosen, if this
334  * primitive were engraved as a stand-alone head.
335  */
336 void
337 check_for_prefix_loss (Item *primitive)
338 {
339   int prefix_set
340     = scm_to_int (primitive->get_property ("prefix-set"));
341   if (prefix_set & ~PES_OR_FLEXA)
342     {
343       String prefs = Gregorian_ligature::prefixes_to_str (primitive);
344       primitive->warning (_f ("ignored prefix (es) `%s' of this head according "
345                               "to restrictions of the selected ligature style",
346                               prefs.to_str0 ()));
347     }
348 }
349
350 void
351 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
352                                              Array<Grob_info> primitives)
353 {
354   Real flexa_width = robust_scm2double (ligature->get_property ("flexa-width"), 2);
355
356   Real thickness = robust_scm2double (ligature->get_property ("thickness"), 1);
357
358   Item *prev_primitive = 0;
359   int prev_prefix_set = 0;
360   int prev_context_info = 0;
361   int prev_delta_pitch = 0;
362   String prev_glyph_name = "";
363   for (int i = 0; i < primitives.size (); i++)
364     {
365       Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
366
367       int delta_pitch;
368       SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
369       if (delta_pitch_scm != SCM_EOL)
370         delta_pitch = scm_to_int (delta_pitch_scm);
371       else
372         {
373           primitive->programming_error ("Vaticana_ligature:"
374                                         "delta-pitch undefined -> "
375                                         "ignoring grob");
376           continue;
377         }
378
379       /* retrieve & complete prefix_set and context_info */
380       int prefix_set
381         = scm_to_int (primitive->get_property ("prefix-set"));
382       int context_info
383         = scm_to_int (primitive->get_property ("context-info"));
384       if (is_stacked_head (prefix_set, context_info))
385         {
386           context_info |= STACKED_HEAD;
387           primitive->set_property ("context-info",
388                                    scm_from_int (context_info));
389         }
390
391       /*
392        * Now determine which head to typeset (this is context sensitive
393        * information, since it depends on neighbouring heads; therefore,
394        * this decision must be made here in the engraver rather than in
395        * the backend).
396        */
397       String glyph_name;
398       if (prefix_set & VIRGA)
399         {
400           glyph_name = "svaticana.punctum";
401           primitive->set_property ("add-stem", ly_bool2scm (true));
402         }
403       else if (prefix_set & QUILISMA)
404         glyph_name = "svaticana.quilisma";
405       else if (prefix_set & ORISCUS)
406         glyph_name = "ssolesmes.oriscus";
407       else if (prefix_set & STROPHA)
408         if (prefix_set & AUCTUM)
409           glyph_name = "ssolesmes.stropha.aucta";
410         else glyph_name = "ssolesmes.stropha";
411       else if (prefix_set & INCLINATUM)
412         if (prefix_set & AUCTUM)
413           glyph_name = "ssolesmes.incl.auctum";
414         else if (prefix_set & DEMINUTUM)
415           glyph_name = "ssolesmes.incl.parvum";
416         else
417           glyph_name = "svaticana.inclinatum";
418       else if (prefix_set & DEMINUTUM)
419         if (i == 0)
420           {
421             // initio debilis
422             glyph_name = "svaticana.reverse.plica";
423           }
424         else if (prev_delta_pitch > 0)
425           {
426             // epiphonus
427             if (! (prev_context_info & FLEXA_RIGHT))
428               /* correct head of previous primitive */
429               if (prev_delta_pitch > 1)
430                 {
431                   prev_glyph_name = "svaticana.epiphonus";
432                   glyph_name = "svaticana.plica";
433                 }
434               else
435                 {
436                   prev_glyph_name = "svaticana.vepiphonus";
437                   glyph_name = "svaticana.vplica";
438                 }
439           }
440         else if (prev_delta_pitch < 0)
441           {
442             // cephalicus
443             if (! (prev_context_info & FLEXA_RIGHT))
444               /* correct head of previous primitive */
445               {
446                 if (i > 1)
447                   {
448                     /* cephalicus head with fixed size cauda */
449                     prev_glyph_name = "svaticana.inner.cephalicus";
450                   }
451                 else
452                   {
453                     /* cephalicus head without cauda */
454                     prev_glyph_name = "svaticana.cephalicus";
455                   }
456
457                 /*
458                  * Flexa has no variable size cauda if its left head is
459                  * stacked on the right head.  This is true for
460                  * cephalicus.  Hence, remove the cauda.
461                  *
462                  * Urgh: for the current implementation, this rule only
463                  * applies for cephalicus; but it is a fundamental rule.
464                  * Therefore, the following line of code should be
465                  * placed somewhere else.
466                  */
467                 prev_primitive->set_property ("add-cauda",
468                                               ly_bool2scm (false));
469               }
470             if (prev_delta_pitch < - 1)
471               {
472                 glyph_name = "svaticana.reverse.plica";
473               }
474             else
475               {
476                 glyph_name = "svaticana.reverse.vplica";
477               }
478           }
479         else // (prev_delta_pitch == 0)
480           {
481             primitive->programming_error ("Vaticana_ligature:"
482                                           "deminutum head must have different "
483                                           "pitch -> ignoring grob");
484           }
485       else if (prefix_set & (CAVUM | LINEA))
486         if ((prefix_set & CAVUM) && (prefix_set & LINEA))
487           glyph_name = "svaticana.linea.punctum.cavum";
488         else if (prefix_set & CAVUM)
489           glyph_name = "svaticana.punctum.cavum";
490         else
491           glyph_name = "svaticana.linea.punctum";
492       else if (prefix_set & AUCTUM)
493         if (prefix_set & ASCENDENS)
494           glyph_name = "ssolesmes.auct.asc";
495         else
496           glyph_name = "ssolesmes.auct.desc";
497       else if ((context_info & STACKED_HEAD)
498                && (context_info & PES_UPPER))
499         if (prev_delta_pitch > 1)
500           glyph_name = "svaticana.upes";
501         else
502           glyph_name = "svaticana.vupes";
503       else
504         glyph_name = "svaticana.punctum";
505
506       /*
507        * This head needs a cauda, if it starts a flexa, is not the upper
508        * head of a pes, and if it is a punctum.
509        */
510       if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
511         if (!String::compare (glyph_name, "svaticana.punctum"))
512           primitive->set_property ("add-cauda", ly_bool2scm (true));
513
514       /*
515        * Execptional rule for porrectus:
516        *
517        * If the current head is preceded by a \flexa and succeded by a
518        * \pes (e.g. "a \flexa g \pes a"), then join the current head and
519        * the previous head into a single curved flexa shape.
520        */
521       if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
522         {
523           check_for_prefix_loss (prev_primitive);
524           prev_glyph_name = "flexa";
525           prev_primitive->set_property ("flexa-height",
526                                         scm_from_int (prev_delta_pitch));
527           prev_primitive->set_property ("flexa-width",
528                                         scm_from_double (flexa_width));
529           bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
530           prev_primitive->set_property ("add-cauda",
531                                         ly_bool2scm (add_cauda));
532           check_for_prefix_loss (primitive);
533           glyph_name = "";
534           primitive->set_property ("flexa-width",
535                                    scm_from_double (flexa_width));
536         }
537
538       /*
539        * Exceptional rule for pes:
540        *
541        * If this head is stacked on the previous one due to a \pes, then
542        * set the glyph of the previous head to that for this special
543        * case, thereby avoiding potential vertical collision with the
544        * current head.
545        */
546       if (prefix_set & PES_OR_FLEXA)
547         {
548           if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
549             {
550               if (!String::compare (prev_glyph_name, "svaticana.punctum"))
551                 if (prev_delta_pitch > 1)
552                   prev_glyph_name = "svaticana.lpes";
553                 else
554                   prev_glyph_name = "svaticana.vlpes";
555             }
556         }
557
558       if (prev_primitive)
559         prev_primitive->set_property ("glyph-name",
560                                       scm_makfrom0str (prev_glyph_name.to_str0 ()));
561
562       /*
563        * In the backend, flexa shapes and joins need to know about line
564        * thickness.  Hence, for simplicity, let's distribute the
565        * ligature grob's value for thickness to each ligature head (even
566        * if not all of them need to know).
567        */
568       primitive->set_property ("thickness", scm_from_double (thickness));
569
570       prev_primitive = primitive;
571       prev_prefix_set = prefix_set;
572       prev_context_info = context_info;
573       prev_delta_pitch = delta_pitch;
574       prev_glyph_name = glyph_name;
575     }
576
577   prev_primitive->set_property ("glyph-name",
578                                 scm_makfrom0str (prev_glyph_name.to_str0 ()));
579
580   align_heads (primitives, flexa_width, thickness);
581
582 #if 0 // experimental code to collapse spacing after ligature
583   /* TODO: set to max (old/new spacing-increment), since other
584      voices/staves also may want to set this property. */
585   Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
586   Paper_column *paper_column = first_primitive->get_column ();
587   paper_column->warning (_f ("Vaticana_ligature_engraver: "
588                              "setting `spacing-increment = %f': ptr =%ul",
589                              ligature_width, paper_column));
590   paper_column->
591     set_property ("forced-spacing", scm_from_double (ligature_width));
592 #endif
593 }
594
595 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, rest);
596 ADD_ACKNOWLEDGER (Vaticana_ligature_engraver, note_head);
597 ADD_TRANSLATOR (Vaticana_ligature_engraver,
598                 /* doc */ "Handles ligatures by glueing special ligature heads together.",
599                 /* create */ "VaticanaLigature",
600                 /* accept */ "ligature-event",
601                 /* read */ "",
602                 /* write */ "");