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