]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature-engraver.cc
new file, move from
[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 "item.hh"
13 #include "spanner.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "font-interface.hh"
16 #include "warn.hh"
17 #include "output-def.hh"
18 #include "paper-column.hh"
19
20 /*
21  * This class implements the notation specific aspects of Vaticana
22  * style ligatures for Gregorian chant notation.
23  */
24
25 class Vaticana_ligature_engraver : public Gregorian_ligature_engraver
26 {
27
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);
34
35 public:
36   TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
37
38 protected:
39   virtual Spanner *create_ligature_spanner ();
40   virtual void transform_heads (Spanner *ligature,
41                                 Array<Grob_info> primitives);
42 };
43
44 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
45 {
46 }
47
48 Spanner *
49 Vaticana_ligature_engraver::create_ligature_spanner ()
50 {
51   return make_spanner ("VaticanaLigature", SCM_EOL);
52 }
53
54 bool
55 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
56                                              int context_info)
57 {
58       bool is_stacked_b;
59
60       // upper head of pes is stacked upon lower head of pes ...
61       is_stacked_b = context_info & PES_UPPER;
62
63       // ... unless this note starts a flexa
64       if (context_info & FLEXA_LEFT)
65         is_stacked_b = false;
66
67       // ... or another pes
68       if (context_info & PES_LOWER)
69         is_stacked_b = false;
70
71       // ... or the previous note is a semivocalis or inclinatum
72       if (context_info & AFTER_DEMINUTUM)
73         is_stacked_b = false;
74
75       // auctum head is never stacked upon preceding note
76       if (prefix_set & AUCTUM)
77         is_stacked_b = false;
78
79       // virga is never stacked upon preceding note
80       if (prefix_set & VIRGA)
81         is_stacked_b = false;
82
83       // oriscus is never stacked upon preceding note
84       if (prefix_set & ORISCUS)
85         is_stacked_b = false;
86
87       if ((prefix_set & DEMINUTUM) &&
88           !(prefix_set & INCLINATUM) &&
89           (context_info & FLEXA_RIGHT))
90         is_stacked_b = true; // semivocalis head of deminutus form
91
92       return is_stacked_b;
93 }
94
95 /*
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.
100  */
101 bool
102 need_extra_space (int prev_prefix_set, int prefix_set,
103                   int context_info, int delta_pitch)
104 {
105   if (prev_prefix_set & VIRGA)
106     /*
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.
110      */
111     return true;
112
113   if ((prefix_set & INCLINATUM) &&
114            !(prev_prefix_set & INCLINATUM))
115     /*
116      * Always start a series of inclinatum heads with an extra space.
117      */
118     return true;
119
120   if ((context_info & FLEXA_LEFT) && !(context_info & PES_UPPER))
121     /*
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.
125      */
126     return true;
127
128   if (delta_pitch == 0)
129     /*
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.
133      */
134     return true;
135
136   return false;
137 }
138
139 Real
140 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
141                                          Real flexa_width,
142                                          Real thickness)
143 {
144   if (!primitives.size ())
145     {
146       programming_error ("Vaticana_ligature: "
147                          "empty ligature [ignored]");
148       return 0.0;
149     }
150
151   /*
152    * The paper column where we put the whole ligature into.
153    */
154   Paper_column *column =
155     dynamic_cast<Item*> (primitives[0].grob_)->get_column ();
156
157   Real join_thickness =
158     thickness * column->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
159
160   /*
161    * Amount of extra space two put between some particular
162    * configurations of adjacent heads.
163    *
164    * TODO: make this a property of primtive grobs.
165    */
166   Real extra_space = 4.0 * join_thickness;
167
168   /*
169    * Keep track of the total width of the ligature.
170    */
171   Real ligature_width = 0.0;
172
173   Item *prev_primitive = 0;
174   int prev_prefix_set = 0;
175   for (int i = 0; i < primitives.size (); i++)
176     {
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"));
182
183       /*
184        * Get glyph_name, delta_pitch and context_info for this head.
185        */
186
187       SCM glyph_name_scm = primitive->get_property ("glyph-name");
188       if (glyph_name_scm == SCM_EOL)
189         {
190           primitive->programming_error ("Vaticana_ligature:"
191                                         "undefined glyph-name -> "
192                                         "ignoring grob");
193           continue;
194         }
195       String glyph_name = ly_scm2string (glyph_name_scm);
196
197       int delta_pitch = 0;
198       if (prev_primitive) /* urgh, need prev_primitive only here */
199         {
200           SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
201           if (delta_pitch_scm != SCM_EOL)
202             {
203               delta_pitch = ly_scm2int (delta_pitch_scm);
204             }
205           else
206             {
207               primitive->programming_error ("Vaticana_ligature:"
208                                             "delta-pitch undefined -> "
209                                             "ignoring grob");
210               continue;
211             }
212         }
213
214       /*
215        * Now determine width and x-offset of head.
216        */
217
218       Real head_width;
219       Real x_offset;
220
221       if (context_info & STACKED_HEAD)
222         {
223           /*
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.
229            */
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 ();
234         }
235       else if (!String::compare (glyph_name, "flexa") ||
236                !String::compare (glyph_name, ""))
237         {
238           /*
239            * This head represents either half of a flexa shape.
240            * Hence, it is assigned half the width of this shape.
241            */
242           head_width = 0.5 * flexa_width;
243           x_offset = 0.0;
244         }
245       else
246         {
247           /*
248            * This is a regular head, placed right to the previous one.
249            * Retrieve its width from corresponding font.
250            */
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;
255         }
256
257       /*
258        * Save the head's final x-offset.
259        */
260       primitive->set_property ("x-offset",
261                                     scm_make_real (x_offset));
262
263       /*
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.
267        */
268       if ((context_info & PES_UPPER) ||
269           ((context_info & FLEXA_RIGHT) &&
270            !(context_info & PES_LOWER)))
271         {
272           if (!prev_primitive)
273             {
274               primitive->programming_error ("vaticana ligature: add-join: "
275                                             "missing previous primitive");
276             }
277           else
278             {
279               prev_primitive->set_property ("add-join",
280                                                  ly_bool2scm (true));
281
282               /*
283                * Create a small overlap of adjacent heads so that the join
284                * can be drawn perfectly between them.
285                */
286               ligature_width -= join_thickness;
287             }
288         }
289       else if (!String::compare (glyph_name, ""))
290         {
291           /*
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.
296            */
297         }
298
299       if (need_extra_space (prev_prefix_set, prefix_set,
300                             context_info, delta_pitch))
301         ligature_width += extra_space;
302
303       /*
304        * Horizontally line-up this head to form a ligature.
305        */
306       get_set_column (primitive, column);
307       primitive->translate_axis (ligature_width, X_AXIS);
308       ligature_width += head_width;
309
310       prev_primitive = primitive;
311       prev_prefix_set = prefix_set;
312     }
313
314   /*
315    * Add extra horizontal padding space after ligature, such that
316    * neighbouring ligatures do not touch each other.
317    */
318   ligature_width += extra_space;
319
320   return ligature_width;
321 }
322
323 /*
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.
329  *
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.
333  */
334 void
335 check_for_prefix_loss (Item *primitive)
336 {
337   int prefix_set =
338     ly_scm2int (primitive->get_property ("prefix-set"));
339   if (prefix_set & ~PES_OR_FLEXA)
340     {
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 ()));
345     }
346 }
347
348 void
349 Vaticana_ligature_engraver::transform_heads (Spanner *ligature,
350                                              Array<Grob_info> primitives)
351 {
352   Real flexa_width= robust_scm2double ( ligature->get_property ("flexa-width"), 2);
353
354   Real thickness= robust_scm2double ( ligature->get_property ("thickness"), 1);
355
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_);
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 = ly_scm2int (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       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))
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 = "vaticana_punctum";
400         primitive->set_property ("add-stem", ly_bool2scm (true));
401       }
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)
419         {
420           // initio debilis
421           glyph_name = "vaticana_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 = "vaticana_epiphonus";
430             else
431               prev_glyph_name = "vaticana_vepiphonus";
432           glyph_name = "vaticana_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 = "vaticana_inner_cephalicus";
444                 }
445               else
446                 {
447                   /* cephalicus head without cauda */
448                   prev_glyph_name = "vaticana_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 = "vaticana_reverse_plica";
465         }
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";
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, "vaticana_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, "vaticana_punctum"))
532               if (prev_delta_pitch > 1)
533                 prev_glyph_name = "vaticana_lpes";
534               else
535                 prev_glyph_name = "vaticana_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 #if 0
562   Real ligature_width =
563 #endif
564
565   align_heads (primitives, flexa_width, thickness);
566
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
578 }
579
580
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 */       "");