2 ledger-line-spanner.cc -- implement Ledger_line_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "note-head.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "staff-symbol.hh"
17 #include "group-interface.hh"
18 #include "paper-column.hh"
20 struct Ledger_line_spanner
22 DECLARE_SCHEME_CALLBACK (print, (SCM));
23 DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
24 static Stencil brew_ledger_lines (Grob *me,
31 static bool has_interface (Grob *);
35 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
39 Real ledgerlinethickness,
43 int line_count = ((abs (pos) < interspaces)
45 : (abs (pos) - interspaces) / 2);
49 Real blotdiameter = ledgerlinethickness;
51 = Interval (-0.5* (ledgerlinethickness),
52 +0.5* (ledgerlinethickness));
53 Stencil proto_ledger_line
54 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
56 x_extent[LEFT] += left_shorten;
57 Stencil proto_first_line
58 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
60 Direction dir = (Direction)sign (pos);
61 Real offs = (Staff_symbol_referencer::on_staffline (staff, pos))
65 offs += pos * halfspace;
66 for (int i = 0; i < line_count; i++)
68 Stencil ledger_line ((i == 0)
71 ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
72 stencil.add_stencil (ledger_line);
79 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
81 Ledger_line_spanner::set_spacing_rods (SCM smob)
83 Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
85 // find size of note heads.
86 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
90 SCM heads = me->get_property ("note-heads");
93 Real min_length_fraction
94 = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
97 Drul_array<Interval> current_extents;
98 Drul_array<Interval> previous_extents;
99 Item *previous_column = 0;
100 Item *current_column = 0;
103 Run through heads using a loop. Since Legder_line_spanner can
104 contain a lot of noteheads, we don't use an STL map or set.
106 int interspaces = Staff_symbol::line_count (staff) - 1;
107 for (SCM hp = heads; scm_is_pair (hp); hp = scm_cdr (hp))
109 Item *h = dynamic_cast<Item *> (unsmob_grob (scm_car (hp)));
111 int pos = Staff_symbol_referencer::get_rounded_position (h);
112 if (abs (pos) <= interspaces)
115 Item *column = h->get_column ();
116 if (current_column != column)
121 if (!current_extents[d].is_empty ()
122 && !previous_extents[d].is_empty ())
124 Real total_head_length = previous_extents[d].length ()
125 + current_extents[d].length ();
128 rod.distance_ = total_head_length
129 * (3/2 * min_length_fraction)
131 we go from right to left.
133 - previous_extents[d][LEFT]
134 + current_extents[d][RIGHT];
136 rod.item_drul_[LEFT] = current_column;
137 rod.item_drul_[RIGHT] = previous_column;
141 while (flip (&d) != DOWN);
143 previous_column = current_column;
144 current_column = column;
145 previous_extents = current_extents;
147 current_extents[DOWN].set_empty();
148 current_extents[UP].set_empty();
152 Interval head_extent = h->extent (column, X_AXIS);
153 Direction vdir = Direction (sign (pos));
157 current_extents[vdir].unite (head_extent);
160 return SCM_UNSPECIFIED;
163 struct Ledger_request
165 Interval ledger_extent_;
166 Interval head_extent_;
171 ledger_extent_.set_empty ();
172 head_extent_.set_empty ();
177 typedef std::map < int, Drul_array<Ledger_request> > Ledger_requests;
180 TODO: ledger share a lot of info. Lots of room to optimize away
181 common use of objects/variables.
184 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
186 Ledger_line_spanner::print (SCM smob)
188 Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
189 Link_array<Grob> heads (extract_grob_array (me, ly_symbol2scm ("note-heads")));
191 if (heads.is_empty ())
194 // find size of note heads.
195 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
200 = robust_scm2double (me->get_property ("length-fraction"), 0.25);
203 Stencil default_ledger;
205 Grob *common[NO_AXES];
207 for (int i = X_AXIS; i < NO_AXES; i++)
210 common[a] = common_refpoint_of_array (heads, me, a);
211 for (int i = heads.size (); i--;)
212 if (Grob *g = unsmob_grob (me->get_property ("accidental-grob")))
213 common[a] = common[a]->common_refpoint (g, a);
216 int interspaces = Staff_symbol::line_count (staff) - 1;
217 Ledger_requests reqs;
218 for (int i = heads.size (); i--;)
220 Item *h = dynamic_cast<Item *> (heads[i]);
222 int pos = Staff_symbol_referencer::get_rounded_position (h);
224 && abs (pos) > interspaces)
226 Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
227 Interval ledger_extent = head_extent;
228 ledger_extent.widen (length_fraction * head_extent.length ());
230 Direction vdir = Direction (sign (pos));
231 int rank = Paper_column::get_rank (h->get_column ());
233 reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
234 reqs[rank][vdir].head_extent_.unite (head_extent);
235 reqs[rank][vdir].position_
236 = vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos));
240 // determine maximum size for non-colliding ledger.
241 Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
242 Ledger_requests::iterator last (reqs.end ());
243 for (Ledger_requests::iterator i (reqs.begin ());
244 i != reqs.end (); last = i++)
246 if (last == reqs.end ())
254 if (abs (last->second[d].position_) > interspaces
255 && abs (i->second[d].position_) > interspaces)
258 = (last->second[d].head_extent_[RIGHT]
259 + i->second[d].head_extent_[LEFT]) / 2;
261 Direction which = LEFT;
264 Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d];
266 // due tilt of quarter note-heads
268 = (abs (last->second[d].position_) > interspaces + 1
269 && abs (i->second[d].position_) > interspaces + 1);
271 Real limit = (center + (both? which * gap / 2 : 0));
272 lr.ledger_extent_.elem_ref (-which)
273 = which * (which * lr.ledger_extent_[-which] >? which * limit);
275 while (flip (&which) != LEFT);
278 while (flip (&d) != DOWN);
281 // create ledgers for note heads
282 Real ledgerlinethickness
283 = Staff_symbol::get_ledger_line_thickness (staff);
284 Real halfspace = Staff_symbol::staff_space (staff) / 2;
285 for (int i = heads.size (); i--;)
287 Item *h = dynamic_cast<Item *> (heads[i]);
289 int pos = Staff_symbol_referencer::get_rounded_position (h);
290 if (abs (pos) > interspaces + 1)
292 Interval head_size = h->extent (common[X_AXIS], X_AXIS);
293 Interval ledger_size = head_size;
294 ledger_size.widen (ledger_size.length ()* length_fraction);
296 Interval max_size = reqs[Paper_column::get_rank (h->get_column ())][Direction (sign (pos))].ledger_extent_;
298 ledger_size.intersect (max_size);
299 Real left_shorten = 0.0;
300 if (Grob *g = unsmob_grob (h->get_property ("accidental-grob")))
302 Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
304 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
308 left_shorten = (-ledger_size[LEFT] + d) >? 0;
311 TODO: shorten 2 ledger lines for the case natural +
317 ledgers.add_stencil (brew_ledger_lines (staff, pos, interspaces,
325 ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
328 return ledgers.smobbed_copy ();
331 ADD_INTERFACE (Ledger_line_spanner,
332 "ledger-line-interface",
333 "This spanner draws the ledger lines of a staff, for note heads that stick out. ",
334 "note-heads thickness minimum-length-fraction length-fraction gap");
336 struct Ledgered_interface
338 static bool has_interface (Grob *);
341 ADD_INTERFACE (Ledgered_interface,
342 "ledgered-interface",
343 "Objects that need ledger lines.",