2 line-spanner.cc -- implement Line_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2002 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 Line_spanner::line_molecule (Grob* me, Real thick, Real dx, Real dy)
68 SCM type = me->get_grob_property ("type");
69 if (gh_symbol_p (type)
70 && (type == ly_symbol2scm ("line")
71 || type == ly_symbol2scm ("dashed-line")
72 || type == ly_symbol2scm ("dotted-line")
73 || (type == ly_symbol2scm ("trill") && dy != 0)))
75 Box b (Interval (-0.5* thick + (0 <? dx) ,0.5* thick+ (0 >? dx)),
76 Interval (- 0.5* thick + (0<? dy), 0.5*thick + (0 >? dy)));
77 mol = Molecule (b, line_atom (me, thick, dx, dy));
79 else if (gh_symbol_p (type)
80 && type == ly_symbol2scm ("trill"))
82 SCM alist_chain = Font_interface::font_alist_chain (me);
83 SCM style_chain = scm_list_n (gh_cons (ly_symbol2scm ("font-family"),
84 ly_symbol2scm ("music")),
87 Font_metric *fm = Font_interface::get_font (me,
88 scm_list_n (style_chain,
91 Molecule m = fm->find_by_name ("scripts-trill-element");
93 mol.add_at_edge (X_AXIS, RIGHT, m, 0);
94 while (m.extent (X_AXIS).length ()
95 && mol.extent (X_AXIS).length ()
96 + m.extent (X_AXIS).length () < dx);
99 FIXME: should center element on x/y
101 mol.translate_axis (m.extent (X_AXIS).length () / 2, X_AXIS);
102 mol.translate_axis (-(mol.extent (Y_AXIS)[DOWN]
103 + mol.extent (Y_AXIS).length ())/2, Y_AXIS);
109 Line_spanner::get_broken_offset (Grob *me, Direction dir)
111 Spanner *spanner = dynamic_cast<Spanner*> (me);
112 Item* bound = spanner->get_bound (dir);
114 if (!bound->break_status_dir ())
117 bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
119 bound->common_refpoint (Staff_symbol_referencer::get_staff_symbol (me),
123 return Offset (abs (bound->extent (common[X_AXIS], X_AXIS)[-dir]),
124 bound->extent (common[Y_AXIS], Y_AXIS).center ());
130 Line_spanner::broken_trend_offset (Grob *me, Direction dir)
132 /* A broken line-spaner should maintain the same vertical trend
133 the unbroken line-spanner would have had.
136 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_))
138 for (int i = dir == LEFT ? 0 : mother->broken_intos_.size () - 1;
139 dir == LEFT ? i < mother->broken_intos_.size () : i > 0;
140 dir == LEFT ? i++ : i--)
142 if (mother->broken_intos_[i - dir] == me)
144 Grob *neighbour = mother->broken_intos_[i];
145 Offset neighbour_o = get_broken_offset (neighbour, dir);
146 Offset me_o = get_broken_offset (me, -dir);
147 // Hmm, why not return me_o[X], but recalc in brew_mol?
149 (neighbour_o[Y_AXIS]*me_o[X_AXIS]
150 - me_o[Y_AXIS]*neighbour_o[X_AXIS]) * dir /
151 (me_o[X_AXIS] + neighbour_o[X_AXIS]));
161 Warning: this thing is a cross-staff object, so it should have empty Y-dimensions.
163 (If not, you risk that this is called from the staff-alignment
164 routine, via molecule_extent. At this point, the staves aren't
165 separated yet, so it doesn't work cross-staff.
169 MAKE_SCHEME_CALLBACK (Line_spanner, brew_molecule, 1);
171 Line_spanner::brew_molecule (SCM smob)
173 Grob *me= unsmob_grob (smob);
175 Spanner *spanner = dynamic_cast<Spanner*> (me);
176 Item* bound_drul[] = {
177 spanner->get_bound (LEFT),
179 spanner->get_bound (RIGHT)
182 Item** bound = bound_drul + 1;
184 Grob *common[] = { 0, 0 };
185 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
187 common[a] = bound[LEFT]->common_refpoint (bound[RIGHT], a);
193 Real gap = gh_scm2double (me->get_grob_property ("gap"));
194 Real dist; /*distance between points */
196 Offset ofxy (gap, 0); /*offset from start point to start of line*/
202 if (bound[LEFT]->break_status_dir () || bound[RIGHT]->break_status_dir ())
203 /* across line break */
205 Direction broken = bound[LEFT]->break_status_dir () ? LEFT : RIGHT;
207 dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS)[LEFT]
208 - bound[LEFT]->extent (common[X_AXIS], X_AXIS)[RIGHT];
210 dxy += broken_trend_offset (me, broken);
211 dxy[X_AXIS] -= 1 * gap;
214 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
217 bound[-broken]->relative_coordinate (common[Y_AXIS],
222 my_off[Y_AXIS] += dxy[Y_AXIS];
227 Real off = gap + ((bound[LEFT]->extent (bound[LEFT], X_AXIS).length ()*3)/4); // distance from center to start of line
228 dxy[X_AXIS] = bound[RIGHT]->extent (common[X_AXIS], X_AXIS).center ()
229 - bound[LEFT]->extent (common[X_AXIS], X_AXIS).center ();
230 dxy[Y_AXIS] = bound[RIGHT]->extent (common[Y_AXIS], Y_AXIS).center ()
231 - bound[LEFT]->extent (common[Y_AXIS], Y_AXIS).center ();
233 dist = sqrt (dxy[X_AXIS]*dxy[X_AXIS]+dxy[Y_AXIS]*dxy[Y_AXIS]);
234 ofxy = dxy* (off/dist);
237 my_off = Offset (me->relative_coordinate (common[X_AXIS], X_AXIS),
238 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
240 his_off = Offset (bound[LEFT]->relative_coordinate (common[X_AXIS],
242 bound[LEFT]->relative_coordinate (common[Y_AXIS],
247 Real thick = me->get_paper ()->get_var ("linethickness");
249 SCM s = me->get_grob_property ("thickness");
251 thick *= gh_scm2double (s);
254 Molecule line = line_molecule (me, thick, dxy[X_AXIS], dxy[Y_AXIS]);
255 line.translate_axis (bound[LEFT]->extent (bound[LEFT],
256 X_AXIS).length ()/2, X_AXIS);
257 line.translate (ofxy - my_off + his_off);
258 return line.smobbed_copy ();
263 ADD_INTERFACE (Line_spanner, "line-spanner-interface",
264 "Generic line drawn between two objects, eg. for use with glissandi.
265 gap is measured in staff-spaces. ",
266 "gap dash-period dash-length thickness type");