]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-spanner.cc
* lily/slur.cc (get_boundary_notecolumn_y): change name from
[lilypond.git] / lily / line-spanner.cc
1 /*
2   line-spanner.cc -- implement Line_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "molecule.hh"
10 #include "item.hh"
11 #include "spanner.hh"
12 #include "line-spanner.hh"
13 #include "paper-def.hh"
14 #include "paper-column.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "font-interface.hh"
17 #include "warn.hh"
18
19 #include <math.h>
20
21
22 /*
23   slightishly clumsy interface?
24
25   Make  a Scheme expression for a line going from (0,0) to (dx,dy). 
26  */
27
28 static SCM
29 line_atom (Grob* me, Real thick, Real dx, Real dy)
30 {
31   SCM type = me->get_grob_property ("type");
32   Real staff_space = Staff_symbol_referencer::staff_space (me);
33   
34       // maybe these should be in line-thickness?
35   Real length = staff_space;
36   SCM s = me->get_grob_property ("dash-length");
37   if (gh_number_p (s))
38     length = gh_scm2double (s) * staff_space;
39
40   Real period = 2 * length + thick;
41   s = me->get_grob_property ("dash-period");
42   if (gh_number_p (s))
43     period = gh_scm2double (s) * staff_space;
44       
45   if (type == ly_symbol2scm ("dotted-line"))
46     length = thick;
47         
48   if (type == ly_symbol2scm ("line"))
49     length = period + thick;
50
51   Real on = length - thick;
52   Real off = period - on;
53
54   SCM list = scm_list_n (ly_symbol2scm ("dashed-line"),
55                       gh_double2scm (thick),
56                       gh_double2scm (on),
57                       gh_double2scm (off),
58                       gh_double2scm (dx),
59                       gh_double2scm (dy),
60                       SCM_UNDEFINED);
61
62   return list;
63 }
64
65 static SCM
66 zigzag_atom (Grob* me, Real thick, Real dx, Real dy)
67 {
68   Real staff_space = Staff_symbol_referencer::staff_space (me);
69   SCM ws = me->get_grob_property ("zigzag-width");
70   SCM ls = me->get_grob_property ("zigzag-length");
71   double w = (gh_number_p(ws) ? gh_scm2double(ws) : 1)*staff_space;
72   double l = (gh_number_p(ls) ? gh_scm2double(ls) : 1)*w;
73   double h = l>w/2 ? sqrt(l*l-w*w/4) : 0;
74   
75   SCM list = scm_list_n (ly_symbol2scm ("zigzag-line"),
76                       gh_bool2scm (true),
77                       gh_double2scm (w),
78                       gh_double2scm (h),
79                       gh_double2scm (thick),
80                       gh_double2scm (dx),
81                       gh_double2scm (dy),
82                       SCM_UNDEFINED);
83
84   return list;
85 }
86
87
88
89 Molecule
90 Line_spanner::line_molecule (Grob* me, Real thick, Real dx, Real dy)
91 {
92   Molecule mol;
93   SCM type = me->get_grob_property ("type");
94   if (gh_symbol_p (type)
95       && (type == ly_symbol2scm ("line")
96           || type == ly_symbol2scm ("dashed-line")
97           || type == ly_symbol2scm ("dotted-line")
98           || (type == ly_symbol2scm ("trill") && dy != 0)))
99     {
100       Box b (Interval (-0.5* thick +  (0 <? dx) ,0.5* thick+ (0 >? dx)),
101              Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
102       mol = Molecule (b, line_atom (me, thick, dx, dy));
103     }
104   else if (gh_symbol_p (type)
105            && type == ly_symbol2scm ("zigzag"))
106     {
107       // TODO:
108       Box b (Interval (-0.5* thick +  (0 <? dx) ,0.5* thick+ (0 >? dx)),
109              Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
110       mol = Molecule (b, zigzag_atom (me, thick, dx, dy));
111
112     }
113   else if (gh_symbol_p (type)
114            && type == ly_symbol2scm ("trill"))
115     {
116       SCM alist_chain = Font_interface::font_alist_chain (me);
117       SCM style_chain = scm_list_n (gh_cons (ly_symbol2scm ("font-family"),
118                                           ly_symbol2scm ("music")),
119                                  SCM_UNDEFINED);
120       
121       Font_metric *fm = Font_interface::get_font (me,
122                                                   scm_list_n (style_chain,
123                                                            alist_chain,
124                                                            SCM_UNDEFINED));
125       Molecule m = fm->find_by_name ("scripts-trill-element");
126       do
127         mol.add_at_edge (X_AXIS, RIGHT, m, 0,0);
128       while (m.extent (X_AXIS).length ()
129              && mol.extent (X_AXIS).length ()
130              + m.extent (X_AXIS).length () < dx);
131
132       /*
133         FIXME: should center element on x/y
134        */
135       mol.translate_axis (m.extent (X_AXIS).length () / 2, X_AXIS);
136       mol.translate_axis (-(mol.extent (Y_AXIS)[DOWN]
137                             + mol.extent (Y_AXIS).length ())/2, Y_AXIS); 
138     }
139   return mol;
140 }
141
142 Offset
143 Line_spanner::get_broken_offset (Grob *me, Direction dir)
144 {
145   Spanner *spanner = dynamic_cast<Spanner*> (me);
146   Item* bound = spanner->get_bound (dir);
147   
148   if (!bound->break_status_dir ())
149     {
150       Grob *common[] = {
151         bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
152                                 X_AXIS),
153         bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
154                                 Y_AXIS)
155       };
156   
157       return Offset (abs (bound->extent (common[X_AXIS], X_AXIS)[-dir]),
158                       bound->extent (common[Y_AXIS], Y_AXIS).center ());
159     }
160   return Offset ();
161 }
162
163 /* A broken line-spaner should maintain the same vertical trend
164    the unbroken line-spanner would have had.
165    From slur */
166 Offset
167 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
168 {
169   Offset o;
170   
171   if (Spanner *mother =  dynamic_cast<Spanner*> (me->original_))
172     {
173       int k = broken_spanner_index (dynamic_cast<Spanner*> (me));
174       Grob *neighbour = mother->broken_intos_[k + dir];      
175       Offset neighbour_o = get_broken_offset (neighbour, dir);
176       Offset me_o = get_broken_offset (me, -dir);
177
178       // Hmm, why not return me_o[X], but recalc in brew_mol?
179       o = Offset (0,
180                   (neighbour_o[Y_AXIS]*me_o[X_AXIS]
181                    - me_o[Y_AXIS]*neighbour_o[X_AXIS]) * dir /
182                   (me_o[X_AXIS] + neighbour_o[X_AXIS]));
183     }
184   return o;
185 }
186
187
188 /*
189   Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
190
191  (If not, you risk that this is called from the staff-alignment
192   routine, via molecule_extent. At this point, the staves aren't
193   separated yet, so it doesn't work cross-staff.
194
195 */
196
197 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
198 SCM
199 Line_spanner::brew_molecule (SCM smob) 
200 {
201   Grob *me= unsmob_grob (smob);
202
203   Spanner *spanner = dynamic_cast<Spanner*> (me);
204   Drul_array<Item*>  bound (spanner->get_bound (LEFT),
205                             spanner->get_bound (RIGHT));
206   
207   Grob *common[] = { me, me };
208   for (int a = X_AXIS;  a < NO_AXES; a++)
209     {
210       common[a] = me->common_refpoint (bound[RIGHT], Axis (a));
211       common[a] = common[a]->common_refpoint (bound[LEFT], Axis (a));
212       
213       if (!common[a])
214         {
215           programming_error ("No common point!");
216           return SCM_EOL;
217         }
218     }
219   
220   Real gap = gh_scm2double (me->get_grob_property ("gap"));
221
222   Offset ofxy (gap, 0); /*offset from start point to start of line*/
223   Offset dxy ;
224   Offset my_off;
225   Offset his_off;
226
227
228   
229   if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
230     /* across line break */
231     {
232       Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
233
234       dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
235         - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
236       
237       dxy += broken_trend_offset (me, broken);
238       dxy[X_AXIS] -= 1 * gap;
239
240       my_off = Offset (0,
241                        me->relative_coordinate (common[Y_AXIS], Y_AXIS));
242
243       his_off = Offset (0, 
244                         bound[-broken]->relative_coordinate (common[Y_AXIS],
245                                                              Y_AXIS));
246
247       if (broken == LEFT)
248         {
249           my_off[Y_AXIS] += dxy[Y_AXIS];
250         }
251     }
252   else
253     {
254       Real off = gap + ((bound[LEFT]->extent (bound[LEFT], X_AXIS).length ()*3)/4); // distance from center to start of line
255
256       for (int a = X_AXIS; a < NO_AXES; a++)
257         {
258           Axis ax = (Axis)a;
259           dxy[ax] =
260             + bound[RIGHT]->extent (common[X_AXIS], ax).center ()
261             - bound[LEFT]->extent (common[X_AXIS], ax).center ();
262
263           my_off[ax] =me->relative_coordinate (common[a], ax);
264           his_off[ax] = bound[LEFT]->relative_coordinate (common[a], ax);
265           
266         }
267
268       ofxy = dxy * (off/dxy.length ());
269       dxy -= 2*ofxy;
270     }
271
272   Real thick = me->get_paper ()->get_var ("linethickness");  
273
274   SCM s = me->get_grob_property ("thickness");
275   if (gh_number_p (s))
276     thick *= gh_scm2double (s);
277
278   
279   Molecule line = line_molecule (me, thick, dxy[X_AXIS], dxy[Y_AXIS]);
280   line.translate_axis (bound[LEFT]->extent (bound[LEFT],
281                                             X_AXIS).length ()/2, X_AXIS); 
282   line.translate (ofxy - my_off + his_off);
283   return line.smobbed_copy ();
284 }
285
286
287
288 ADD_INTERFACE (Line_spanner, "line-spanner-interface",
289   "Generic line drawn between two objects, eg. for use with glissandi.\n"
290 "gap is measured in staff-spaces.\n"
291 "The property 'type is one of: line, dashed-line, trill, dotted-line or zigzag.\n"
292 "\n",
293   "gap dash-period dash-length zigzag-width zigzag-length thickness type");
294
295