]> git.donarmstrong.com Git - lilypond.git/blob - lily/ledger-line-spanner.cc
Issue 2552: Non-initialized member variables
[lilypond.git] / lily / ledger-line-spanner.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <map>
21 using namespace std;
22
23 #include "note-head.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "staff-symbol.hh"
26 #include "lookup.hh"
27 #include "spanner.hh"
28 #include "pointer-group-interface.hh"
29 #include "paper-column.hh"
30
31 struct Ledger_line_spanner
32 {
33   DECLARE_SCHEME_CALLBACK (print, (SCM));
34   DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM));
35   DECLARE_GROB_INTERFACE ();
36 };
37
38 static void
39 set_rods (Drul_array<Interval> const &current_extents,
40           Drul_array<Interval> const &previous_extents,
41           Item *current_column,
42           Item *previous_column,
43           Real min_length_fraction)
44 {
45   Direction d = UP;
46   do
47     {
48       if (!current_extents[d].is_empty ()
49           && !previous_extents[d].is_empty ())
50         {
51           Real total_head_length = previous_extents[d].length ()
52                                    + current_extents[d].length ();
53
54           Rod rod;
55           rod.distance_ = total_head_length
56                           * (3 / 2 * min_length_fraction)
57                           /*
58                             we go from right to left.
59                           */
60                           - previous_extents[d][LEFT]
61                           + current_extents[d][RIGHT];
62
63           rod.item_drul_[LEFT] = current_column;
64           rod.item_drul_[RIGHT] = previous_column;
65           rod.add_to_cols ();
66         }
67     }
68   while (flip (&d) != DOWN);
69 }
70
71 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
72 SCM
73 Ledger_line_spanner::set_spacing_rods (SCM smob)
74 {
75   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
76
77   // find size of note heads.
78   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
79   if (!staff)
80     {
81       me->suicide ();
82       return SCM_EOL;
83     }
84
85   Real min_length_fraction
86     = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
87
88   Drul_array<Interval> current_extents;
89   Drul_array<Interval> previous_extents;
90   Item *previous_column = 0;
91   Item *current_column = 0;
92
93   Real halfspace = Staff_symbol::staff_space (staff) / 2;
94
95   Interval staff_extent = staff->extent (staff, Y_AXIS);
96   staff_extent *= 1 / halfspace;
97
98   /*
99     Run through heads using a loop. Since Ledger_line_spanner can
100     contain a lot of noteheads, superlinear performance is too slow.
101   */
102   extract_item_set (me, "note-heads", heads);
103   for (vsize i = heads.size (); i--;)
104     {
105       Item *h = heads[i];
106
107       int pos = Staff_symbol_referencer::get_rounded_position (h);
108       if (staff_extent.contains (pos))
109         continue;
110
111       Item *column = h->get_column ();
112       if (current_column != column)
113         {
114           set_rods (current_extents, previous_extents,
115                     current_column, previous_column,
116                     min_length_fraction);
117
118           previous_column = current_column;
119           current_column = column;
120           previous_extents = current_extents;
121
122           current_extents[DOWN].set_empty ();
123           current_extents[UP].set_empty ();
124         }
125
126       Interval head_extent = h->extent (column, X_AXIS);
127       Direction vdir = Direction (sign (pos));
128       if (!vdir)
129         continue;
130
131       current_extents[vdir].unite (head_extent);
132     }
133
134   if (previous_column && current_column)
135     set_rods (current_extents, previous_extents,
136               current_column, previous_column,
137               min_length_fraction);
138
139   return SCM_UNSPECIFIED;
140 }
141
142 struct Ledger_request
143 {
144   Interval ledger_extent_;
145   Interval head_extent_;
146   int position_;
147   Ledger_request ()
148   {
149     ledger_extent_.set_empty ();
150     head_extent_.set_empty ();
151     position_ = 0;
152   }
153 };
154
155 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
156
157 /*
158   TODO: ledger share a lot of info. Lots of room to optimize away
159   common use of objects/variables.
160 */
161 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
162 SCM
163 Ledger_line_spanner::print (SCM smob)
164 {
165   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
166
167   extract_grob_set (me, "note-heads", heads);
168
169   if (heads.empty ())
170     return SCM_EOL;
171
172   // find size of note heads.
173   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
174   if (!staff)
175     return SCM_EOL;
176
177   Real halfspace = Staff_symbol::staff_space (staff) / 2;
178
179   Interval staff_extent = staff->extent (staff, Y_AXIS);
180   staff_extent *= 1 / halfspace;
181
182   Real length_fraction
183     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
184
185   Stencil ledgers;
186
187   Grob *common[NO_AXES];
188
189   for (int i = X_AXIS; i < NO_AXES; i++)
190     {
191       Axis a = Axis (i);
192       common[a] = common_refpoint_of_array (heads, me, a);
193       for (vsize i = heads.size (); i--;)
194         if (Grob *g = unsmob_grob (me->get_object ("accidental-grob")))
195           common[a] = common[a]->common_refpoint (g, a);
196     }
197
198   Ledger_requests reqs;
199   for (vsize i = heads.size (); i--;)
200     {
201       Item *h = dynamic_cast<Item *> (heads[i]);
202
203       int pos = Staff_symbol_referencer::get_rounded_position (h);
204       if (pos && !staff_extent.contains (pos))
205         {
206           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
207           Interval ledger_extent = head_extent;
208           ledger_extent.widen (length_fraction * head_extent.length ());
209
210           Direction vdir = Direction (sign (pos));
211           int rank = h->get_column ()->get_rank ();
212
213           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
214           reqs[rank][vdir].head_extent_.unite (head_extent);
215           reqs[rank][vdir].position_
216             = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
217         }
218     }
219
220   // determine maximum size for non-colliding ledger.
221   Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
222   Ledger_requests::iterator last (reqs.end ());
223   for (Ledger_requests::iterator i (reqs.begin ());
224        i != reqs.end (); last = i++)
225     {
226       if (last == reqs.end ())
227         continue;
228
229       for (DOWN_and_UP (d))
230         {
231           if (!staff_extent.contains (last->second[d].position_)
232               && !staff_extent.contains (i->second[d].position_))
233             {
234               Real center
235                 = (last->second[d].head_extent_[RIGHT]
236                    + i->second[d].head_extent_[LEFT]) / 2;
237
238               for (LEFT_and_RIGHT (which))
239                 {
240                   Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
241
242                   // due tilt of quarter note-heads
243                   /* FIXME */
244                   bool both
245                     = (!staff_extent.contains (last->second[d].position_
246                                                - sign (last->second[d].position_))
247                        && !staff_extent.contains (i->second[d].position_
248                                                   - sign (i->second[d].position_)));
249                   Real limit = (center + (both ? which * gap / 2 : 0));
250                   lr.ledger_extent_.at (-which)
251                     = which * max (which * lr.ledger_extent_[-which], which * limit);
252                 }
253             }
254         }
255     }
256
257   // create ledgers for note heads
258   Real ledgerlinethickness
259     = Staff_symbol::get_ledger_line_thickness (staff);
260   for (vsize i = heads.size (); i--;)
261     {
262       Item *h = dynamic_cast<Item *> (heads[i]);
263
264       int pos = Staff_symbol_referencer::get_rounded_position (h);
265       vector<Real> ledger_positions = Staff_symbol::ledger_positions (staff, pos);
266       if (!ledger_positions.empty ())
267         {
268           int ledger_count = ledger_positions.size ();
269           Interval head_size = h->extent (common[X_AXIS], X_AXIS);
270           Interval ledger_size = head_size;
271           ledger_size.widen (ledger_size.length () * length_fraction);
272
273           if (pos && !staff_extent.contains (pos))
274             {
275               Interval max_size = reqs[h->get_column ()->get_rank ()]
276                                   [Direction (sign (pos))].ledger_extent_;
277
278               if (!max_size.is_empty ())
279                 ledger_size.intersect (max_size);
280             }
281
282           for (int i = 0; i < ledger_count; i++)
283             {
284               Real lpos = ledger_positions[i];
285               Interval x_extent = ledger_size;
286
287               if (i == 0)
288                 if (Grob *g = unsmob_grob (h->get_object ("accidental-grob")))
289                   {
290                     Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
291                     Real d
292                       = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
293                                                               head_size[LEFT]),
294                                             0.0);
295
296                     Real left_shorten = max (-ledger_size[LEFT] + d, 0.0);
297
298                     x_extent[LEFT] += left_shorten;
299                     /*
300                       TODO: shorten 2 ledger lines for the case natural +
301                       downstem.
302                     */
303                   }
304
305               Real blotdiameter = ledgerlinethickness;
306               Interval y_extent
307                 = Interval (-0.5 * (ledgerlinethickness),
308                             +0.5 * (ledgerlinethickness));
309               Stencil ledger_line
310                 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
311
312               ledger_line.translate_axis ( lpos * halfspace, Y_AXIS);
313               ledgers.add_stencil (ledger_line);
314             }
315         }
316     }
317
318   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
319                           X_AXIS);
320
321   return ledgers.smobbed_copy ();
322 }
323
324 ADD_INTERFACE (Ledger_line_spanner,
325                "This spanner draws the ledger lines of a staff.  This is a"
326                " separate grob because it has to process all potential"
327                " collisions between all note heads.",
328
329                /* properties */
330                "gap "
331                "length-fraction "
332                "minimum-length-fraction "
333                "note-heads "
334                "thickness "
335               );
336
337 struct Ledgered_interface
338 {
339   DECLARE_GROB_INTERFACE ();
340 };
341
342 ADD_INTERFACE (Ledgered_interface,
343                "Objects that need ledger lines, typically note heads.  See"
344                " also @ref{ledger-line-spanner-interface}.",
345
346                /* properties */
347                "no-ledgers "
348               );