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