lilypond-1.3.122
[lilypond.git] / lily / line-spanner.cc
blob38fa223dc09706194b3c653622ebdf4a8db24a80
1 /*
2 line-spanner.cc -- implement Line_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
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"
17 SCM
18 Line_spanner::line_atom (Grob* me, Real dx, Real dy)
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")))
27 Real staff_space = Staff_symbol_referencer::staff_space (me);
28 Real thick = me->paper_l ()->get_var ("stafflinethickness");
30 SCM s = me->get_grob_property ("line-thickness");
31 if (gh_number_p (s))
32 thick *= gh_scm2double (s);
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;
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;
45 if (type == ly_symbol2scm ("dotted-line"))
46 length = thick;
48 if (type == ly_symbol2scm ("line"))
49 length = period + thick;
51 Real on = length - thick;
52 Real off = period - on;
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);
62 return list;
65 Offset
66 Line_spanner::get_broken_offset (Grob *me, Direction dir)
68 Spanner *spanner = dynamic_cast<Spanner*> (me);
69 Item* bound = spanner->get_bound (dir);
71 if (!bound->break_status_dir ())
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)
80 return Offset ( abs (bound->extent (common[X_AXIS], X_AXIS)[-dir]),
81 bound->extent (common[Y_AXIS], Y_AXIS).center ());
83 return Offset ();
86 Offset
87 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
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_))
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--)
99 if (mother->broken_into_l_arr_[i - dir] == me)
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;
113 return o;
118 Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
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.
126 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
128 Line_spanner::brew_molecule (SCM smob)
130 Grob *me= unsmob_grob (smob);
132 Spanner *spanner = dynamic_cast<Spanner*> (me);
133 Item* bound_drul[] = {
134 spanner->get_bound (LEFT),
136 spanner->get_bound (RIGHT)
139 Item** bound = bound_drul + 1;
141 Grob *common[] = { 0, 0 };
142 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
144 common[a] = bound[LEFT]->common_refpoint (bound[RIGHT], a);
146 if (!common[a])
147 return SCM_EOL;
150 Real gap = gh_scm2double (me->get_grob_property ("gap"));
152 Offset dxy ;
153 Offset my_off;
154 Offset his_off;
156 if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
157 /* across line break */
159 Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
161 dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
162 - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
164 dxy += broken_trend_offset (me, broken);
165 dxy[X_AXIS] -= 1 * gap;
167 my_off = Offset (0,
168 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
170 his_off = Offset (0,
171 bound[-broken]->relative_coordinate (common[Y_AXIS],
172 Y_AXIS));
174 if (broken == LEFT)
176 my_off[Y_AXIS] += dxy[Y_AXIS];
179 else
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;
187 my_off = Offset (me->relative_coordinate (common[X_AXIS], X_AXIS),
188 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
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));
196 Molecule line;
197 SCM list = Line_spanner::line_atom (me, dxy[X_AXIS], dxy[Y_AXIS]);
199 if (list == SCM_EOL)
200 return SCM_EOL;
202 Box b (Interval (0, dxy[X_AXIS]), Interval (0, dxy[Y_AXIS]));
204 line = Molecule (b, list);
205 line.translate_axis (bound[LEFT]->extent (bound[LEFT], X_AXIS).length (), X_AXIS);
207 Offset g (gap, 0);
208 line.translate (g - my_off + his_off);
210 return line.smobbed_copy ();