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