2 ledger-line-spanner.cc -- implement Ledger_line_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include "note-head.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "staff-symbol.hh"
18 #include "pointer-group-interface.hh"
19 #include "paper-column.hh"
21 struct Ledger_line_spanner
23 DECLARE_SCHEME_CALLBACK (print, (SCM));
24 DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
25 static Stencil brew_ledger_lines (Grob *me,
32 static bool has_interface (Grob *);
36 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
38 Interval staff_extent,
40 Real ledgerlinethickness,
44 int line_count = (staff_extent.contains (pos)
46 : sign (pos) * int (rint(pos - staff_extent[Direction (sign (pos))])) / 2);
50 Real blotdiameter = ledgerlinethickness;
52 = Interval (-0.5 * (ledgerlinethickness),
53 +0.5 * (ledgerlinethickness));
54 Stencil proto_ledger_line
55 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
57 x_extent[LEFT] += left_shorten;
58 Stencil proto_first_line
59 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
61 Direction dir = (Direction)sign (pos);
62 Real offs = (Staff_symbol_referencer::on_line (staff, pos))
66 offs += pos * halfspace;
67 for (int i = 0; i < line_count; i++)
69 Stencil ledger_line ((i == 0)
72 ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
73 stencil.add_stencil (ledger_line);
81 set_rods (Drul_array<Interval> const ¤t_extents,
82 Drul_array<Interval> const &previous_extents,
84 Item *previous_column,
85 Real min_length_fraction)
90 if (!current_extents[d].is_empty ()
91 && !previous_extents[d].is_empty ())
93 Real total_head_length = previous_extents[d].length ()
94 + current_extents[d].length ();
97 rod.distance_ = total_head_length
98 * (3 / 2 * min_length_fraction)
100 we go from right to left.
102 - previous_extents[d][LEFT]
103 + current_extents[d][RIGHT];
105 rod.item_drul_[LEFT] = current_column;
106 rod.item_drul_[RIGHT] = previous_column;
110 while (flip (&d) != DOWN);
113 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
115 Ledger_line_spanner::set_spacing_rods (SCM smob)
117 Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
119 // find size of note heads.
120 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
124 Real min_length_fraction
125 = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
127 Drul_array<Interval> current_extents;
128 Drul_array<Interval> previous_extents;
129 Item *previous_column = 0;
130 Item *current_column = 0;
132 Real halfspace = Staff_symbol::staff_space (staff) / 2;
134 Interval staff_extent = staff->extent (staff, Y_AXIS);
135 staff_extent *= 1 / halfspace;
138 Run through heads using a loop. Since Ledger_line_spanner can
139 contain a lot of noteheads, superlinear performance is too slow.
141 extract_item_set (me, "note-heads", heads);
142 for (int i = heads.size (); i--;)
146 int pos = Staff_symbol_referencer::get_rounded_position (h);
147 if (staff_extent.contains (pos))
150 Item *column = h->get_column ();
151 if (current_column != column)
153 set_rods (current_extents, previous_extents,
154 current_column, previous_column,
155 min_length_fraction);
157 previous_column = current_column;
158 current_column = column;
159 previous_extents = current_extents;
161 current_extents[DOWN].set_empty ();
162 current_extents[UP].set_empty ();
165 Interval head_extent = h->extent (column, X_AXIS);
166 Direction vdir = Direction (sign (pos));
170 current_extents[vdir].unite (head_extent);
173 if (previous_column && current_column)
174 set_rods (current_extents, previous_extents,
175 current_column, previous_column,
176 min_length_fraction);
178 return SCM_UNSPECIFIED;
181 struct Ledger_request
183 Interval ledger_extent_;
184 Interval head_extent_;
189 ledger_extent_.set_empty ();
190 head_extent_.set_empty ();
195 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
198 TODO: ledger share a lot of info. Lots of room to optimize away
199 common use of objects/variables.
201 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
203 Ledger_line_spanner::print (SCM smob)
205 Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
207 extract_grob_set (me, "note-heads", heads);
209 if (heads.is_empty ())
212 // find size of note heads.
213 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
217 Real halfspace = Staff_symbol::staff_space (staff) / 2;
219 Interval staff_extent = staff->extent (staff, Y_AXIS);
220 staff_extent *= 1 / halfspace;
223 = robust_scm2double (me->get_property ("length-fraction"), 0.25);
226 Stencil default_ledger;
228 Grob *common[NO_AXES];
230 for (int i = X_AXIS; i < NO_AXES; i++)
233 common[a] = common_refpoint_of_array (heads, me, a);
234 for (int i = heads.size (); i--;)
235 if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
236 common[a] = common[a]->common_refpoint (g, a);
239 Ledger_requests reqs;
240 for (int i = heads.size (); i--;)
242 Item *h = dynamic_cast<Item *> (heads[i]);
244 int pos = Staff_symbol_referencer::get_rounded_position (h);
245 if (pos && !staff_extent.contains (pos))
247 Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
248 Interval ledger_extent = head_extent;
249 ledger_extent.widen (length_fraction * head_extent.length ());
251 Direction vdir = Direction (sign (pos));
252 int rank = h->get_column ()->get_rank ();
254 reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
255 reqs[rank][vdir].head_extent_.unite (head_extent);
256 reqs[rank][vdir].position_
257 = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
261 // determine maximum size for non-colliding ledger.
262 Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
263 Ledger_requests::iterator last (reqs.end ());
264 for (Ledger_requests::iterator i (reqs.begin ());
265 i != reqs.end (); last = i++)
267 if (last == reqs.end ())
273 if (!staff_extent.contains (last->second[d].position_)
274 && !staff_extent.contains (i->second[d].position_))
277 = (last->second[d].head_extent_[RIGHT]
278 + i->second[d].head_extent_[LEFT]) / 2;
280 Direction which = LEFT;
283 Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
285 // due tilt of quarter note-heads
288 = (!staff_extent.contains (last->second[d].position_
289 - sign (last->second[d].position_))
290 && !staff_extent.contains (i->second[d].position_
291 - sign (i->second[d].position_)));
292 Real limit = (center + (both ? which * gap / 2 : 0));
293 lr.ledger_extent_.elem_ref (-which)
294 = which * max (which * lr.ledger_extent_[-which], which * limit);
296 while (flip (&which) != LEFT);
299 while (flip (&d) != DOWN);
302 // create ledgers for note heads
303 Real ledgerlinethickness
304 = Staff_symbol::get_ledger_line_thickness (staff);
305 for (int i = heads.size (); i--;)
307 Item *h = dynamic_cast<Item *> (heads[i]);
309 int pos = Staff_symbol_referencer::get_rounded_position (h);
310 if (!staff_extent.contains (pos - sign (pos)))
312 Interval head_size = h->extent (common[X_AXIS], X_AXIS);
313 Interval ledger_size = head_size;
314 ledger_size.widen (ledger_size.length () * length_fraction);
316 Interval max_size = reqs[h->get_column ()->get_rank ()]
317 [Direction (sign (pos))].ledger_extent_;
319 ledger_size.intersect (max_size);
320 Real left_shorten = 0.0;
321 if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
323 Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
325 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
329 left_shorten = max (-ledger_size[LEFT] + d, 0.0);
332 TODO: shorten 2 ledger lines for the case natural +
337 ledgers.add_stencil (brew_ledger_lines (staff, pos, staff_extent,
345 ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
348 return ledgers.smobbed_copy ();
351 ADD_INTERFACE (Ledger_line_spanner,
352 "ledger-line-interface",
354 "This spanner draws the ledger lines of a staff.\n"
355 "This is a separate grob because it has to process\n"
356 "all potential collisions between all note heads.",
361 "minimum-length-fraction "
365 struct Ledgered_interface
367 static bool has_interface (Grob *);
370 ADD_INTERFACE (Ledgered_interface,
371 "ledgered-interface",
373 "Objects that need ledger lines, typically "
374 "note heads. See also @ref{ledger-line-interface}.",