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