]> git.donarmstrong.com Git - lilypond.git/blob - lily/ledger-line-spanner.cc
(set_spacing_rods): rewrite to O(n)
[lilypond.git] / lily / ledger-line-spanner.cc
1 /*
2   ledger-line-spanner.cc -- implement Ledger_line_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <map>
10
11 #include "item.hh"
12 #include "note-head.hh"
13 #include "staff-symbol-referencer.hh"
14 #include "staff-symbol.hh"
15 #include "lookup.hh"
16 #include "spanner.hh"
17 #include "group-interface.hh"
18 #include "paper-column.hh"
19
20 struct Ledger_line_spanner
21 {
22   DECLARE_SCHEME_CALLBACK (print, (SCM));
23   DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
24   static Stencil brew_ledger_lines (Grob *me,
25                                     int pos,
26                                     int interspaces,
27                                     Real, Real,
28                                     Interval x_extent,
29                                     Real left_shorten);
30
31   static bool has_interface (Grob *);
32 };
33
34 Stencil
35 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
36                                         int pos,
37                                         int interspaces,
38                                         Real halfspace,
39                                         Real ledgerlinethickness,
40                                         Interval x_extent,
41                                         Real left_shorten)
42 {
43   int line_count = ((abs (pos) < interspaces)
44                     ? 0
45                     : (abs (pos) - interspaces) / 2);
46   Stencil stencil;
47   if (line_count)
48     {
49       Real blotdiameter = ledgerlinethickness;
50       Interval y_extent
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);
55
56       x_extent[LEFT] += left_shorten;
57       Stencil proto_first_line
58         = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
59
60       Direction dir = (Direction)sign (pos);
61       Real offs = (Staff_symbol_referencer::on_staffline (staff, pos))
62         ? 0.0
63         : -dir * halfspace;
64
65       offs += pos * halfspace;
66       for (int i = 0; i < line_count; i++)
67         {
68           Stencil ledger_line ((i == 0)
69                                ? proto_first_line
70                                : proto_ledger_line);
71           ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
72           stencil.add_stencil (ledger_line);
73         }
74     }
75
76   return stencil;
77 }
78
79
80 static void
81 set_rods (Drul_array<Interval> const &current_extents,
82           Drul_array<Interval> const &previous_extents,
83           Item *current_column,
84           Item *previous_column,
85           Real min_length_fraction)
86 {
87   Direction d = UP;
88   do
89     {
90       if (!current_extents[d].is_empty ()
91           && !previous_extents[d].is_empty ())
92         {
93           Real total_head_length = previous_extents[d].length ()
94             + current_extents[d].length ();
95
96           Rod rod;
97           rod.distance_ = total_head_length
98             * (3/2 * min_length_fraction)
99             /*
100               we go from right to left.
101             */
102             - previous_extents[d][LEFT]
103             + current_extents[d][RIGHT];
104
105           rod.item_drul_[LEFT] = current_column;
106           rod.item_drul_[RIGHT] = previous_column;
107           rod.add_to_cols ();
108         }
109
110     }
111   while (flip (&d) != DOWN);
112 }
113
114
115 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
116 SCM
117 Ledger_line_spanner::set_spacing_rods (SCM smob)
118 {
119   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
120
121   // find size of note heads.
122   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
123   if (!staff)
124     return SCM_EOL;
125
126   SCM heads = me->get_property ("note-heads");
127
128
129   Real min_length_fraction
130     = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
131
132
133   Drul_array<Interval> current_extents;
134   Drul_array<Interval> previous_extents;
135   Item *previous_column = 0;
136   Item *current_column = 0;
137
138   /*
139     Run through heads using a loop. Since Legder_line_spanner can
140     contain a lot of noteheads, superlinear performance is too slow.
141   */
142   int interspaces = Staff_symbol::line_count (staff) - 1;
143   for (SCM hp = heads; scm_is_pair (hp); hp = scm_cdr (hp))
144     {
145       Item *h = dynamic_cast<Item *> (unsmob_grob (scm_car (hp)));
146       
147       int pos = Staff_symbol_referencer::get_rounded_position (h);
148       if (abs (pos) <= interspaces)
149         continue;
150       
151       Item *column = h->get_column ();
152       if (current_column != column)
153         {
154           set_rods (current_extents, previous_extents,
155                     current_column, previous_column,
156                     min_length_fraction);
157           
158           previous_column = current_column;
159           current_column = column;
160           previous_extents = current_extents;
161           
162           current_extents[DOWN].set_empty();
163           current_extents[UP].set_empty();
164         }
165       
166       
167       Interval head_extent = h->extent (column, X_AXIS);
168       Direction vdir = Direction (sign (pos));
169       if (!vdir)
170         continue;
171
172       current_extents[vdir].unite (head_extent);
173     }
174
175   if (previous_column && current_column)
176     set_rods (current_extents, previous_extents,
177               current_column, previous_column,
178               min_length_fraction);
179
180   
181   
182   return SCM_UNSPECIFIED;
183 }
184
185 struct Ledger_request
186 {
187   Interval ledger_extent_;
188   Interval head_extent_;
189   int position_;
190   bool excentric_;
191   Ledger_request ()
192   {
193     ledger_extent_.set_empty ();
194     head_extent_.set_empty ();
195     position_ = 0;
196   }
197 };
198
199 typedef std::map < int, Drul_array<Ledger_request> > Ledger_requests;
200
201 /*
202   TODO: ledger share a lot of info. Lots of room to optimize away
203   common use of objects/variables.
204
205 */
206 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
207 SCM
208 Ledger_line_spanner::print (SCM smob)
209 {
210   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
211   Link_array<Grob> heads (extract_grob_array (me, ly_symbol2scm ("note-heads")));
212
213   if (heads.is_empty ())
214     return SCM_EOL;
215
216   // find size of note heads.
217   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
218   if (!staff)
219     return SCM_EOL;
220
221   Real length_fraction
222     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
223
224   Stencil ledgers;
225   Stencil default_ledger;
226
227   Grob *common[NO_AXES];
228
229   for (int i = X_AXIS; i < NO_AXES; i++)
230     {
231       Axis a = Axis (i);
232       common[a] = common_refpoint_of_array (heads, me, a);
233       for (int i = heads.size (); i--;)
234         if (Grob *g = unsmob_grob (me->get_property ("accidental-grob")))
235           common[a] = common[a]->common_refpoint (g, a);
236     }
237
238   int interspaces = Staff_symbol::line_count (staff) - 1;
239   Ledger_requests reqs;
240   for (int i = heads.size (); i--;)
241     {
242       Item *h = dynamic_cast<Item *> (heads[i]);
243
244       int pos = Staff_symbol_referencer::get_rounded_position (h);
245       if (pos
246           && abs (pos) > interspaces)
247         {
248           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
249           Interval ledger_extent = head_extent;
250           ledger_extent.widen (length_fraction * head_extent.length ());
251
252           Direction vdir = Direction (sign (pos));
253           int rank = Paper_column::get_rank (h->get_column ());
254
255           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
256           reqs[rank][vdir].head_extent_.unite (head_extent);
257           reqs[rank][vdir].position_
258             = vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos));
259         }
260     }
261
262   // determine maximum size for non-colliding ledger.
263   Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
264   Ledger_requests::iterator last (reqs.end ());
265   for (Ledger_requests::iterator i (reqs.begin ());
266        i != reqs.end (); last = i++)
267     {
268       if (last == reqs.end ())
269         {
270           continue;
271         }
272
273       Direction d = DOWN;
274       do
275         {
276           if (abs (last->second[d].position_) > interspaces
277               && abs (i->second[d].position_) > interspaces)
278             {
279               Real center
280                 = (last->second[d].head_extent_[RIGHT]
281                    + i->second[d].head_extent_[LEFT]) / 2;
282
283               Direction which = LEFT;
284               do
285                 {
286                   Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d];
287
288                   // due tilt of quarter note-heads
289                   bool both
290                     = (abs (last->second[d].position_) > interspaces + 1
291                        && abs (i->second[d].position_) > interspaces + 1);
292
293                   Real limit = (center + (both? which * gap / 2 : 0));
294                   lr.ledger_extent_.elem_ref (-which)
295                     = which * (which * lr.ledger_extent_[-which] >? which * limit);
296                 }
297               while (flip (&which) != LEFT);
298             }
299         }
300       while (flip (&d) != DOWN);
301     }
302
303   // create  ledgers for note heads
304   Real ledgerlinethickness
305     = Staff_symbol::get_ledger_line_thickness (staff);
306   Real halfspace = Staff_symbol::staff_space (staff) / 2;
307   for (int i = heads.size (); i--;)
308     {
309       Item *h = dynamic_cast<Item *> (heads[i]);
310
311       int pos = Staff_symbol_referencer::get_rounded_position (h);
312       if (abs (pos) > interspaces + 1)
313         {
314           Interval head_size = h->extent (common[X_AXIS], X_AXIS);
315           Interval ledger_size = head_size;
316           ledger_size.widen (ledger_size.length ()* length_fraction);
317
318           Interval max_size = reqs[Paper_column::get_rank (h->get_column ())][Direction (sign (pos))].ledger_extent_;
319
320           ledger_size.intersect (max_size);
321           Real left_shorten = 0.0;
322           if (Grob *g = unsmob_grob (h->get_property ("accidental-grob")))
323             {
324               Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
325               Real d
326                 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
327                                                         head_size[LEFT]),
328                                       0.0);
329
330               left_shorten = (-ledger_size[LEFT] + d) >? 0;
331
332               /*
333                 TODO: shorten 2 ledger lines for the case natural +
334                 downstem.
335               */
336
337             }
338
339           ledgers.add_stencil (brew_ledger_lines (staff, pos, interspaces,
340                                                   halfspace,
341                                                   ledgerlinethickness,
342                                                   ledger_size,
343                                                   left_shorten));
344         }
345     }
346
347   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
348                           X_AXIS);
349
350   return ledgers.smobbed_copy ();
351 }
352
353 ADD_INTERFACE (Ledger_line_spanner,
354                "ledger-line-interface",
355                "This spanner draws the ledger lines of a staff, for note heads that stick out. ",
356                "note-heads thickness minimum-length-fraction length-fraction gap");
357
358 struct Ledgered_interface
359 {
360   static bool has_interface (Grob *);
361 };
362
363 ADD_INTERFACE (Ledgered_interface,
364                "ledgered-interface",
365                "Objects that need ledger lines.",
366                "no-ledgers");