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