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"
22 slightishly clumsy interface?
24 Make a Scheme expression for a line going from (0,0) to (dx,dy).
28 line_atom (Grob* me, Real thick, Real dx, Real dy)
30 SCM type = me->get_grob_property ("type");
31 Real staff_space = Staff_symbol_referencer::staff_space (me);
33 // maybe these should be in line-thickness?
34 Real length = staff_space;
35 SCM s = me->get_grob_property ("dash-length");
37 length = gh_scm2double (s) * staff_space;
39 Real period = 2 * length + thick;
40 s = me->get_grob_property ("dash-period");
42 period = gh_scm2double (s) * staff_space;
44 if (type == ly_symbol2scm ("dotted-line"))
47 if (type == ly_symbol2scm ("line"))
48 length = period + thick;
50 Real on = length - thick;
51 Real off = period - on;
53 SCM list = scm_list_n (ly_symbol2scm ("dashed-line"),
54 gh_double2scm (thick),
65 zigzag_atom (Grob* me, Real thick, Real dx, Real dy)
67 Real staff_space = Staff_symbol_referencer::staff_space (me);
68 SCM ws = me->get_grob_property ("zigzag-width");
69 SCM ls = me->get_grob_property ("zigzag-length");
70 double w = (gh_number_p(ws) ? gh_scm2double(ws) : 1)*staff_space;
71 double l = (gh_number_p(ls) ? gh_scm2double(ls) : 1)*w;
72 double h = l>w/2 ? sqrt(l*l-w*w/4) : 0;
74 SCM list = scm_list_n (ly_symbol2scm ("zigzag-line"),
78 gh_double2scm (thick),
89 Line_spanner::line_molecule (Grob* me, Real thick, Real dx, Real dy)
92 SCM type = me->get_grob_property ("type");
93 if (gh_symbol_p (type)
94 && (type == ly_symbol2scm ("line")
95 || type == ly_symbol2scm ("dashed-line")
96 || type == ly_symbol2scm ("dotted-line")
97 || (type == ly_symbol2scm ("trill") && dy != 0)))
99 Box b (Interval (-0.5* thick + (0 <? dx) ,0.5* thick+ (0 >? dx)),
100 Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
101 mol = Molecule (b, line_atom (me, thick, dx, dy));
103 else if (gh_symbol_p (type)
104 && type == ly_symbol2scm ("zigzag"))
107 Box b (Interval (-0.5* thick + (0 <? dx) ,0.5* thick+ (0 >? dx)),
108 Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
109 mol = Molecule (b, zigzag_atom (me, thick, dx, dy));
112 else if (gh_symbol_p (type)
113 && type == ly_symbol2scm ("trill"))
115 SCM alist_chain = Font_interface::font_alist_chain (me);
116 SCM style_chain = scm_list_n (gh_cons (ly_symbol2scm ("font-family"),
117 ly_symbol2scm ("music")),
120 Font_metric *fm = Font_interface::get_font (me,
121 scm_list_n (style_chain,
124 Molecule m = fm->find_by_name ("scripts-trill-element");
126 mol.add_at_edge (X_AXIS, RIGHT, m, 0,0);
127 while (m.extent (X_AXIS).length ()
128 && mol.extent (X_AXIS).length ()
129 + m.extent (X_AXIS).length () < dx);
132 FIXME: should center element on x/y
134 mol.translate_axis (m.extent (X_AXIS).length () / 2, X_AXIS);
135 mol.translate_axis (-(mol.extent (Y_AXIS)[DOWN]
136 + mol.extent (Y_AXIS).length ())/2, Y_AXIS);
142 Line_spanner::get_broken_offset (Grob *me, Direction dir)
144 Spanner *spanner = dynamic_cast<Spanner*> (me);
145 Item* bound = spanner->get_bound (dir);
147 if (!bound->break_status_dir ())
150 bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
152 bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
156 return Offset (abs (bound->extent (common[X_AXIS], X_AXIS)[-dir]),
157 bound->extent (common[Y_AXIS], Y_AXIS).center ());
163 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
165 /* A broken line-spaner should maintain the same vertical trend
166 the unbroken line-spanner would have had.
169 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_))
171 for (int i = dir == LEFT ? 0 : mother->broken_intos_.size () - 1;
172 dir == LEFT ? i < mother->broken_intos_.size () : i > 0;
173 dir == LEFT ? i++ : i--)
175 if (mother->broken_intos_[i - dir] == me)
177 Grob *neighbour = mother->broken_intos_[i];
178 Offset neighbour_o = get_broken_offset (neighbour, dir);
179 Offset me_o = get_broken_offset (me, -dir);
180 // Hmm, why not return me_o[X], but recalc in brew_mol?
182 (neighbour_o[Y_AXIS]*me_o[X_AXIS]
183 - me_o[Y_AXIS]*neighbour_o[X_AXIS]) * dir /
184 (me_o[X_AXIS] + neighbour_o[X_AXIS]));
194 Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
196 (If not, you risk that this is called from the staff-alignment
197 routine, via molecule_extent. At this point, the staves aren't
198 separated yet, so it doesn't work cross-staff.
202 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
204 Line_spanner::brew_molecule (SCM smob)
206 Grob *me= unsmob_grob (smob);
208 Spanner *spanner = dynamic_cast<Spanner*> (me);
209 Item* bound_drul[] = {
210 spanner->get_bound (LEFT),
212 spanner->get_bound (RIGHT)
215 Item** bound = bound_drul + 1;
217 Grob *common[] = { 0, 0 };
218 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
220 common[a] = bound[LEFT]->common_refpoint (bound[RIGHT], a);
226 Real gap = gh_scm2double (me->get_grob_property ("gap"));
227 Real dist; /*distance between points */
229 Offset ofxy (gap, 0); /*offset from start point to start of line*/
235 if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
236 /* across line break */
238 Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
240 dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
241 - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
243 dxy += broken_trend_offset (me, broken);
244 dxy[X_AXIS] -= 1 * gap;
247 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
250 bound[-broken]->relative_coordinate (common[Y_AXIS],
255 my_off[Y_AXIS] += dxy[Y_AXIS];
260 Real off = gap + ((bound[LEFT]->extent (bound[LEFT], X_AXIS).length ()*3)/4); // distance from center to start of line
261 dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS).center ()
262 - bound[LEFT]->extent (common[X_AXIS], X_AXIS).center ();
263 dxy[Y_AXIS] = bound[RIGHT]->extent (common[Y_AXIS], Y_AXIS).center ()
264 - bound[LEFT]->extent (common[Y_AXIS], Y_AXIS).center ();
266 dist = sqrt (dxy[X_AXIS]*dxy[X_AXIS]+dxy[Y_AXIS]*dxy[Y_AXIS]);
267 ofxy = dxy* (off/dist);
270 my_off = Offset (me->relative_coordinate (common[X_AXIS], X_AXIS),
271 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
273 his_off = Offset (bound[LEFT]->relative_coordinate (common[X_AXIS],
275 bound[LEFT]->relative_coordinate (common[Y_AXIS],
280 Real thick = me->get_paper ()->get_var ("linethickness");
282 SCM s = me->get_grob_property ("thickness");
284 thick *= gh_scm2double (s);
287 Molecule line = line_molecule (me, thick, dxy[X_AXIS], dxy[Y_AXIS]);
288 line.translate_axis (bound[LEFT]->extent (bound[LEFT],
289 X_AXIS).length ()/2, X_AXIS);
290 line.translate (ofxy - my_off + his_off);
291 return line.smobbed_copy ();
296 ADD_INTERFACE (Line_spanner, "line-spanner-interface",
297 "Generic line drawn between two objects, eg. for use with glissandi.\n"
298 "gap is measured in staff-spaces.\n"
299 "The property 'type is one of: line, dashed-line, trill, dotted-line or zigzag.\n"
301 "gap dash-period dash-length zigzag-width zigzag-length thickness type");