]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-spanner.cc
''
[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 Molecule
65 Line_spanner::line_molecule (Grob* me, Real thick, Real dx, Real dy)
66 {
67   Molecule mol;
68   SCM type = me->get_grob_property ("type");
69   if (gh_symbol_p (type)
70       && (type == ly_symbol2scm ("line")
71           || type == ly_symbol2scm ("dashed-line")
72           || type == ly_symbol2scm ("dotted-line")
73           || (type == ly_symbol2scm ("trill") && dy != 0)))
74     {
75       Box b (Interval (-0.5* thick +  (0 <? dx) ,0.5* thick+ (0 >? dx)),
76              Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
77       mol = Molecule (b, line_atom (me, thick, dx, dy));
78     }
79   else if (gh_symbol_p (type)
80            && type == ly_symbol2scm ("trill"))
81     {
82       SCM alist_chain = Font_interface::font_alist_chain (me);
83       SCM style_chain = scm_list_n (gh_cons (ly_symbol2scm ("font-family"),
84                                           ly_symbol2scm ("music")),
85                                  SCM_UNDEFINED);
86       
87       Font_metric *fm = Font_interface::get_font (me,
88                                                   scm_list_n (style_chain,
89                                                            alist_chain,
90                                                            SCM_UNDEFINED));
91       Molecule m = fm->find_by_name ("scripts-trill-element");
92       do
93         mol.add_at_edge (X_AXIS, RIGHT, m, 0);
94       while (m.extent (X_AXIS).length ()
95              && mol.extent (X_AXIS).length ()
96              + m.extent (X_AXIS).length () < dx);
97
98       /*
99         FIXME: should center element on x/y
100        */
101       mol.translate_axis (m.extent (X_AXIS).length () / 2, X_AXIS);
102       mol.translate_axis (-(mol.extent (Y_AXIS)[DOWN]
103                             + mol.extent (Y_AXIS).length ())/2, Y_AXIS); 
104     }
105   return mol;
106 }
107
108 Offset
109 Line_spanner::get_broken_offset (Grob *me, Direction dir)
110 {
111   Spanner *spanner = dynamic_cast<Spanner*> (me);
112   Item* bound = spanner->get_bound (dir);
113   
114   if (!bound->break_status_dir ())
115     {
116       Grob *common[] = {
117         bound->common_refpoint (Staff_symbol_referencer::staff_symbol_l (me),
118                                 X_AXIS),
119         bound->common_refpoint (Staff_symbol_referencer::staff_symbol_l (me),
120                                 Y_AXIS)
121       };
122   
123       return Offset (abs (bound->extent (common[X_AXIS], X_AXIS)[-dir]),
124                       bound->extent (common[Y_AXIS], Y_AXIS).center ());
125     }
126   return Offset ();
127 }
128
129 Offset
130 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
131 {
132   /* A broken line-spaner should maintain the same vertical trend
133      the unbroken line-spanner would have had.
134      From slur */
135   Offset o;
136   if (Spanner *mother =  dynamic_cast<Spanner*> (me->original_l_))
137     {
138       for (int i = dir == LEFT ? 0 : mother->broken_into_l_arr_.size () - 1;
139            dir == LEFT ? i < mother->broken_into_l_arr_.size () : i > 0;
140            dir == LEFT ? i++ : i--)
141         {
142           if (mother->broken_into_l_arr_[i - dir] == me)
143             {
144               Grob *neighbour = mother->broken_into_l_arr_[i];
145               Offset neighbour_o = get_broken_offset (neighbour, dir);
146               Offset me_o = get_broken_offset (me, -dir);
147               // Hmm, why not return me_o[X], but recalc in brew_mol?
148               o = Offset (0,
149  (neighbour_o[Y_AXIS]*me_o[X_AXIS]
150                            - me_o[Y_AXIS]*neighbour_o[X_AXIS]) * dir /
151  (me_o[X_AXIS] + neighbour_o[X_AXIS]));
152               break;
153             }
154         }
155     }
156   return o;
157 }
158
159
160 /*
161   Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
162
163  (If not, you risk that this is called from the staff-alignment
164   routine, via molecule_extent. At this point, the staves aren't
165   separated yet, so it doesn't work cross-staff.
166
167 */
168
169 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
170 SCM
171 Line_spanner::brew_molecule (SCM smob) 
172 {
173   Grob *me= unsmob_grob (smob);
174
175   Spanner *spanner = dynamic_cast<Spanner*> (me);
176   Item* bound_drul[] = {
177     spanner->get_bound (LEFT),
178     0,
179     spanner->get_bound (RIGHT)
180   };
181   
182   Item** bound = bound_drul + 1;
183
184   Grob *common[] = { 0, 0 };
185   for (Axis a = X_AXIS;  a < NO_AXES; a = Axis (a + 1))
186     {
187       common[a] = bound[LEFT]->common_refpoint (bound[RIGHT], a);
188       
189       if (!common[a])
190         return SCM_EOL;
191     }
192   
193   Real gap = gh_scm2double (me->get_grob_property ("gap"));
194   Real dist; /*distance between points */
195
196   Offset ofxy (gap, 0); /*offset from start point to start of line*/
197   Offset dxy ;
198   Offset my_off;
199   Offset his_off;
200
201   
202   if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
203     /* across line break */
204     {
205       Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
206
207       dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
208         - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
209       
210       dxy += broken_trend_offset (me, broken);
211       dxy[X_AXIS] -= 1 * gap;
212
213       my_off = Offset (0,
214                        me->relative_coordinate (common[Y_AXIS], Y_AXIS));
215
216       his_off = Offset (0, 
217                         bound[-broken]->relative_coordinate (common[Y_AXIS],
218                                                              Y_AXIS));
219
220       if (broken == LEFT)
221         {
222           my_off[Y_AXIS] += dxy[Y_AXIS];
223         }
224     }
225   else
226     {
227       Real off = gap + ((bound[LEFT]->extent (bound[LEFT], X_AXIS).length ()*3)/4); // distance from center to start of line
228       dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS).center ()
229         - bound[LEFT]->extent (common[X_AXIS], X_AXIS).center ();
230       dxy[Y_AXIS] = bound[RIGHT]->extent (common[Y_AXIS], Y_AXIS).center ()
231         - bound[LEFT]->extent (common[Y_AXIS], Y_AXIS).center ();
232
233       dist = sqrt (dxy[X_AXIS]*dxy[X_AXIS]+dxy[Y_AXIS]*dxy[Y_AXIS]);
234       ofxy = dxy* (off/dist);
235       dxy -= 2*ofxy;
236
237       my_off = Offset (me->relative_coordinate (common[X_AXIS], X_AXIS),
238                        me->relative_coordinate (common[Y_AXIS], Y_AXIS)); 
239       
240       his_off = Offset (bound[LEFT]->relative_coordinate (common[X_AXIS],
241                                                           X_AXIS),
242                         bound[LEFT]->relative_coordinate (common[Y_AXIS],
243                                                           Y_AXIS)); 
244       
245       }
246
247   Real thick = me->paper_l ()->get_var ("linethickness");  
248
249   SCM s = me->get_grob_property ("thickness");
250   if (gh_number_p (s))
251     thick *= gh_scm2double (s);
252
253   
254   Molecule line = line_molecule (me, thick, dxy[X_AXIS], dxy[Y_AXIS]);
255   line.translate_axis (bound[LEFT]->extent (bound[LEFT],
256                                             X_AXIS).length ()/2, X_AXIS); 
257   line.translate (ofxy - my_off + his_off);
258   return line.smobbed_copy ();
259 }
260
261
262
263 ADD_INTERFACE (Line_spanner, "line-spanner-interface",
264   "Generic line drawn between two objects, eg. for use with glissandi.
265 gap is measured in staff-spaces.   ",
266   "gap dash-period dash-length thickness type");
267
268