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,
30 static bool has_interface (Grob*);
35 Ledger_line_spanner::brew_ledger_lines (Grob *me,
41 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
42 Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
43 int line_count = ((abs (pos) < interspaces)
45 : (abs (pos) - interspaces) / 2);
49 Real ledgerlinethickness =
50 Staff_symbol::get_ledger_line_thickness (staff);
51 Real blotdiameter = ledgerlinethickness;
53 Interval (-0.5*(ledgerlinethickness),
54 +0.5*(ledgerlinethickness));
55 Stencil proto_ledger_line =
56 Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
58 x_extent[LEFT] += left_shorten;
59 Stencil proto_first_line =
60 Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
62 Direction dir = (Direction)sign (pos);
63 Real offs = (Staff_symbol_referencer::on_staffline (me, pos))
68 offs += pos * inter_f;
69 for (int i = 0; i < line_count; i++)
71 Stencil ledger_line ((i == 0)
75 ledger_line.translate_axis (-dir * inter_f * i * 2 + offs, Y_AXIS);
76 stencil.add_stencil (ledger_line);
86 Interval ledger_extent_;
87 Interval head_extent_;
91 ledger_extent_.set_empty ();
92 head_extent_.set_empty ();
97 typedef std::map<int, Drul_array<Ledger_request> > Ledger_requests;
101 TODO: ledger share a lot of info. Lots of room to optimize away common
102 use of objects/variables.
104 MAKE_SCHEME_CALLBACK (Ledger_line_spanner,print,1);
106 Ledger_line_spanner::print (SCM smob)
108 Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (smob));
109 Link_array<Grob> heads (Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-heads"));
112 Stencil default_ledger;
114 Grob * common[NO_AXES];
115 for (int i = X_AXIS; i < NO_AXES; i++)
116 common[Axis (i)] = common_refpoint_of_array (heads, me, Axis(i));
118 int interspaces = Staff_symbol_referencer::line_count (me)-1;
119 Ledger_requests reqs;
120 Real length_fraction = 0.25 * 2;
121 for (int i = heads.size (); i--; )
123 Item *h = dynamic_cast<Item*> (heads[i]);
125 int pos = Staff_symbol_referencer::get_rounded_position (h);
126 if (abs (pos) > interspaces + 1)
128 Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
129 Interval ledger_extent = Interval (head_extent.linear_combination (-1 - length_fraction),
130 head_extent.linear_combination (1 + length_fraction));
132 Direction vdir = Direction (sign (pos));
133 int rank = Paper_column::get_rank (h->get_column ());
135 reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
136 reqs[rank][vdir].head_extent_.unite (head_extent);
137 reqs[rank][vdir].position_ =
138 vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos));
142 Real gap = robust_scm2double (me->get_property ("gap"), 0.15);
143 Ledger_requests::iterator last (reqs.end ());
144 for (Ledger_requests::iterator i (reqs.begin ());
145 i != reqs.end (); last = i++)
147 if (last == reqs.end ())
155 if (abs (last->second[d].position_) > interspaces + 1
156 && abs (i->second[d].position_) > interspaces + 1)
159 (last->second[d].head_extent_[RIGHT]
160 + i->second[d].head_extent_[LEFT] )/2;
162 Direction which = LEFT;
165 Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d];
168 // due tilt of quarter note-heads
169 Real excentricity = 0; //.1;
170 Real limit = (center + which * gap/2 + excentricity);
171 lr.ledger_extent_.elem_ref (-which)
172 = which * (which * lr.ledger_extent_[-which] >? which * limit);
174 while (flip (&which) != LEFT);
177 while (flip (&d) != DOWN);
180 for (Ledger_requests::const_iterator i (reqs.begin ());
181 i != reqs.end (); i++)
186 Ledger_request lr = (*i).second[d];
187 ledgers.add_stencil (brew_ledger_lines (me,
193 while (flip (&d) != DOWN);
196 ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
199 return ledgers.smobbed_copy ();
202 ADD_INTERFACE (Ledger_line_spanner,
203 "ledger-line-interface",
204 "This spanner draws the ledger lines of a staff on note heads. ",
205 "note-heads thickness gap length minimum-length")