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