]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature-engraver.cc
*** empty log message ***
[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 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     Item *primitive = dynamic_cast<Item*> (primitives[i].grob_);
362
363     int delta_pitch;
364     SCM delta_pitch_scm = primitive->get_property ("delta-pitch");
365     if (delta_pitch_scm != SCM_EOL)
366       {
367         delta_pitch = scm_to_int (delta_pitch_scm);
368       }
369     else
370       {
371         primitive->programming_error ("Vaticana_ligature:"
372                                       "delta-pitch undefined -> "
373                                       "ignoring grob");
374         continue;
375       }
376
377     /* retrieve & complete prefix_set and context_info */
378     int prefix_set =
379       scm_to_int (primitive->get_property ("prefix-set"));
380     int context_info =
381       scm_to_int (primitive->get_property ("context-info"));
382     if (is_stacked_head (prefix_set, context_info))
383       {
384         context_info |= STACKED_HEAD;
385         primitive->set_property ("context-info",
386                                       scm_int2num (context_info));
387       }
388
389     /*
390      * Now determine which head to typeset (this is context sensitive
391      * information, since it depends on neighbouring heads; therefore,
392      * this decision must be made here in the engraver rather than in
393      * the backend).
394      */
395     String glyph_name;
396     if (prefix_set & VIRGA)
397       {
398         glyph_name = "vaticana.punctum";
399         primitive->set_property ("add.stem", ly_bool2scm (true));
400       }
401     else if (prefix_set & QUILISMA)
402       glyph_name = "vaticana.quilisma";
403     else if (prefix_set & ORISCUS)
404       glyph_name = "solesmes.oriscus";
405     else if (prefix_set & STROPHA)
406       if (prefix_set & AUCTUM)
407         glyph_name = "solesmes.stropha.aucta";
408       else glyph_name = "solesmes.stropha";
409     else if (prefix_set & INCLINATUM)
410       if (prefix_set & AUCTUM)
411         glyph_name = "solesmes.incl.auctum";
412       else if (prefix_set & DEMINUTUM)
413         glyph_name = "solesmes.incl.parvum";
414       else
415         glyph_name = "vaticana.inclinatum";
416     else if (prefix_set & DEMINUTUM)
417       if (i == 0)
418         {
419           // initio debilis
420           glyph_name = "vaticana.reverse.plica";
421         }
422       else if (prev_delta_pitch > 0)
423         {
424           // epiphonus
425           if (!(prev_context_info & FLEXA_RIGHT))
426             /* correct head of previous primitive */
427             if (prev_delta_pitch > 1)
428               prev_glyph_name = "vaticana.epiphonus";
429             else
430               prev_glyph_name = "vaticana.vepiphonus";
431           glyph_name = "vaticana.plica";
432         }
433       else // (prev_delta_pitch <= 0)
434         {
435           // cephalicus
436           if (!(prev_context_info & FLEXA_RIGHT))
437             /* correct head of previous primitive */
438             {
439               if (i > 1)
440                 {
441                   /* cephalicus head with fixed size cauda */
442                   prev_glyph_name = "vaticana.inner.cephalicus";
443                 }
444               else
445                 {
446                   /* cephalicus head without cauda */
447                   prev_glyph_name = "vaticana.cephalicus";
448                 }
449
450               /*
451                * Flexa has no variable size cauda if its left head is
452                * stacked on the right head.  This is true for
453                * cephalicus.  Hence, remove the cauda.
454                *
455                * Urgh: for the current implementation, this rule only
456                * applies for cephalicus; but it is a fundamental rule.
457                * Therefore, the following line of code should be
458                * placed somewhere else.
459                */
460               prev_primitive->set_property ("add-cauda",
461                                                  ly_bool2scm (false));
462             }
463           glyph_name = "vaticana.reverse.plica";
464         }
465     else if (prefix_set & (CAVUM | LINEA))
466       if ((prefix_set & CAVUM) && (prefix_set & LINEA))
467         glyph_name = "vaticana.linea.punctum.cavum";
468       else if (prefix_set & CAVUM)
469         glyph_name = "vaticana.punctum.cavum";
470       else
471         glyph_name = "vaticana.linea.punctum";
472     else if (prefix_set & AUCTUM)
473       if (prefix_set & ASCENDENS)
474         glyph_name = "solesmes.auct.asc";
475       else
476         glyph_name = "solesmes.auct.desc";
477     else if ((context_info & STACKED_HEAD) &&
478              (context_info & PES_UPPER))
479       if (prev_delta_pitch > 1)
480         glyph_name = "vaticana.upes";
481       else
482         glyph_name = "vaticana.vupes";
483     else
484       glyph_name = "vaticana.punctum";
485
486     /*
487      * This head needs a cauda, if it starts a flexa, is not the upper
488      * head of a pes, and if it is a punctum.
489      */
490     if ((context_info & FLEXA_LEFT) && !(context_info & PES_UPPER))
491       if (!String::compare (glyph_name, "vaticana.punctum"))
492         primitive->set_property ("add-cauda", ly_bool2scm (true));
493
494     /*
495      * Execptional rule for porrectus:
496      *
497      * If the current head is preceded by a \flexa and succeded by a
498      * \pes (e.g. "a \flexa g \pes a"), then join the current head and
499      * the previous head into a single curved flexa shape.
500      */
501     if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
502       {
503         check_for_prefix_loss (prev_primitive);
504         prev_glyph_name = "flexa";
505         prev_primitive->set_property ("flexa-height",
506                                            scm_int2num (prev_delta_pitch));
507         prev_primitive->set_property ("flexa-width",
508                                            scm_make_real (flexa_width));
509         bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
510         prev_primitive->set_property ("add-cauda",
511                                            ly_bool2scm (add_cauda));
512         check_for_prefix_loss (primitive);
513         glyph_name = "";
514         primitive->set_property ("flexa-width",
515                                       scm_make_real (flexa_width));
516       }
517
518     /*
519      * Exceptional rule for pes:
520      *
521      * If this head is stacked on the previous one due to a \pes, then
522      * set the glyph of the previous head to that for this special
523      * case, thereby avoiding potential vertical collision with the
524      * current head.
525      */
526     if (prefix_set & PES_OR_FLEXA)
527       {
528         if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
529           {
530             if (!String::compare (prev_glyph_name, "vaticana.punctum"))
531               if (prev_delta_pitch > 1)
532                 prev_glyph_name = "vaticana.lpes";
533               else
534                 prev_glyph_name = "vaticana.vlpes";
535           }
536       }
537
538     if (prev_primitive)
539       prev_primitive->set_property ("glyph-name",
540                                     scm_makfrom0str (prev_glyph_name.to_str0 ()));
541
542     /*
543      * In the backend, flexa shapes and joins need to know about line
544      * thickness.  Hence, for simplicity, let's distribute the
545      * ligature grob's value for thickness to each ligature head (even
546      * if not all of them need to know).
547      */
548     primitive->set_property ("thickness", scm_make_real (thickness));
549
550     prev_primitive = primitive;
551     prev_prefix_set = prefix_set;
552     prev_context_info = context_info;
553     prev_delta_pitch = delta_pitch;
554     prev_glyph_name = glyph_name;
555   }
556
557   prev_primitive->set_property ("glyph-name",
558                                 scm_makfrom0str (prev_glyph_name.to_str0 ()));
559
560 #if 0
561   Real ligature_width =
562 #endif
563
564   align_heads (primitives, flexa_width, thickness);
565
566 #if 0 // experimental code to collapse spacing after ligature
567   /* TODO: set to max (old/new spacing-increment), since other
568      voices/staves also may want to set this property. */
569   Item *first_primitive = dynamic_cast<Item*> (primitives[0].grob_);
570   Paper_column *paper_column = first_primitive->get_column ();
571   paper_column->warning (_f ("Vaticana_ligature_engraver: "
572                              "setting `spacing-increment = %f': ptr =%ul",
573                              ligature_width, paper_column));
574   paper_column->
575     set_property ("forced-spacing", scm_make_real (ligature_width));
576 #endif
577 }
578
579
580 ADD_TRANSLATOR (Vaticana_ligature_engraver,
581 /* descr */       "Handles ligatures by glueing special ligature heads together.",
582 /* creats*/       "VaticanaLigature",
583 /* accepts */     "ligature-event",
584 /* acks  */      "note-head-interface rest-interface",
585 /* reads */       "",
586 /* write */       "");