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