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);
127 Real min_length_fraction
128 = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
130 Drul_array<Interval> current_extents;
131 Drul_array<Interval> previous_extents;
132 Item *previous_column = 0;
133 Item *current_column = 0;
135 Real halfspace = Staff_symbol::staff_space (staff) / 2;
137 Interval staff_extent = staff->extent (staff, Y_AXIS);
138 staff_extent *= 1 / halfspace;
141 Run through heads using a loop. Since Ledger_line_spanner can
142 contain a lot of noteheads, superlinear performance is too slow.
144 extract_item_set (me, "note-heads", heads);
145 for (vsize i = heads.size (); i--;)
149 int pos = Staff_symbol_referencer::get_rounded_position (h);
150 if (staff_extent.contains (pos))
153 Item *column = h->get_column ();
154 if (current_column != column)
156 set_rods (current_extents, previous_extents,
157 current_column, previous_column,
158 min_length_fraction);
160 previous_column = current_column;
161 current_column = column;
162 previous_extents = current_extents;
164 current_extents[DOWN].set_empty ();
165 current_extents[UP].set_empty ();
168 Interval head_extent = h->extent (column, X_AXIS);
169 Direction vdir = Direction (sign (pos));
173 current_extents[vdir].unite (head_extent);
176 if (previous_column && current_column)
177 set_rods (current_extents, previous_extents,
178 current_column, previous_column,
179 min_length_fraction);
181 return SCM_UNSPECIFIED;
184 struct Ledger_request
186 Interval ledger_extent_;
187 Interval head_extent_;
192 ledger_extent_.set_empty ();
193 head_extent_.set_empty ();
198 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
201 TODO: ledger share a lot of info. Lots of room to optimize away
202 common use of objects/variables.
204 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
206 Ledger_line_spanner::print (SCM smob)
208 Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
210 extract_grob_set (me, "note-heads", heads);
215 // find size of note heads.
216 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
220 Real halfspace = Staff_symbol::staff_space (staff) / 2;
222 Interval staff_extent = staff->extent (staff, Y_AXIS);
223 staff_extent *= 1 / halfspace;
226 = robust_scm2double (me->get_property ("length-fraction"), 0.25);
229 Stencil default_ledger;
231 Grob *common[NO_AXES];
233 for (int i = X_AXIS; i < NO_AXES; i++)
236 common[a] = common_refpoint_of_array (heads, me, a);
237 for (vsize i = heads.size (); i--;)
238 if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
239 common[a] = common[a]->common_refpoint (g, a);
242 Ledger_requests reqs;
243 for (vsize i = heads.size (); i--;)
245 Item *h = dynamic_cast<Item *> (heads[i]);
247 int pos = Staff_symbol_referencer::get_rounded_position (h);
248 if (pos && !staff_extent.contains (pos))
250 Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
251 Interval ledger_extent = head_extent;
252 ledger_extent.widen (length_fraction * head_extent.length ());
254 Direction vdir = Direction (sign (pos));
255 int rank = h->get_column ()->get_rank ();
257 reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
258 reqs[rank][vdir].head_extent_.unite (head_extent);
259 reqs[rank][vdir].position_
260 = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
264 // determine maximum size for non-colliding ledger.
265 Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
266 Ledger_requests::iterator last (reqs.end ());
267 for (Ledger_requests::iterator i (reqs.begin ());
268 i != reqs.end (); last = i++)
270 if (last == reqs.end ())
276 if (!staff_extent.contains (last->second[d].position_)
277 && !staff_extent.contains (i->second[d].position_))
280 = (last->second[d].head_extent_[RIGHT]
281 + i->second[d].head_extent_[LEFT]) / 2;
283 Direction which = LEFT;
286 Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
288 // due tilt of quarter note-heads
291 = (!staff_extent.contains (last->second[d].position_
292 - sign (last->second[d].position_))
293 && !staff_extent.contains (i->second[d].position_
294 - sign (i->second[d].position_)));
295 Real limit = (center + (both ? which * gap / 2 : 0));
296 lr.ledger_extent_.at (-which)
297 = which * max (which * lr.ledger_extent_[-which], which * limit);
299 while (flip (&which) != LEFT);
302 while (flip (&d) != DOWN);
305 // create ledgers for note heads
306 Real ledgerlinethickness
307 = Staff_symbol::get_ledger_line_thickness (staff);
308 for (vsize i = heads.size (); i--;)
310 Item *h = dynamic_cast<Item *> (heads[i]);
312 int pos = Staff_symbol_referencer::get_rounded_position (h);
313 if (!staff_extent.contains (pos - sign (pos)))
315 Interval head_size = h->extent (common[X_AXIS], X_AXIS);
316 Interval ledger_size = head_size;
317 ledger_size.widen (ledger_size.length () * length_fraction);
319 Interval max_size = reqs[h->get_column ()->get_rank ()]
320 [Direction (sign (pos))].ledger_extent_;
322 ledger_size.intersect (max_size);
323 Real left_shorten = 0.0;
324 if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
326 Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
328 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
332 left_shorten = max (-ledger_size[LEFT] + d, 0.0);
335 TODO: shorten 2 ledger lines for the case natural +
340 ledgers.add_stencil (brew_ledger_lines (staff, pos, staff_extent,
348 ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
351 return ledgers.smobbed_copy ();
354 ADD_INTERFACE (Ledger_line_spanner,
355 "ledger-line-interface",
357 "This spanner draws the ledger lines of a staff.\n"
358 "This is a separate grob because it has to process\n"
359 "all potential collisions between all note heads.",
364 "minimum-length-fraction "
368 struct Ledgered_interface
370 static bool has_interface (Grob *);
373 ADD_INTERFACE (Ledgered_interface,
374 "ledgered-interface",
376 "Objects that need ledger lines, typically "
377 "note heads. See also @ref{ledger-line-interface}.",