]> git.donarmstrong.com Git - lilypond.git/blob - lily/mensural-ligature.cc
* lily/lily-guile.cc (robust_scm2double): new function. Use throughout.
[lilypond.git] / lily / mensural-ligature.cc
1 /*
2   mensural-ligature.cc -- implement Mensural_ligature
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2002--2003 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include <math.h>
10 #include "item.hh"
11 #include "mensural-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 "warn.hh"
19
20 /*
21  * TODO: divide this function into mensural and neo-mensural style.
22  *
23  * TODO: move this function to class Lookup?
24  */
25 Molecule
26 brew_flexa (Grob *me,
27             Real interval,
28             bool solid,
29             Real width,
30             Real thickness,
31             bool add_cauda,
32             Direction cauda_direction)
33 {
34   Real staff_space = Staff_symbol_referencer::staff_space (me);
35   Real height = 0.6 * staff_space;
36   Molecule molecule = Molecule ();
37
38   if (add_cauda)
39     {
40       bool consider_interval =
41         cauda_direction * interval > 0.0;
42
43       Interval cauda_box_x (0, thickness);
44       Interval cauda_box_y;
45
46       if (consider_interval)
47         {
48           Real y_length = max (interval/2.0*staff_space, 1.2*staff_space);
49           cauda_box_y = Interval (0, y_length);
50         }
51       else
52         cauda_box_y = Interval (0, staff_space);
53
54       Real y_correction =
55         (cauda_direction == UP) ?
56         +0.5*height :
57         -0.5*height - cauda_box_y.length();
58
59       Box cauda_box (cauda_box_x, cauda_box_y);
60       Molecule cauda = Lookup::filled_box (cauda_box);
61       cauda.translate_axis (y_correction, Y_AXIS);
62       molecule.add_molecule (cauda);
63     }
64
65   Real slope = (interval / 2.0 * staff_space) / width;
66
67   // Compensate optical illusion regarding vertical position of left
68   // and right endings due to slope.
69   Real ypos_correction = -0.1*staff_space * sign(slope);
70   Real slope_correction = 0.2*staff_space * sign(slope);
71   Real corrected_slope = slope + slope_correction/width;
72
73   if (solid)
74     {
75       Molecule solid_head =
76         Lookup::beam (corrected_slope, width, height, 0.0);
77       molecule.add_molecule (solid_head);
78     }
79   else // outline
80     {
81       Molecule left_edge =
82         Lookup::beam (corrected_slope, thickness, height, 0.0);
83       molecule.add_molecule(left_edge);
84
85       Molecule right_edge =
86         Lookup::beam (corrected_slope, thickness, height, 0.0);
87       right_edge.translate_axis (width-thickness, X_AXIS);
88       right_edge.translate_axis (corrected_slope * (width-thickness), Y_AXIS);
89       molecule.add_molecule(right_edge);
90
91       Molecule bottom_edge =
92         Lookup::beam (corrected_slope, width, thickness, 0.0);
93       bottom_edge.translate_axis (-0.5*height, Y_AXIS);
94       molecule.add_molecule (bottom_edge);
95
96       Molecule top_edge =
97         Lookup::beam (corrected_slope, width, thickness, 0.0);
98       top_edge.translate_axis (+0.5*height, Y_AXIS);
99       molecule.add_molecule (top_edge);
100     }
101   molecule.translate_axis (ypos_correction, Y_AXIS);
102   return molecule;
103 }
104
105 void
106 add_ledger_lines (Grob *me, Molecule *out, int pos, Real offs,
107                   bool ledger_take_space)
108 {
109   int interspaces = Staff_symbol_referencer::line_count (me)-1;
110   if (abs (pos) - interspaces > 1)
111     {
112       Interval hd = out->extent (X_AXIS);
113       Real left_ledger_protusion = hd.length ()/4;
114       Real right_ledger_protusion = left_ledger_protusion;
115
116       Interval l_extents = Interval (hd[LEFT] - left_ledger_protusion,
117                                      hd[RIGHT] + right_ledger_protusion);
118       Molecule ledger_lines =
119         Note_head::brew_ledger_lines (me, pos, interspaces,
120                                       l_extents,
121                                       ledger_take_space);
122       ledger_lines.translate_axis (offs, Y_AXIS);
123       out->add_molecule (ledger_lines);
124     }
125 }
126
127 Molecule
128 internal_brew_primitive (Grob *me, bool ledger_take_space)
129 {
130   SCM primitive_scm = me->get_grob_property ("primitive");
131   if (primitive_scm == SCM_EOL)
132     {
133       programming_error ("Mensural_ligature:"
134                          "undefined primitive -> ignoring grob");
135       return Molecule ();
136     }
137
138   Molecule out;
139   int primitive = gh_scm2int (primitive_scm);
140   int delta_pitch = 0;
141   Real thickness = 0.0;
142   Real flexa_width = 0.0;
143   Real staff_space = Staff_symbol_referencer::staff_space (me);
144   if (primitive & MLP_ANY)
145     {
146       SCM thickness_scm = me->get_grob_property ("thickness");
147       if (thickness_scm != SCM_EOL)
148         {
149           thickness = gh_scm2double (thickness_scm);
150         }
151       else
152         {
153           programming_error (_f ("Mensural_ligature:"
154                                  "thickness undefined on flexa %d; assuming 1.4",
155                                  primitive));
156           thickness = 1.4 * me->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));
157         }
158     }
159
160   if (primitive & MLP_FLEXA)
161     {
162       SCM delta_pitch_scm = me->get_grob_property ("delta-pitch");
163       if (delta_pitch_scm != SCM_EOL)
164         {
165           delta_pitch = gh_scm2int (delta_pitch_scm);
166         }
167       else
168         {
169           programming_error (_f ("Mensural_ligature:"
170                                  "delta-pitch undefined on flexa %d; assuming 0",
171                                  primitive));
172           delta_pitch = 0;
173         }
174
175       flexa_width = robust_scm2double (me->get_grob_property ("flexa-width"), 2.0 * staff_space);
176     }
177
178   switch (primitive)
179     {
180       case MLP_NONE:
181         return Molecule();
182       case MLP_BB:
183         out = brew_flexa (me, delta_pitch, false,
184                           flexa_width, thickness, true, DOWN);
185         break;
186       case MLP_sc:
187         out = Font_interface::get_default_font (me)->find_by_name ("noteheads--2mensural");
188         break;
189       case MLP_ss:
190         out = Font_interface::get_default_font (me)->find_by_name ("noteheads--1mensural");
191         break;
192       case MLP_cs:
193         out = Font_interface::get_default_font (me)->find_by_name ("noteheads-lmensural");
194         break;
195       case MLP_SS:
196         out = brew_flexa (me, delta_pitch, false,
197                           flexa_width, thickness, true, UP);
198         break;
199       case MLP_LB:
200         out = brew_flexa (me, delta_pitch, false,
201                           flexa_width, thickness, false, CENTER);
202         break;
203       default:
204         programming_error (_f ("Mensural_ligature:"
205                                "unexpected case fall-through"));
206         return Molecule ();
207     }
208
209   SCM join_left_scm = me->get_grob_property ("join-left-amount");
210   if (join_left_scm != SCM_EOL)
211     {
212       int join_left = gh_scm2int (join_left_scm);
213       if (!join_left)
214         programming_error (_f ("Mensural_ligature: (join_left == 0)"));
215       Real blotdiameter = (me->get_paper ()->get_realvar (ly_symbol2scm ("blotdiameter")));
216       Interval x_extent = Interval (0, thickness);
217       Interval y_extent = (join_left > 0) ?
218         Interval (-join_left * 0.5 * staff_space, 0) :
219         Interval (0, -join_left * 0.5 * staff_space);
220       Box join_box (x_extent, y_extent);
221
222       Molecule join = Lookup::round_filled_box (join_box, blotdiameter);
223       out.add_molecule (join);
224     }
225
226   int pos = (int)rint (Staff_symbol_referencer::get_position (me));
227   add_ledger_lines(me, &out, pos, 0, ledger_take_space);
228   if (primitive & MLP_FLEXA)
229     {
230       pos += delta_pitch;
231       add_ledger_lines(me, &out, pos, 0.5*delta_pitch, ledger_take_space);
232     }
233
234   return out;
235 }
236
237 MAKE_SCHEME_CALLBACK (Mensural_ligature, brew_ligature_primitive, 1);
238 SCM
239 Mensural_ligature::brew_ligature_primitive (SCM smob)
240 {
241   Grob *me = unsmob_grob (smob);
242   return internal_brew_primitive (me, false).smobbed_copy ();
243 }
244
245 MAKE_SCHEME_CALLBACK (Mensural_ligature, brew_molecule, 1);
246 SCM
247 Mensural_ligature::brew_molecule (SCM)
248 {
249   return SCM_EOL;
250 }
251
252 ADD_INTERFACE (Mensural_ligature, "mensural-ligature-interface",
253                "A mensural ligature",
254                "delta-pitch flexa-width head-width join-left join-left-amount "
255                "ligature-primitive-callback primitive thickness");