]> git.donarmstrong.com Git - lilypond.git/blob - lily/ledger-line-spanner.cc
Issue 4541 (2/2) Clean up...
[lilypond.git] / lily / ledger-line-spanner.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 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 };
36
37 static void
38 set_rods (Drul_array<Interval> const &current_extents,
39           Drul_array<Interval> const &previous_extents,
40           Item *current_column,
41           Item *previous_column,
42           Real min_length)
43 {
44   for (UP_and_DOWN (d))
45     {
46       if (!current_extents[d].is_empty ()
47           && !previous_extents[d].is_empty ())
48         {
49           Rod rod;
50           rod.distance_ = 2 * min_length
51                           /*
52                             we go from right to left.
53                           */
54                           - previous_extents[d][LEFT]
55                           + current_extents[d][RIGHT];
56
57           rod.item_drul_[LEFT] = current_column;
58           rod.item_drul_[RIGHT] = previous_column;
59           rod.add_to_cols ();
60         }
61     }
62 }
63
64 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1);
65 SCM
66 Ledger_line_spanner::set_spacing_rods (SCM smob)
67 {
68   Spanner *me = unsmob<Spanner> (smob);
69
70   // find size of note heads.
71   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
72   if (!staff)
73     {
74       me->suicide ();
75       return SCM_EOL;
76     }
77
78   Real min_length_fraction
79     = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15);
80
81   Drul_array<Interval> current_extents;
82   Drul_array<Interval> previous_extents;
83   Real current_head_width = 0.0;
84   Item *previous_column = 0;
85   Item *current_column = 0;
86
87   Real halfspace = Staff_symbol::staff_space (staff) / 2;
88
89   Interval staff_extent = staff->extent (staff, Y_AXIS);
90   staff_extent *= 1 / halfspace;
91
92   /*
93     Run through heads using a loop. Since Ledger_line_spanner can
94     contain a lot of noteheads, superlinear performance is too slow.
95   */
96   extract_item_set (me, "note-heads", heads);
97   for (vsize i = heads.size (); i--;)
98     {
99       Item *h = heads[i];
100
101       int pos = Staff_symbol_referencer::get_rounded_position (h);
102       if  (Staff_symbol::ledger_positions (staff, pos).empty ())
103         continue;
104
105       /* Ambitus heads can appear out-of-order in heads[],
106        * but as part of prefatory matter, they need no rods */
107       if (h->internal_has_interface (ly_symbol2scm ("ambitus-interface")))
108         continue;
109
110       Item *column = h->get_column ();
111       if (current_column != column)
112         {
113           set_rods (current_extents, previous_extents,
114                     current_column, previous_column,
115                     current_head_width * min_length_fraction);
116
117           previous_column = current_column;
118           current_column = column;
119           previous_extents = current_extents;
120
121           current_extents[DOWN].set_empty ();
122           current_extents[UP].set_empty ();
123           current_head_width = 0.0;
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       current_head_width = max (current_head_width, head_extent.length ());
133     }
134
135   if (previous_column && current_column)
136     set_rods (current_extents, previous_extents,
137               current_column, previous_column,
138               current_head_width * min_length_fraction);
139
140   return SCM_UNSPECIFIED;
141 }
142
143 struct Ledger_request
144 {
145   Interval ledger_extent_;
146   Interval head_extent_;
147   int position_;
148   Ledger_request ()
149   {
150     ledger_extent_.set_empty ();
151     head_extent_.set_empty ();
152     position_ = 0;
153   }
154 };
155
156 typedef map < int, Drul_array<Ledger_request> > Ledger_requests;
157
158 /*
159   TODO: ledger share a lot of info. Lots of room to optimize away
160   common use of objects/variables.
161 */
162 MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1);
163 SCM
164 Ledger_line_spanner::print (SCM smob)
165 {
166   Spanner *me = unsmob<Spanner> (smob);
167
168   extract_grob_set (me, "note-heads", heads);
169
170   if (heads.empty ())
171     return SCM_EOL;
172
173   // find size of note heads.
174   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
175   if (!staff)
176     return SCM_EOL;
177
178   Real halfspace = Staff_symbol::staff_space (staff) / 2;
179
180   Interval staff_extent = staff->extent (staff, Y_AXIS);
181   staff_extent *= 1 / halfspace;
182
183   Real length_fraction
184     = robust_scm2double (me->get_property ("length-fraction"), 0.25);
185
186   Stencil ledgers;
187
188   Grob *common[NO_AXES];
189
190   for (int i = X_AXIS; i < NO_AXES; i++)
191     {
192       Axis a = Axis (i);
193       common[a] = common_refpoint_of_array (heads, me, a);
194       for (vsize i = heads.size (); i--;)
195         if (Grob *g = unsmob<Grob> (me->get_object ("accidental-grob")))
196           common[a] = common[a]->common_refpoint (g, a);
197     }
198
199   Ledger_requests reqs;
200   for (vsize i = heads.size (); i--;)
201     {
202       Item *h = dynamic_cast<Item *> (heads[i]);
203
204       int pos = Staff_symbol_referencer::get_rounded_position (h);
205       if (pos && !staff_extent.contains (pos))
206         {
207           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
208           Interval ledger_extent = head_extent;
209           ledger_extent.widen (length_fraction * head_extent.length ());
210
211           Direction vdir = Direction (sign (pos));
212           int rank = h->get_column ()->get_rank ();
213
214           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
215           reqs[rank][vdir].head_extent_.unite (head_extent);
216           reqs[rank][vdir].position_
217             = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos);
218         }
219     }
220
221   // determine maximum size for non-colliding ledger.
222   Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
223   Ledger_requests::iterator last (reqs.end ());
224   for (Ledger_requests::iterator i (reqs.begin ());
225        i != reqs.end (); last = i++)
226     {
227       if (last == reqs.end ())
228         continue;
229
230       for (DOWN_and_UP (d))
231         {
232           if (!staff_extent.contains (last->second[d].position_)
233               && !staff_extent.contains (i->second[d].position_))
234             {
235               Real center
236                 = (last->second[d].head_extent_[RIGHT]
237                    + i->second[d].head_extent_[LEFT]) / 2;
238
239               for (LEFT_and_RIGHT (which))
240                 {
241                   Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d];
242
243                   // due tilt of quarter note-heads
244                   /* FIXME */
245                   bool both
246                     = (!staff_extent.contains (last->second[d].position_
247                                                - sign (last->second[d].position_))
248                        && !staff_extent.contains (i->second[d].position_
249                                                   - sign (i->second[d].position_)));
250                   Real limit = (center + (both ? which * gap / 2 : 0));
251                   lr.ledger_extent_.at (-which)
252                     = which * max (which * lr.ledger_extent_[-which], which * limit);
253                 }
254             }
255         }
256     }
257
258   // create ledgers for note heads
259   Real ledgerlinethickness
260     = Staff_symbol::get_ledger_line_thickness (staff);
261   for (vsize i = heads.size (); i--;)
262     {
263       Item *h = dynamic_cast<Item *> (heads[i]);
264
265       int pos = Staff_symbol_referencer::get_rounded_position (h);
266       vector<Real> ledger_positions = Staff_symbol::ledger_positions (staff, pos);
267       if (!ledger_positions.empty ())
268         {
269           int ledger_count = ledger_positions.size ();
270           Interval head_size = h->extent (common[X_AXIS], X_AXIS);
271           Interval ledger_size = head_size;
272           ledger_size.widen (ledger_size.length () * length_fraction);
273
274           if (pos && !staff_extent.contains (pos))
275             {
276               Interval max_size = reqs[h->get_column ()->get_rank ()]
277                                   [Direction (sign (pos))].ledger_extent_;
278
279               if (!max_size.is_empty ())
280                 ledger_size.intersect (max_size);
281             }
282
283           for (int i = 0; i < ledger_count; i++)
284             {
285               Real lpos = ledger_positions[i];
286               Interval x_extent = ledger_size;
287
288               if (i == 0)
289                 if (Grob *g = unsmob<Grob> (h->get_object ("accidental-grob")))
290                   {
291                     Interval accidental_size = g->extent (common[X_AXIS], X_AXIS);
292                     Real d
293                       = linear_combination (Drul_array<Real> (accidental_size[RIGHT],
294                                                               head_size[LEFT]),
295                                             0.0);
296
297                     Real left_shorten = max (-ledger_size[LEFT] + d, 0.0);
298
299                     x_extent[LEFT] += left_shorten;
300                     /*
301                       TODO: shorten 2 ledger lines for the case natural +
302                       downstem.
303                     */
304                   }
305
306               Real blotdiameter = ledgerlinethickness;
307               Interval y_extent
308                 = Interval (-0.5 * (ledgerlinethickness),
309                             +0.5 * (ledgerlinethickness));
310               Stencil ledger_line
311                 = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
312
313               ledger_line.translate_axis ( lpos * halfspace, Y_AXIS);
314               ledgers.add_stencil (ledger_line);
315             }
316         }
317     }
318
319   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
320                           X_AXIS);
321
322   return ledgers.smobbed_copy ();
323 }
324
325 ADD_INTERFACE (Ledger_line_spanner,
326                "This spanner draws the ledger lines of a staff.  This is a"
327                " separate grob because it has to process all potential"
328                " collisions between all note heads.  The thickness of ledger"
329                " lines is controlled by the @code{ledger-line-thickness}"
330                " property of the @ref{StaffSymbol} grob.",
331
332                /* properties */
333                "gap "
334                "length-fraction "
335                "minimum-length-fraction "
336                "note-heads "
337               );
338
339 struct Ledgered_interface
340 {
341 };
342
343 ADD_INTERFACE (Ledgered_interface,
344                "Objects that need ledger lines, typically note heads.  See"
345                " also @ref{ledger-line-spanner-interface}.",
346
347                /* properties */
348                "no-ledgers "
349               );