2 ledger-line-spanner.cc -- implement Ledger_line_spanner
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include "note-head.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "staff-symbol.hh"
18 #include "group-interface.hh"
19 #include "paper-column.hh"
21 struct Ledger_line_spanner
23 DECLARE_SCHEME_CALLBACK (print, (SCM ));
24 static Stencil brew_ledger_lines (Grob *me,
31 static bool has_interface (Grob*);
36 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
40 Real ledgerlinethickness,
44 int line_count = ((abs (pos) < interspaces)
46 : (abs (pos) - interspaces) / 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_staffline (staff, pos))
67 offs += pos * halfspace;
68 for (int i = 0; i < line_count; i++)
70 Stencil ledger_line ((i == 0)
74 ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
75 stencil.add_stencil (ledger_line);
85 Interval ledger_extent_;
86 Interval head_extent_;
90 ledger_extent_.set_empty ();
91 head_extent_.set_empty ();
96 typedef std::map<int, Drul_array<Ledger_request> > Ledger_requests;
100 TODO: ledger share a lot of info. Lots of room to optimize away common
101 use of objects/variables.
103 MAKE_SCHEME_CALLBACK (Ledger_line_spanner,print,1);
105 Ledger_line_spanner::print (SCM smob)
107 Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (smob));
108 Link_array<Grob> heads (Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-heads"));
110 if (heads.is_empty ())
113 // find size of note heads.
114 Grob * staff = Staff_symbol_referencer::get_staff_symbol (me);
120 Stencil default_ledger;
122 Grob * common[NO_AXES];
124 for (int i = X_AXIS; i < NO_AXES; i++)
127 common[a] = common_refpoint_of_array (heads, me, a);
128 for (int i = heads.size (); i--; )
129 if (Grob * g = unsmob_grob (me->get_property ("accidental-grob")))
130 common[a] = common[a]->common_refpoint (g, a);
133 int interspaces = Staff_symbol::line_count (staff)-1;
134 Ledger_requests reqs;
135 Real length_fraction = 0.25;
136 for (int i = heads.size (); i--; )
138 Item *h = dynamic_cast<Item*> (heads[i]);
140 int pos = Staff_symbol_referencer::get_rounded_position (h);
142 && abs (pos) > interspaces)
144 Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
145 Interval ledger_extent = head_extent;
146 ledger_extent.widen (length_fraction * head_extent.length ());
148 Direction vdir = Direction (sign (pos));
149 int rank = Paper_column::get_rank (h->get_column ());
151 reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
152 reqs[rank][vdir].head_extent_.unite (head_extent);
153 reqs[rank][vdir].position_ =
154 vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos));
158 // determine maximum size for non-colliding ledger.
159 Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
160 Ledger_requests::iterator last (reqs.end ());
161 for (Ledger_requests::iterator i (reqs.begin ());
162 i != reqs.end (); last = i++)
164 if (last == reqs.end ())
172 if (abs (last->second[d].position_) > interspaces
173 && abs (i->second[d].position_) > interspaces)
176 (last->second[d].head_extent_[RIGHT]
177 + i->second[d].head_extent_[LEFT] )/2;
179 Direction which = LEFT;
182 Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d];
184 // due tilt of quarter note-heads
186 (abs (last->second[d].position_) > interspaces + 1
187 && abs (i->second[d].position_) > interspaces + 1);
189 Real limit = (center + (both? which * gap/2 : 0));
190 lr.ledger_extent_.elem_ref (-which)
191 = which * (which * lr.ledger_extent_[-which] >? which * limit);
193 while (flip (&which) != LEFT);
196 while (flip (&d) != DOWN);
199 // create ledgers for note heads
200 Real ledgerlinethickness =
201 Staff_symbol::get_ledger_line_thickness (staff);
202 Real halfspace = Staff_symbol::staff_space (staff)/2;
203 for (int i = heads.size (); i--; )
205 Item *h = dynamic_cast<Item*> (heads[i]);
207 int pos = Staff_symbol_referencer::get_rounded_position (h);
208 if (abs (pos) > interspaces + 1)
210 Interval ledger_size = h->extent (common[X_AXIS], X_AXIS);
211 ledger_size.widen (ledger_size.length ()* length_fraction);
213 Interval max_size = reqs[Paper_column::get_rank (h->get_column ())][Direction (sign(pos))].ledger_extent_;
215 ledger_size.intersect (max_size);
216 Real left_shorten =0.0;
217 if (Grob * g = unsmob_grob (h->get_property ("accidental-grob")))
220 linear_combination (Drul_array<Real> (h->extent (common[X_AXIS], X_AXIS)[LEFT],
221 g->extent (common[X_AXIS], X_AXIS)[RIGHT]),
225 left_shorten = (-ledger_size[LEFT] + d) >? 0 ;
228 TODO: shorten 2 ledger lines for the case natural +
234 ledgers.add_stencil (brew_ledger_lines (staff, pos, interspaces,
243 ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
246 return ledgers.smobbed_copy ();
249 ADD_INTERFACE (Ledger_line_spanner,
250 "ledger-line-interface",
251 "This spanner draws the ledger lines of a staff, for note heads that stick out. ",
252 "note-heads thickness gap length minimum-length")