]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-spanner.cc
* ly/engraver-init.ly (VoiceContext): move Note_head_line_engraver
[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 Offset
164 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
165 {
166   /* A broken line-spaner should maintain the same vertical trend
167      the unbroken line-spanner would have had.
168      From slur */
169   Offset o;
170   if (Spanner *mother =  dynamic_cast<Spanner*> (me->original_))
171     {
172       for (int i = dir == LEFT ? 0 : mother->broken_intos_.size () - 1;
173            dir == LEFT ? i < mother->broken_intos_.size () : i > 0;
174            dir == LEFT ? i++ : i--)
175         {
176           if (mother->broken_intos_[i - dir] == me)
177             {
178               Grob *neighbour = mother->broken_intos_[i];
179               Offset neighbour_o = get_broken_offset (neighbour, dir);
180               Offset me_o = get_broken_offset (me, -dir);
181               // Hmm, why not return me_o[X], but recalc in brew_mol?
182               o = Offset (0,
183  (neighbour_o[Y_AXIS]*me_o[X_AXIS]
184                            - me_o[Y_AXIS]*neighbour_o[X_AXIS]) * dir /
185  (me_o[X_AXIS] + neighbour_o[X_AXIS]));
186               break;
187             }
188         }
189     }
190   return o;
191 }
192
193
194 /*
195   Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
196
197  (If not, you risk that this is called from the staff-alignment
198   routine, via molecule_extent. At this point, the staves aren't
199   separated yet, so it doesn't work cross-staff.
200
201 */
202
203 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
204 SCM
205 Line_spanner::brew_molecule (SCM smob) 
206 {
207   Grob *me= unsmob_grob (smob);
208
209   Spanner *spanner = dynamic_cast<Spanner*> (me);
210   Item* bound_drul[] = {
211     spanner->get_bound (LEFT),
212     0,
213     spanner->get_bound (RIGHT)
214   };
215   
216   Item** bound = bound_drul + 1;
217
218   Grob *common[] = { me, me };
219   for (int a = X_AXIS;  a < NO_AXES; a++)
220     {
221       common[a] = me->common_refpoint (bound[RIGHT], Axis (a));
222       common[a] = common[a]->common_refpoint (bound[LEFT], Axis (a));
223       
224       if (!common[a])
225         {
226           programming_error ("No common point!");
227           return SCM_EOL;
228         }
229     }
230   
231   Real gap = gh_scm2double (me->get_grob_property ("gap"));
232   Real dist; /*distance between points */
233
234   Offset ofxy (gap, 0); /*offset from start point to start of line*/
235   Offset dxy ;
236   Offset my_off;
237   Offset his_off;
238
239   
240   if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
241     /* across line break */
242     {
243       Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
244
245       dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
246         - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
247       
248       dxy += broken_trend_offset (me, broken);
249       dxy[X_AXIS] -= 1 * gap;
250
251       my_off = Offset (0,
252                        me->relative_coordinate (common[Y_AXIS], Y_AXIS));
253
254       his_off = Offset (0, 
255                         bound[-broken]->relative_coordinate (common[Y_AXIS],
256                                                              Y_AXIS));
257
258       if (broken == LEFT)
259         {
260           my_off[Y_AXIS] += dxy[Y_AXIS];
261         }
262     }
263   else
264     {
265       Real off = gap + ((bound[LEFT]->extent (bound[LEFT], X_AXIS).length ()*3)/4); // distance from center to start of line
266
267       for (int a = X_AXIS; a < NO_AXES; a++)
268         {
269           Axis ax = (Axis)a;
270           dxy[ax] =
271             + bound[RIGHT]->extent (common[X_AXIS], ax).center ()
272             - bound[LEFT]->extent (common[X_AXIS], ax).center ();
273
274           my_off[ax] =me->relative_coordinate (common[a], ax);
275           his_off[ax] = bound[LEFT]->relative_coordinate (common[a], ax);
276           
277         }
278
279       ofxy = dxy * (off/dxy.length ());
280       dxy -= 2*ofxy;
281     }
282
283   Real thick = me->get_paper ()->get_var ("linethickness");  
284
285   SCM s = me->get_grob_property ("thickness");
286   if (gh_number_p (s))
287     thick *= gh_scm2double (s);
288
289   
290   Molecule line = line_molecule (me, thick, dxy[X_AXIS], dxy[Y_AXIS]);
291   line.translate_axis (bound[LEFT]->extent (bound[LEFT],
292                                             X_AXIS).length ()/2, X_AXIS); 
293   line.translate (ofxy - my_off + his_off);
294   return line.smobbed_copy ();
295 }
296
297
298
299 ADD_INTERFACE (Line_spanner, "line-spanner-interface",
300   "Generic line drawn between two objects, eg. for use with glissandi.\n"
301 "gap is measured in staff-spaces.\n"
302 "The property 'type is one of: line, dashed-line, trill, dotted-line or zigzag.\n"
303 "\n",
304   "gap dash-period dash-length zigzag-width zigzag-length thickness type");
305
306