2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "note-head.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "staff-symbol.hh"
27 #include "pointer-group-interface.hh"
28 #include "paper-column.hh"
33 struct Ledger_line_spanner
35 DECLARE_SCHEME_CALLBACK (print, (SCM));
36 DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
40 set_rods (Drul_array<Interval> const ¤t_extents,
41 Drul_array<Interval> const &previous_extents,
43 Item *previous_column,
48 if (!current_extents[d].is_empty ()
49 && !previous_extents[d].is_empty ())
52 rod.distance_ = 2 * min_length
54 we go from right to left.
56 - previous_extents[d][LEFT]
57 + current_extents[d][RIGHT];
59 rod.item_drul_[LEFT] = current_column;
60 rod.item_drul_[RIGHT] = previous_column;
66 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
68 Ledger_line_spanner::set_spacing_rods (SCM smob)
70 Spanner *me = unsmob<Spanner> (smob);
72 // find size of note heads.
73 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
80 Real min_length_fraction
81 = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
83 Drul_array<Interval> current_extents;
84 Drul_array<Interval> previous_extents;
85 Real current_head_width = 0.0;
86 Item *previous_column = 0;
87 Item *current_column = 0;
89 Real halfspace = Staff_symbol::staff_space (staff) / 2;
91 Interval staff_extent = staff->extent (staff, Y_AXIS);
92 staff_extent *= 1 / halfspace;
95 Run through heads using a loop. Since Ledger_line_spanner can
96 contain a lot of noteheads, superlinear performance is too slow.
98 extract_item_set (me, "note-heads", heads);
99 for (vsize i = heads.size (); i--;)
103 int pos = Staff_symbol_referencer::get_rounded_position (h);
104 if (Staff_symbol::ledger_positions (staff, pos).empty ())
107 /* Ambitus heads can appear out-of-order in heads[],
108 * but as part of prefatory matter, they need no rods */
109 if (h->internal_has_interface (ly_symbol2scm ("ambitus-interface")))
112 Item *column = h->get_column ();
113 if (current_column != column)
115 set_rods (current_extents, previous_extents,
116 current_column, previous_column,
117 current_head_width * min_length_fraction);
119 previous_column = current_column;
120 current_column = column;
121 previous_extents = current_extents;
123 current_extents[DOWN].set_empty ();
124 current_extents[UP].set_empty ();
125 current_head_width = 0.0;
128 Interval head_extent = h->extent (column, X_AXIS);
129 Direction vdir = Direction (sign (pos));
133 current_extents[vdir].unite (head_extent);
134 current_head_width = std::max (current_head_width, head_extent.length ());
137 if (previous_column && current_column)
138 set_rods (current_extents, previous_extents,
139 current_column, previous_column,
140 current_head_width * min_length_fraction);
142 return SCM_UNSPECIFIED;
145 struct Ledger_request
147 Interval ledger_extent_;
148 Interval head_extent_;
152 ledger_extent_.set_empty ();
153 head_extent_.set_empty ();
158 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
161 TODO: ledger share a lot of info. Lots of room to optimize away
162 common use of objects/variables.
164 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
166 Ledger_line_spanner::print (SCM smob)
168 Spanner *me = unsmob<Spanner> (smob);
170 extract_grob_set (me, "note-heads", heads);
175 // find size of note heads.
176 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
180 Real halfspace = Staff_symbol::staff_space (staff) / 2;
182 Interval staff_extent = staff->extent (staff, Y_AXIS);
183 staff_extent *= 1 / halfspace;
186 = robust_scm2double (me->get_property ("length-fraction"), 0.25);
190 Grob *common[NO_AXES];
192 for (int i = X_AXIS; i < NO_AXES; i++)
195 common[a] = common_refpoint_of_array (heads, me, a);
196 for (vsize i = heads.size (); i--;)
197 if (Grob *g = unsmob<Grob> (me->get_object ("accidental-grob")))
198 common[a] = common[a]->common_refpoint (g, a);
201 Ledger_requests reqs;
202 for (vsize i = heads.size (); i--;)
204 Item *h = dynamic_cast<Item *> (heads[i]);
206 int pos = Staff_symbol_referencer::get_rounded_position (h);
207 if (pos && !staff_extent.contains (pos))
209 Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
210 Interval ledger_extent = head_extent;
211 ledger_extent.widen (length_fraction * head_extent.length ());
213 Direction vdir = Direction (sign (pos));
214 int rank = h->get_column ()->get_rank ();
216 reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
217 reqs[rank][vdir].head_extent_.unite (head_extent);
218 reqs[rank][vdir].position_
219 = vdir * std::max (vdir * reqs[rank][vdir].position_, vdir * pos);
223 // determine maximum size for non-colliding ledger.
224 Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
225 Ledger_requests::iterator last (reqs.end ());
226 for (Ledger_requests::iterator i (reqs.begin ());
227 i != reqs.end (); last = i++)
229 if (last == reqs.end ())
232 for (DOWN_and_UP (d))
234 if (!staff_extent.contains (last->second[d].position_)
235 && !staff_extent.contains (i->second[d].position_))
238 = (last->second[d].head_extent_[RIGHT]
239 + i->second[d].head_extent_[LEFT]) / 2;
241 for (LEFT_and_RIGHT (which))
243 Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
245 // due tilt of quarter note-heads
248 = (!staff_extent.contains (last->second[d].position_
249 - sign (last->second[d].position_))
250 && !staff_extent.contains (i->second[d].position_
251 - sign (i->second[d].position_)));
252 Real limit = (center + (both ? which * gap / 2 : 0));
253 lr.ledger_extent_.at (-which)
254 = which * std::max (which * lr.ledger_extent_[-which], which * limit);
260 // create ledgers for note heads
261 Real ledgerlinethickness
262 = Staff_symbol::get_ledger_line_thickness (staff);
263 for (vsize i = heads.size (); i--;)
265 Item *h = dynamic_cast<Item *> (heads[i]);
267 int pos = Staff_symbol_referencer::get_rounded_position (h);
268 vector<Real> ledger_positions = Staff_symbol::ledger_positions (staff, pos);
269 if (!ledger_positions.empty ())
271 int ledger_count = ledger_positions.size ();
272 Interval head_size = h->extent (common[X_AXIS], X_AXIS);
273 Interval ledger_size = head_size;
274 ledger_size.widen (ledger_size.length () * length_fraction);
276 if (pos && !staff_extent.contains (pos))
278 Interval max_size = reqs[h->get_column ()->get_rank ()]
279 [Direction (sign (pos))].ledger_extent_;
281 if (!max_size.is_empty ())
282 ledger_size.intersect (max_size);
285 for (int i = 0; i < ledger_count; i++)
287 Real lpos = ledger_positions[i];
288 Interval x_extent = ledger_size;
291 if (Grob *g = unsmob<Grob> (h->get_object ("accidental-grob")))
293 Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
295 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
299 Real left_shorten = std::max (-ledger_size[LEFT] + d, 0.0);
301 x_extent[LEFT] += left_shorten;
303 TODO: shorten 2 ledger lines for the case natural +
308 Real blotdiameter = ledgerlinethickness;
310 = Interval (-0.5 * (ledgerlinethickness),
311 +0.5 * (ledgerlinethickness));
313 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
315 ledger_line.translate_axis ( lpos * halfspace, Y_AXIS);
316 ledgers.add_stencil (ledger_line);
321 ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
324 return ledgers.smobbed_copy ();
327 ADD_INTERFACE (Ledger_line_spanner,
328 "This spanner draws the ledger lines of a staff. This is a"
329 " separate grob because it has to process all potential"
330 " collisions between all note heads. The thickness of ledger"
331 " lines is controlled by the @code{ledger-line-thickness}"
332 " property of the @ref{StaffSymbol} grob.",
337 "minimum-length-fraction "
341 struct Ledgered_interface
345 ADD_INTERFACE (Ledgered_interface,
346 "Objects that need ledger lines, typically note heads. See"
347 " also @ref{ledger-line-spanner-interface}.",