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