]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature.cc
Nitpick run.
[lilypond.git] / lily / vaticana-ligature.cc
1 /*
2   vaticana-ligature.cc -- implement Vaticana_ligature
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 "vaticana-ligature.hh"
10
11 #include <math.h>
12
13 #include "item.hh"
14 #include "font-interface.hh"
15 #include "lookup.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "note-head.hh"
18 #include "output-def.hh"
19 #include "bezier.hh"
20 #include "warn.hh"
21
22 Stencil
23 vaticana_brew_cauda (Grob *me,
24                      int pos,
25                      int delta_pitch,
26                      Real thickness,
27                      Real blotdiameter)
28 {
29   bool on_staffline = Staff_symbol_referencer::on_staffline (me, pos);
30   int interspaces = Staff_symbol_referencer::line_count (me) - 1;
31   bool above_staff = pos > interspaces;
32
33   if (delta_pitch > -1)
34     {
35       me->programming_error ("flexa cauda: invalid delta_pitch; assuming -1");
36       delta_pitch = -1;
37     }
38   Real length;
39   if (on_staffline)
40     {
41       if (delta_pitch >= -1)
42         length = 1.30;
43       else if (delta_pitch >= -2)
44         length = 1.35;
45       else
46         length = 1.85;
47     }
48   else
49     {
50       if (delta_pitch >= -1)
51         if (above_staff)
52           length = 1.30;
53         else
54           length = 1.00;
55       else if (delta_pitch >= -2)
56         length = 1.35;
57       else if (delta_pitch >= -3)
58         length = 1.50;
59       else
60         length = 1.85;
61     }
62   Box cauda_box (Interval (0, thickness), Interval (-length, 0));
63   return Lookup::round_filled_box (cauda_box, blotdiameter);
64 }
65
66 /*
67  * TODO: move this function to class Lookup?
68  */
69 Stencil
70 vaticana_brew_flexa (Grob *me,
71                      bool solid,
72                      Real line_thickness)
73 {
74   Real staff_space = Staff_symbol_referencer::staff_space (me);
75   Stencil stencil;
76   Real right_height = 0.6 * staff_space;
77
78   Real interval;
79   SCM flexa_height_scm = me->get_property ("flexa-height");
80   if (flexa_height_scm != SCM_EOL)
81     interval = scm_to_int (flexa_height_scm);
82   else
83     {
84       me->warning ("Vaticana_ligature: "
85                    + _ ("flexa-height undefined; assuming 0"));
86       interval = 0.0;
87     }
88
89   if (interval >= 0.0)
90     me->warning (_ ("ascending vaticana style flexa"));
91
92   Real width = robust_scm2double (me->get_property ("flexa-width"), 2);
93
94   /*
95    * Compensate curve thickness that appears to be smaller in steep
96    * section of bend.
97    */
98   Real left_height
99     = right_height
100     + min (0.12 * abs (interval), 0.3) * staff_space;
101
102   /*
103    * Compensate optical illusion regarding vertical position of left
104    * and right endings due to curved shape.
105    */
106   Real ypos_correction = -0.1 * staff_space * sign (interval);
107   Real interval_correction = 0.2 * staff_space * sign (interval);
108   Real corrected_interval = interval * staff_space + interval_correction;
109
110   /*
111    * middle curve of flexa shape
112    */
113   Bezier curve;
114   curve.control_[0] = Offset (0.00 * width, 0.0);
115   curve.control_[1] = Offset (0.33 * width, corrected_interval / 2.0);
116   curve.control_[2] = Offset (0.66 * width, corrected_interval / 2.0);
117   curve.control_[3] = Offset (1.00 * width, corrected_interval / 2.0);
118
119   Bezier top_curve = curve, bottom_curve = curve;
120   for (int i = 0; i < 4; i++)
121     {
122       Real curve_thickness = 0.33 * ((3 - i) * left_height + i * right_height);
123       top_curve.control_[i] += Offset (0, 0.5 * curve_thickness);
124       bottom_curve.control_[i] -= Offset (0, 0.5 * curve_thickness);
125     }
126
127   if (solid)
128     {
129       Stencil solid_head
130         = Lookup::bezier_sandwich (top_curve, bottom_curve);
131       stencil.add_stencil (solid_head);
132     }
133   else // outline
134     {
135       Bezier inner_top_curve = top_curve;
136       inner_top_curve.translate (Offset (0.0, -line_thickness));
137       Stencil top_edge
138         = Lookup::bezier_sandwich (top_curve, inner_top_curve);
139       stencil.add_stencil (top_edge);
140
141       Bezier inner_bottom_curve = bottom_curve;
142       inner_bottom_curve.translate (Offset (0.0, +line_thickness));
143       Stencil bottom_edge
144         = Lookup::bezier_sandwich (bottom_curve, inner_bottom_curve);
145       stencil.add_stencil (bottom_edge);
146
147       /*
148        * TODO: Use horizontal slope with proper slope value rather
149        * than filled box for left edge, since the filled box stands
150        * out from the flexa shape if the interval is big and the line
151        * thickness small.  The difficulty here is to compute a proper
152        * slope value, as it should roughly be equal with the slope of
153        * the left end of the bezier curve.
154        */
155       Box left_edge_box (Interval (0, line_thickness),
156                          Interval (-0.5 * left_height, +0.5 * left_height));
157       Stencil left_edge = Lookup::filled_box (left_edge_box);
158       stencil.add_stencil (left_edge);
159
160       Box right_edge_box (Interval (-line_thickness, 0),
161                           Interval (-0.5 * right_height, +0.5 * right_height));
162       Stencil right_edge = Lookup::filled_box (right_edge_box);
163       right_edge.translate_axis (width, X_AXIS);
164       right_edge.translate_axis (corrected_interval / 2.0, Y_AXIS);
165       stencil.add_stencil (right_edge);
166     }
167   stencil.translate_axis (ypos_correction, Y_AXIS);
168   return stencil;
169 }
170
171 Stencil
172 vaticana_brew_join (Grob *me, int delta_pitch,
173                     Real join_thickness, Real blotdiameter)
174 {
175   Real staff_space = Staff_symbol_referencer::staff_space (me);
176   if (!delta_pitch)
177     {
178       me->programming_error (_f ("Vaticana_ligature: "
179                                  "zero join (delta_pitch == 0)"));
180       return Stencil ();
181     }
182   Interval x_extent = Interval (0, join_thickness);
183   Interval y_extent = (delta_pitch > 0)
184     ? Interval (0, delta_pitch * 0.5 * staff_space) : // ascending join
185     Interval (delta_pitch * 0.5 * staff_space, 0); // descending join
186   Box join_box (x_extent, y_extent);
187   return Lookup::round_filled_box (join_box, blotdiameter);
188 }
189
190 Stencil
191 vaticana_brew_primitive (Grob *me)
192 {
193   SCM glyph_name_scm = me->get_property ("glyph-name");
194   if (glyph_name_scm == SCM_EOL)
195     {
196       me->programming_error ("Vaticana_ligature: "
197                              "undefined glyph-name -> ignoring grob");
198       return Stencil ();
199     }
200
201   String glyph_name = ly_scm2string (glyph_name_scm);
202
203   Stencil out;
204   Real thickness = robust_scm2double (me->get_property ("thickness"), 1);
205
206   Real line_thickness
207     = thickness * me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
208
209   Real blotdiameter
210     = (me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter")));
211
212   int pos = Staff_symbol_referencer::get_rounded_position (me);
213
214   SCM delta_pitch_scm = me->get_property ("delta-pitch");
215   int delta_pitch;
216   if (delta_pitch_scm != SCM_EOL)
217     delta_pitch = scm_to_int (delta_pitch_scm);
218   else
219     delta_pitch = 0;
220
221   Real x_offset = robust_scm2double (me->get_property ("x-offset"), 0);
222
223   bool add_stem = to_boolean (me->get_property ("add-stem"));
224   bool add_cauda = to_boolean (me->get_property ("add-cauda"));
225   bool add_join = to_boolean (me->get_property ("add-join"));
226
227   if (!String::compare (glyph_name, ""))
228     {
229       /*
230        * This is an empty head.  This typically applies for the right
231        * side of a curved flexa shape, which is already typeset by the
232        * associated left side head.  The only possible thing left to
233        * do is to draw a vertical join to the next head.  (Urgh: need
234        * flexa_width.)
235        */
236       Real staff_space = Staff_symbol_referencer::staff_space (me);
237       Real flexa_width = robust_scm2double (me->get_property ("flexa-width"), 2) * staff_space;
238       out
239         = Lookup::blank (Box (Interval (0, 0.5 * flexa_width), Interval (0, 0)));
240     }
241   else if (!String::compare (glyph_name, "flexa"))
242     out = vaticana_brew_flexa (me, true, line_thickness);
243   else
244     {
245       out
246         = Font_interface::get_default_font (me)->
247         find_by_name ("noteheads." + glyph_name);
248     }
249   out.translate_axis (x_offset, X_AXIS);
250   Real head_width = out.extent (X_AXIS).length ();
251
252   if (add_cauda)
253     {
254       Stencil cauda
255         = vaticana_brew_cauda (me, pos, delta_pitch,
256                                line_thickness, blotdiameter);
257       out.add_stencil (cauda);
258     }
259
260   if (add_stem)
261     {
262       Stencil stem
263         = vaticana_brew_cauda (me, pos, -1,
264                                line_thickness, blotdiameter);
265       stem.translate_axis (head_width - line_thickness, X_AXIS);
266       out.add_stencil (stem);
267     }
268
269   if (add_join)
270     {
271       Stencil join
272         = vaticana_brew_join (me, delta_pitch, line_thickness, blotdiameter);
273       join.translate_axis (head_width - line_thickness, X_AXIS);
274       out.add_stencil (join);
275     }
276
277   return out;
278 }
279
280 MAKE_SCHEME_CALLBACK (Vaticana_ligature, brew_ligature_primitive, 1);
281 SCM
282 Vaticana_ligature::brew_ligature_primitive (SCM smob)
283 {
284   Grob *me = unsmob_grob (smob);
285   SCM primitive = vaticana_brew_primitive (me).smobbed_copy ();
286   return primitive;
287 }
288
289 MAKE_SCHEME_CALLBACK (Vaticana_ligature, print, 1);
290 SCM
291 Vaticana_ligature::print (SCM)
292 {
293   return SCM_EOL;
294 }
295
296 ADD_INTERFACE (Vaticana_ligature, "vaticana-ligature-interface",
297                "A vaticana style gregorian ligature",
298                "glyph-name flexa-height flexa-width thickness add-cauda "
299                "add-stem add-join delta-pitch x-offset "
300                "ligature-primitive-callback");