]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature-engraver.cc
* lily/include/grob-info.hh (class Grob_info): make data member
[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   static bool
29   need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
30                                int context_info, int delta_pitch);
31   bool is_stacked_head (int prefix_set,
32                         int context_info);
33   Real align_heads (Array<Grob_info> primitives,
34                     Real flexa_width,
35                     Real thickness);
36
37 public:
38   TRANSLATOR_DECLARATIONS (Vaticana_ligature_engraver);
39
40 protected:
41   virtual Spanner *create_ligature_spanner ();
42   virtual void transform_heads (Spanner *ligature,
43                                 Array<Grob_info> primitives);
44 };
45
46 Vaticana_ligature_engraver::Vaticana_ligature_engraver ()
47 {
48 }
49
50 Spanner *
51 Vaticana_ligature_engraver::create_ligature_spanner ()
52 {
53   return make_spanner ("VaticanaLigature", SCM_EOL);
54 }
55
56 bool
57 Vaticana_ligature_engraver::is_stacked_head (int prefix_set,
58                                              int context_info)
59 {
60   bool is_stacked_b;
61
62   // upper head of pes is stacked upon lower head of pes ...
63   is_stacked_b = context_info & PES_UPPER;
64
65   // ... unless this note starts a flexa
66   if (context_info & FLEXA_LEFT)
67     is_stacked_b = false;
68
69   // ... or another pes
70   if (context_info & PES_LOWER)
71     is_stacked_b = false;
72
73   // ... or the previous note is a semivocalis or inclinatum
74   if (context_info & AFTER_DEMINUTUM)
75     is_stacked_b = false;
76
77   // auctum head is never stacked upon preceding note
78   if (prefix_set & AUCTUM)
79     is_stacked_b = false;
80
81   // virga is never stacked upon preceding note
82   if (prefix_set & VIRGA)
83     is_stacked_b = false;
84
85   // oriscus is never stacked upon preceding note
86   if (prefix_set & ORISCUS)
87     is_stacked_b = false;
88
89   if ((prefix_set & DEMINUTUM)
90       && ! (prefix_set & INCLINATUM)
91       && (context_info & FLEXA_RIGHT))
92     is_stacked_b = true; // semivocalis head of deminutus form
93
94   return is_stacked_b;
95 }
96
97 /*
98  * When aligning the heads, sometimes extra space is needed, e.g. to
99  * avoid clashing with the appendix of an adjacent notehead or with an
100  * adjacent notehead itself if it has the same pitch.  Extra space is
101  * added at most once between to heads.
102  */
103 bool
104 Vaticana_ligature_engraver::need_extra_horizontal_space (int prev_prefix_set, int prefix_set,
105                                                          int context_info, int delta_pitch)
106 {
107   if (prev_prefix_set & VIRGA)
108     /*
109      * After a virga, make a an additional small space such that the
110      * appendix on the right side of the head does not touch the
111      * following head.
112      */
113     return true;
114
115   if ((prefix_set & INCLINATUM)
116       && ! (prev_prefix_set & INCLINATUM))
117     /*
118      * Always start a series of inclinatum heads with an extra space.
119      */
120     return true;
121
122   if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
123     /*
124      * Before a flexa (but not within a torculus), make a an
125      * additional small space such that the appendix on the left side
126      * of the flexa does not touch the this head.
127      */
128     return true;
129
130   if (delta_pitch == 0)
131     /*
132      * If there are two adjacent noteheads with the same pitch, add
133      * additional small space between them, such that they do not
134      * touch each other.
135      */
136     return true;
137
138   return false;
139 }
140
141 Real
142 Vaticana_ligature_engraver::align_heads (Array<Grob_info> primitives,
143                                          Real flexa_width,
144                                          Real thickness)
145 {
146   if (!primitives.size ())
147     {
148       programming_error ("Vaticana_ligature: "
149                          "empty ligature [ignored]");
150       return 0.0;
151     }
152
153   /*
154    * The paper column where we put the whole ligature into.
155    */
156   Paper_column *column
157     = dynamic_cast<Item *> (primitives[0].grob ())->get_column ();
158
159   Real join_thickness
160     = thickness * column->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
161
162   /*
163    * Amount of extra space two put between some particular
164    * configurations of adjacent heads.
165    *
166    * TODO: make this a property of primtive grobs.
167    */
168   Real extra_space = 4.0 * join_thickness;
169
170   /*
171    * Keep track of the total width of the ligature.
172    */
173   Real ligature_width = 0.0;
174
175   Item *prev_primitive = 0;
176   int prev_prefix_set = 0;
177   for (int i = 0; i < primitives.size (); i++)
178     {
179       Item *primitive = dynamic_cast<Item *> (primitives[i].grob ());
180       int prefix_set
181         = scm_to_int (primitive->get_property ("prefix-set"));
182       int context_info
183         = scm_to_int (primitive->get_property ("context-info"));
184
185       /*
186        * Get glyph_name, delta_pitch and context_info for this head.
187        */
188
189       SCM glyph_name_scm = primitive->get_property ("glyph-name");
190       if (glyph_name_scm == SCM_EOL)
191         {
192           primitive->programming_error ("Vaticana_ligature:"
193                                         "undefined glyph-name -> "
194                                         "ignoring grob");
195           continue;
196         }
197       String glyph_name = ly_scm2string (glyph_name_scm);
198
199       int delta_pitch = 0;
200       if (prev_primitive) /* urgh, need prev_primitive only here */
201         {
202           SCM delta_pitch_scm = prev_primitive->get_property ("delta-pitch");
203           if (delta_pitch_scm != SCM_EOL)
204             {
205               delta_pitch = scm_to_int (delta_pitch_scm);
206             }
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_make_real (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         {
371           delta_pitch = scm_to_int (delta_pitch_scm);
372         }
373       else
374         {
375           primitive->programming_error ("Vaticana_ligature:"
376                                         "delta-pitch undefined -> "
377                                         "ignoring grob");
378           continue;
379         }
380
381       /* retrieve & complete prefix_set and context_info */
382       int prefix_set
383         = scm_to_int (primitive->get_property ("prefix-set"));
384       int context_info
385         = scm_to_int (primitive->get_property ("context-info"));
386       if (is_stacked_head (prefix_set, context_info))
387         {
388           context_info |= STACKED_HEAD;
389           primitive->set_property ("context-info",
390                                    scm_int2num (context_info));
391         }
392
393       /*
394        * Now determine which head to typeset (this is context sensitive
395        * information, since it depends on neighbouring heads; therefore,
396        * this decision must be made here in the engraver rather than in
397        * the backend).
398        */
399       String glyph_name;
400       if (prefix_set & VIRGA)
401         {
402           glyph_name = "svaticana.punctum";
403           primitive->set_property ("add-stem", ly_bool2scm (true));
404         }
405       else if (prefix_set & QUILISMA)
406         glyph_name = "svaticana.quilisma";
407       else if (prefix_set & ORISCUS)
408         glyph_name = "ssolesmes.oriscus";
409       else if (prefix_set & STROPHA)
410         if (prefix_set & AUCTUM)
411           glyph_name = "ssolesmes.stropha.aucta";
412         else glyph_name = "ssolesmes.stropha";
413       else if (prefix_set & INCLINATUM)
414         if (prefix_set & AUCTUM)
415           glyph_name = "ssolesmes.incl.auctum";
416         else if (prefix_set & DEMINUTUM)
417           glyph_name = "ssolesmes.incl.parvum";
418         else
419           glyph_name = "svaticana.inclinatum";
420       else if (prefix_set & DEMINUTUM)
421         if (i == 0)
422           {
423             // initio debilis
424             glyph_name = "svaticana.reverse.plica";
425           }
426         else if (prev_delta_pitch > 0)
427           {
428             // epiphonus
429             if (! (prev_context_info & FLEXA_RIGHT))
430               /* correct head of previous primitive */
431               if (prev_delta_pitch > 1)
432                 {
433                   prev_glyph_name = "svaticana.epiphonus";
434                   glyph_name = "svaticana.plica";
435                 }
436               else
437                 {
438                   prev_glyph_name = "svaticana.vepiphonus";
439                   glyph_name = "svaticana.vplica";
440                 }
441           }
442         else if (prev_delta_pitch < 0)
443           {
444             // cephalicus
445             if (! (prev_context_info & FLEXA_RIGHT))
446               /* correct head of previous primitive */
447               {
448                 if (i > 1)
449                   {
450                     /* cephalicus head with fixed size cauda */
451                     prev_glyph_name = "svaticana.inner.cephalicus";
452                   }
453                 else
454                   {
455                     /* cephalicus head without cauda */
456                     prev_glyph_name = "svaticana.cephalicus";
457                   }
458
459                 /*
460                  * Flexa has no variable size cauda if its left head is
461                  * stacked on the right head.  This is true for
462                  * cephalicus.  Hence, remove the cauda.
463                  *
464                  * Urgh: for the current implementation, this rule only
465                  * applies for cephalicus; but it is a fundamental rule.
466                  * Therefore, the following line of code should be
467                  * placed somewhere else.
468                  */
469                 prev_primitive->set_property ("add-cauda",
470                                               ly_bool2scm (false));
471               }
472               if (prev_delta_pitch < - 1)
473                 {
474                   glyph_name = "svaticana.reverse.plica";
475                 }
476               else
477                 {
478                   glyph_name = "svaticana.reverse.vplica";
479                 }
480           }
481         else // (prev_delta_pitch == 0)
482           {
483             primitive->programming_error ("Vaticana_ligature:"
484                                           "deminutum head must have different "
485                                           "pitch -> ignoring grob");
486           }
487       else if (prefix_set & (CAVUM | LINEA))
488         if ((prefix_set & CAVUM) && (prefix_set & LINEA))
489           glyph_name = "svaticana.linea.punctum.cavum";
490         else if (prefix_set & CAVUM)
491           glyph_name = "svaticana.punctum.cavum";
492         else
493           glyph_name = "svaticana.linea.punctum";
494       else if (prefix_set & AUCTUM)
495         if (prefix_set & ASCENDENS)
496           glyph_name = "ssolesmes.auct.asc";
497         else
498           glyph_name = "ssolesmes.auct.desc";
499       else if ((context_info & STACKED_HEAD)
500                && (context_info & PES_UPPER))
501         if (prev_delta_pitch > 1)
502           glyph_name = "svaticana.upes";
503         else
504           glyph_name = "svaticana.vupes";
505       else
506         glyph_name = "svaticana.punctum";
507
508       /*
509        * This head needs a cauda, if it starts a flexa, is not the upper
510        * head of a pes, and if it is a punctum.
511        */
512       if ((context_info & FLEXA_LEFT) && ! (context_info & PES_UPPER))
513         if (!String::compare (glyph_name, "svaticana.punctum"))
514           primitive->set_property ("add-cauda", ly_bool2scm (true));
515
516       /*
517        * Execptional rule for porrectus:
518        *
519        * If the current head is preceded by a \flexa and succeded by a
520        * \pes (e.g. "a \flexa g \pes a"), then join the current head and
521        * the previous head into a single curved flexa shape.
522        */
523       if ((context_info & FLEXA_RIGHT) && (context_info & PES_LOWER))
524         {
525           check_for_prefix_loss (prev_primitive);
526           prev_glyph_name = "flexa";
527           prev_primitive->set_property ("flexa-height",
528                                         scm_int2num (prev_delta_pitch));
529           prev_primitive->set_property ("flexa-width",
530                                         scm_make_real (flexa_width));
531           bool add_cauda = !(prev_prefix_set && PES_OR_FLEXA);
532           prev_primitive->set_property ("add-cauda",
533                                         ly_bool2scm (add_cauda));
534           check_for_prefix_loss (primitive);
535           glyph_name = "";
536           primitive->set_property ("flexa-width",
537                                    scm_make_real (flexa_width));
538         }
539
540       /*
541        * Exceptional rule for pes:
542        *
543        * If this head is stacked on the previous one due to a \pes, then
544        * set the glyph of the previous head to that for this special
545        * case, thereby avoiding potential vertical collision with the
546        * current head.
547        */
548       if (prefix_set & PES_OR_FLEXA)
549         {
550           if ((context_info & PES_UPPER) && (context_info & STACKED_HEAD))
551             {
552               if (!String::compare (prev_glyph_name, "svaticana.punctum"))
553                 if (prev_delta_pitch > 1)
554                   prev_glyph_name = "svaticana.lpes";
555                 else
556                   prev_glyph_name = "svaticana.vlpes";
557             }
558         }
559
560       if (prev_primitive)
561         prev_primitive->set_property ("glyph-name",
562                                       scm_makfrom0str (prev_glyph_name.to_str0 ()));
563
564       /*
565        * In the backend, flexa shapes and joins need to know about line
566        * thickness.  Hence, for simplicity, let's distribute the
567        * ligature grob's value for thickness to each ligature head (even
568        * if not all of them need to know).
569        */
570       primitive->set_property ("thickness", scm_make_real (thickness));
571
572       prev_primitive = primitive;
573       prev_prefix_set = prefix_set;
574       prev_context_info = context_info;
575       prev_delta_pitch = delta_pitch;
576       prev_glyph_name = glyph_name;
577     }
578
579   prev_primitive->set_property ("glyph-name",
580                                 scm_makfrom0str (prev_glyph_name.to_str0 ()));
581
582   align_heads (primitives, flexa_width, thickness);
583
584 #if 0 // experimental code to collapse spacing after ligature
585   /* TODO: set to max (old/new spacing-increment), since other
586      voices/staves also may want to set this property. */
587   Item *first_primitive = dynamic_cast<Item *> (primitives[0].grob ());
588   Paper_column *paper_column = first_primitive->get_column ();
589   paper_column->warning (_f ("Vaticana_ligature_engraver: "
590                              "setting `spacing-increment = %f': ptr =%ul",
591                              ligature_width, paper_column));
592   paper_column->
593     set_property ("forced-spacing", scm_make_real (ligature_width));
594 #endif
595 }
596
597 ADD_TRANSLATOR (Vaticana_ligature_engraver,
598                 /* descr */ "Handles ligatures by glueing special ligature heads together.",
599                 /* creats*/ "VaticanaLigature",
600                 /* accepts */ "ligature-event",
601                 /* acks  */ "note-head-interface rest-interface",
602                 /* reads */ "",
603                 /* write */ "");