]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature-engraver.cc
* lily/vaticana-ligature-engraver.cc: bugfix: fixed programming
[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 "paper-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 new Spanner (get_property ("VaticanaLigature"));
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_realvar (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         gh_scm2int (primitive->get_grob_property ("prefix-set"));
180       int context_info =
181         gh_scm2int (primitive->get_grob_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_grob_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_grob_property ("delta-pitch");
201           if (delta_pitch_scm != SCM_EOL)
202             {
203               delta_pitch = gh_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_grob_property ("x-offset",
261                                     gh_double2scm (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_grob_property ("add-join",
280                                                  gh_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     gh_scm2int (primitive->get_grob_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;
353   SCM flexa_width_scm = ligature->get_grob_property ("flexa-width");
354   if (flexa_width_scm != SCM_EOL)
355     {
356       flexa_width = gh_scm2double (flexa_width_scm);
357     }
358   else
359     {
360       ligature->programming_error ("Vaticana_ligature_engraver:"
361                                    "flexa-width undefined; "
362                                    "assuming 2.0 staff space");
363       flexa_width =
364         2.0 * Staff_symbol_referencer::staff_space (ligature);
365     }
366
367   Real thickness;
368   SCM thickness_scm = ligature->get_grob_property ("thickness");
369   if (thickness_scm != SCM_EOL)
370     {
371       thickness = gh_scm2double (thickness_scm);
372     }
373   else
374     {
375       ligature->programming_error ("Vaticana_ligature_engraver:"
376                                    "thickness undefined; assuming 1.0");
377       thickness = 1.0;
378     }
379
380   Item *prev_primitive = 0;
381   int prev_prefix_set = 0;
382   int prev_context_info = 0;
383   int prev_delta_pitch = 0;
384   String prev_glyph_name = "";
385   for (int i = 0; i < primitives.size(); i++) {
386     Item *primitive = dynamic_cast<Item*> (primitives[i].grob_);
387
388     int delta_pitch;
389     SCM delta_pitch_scm = primitive->get_grob_property ("delta-pitch");
390     if (delta_pitch_scm != SCM_EOL)
391       {
392         delta_pitch = gh_scm2int (delta_pitch_scm);
393       }
394     else
395       {
396         primitive->programming_error ("Vaticana_ligature:"
397                                       "delta-pitch undefined -> "
398                                       "ignoring grob");
399         continue;
400       }
401
402     /* retrieve & complete prefix_set and context_info */
403     int prefix_set =
404       gh_scm2int (primitive->get_grob_property ("prefix-set"));
405     int context_info =
406       gh_scm2int (primitive->get_grob_property ("context-info"));
407     if (is_stacked_head (prefix_set, context_info))
408       {
409         context_info |= STACKED_HEAD;
410         primitive->set_grob_property ("context-info",
411                                       gh_int2scm (context_info));
412       }
413
414     /*
415      * Now determine which head to typeset (this is context sensitive
416      * information, since it depends on neighbouring heads; therefore,
417      * this decision must be made here in the engraver rather than in
418      * the backend).
419      */
420     String glyph_name;
421     if (prefix_set & VIRGA)
422       {
423         glyph_name = "vaticana_punctum";
424         primitive->set_grob_property ("add-stem", gh_bool2scm(true));
425       }
426     else if (prefix_set & QUILISMA)
427       glyph_name = "vaticana_quilisma";
428     else if (prefix_set & ORISCUS)
429       glyph_name = "solesmes_oriscus";
430     else if (prefix_set & STROPHA)
431       if (prefix_set & AUCTUM)
432         glyph_name = "solesmes_stropha_aucta";
433       else glyph_name = "solesmes_stropha";
434     else if (prefix_set & INCLINATUM)
435       if (prefix_set & AUCTUM)
436         glyph_name = "solesmes_incl_auctum";
437       else if (prefix_set & DEMINUTUM)
438         glyph_name = "solesmes_incl_parvum";
439       else
440         glyph_name = "vaticana_inclinatum";
441     else if (prefix_set & DEMINUTUM)
442       if (i == 0)
443         {
444           // initio debilis
445           glyph_name = "vaticana_reverse_plica";
446         }
447       else if (prev_delta_pitch > 0)
448         {
449           // epiphonus
450           if (!(prev_context_info & FLEXA_RIGHT))
451             /* correct head of previous primitive */
452             if (prev_delta_pitch > 1)
453               prev_glyph_name = "vaticana_epiphonus";
454             else
455               prev_glyph_name = "vaticana_vepiphonus";
456           glyph_name = "vaticana_plica";
457         }
458       else // (prev_delta_pitch <= 0)
459         {
460           // cephalicus
461           if (!(prev_context_info & FLEXA_RIGHT))
462             /* correct head of previous primitive */
463             {
464               if (i > 1)
465                 {
466                   /* cephalicus head with fixed size cauda */
467                   prev_glyph_name = "vaticana_inner_cephalicus";
468                 }
469               else
470                 {
471                   /* cephalicus head without cauda */
472                   prev_glyph_name = "vaticana_cephalicus";
473                 }
474
475               /*
476                * Flexa has no variable size cauda if its left head is
477                * stacked on the right head.  This is true for
478                * cephalicus.  Hence, remove the cauda.
479                *
480                * Urgh: for the current implementation, this rule only
481                * applies for cephalicus; but it is a fundamental rule.
482                * Therefore, the following line of code should be
483                * placed somewhere else.
484                */
485               prev_primitive->set_grob_property ("add-cauda",
486                                                  gh_bool2scm(false));
487             }
488           glyph_name = "vaticana_reverse_plica";
489         }
490     else if (prefix_set & (CAVUM | LINEA))
491       if ((prefix_set & CAVUM) && (prefix_set & LINEA))
492         glyph_name = "vaticana_linea_punctum_cavum";
493       else if (prefix_set & CAVUM)
494         glyph_name = "vaticana_punctum_cavum";
495       else
496         glyph_name = "vaticana_linea_punctum";
497     else if (prefix_set & AUCTUM)
498       if (prefix_set & ASCENDENS)
499         glyph_name = "solesmes_auct_asc";
500       else
501         glyph_name = "solesmes_auct_desc";
502     else if ((context_info & STACKED_HEAD) &&
503              (context_info & PES_UPPER))
504       if (prev_delta_pitch > 1)
505         glyph_name = "vaticana_upes";
506       else
507         glyph_name = "vaticana_vupes";
508     else
509       glyph_name = "vaticana_punctum";
510
511     /*
512      * This head needs a cauda, if it starts a flexa, is not the upper
513      * head of a pes, and if it is a punctum.
514      */
515     if ((context_info & FLEXA_LEFT) && !(context_info & PES_UPPER))
516       if (!String::compare (glyph_name, "vaticana_punctum"))
517         primitive->set_grob_property ("add-cauda", gh_bool2scm(true));
518
519     /*
520      * Execptional rule for porrectus:
521      *
522      * If the current head is preceded by a \flexa and succeded by a
523      * \pes (e.g. "a \flexa g \pes a"), then join the current head and
524      * the previous head into a single curved flexa shape.
525      */
526     if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
527       {
528         check_for_prefix_loss (prev_primitive);
529         prev_glyph_name = "flexa";
530         prev_primitive->set_grob_property ("flexa-height",
531                                            gh_int2scm (prev_delta_pitch));
532         prev_primitive->set_grob_property ("flexa-width",
533                                            gh_double2scm (flexa_width));
534         bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
535         prev_primitive->set_grob_property ("add-cauda",
536                                            gh_bool2scm (add_cauda));
537         check_for_prefix_loss (primitive);
538         glyph_name = "";
539         primitive->set_grob_property ("flexa-width",
540                                       gh_double2scm (flexa_width));
541       }
542
543     /*
544      * Exceptional rule for pes:
545      *
546      * If this head is stacked on the previous one due to a \pes, then
547      * set the glyph of the previous head to that for this special
548      * case, thereby avoiding potential vertical collision with the
549      * current head.
550      */
551     if (prefix_set & PES_OR_FLEXA)
552       {
553         if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
554           {
555             if (!String::compare (prev_glyph_name, "vaticana_punctum"))
556               if (prev_delta_pitch > 1)
557                 prev_glyph_name = "vaticana_lpes";
558               else
559                 prev_glyph_name = "vaticana_vlpes";
560           }
561       }
562
563     if (prev_primitive)
564       prev_primitive->set_grob_property ("glyph-name",
565                                          scm_makfrom0str (prev_glyph_name.to_str0 ()));
566
567     /*
568      * In the backend, flexa shapes and joins need to know about line
569      * thickness.  Hence, for simplicity, let's distribute the
570      * ligature grob's value for thickness to each ligature head (even
571      * if not all of them need to know).
572      */
573     primitive->set_grob_property ("thickness", gh_double2scm (thickness));
574
575     prev_primitive = primitive;
576     prev_prefix_set = prefix_set;
577     prev_context_info = context_info;
578     prev_delta_pitch = delta_pitch;
579     prev_glyph_name = glyph_name;
580   }
581
582   prev_primitive->set_grob_property ("glyph-name",
583                                      scm_makfrom0str (prev_glyph_name.to_str0 ()));
584
585 #if 0
586   Real ligature_width =
587 #endif
588
589   align_heads (primitives, flexa_width, thickness);
590
591 #if 0 // experimental code to collapse spacing after ligature
592   /* TODO: set to max(old/new spacing-increment), since other
593      voices/staves also may want to set this property. */
594   Item *first_primitive = dynamic_cast<Item*> (primitives[0].grob_);
595   Paper_column *paper_column = first_primitive->get_column();
596   paper_column->warning (_f ("Vaticana_ligature_engraver: "
597                              "setting `spacing-increment = %f': ptr=%ul",
598                              ligature_width, paper_column));
599   paper_column->
600     set_grob_property("forced-spacing", gh_double2scm (ligature_width));
601 #endif
602 }
603
604
605 ENTER_DESCRIPTION (Vaticana_ligature_engraver,
606 /* descr */       "Handles ligatures by glueing special ligature heads together.",
607 /* creats*/       "VaticanaLigature",
608 /* accepts */     "ligature-event abort-event",
609 /* acks  */      "note-head-interface rest-interface",
610 /* reads */       "",
611 /* write */       "");