2 line-spanner.cc -- implement Line_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2003 Jan Nieuwenhuizen <janneke@gnu.org>
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"
23 slightishly clumsy interface?
25 Make a Scheme expression for a line going from (0,0) to (dx,dy).
29 line_atom (Grob* me, Real thick, Real dx, Real dy)
31 SCM type = me->get_grob_property ("type");
32 Real staff_space = Staff_symbol_referencer::staff_space (me);
34 // maybe these should be in line-thickness?
35 Real length = staff_space;
36 SCM s = me->get_grob_property ("dash-length");
38 length = gh_scm2double (s) * staff_space;
40 Real period = 2 * length + thick;
41 s = me->get_grob_property ("dash-period");
43 period = gh_scm2double (s) * staff_space;
45 if (type == ly_symbol2scm ("dotted-line"))
48 if (type == ly_symbol2scm ("line"))
49 length = period + thick;
51 Real on = length - thick;
52 Real off = period - on;
54 SCM list = scm_list_n (ly_symbol2scm ("dashed-line"),
55 gh_double2scm (thick),
66 zigzag_atom (Grob* me, Real thick, Real dx, Real dy)
68 Real staff_space = Staff_symbol_referencer::staff_space (me);
69 SCM ws = me->get_grob_property ("zigzag-width");
70 SCM ls = me->get_grob_property ("zigzag-length");
71 double w = (gh_number_p(ws) ? gh_scm2double(ws) : 1)*staff_space;
72 double l = (gh_number_p(ls) ? gh_scm2double(ls) : 1)*w;
73 double h = l>w/2 ? sqrt(l*l-w*w/4) : 0;
75 SCM list = scm_list_n (ly_symbol2scm ("zigzag-line"),
79 gh_double2scm (thick),
90 Line_spanner::line_molecule (Grob* me, Real thick, Real dx, Real dy)
93 SCM type = me->get_grob_property ("type");
94 if (gh_symbol_p (type)
95 && (type == ly_symbol2scm ("line")
96 || type == ly_symbol2scm ("dashed-line")
97 || type == ly_symbol2scm ("dotted-line")
98 || (type == ly_symbol2scm ("trill") && dy != 0)))
100 Box b (Interval (-0.5* thick + (0 <? dx) ,0.5* thick+ (0 >? dx)),
101 Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
102 mol = Molecule (b, line_atom (me, thick, dx, dy));
104 else if (gh_symbol_p (type)
105 && type == ly_symbol2scm ("zigzag"))
108 Box b (Interval (-0.5* thick + (0 <? dx) ,0.5* thick+ (0 >? dx)),
109 Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
110 mol = Molecule (b, zigzag_atom (me, thick, dx, dy));
113 else if (gh_symbol_p (type)
114 && type == ly_symbol2scm ("trill"))
116 SCM alist_chain = Font_interface::font_alist_chain (me);
117 SCM style_chain = scm_list_n (gh_cons (ly_symbol2scm ("font-family"),
118 ly_symbol2scm ("music")),
121 Font_metric *fm = Font_interface::get_font (me,
122 scm_list_n (style_chain,
125 Molecule m = fm->find_by_name ("scripts-trill-element");
127 mol.add_at_edge (X_AXIS, RIGHT, m, 0,0);
128 while (m.extent (X_AXIS).length ()
129 && mol.extent (X_AXIS).length ()
130 + m.extent (X_AXIS).length () < dx);
133 FIXME: should center element on x/y
135 mol.translate_axis (m.extent (X_AXIS).length () / 2, X_AXIS);
136 mol.translate_axis (-(mol.extent (Y_AXIS)[DOWN]
137 + mol.extent (Y_AXIS).length ())/2, Y_AXIS);
143 Line_spanner::get_broken_offset (Grob *me, Direction dir)
145 Spanner *spanner = dynamic_cast<Spanner*> (me);
146 Item* bound = spanner->get_bound (dir);
148 if (!bound->break_status_dir ())
151 bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
153 bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
157 return Offset (abs (bound->extent (common[X_AXIS], X_AXIS)[-dir]),
158 bound->extent (common[Y_AXIS], Y_AXIS).center ());
164 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
166 /* A broken line-spaner should maintain the same vertical trend
167 the unbroken line-spanner would have had.
170 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_))
172 for (int i = dir == LEFT ? 0 : mother->broken_intos_.size () - 1;
173 dir == LEFT ? i < mother->broken_intos_.size () : i > 0;
174 dir == LEFT ? i++ : i--)
176 if (mother->broken_intos_[i - dir] == me)
178 Grob *neighbour = mother->broken_intos_[i];
179 Offset neighbour_o = get_broken_offset (neighbour, dir);
180 Offset me_o = get_broken_offset (me, -dir);
181 // Hmm, why not return me_o[X], but recalc in brew_mol?
183 (neighbour_o[Y_AXIS]*me_o[X_AXIS]
184 - me_o[Y_AXIS]*neighbour_o[X_AXIS]) * dir /
185 (me_o[X_AXIS] + neighbour_o[X_AXIS]));
195 Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
197 (If not, you risk that this is called from the staff-alignment
198 routine, via molecule_extent. At this point, the staves aren't
199 separated yet, so it doesn't work cross-staff.
203 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
205 Line_spanner::brew_molecule (SCM smob)
207 Grob *me= unsmob_grob (smob);
209 Spanner *spanner = dynamic_cast<Spanner*> (me);
210 Item* bound_drul[] = {
211 spanner->get_bound (LEFT),
213 spanner->get_bound (RIGHT)
216 Item** bound = bound_drul + 1;
218 Grob *common[] = { me, me };
219 for (int a = X_AXIS; a < NO_AXES; a++)
221 common[a] = me->common_refpoint (bound[RIGHT], Axis (a));
222 common[a] = common[a]->common_refpoint (bound[LEFT], Axis (a));
226 programming_error ("No common point!");
231 Real gap = gh_scm2double (me->get_grob_property ("gap"));
232 Real dist; /*distance between points */
234 Offset ofxy (gap, 0); /*offset from start point to start of line*/
240 if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
241 /* across line break */
243 Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
245 dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
246 - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
248 dxy += broken_trend_offset (me, broken);
249 dxy[X_AXIS] -= 1 * gap;
252 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
255 bound[-broken]->relative_coordinate (common[Y_AXIS],
260 my_off[Y_AXIS] += dxy[Y_AXIS];
265 Real off = gap + ((bound[LEFT]->extent (bound[LEFT], X_AXIS).length ()*3)/4); // distance from center to start of line
267 for (int a = X_AXIS; a < NO_AXES; a++)
271 + bound[RIGHT]->extent (common[X_AXIS], ax).center ()
272 - bound[LEFT]->extent (common[X_AXIS], ax).center ();
274 my_off[ax] =me->relative_coordinate (common[a], ax);
275 his_off[ax] = bound[LEFT]->relative_coordinate (common[a], ax);
279 ofxy = dxy * (off/dxy.length ());
283 Real thick = me->get_paper ()->get_var ("linethickness");
285 SCM s = me->get_grob_property ("thickness");
287 thick *= gh_scm2double (s);
290 Molecule line = line_molecule (me, thick, dxy[X_AXIS], dxy[Y_AXIS]);
291 line.translate_axis (bound[LEFT]->extent (bound[LEFT],
292 X_AXIS).length ()/2, X_AXIS);
293 line.translate (ofxy - my_off + his_off);
294 return line.smobbed_copy ();
299 ADD_INTERFACE (Line_spanner, "line-spanner-interface",
300 "Generic line drawn between two objects, eg. for use with glissandi.\n"
301 "gap is measured in staff-spaces.\n"
302 "The property 'type is one of: line, dashed-line, trill, dotted-line or zigzag.\n"
304 "gap dash-period dash-length zigzag-width zigzag-length thickness type");