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