]> git.donarmstrong.com Git - lilypond.git/blob - lily/ledger-line-spanner.cc
* input/regression/stencil-color-rotation.ly: new file.
[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     {
123       me->suicide ();
124       return SCM_EOL;
125     }
126
127   Real min_length_fraction
128     = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
129
130   Drul_array<Interval> current_extents;
131   Drul_array<Interval> previous_extents;
132   Item *previous_column = 0;
133   Item *current_column = 0;
134
135   Real halfspace = Staff_symbol::staff_space (staff) / 2;
136
137   Interval staff_extent = staff->extent (staff, Y_AXIS);
138   staff_extent *= 1 / halfspace;
139     
140   /*
141     Run through heads using a loop. Since Ledger_line_spanner can
142     contain a lot of noteheads, superlinear performance is too slow.
143   */
144   extract_item_set (me, "note-heads", heads);
145   for (vsize i = heads.size (); i--;)
146     {
147       Item *h = heads[i];
148
149       int pos = Staff_symbol_referencer::get_rounded_position (h);
150       if (staff_extent.contains (pos))
151         continue;
152
153       Item *column = h->get_column ();
154       if (current_column != column)
155         {
156           set_rods (current_extents, previous_extents,
157                     current_column, previous_column,
158                     min_length_fraction);
159
160           previous_column = current_column;
161           current_column = column;
162           previous_extents = current_extents;
163
164           current_extents[DOWN].set_empty ();
165           current_extents[UP].set_empty ();
166         }
167
168       Interval head_extent = h->extent (column, X_AXIS);
169       Direction vdir = Direction (sign (pos));
170       if (!vdir)
171         continue;
172
173       current_extents[vdir].unite (head_extent);
174     }
175
176   if (previous_column && current_column)
177     set_rods (current_extents, previous_extents,
178               current_column, previous_column,
179               min_length_fraction);
180
181   return SCM_UNSPECIFIED;
182 }
183
184 struct Ledger_request
185 {
186   Interval ledger_extent_;
187   Interval head_extent_;
188   int position_;
189   bool excentric_;
190   Ledger_request ()
191   {
192     ledger_extent_.set_empty ();
193     head_extent_.set_empty ();
194     position_ = 0;
195   }
196 };
197
198 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
199
200 /*
201   TODO: ledger share a lot of info. Lots of room to optimize away
202   common use of objects/variables.
203 */
204 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
205 SCM
206 Ledger_line_spanner::print (SCM smob)
207 {
208   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
209
210   extract_grob_set (me, "note-heads", heads);
211
212   if (heads.empty ())
213     return SCM_EOL;
214
215   // find size of note heads.
216   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
217   if (!staff)
218     return SCM_EOL;
219
220   Real halfspace = Staff_symbol::staff_space (staff) / 2;
221
222   Interval staff_extent = staff->extent (staff, Y_AXIS);
223   staff_extent *= 1 / halfspace;
224   
225   Real length_fraction
226     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
227
228   Stencil ledgers;
229   Stencil default_ledger;
230
231   Grob *common[NO_AXES];
232
233   for (int i = X_AXIS; i < NO_AXES; i++)
234     {
235       Axis a = Axis (i);
236       common[a] = common_refpoint_of_array (heads, me, a);
237       for (vsize i = heads.size (); i--;)
238         if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
239           common[a] = common[a]->common_refpoint (g, a);
240     }
241
242   Ledger_requests reqs;
243   for (vsize i = heads.size (); i--;)
244     {
245       Item *h = dynamic_cast<Item *> (heads[i]);
246
247       int pos = Staff_symbol_referencer::get_rounded_position (h);
248       if (pos && !staff_extent.contains (pos))
249         {
250           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
251           Interval ledger_extent = head_extent;
252           ledger_extent.widen (length_fraction * head_extent.length ());
253
254           Direction vdir = Direction (sign (pos));
255           int rank = h->get_column ()->get_rank ();
256
257           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
258           reqs[rank][vdir].head_extent_.unite (head_extent);
259           reqs[rank][vdir].position_
260             = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
261         }
262     }
263
264   // determine maximum size for non-colliding ledger.
265   Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
266   Ledger_requests::iterator last (reqs.end ());
267   for (Ledger_requests::iterator i (reqs.begin ());
268        i != reqs.end (); last = i++)
269     {
270       if (last == reqs.end ())
271         continue;
272
273       Direction d = DOWN;
274       do
275         {
276           if (!staff_extent.contains (last->second[d].position_)
277               && !staff_extent.contains (i->second[d].position_))
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                   /* FIXME */
290                   bool both
291                     = (!staff_extent.contains (last->second[d].position_
292                                                - sign (last->second[d].position_))
293                        && !staff_extent.contains (i->second[d].position_
294                                                   - sign (i->second[d].position_)));
295                   Real limit = (center + (both ? which * gap / 2 : 0));
296                   lr.ledger_extent_.at (-which)
297                     = which * max (which * lr.ledger_extent_[-which], which * limit);
298                 }
299               while (flip (&which) != LEFT);
300             }
301         }
302       while (flip (&d) != DOWN);
303     }
304
305   // create ledgers for note heads
306   Real ledgerlinethickness
307     = Staff_symbol::get_ledger_line_thickness (staff);
308   for (vsize i = heads.size (); i--;)
309     {
310       Item *h = dynamic_cast<Item *> (heads[i]);
311
312       int pos = Staff_symbol_referencer::get_rounded_position (h);
313       if (!staff_extent.contains (pos - sign (pos)))
314         {
315           Interval head_size = h->extent (common[X_AXIS], X_AXIS);
316           Interval ledger_size = head_size;
317           ledger_size.widen (ledger_size.length () * length_fraction);
318
319           Interval max_size = reqs[h->get_column ()->get_rank ()]
320             [Direction (sign (pos))].ledger_extent_;
321
322           ledger_size.intersect (max_size);
323           Real left_shorten = 0.0;
324           if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
325             {
326               Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
327               Real d
328                 = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
329                                                         head_size[LEFT]),
330                                       0.0);
331
332               left_shorten = max (-ledger_size[LEFT] + d, 0.0);
333
334               /*
335                 TODO: shorten 2 ledger lines for the case natural +
336                 downstem.
337               */
338             }
339
340           ledgers.add_stencil (brew_ledger_lines (staff, pos, staff_extent,
341                                                   halfspace,
342                                                   ledgerlinethickness,
343                                                   ledger_size,
344                                                   left_shorten));
345         }
346     }
347
348   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
349                           X_AXIS);
350
351   return ledgers.smobbed_copy ();
352 }
353
354 ADD_INTERFACE (Ledger_line_spanner,
355                "ledger-line-interface",
356
357                "This spanner draws the ledger lines of a staff.\n"
358                "This is a separate grob because it has to process\n"
359                "all potential collisions between all note heads.",
360
361                /* properties */
362                "note-heads "
363                "thickness "
364                "minimum-length-fraction "
365                "length-fraction "
366                "gap");
367
368 struct Ledgered_interface
369 {
370   static bool has_interface (Grob *);
371 };
372
373 ADD_INTERFACE (Ledgered_interface,
374                "ledgered-interface",
375
376                "Objects that need ledger lines, typically "
377                "note heads. See also @ref{ledger-line-interface}.",
378
379                "no-ledgers");