]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-basic.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / spacing-basic.cc
1 /*
2   spacing-basic.cc -- implement Spacing_spanner, simplistic spacing routines
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "spacing-spanner.hh"
10
11 #include "spacing-options.hh"
12 #include "moment.hh"
13 #include "paper-column.hh"
14 #include "warn.hh"
15 #include "pointer-group-interface.hh"
16 #include "system.hh"
17
18 /*
19   LilyPond spaces by taking a simple-minded spacing algorithm, and
20   adding subtle adjustments to that. This file does the simple-minded
21   spacing routines.
22 */
23 /*
24   The one-size-fits all spacing. It doesn't take into account
25   different spacing wishes from one to the next column.
26 */
27 void
28 Spacing_spanner::standard_breakable_column_spacing (Grob *me, Item *l, Item *r,
29                                                     Real *fixed, Real *space,
30                                                     Spacing_options const *options)
31 {
32   *fixed = 0.0;
33   Direction d = LEFT;
34   Drul_array<Item *> cols (l, r);
35
36   do
37     {
38       /*
39         TODO: this is fishy, the extent gets distorted due to wide
40         \marks, so better not do this.
41        */
42       if (!Paper_column::is_musical (cols[d]))
43         {
44           /*
45             Tied accidentals over barlines cause problems, so lets see
46             what happens if we do this for non musical columns only.
47           */
48           Interval lext = cols[d]->extent (cols [d], X_AXIS);
49           if (!lext.is_empty ())
50             *fixed += -d * lext[-d];
51         }
52     }
53   while (flip (&d) != LEFT);
54
55   if (Paper_column::is_breakable (l) && Paper_column::is_breakable (r))
56     {
57       Moment *dt = unsmob_moment (l->get_property ("measure-length"));
58       Moment mlen (1);
59       if (dt)
60         mlen = *dt;
61
62       Real incr = robust_scm2double (me->get_property ("spacing-increment"), 1);
63
64       *space = *fixed + incr * double (mlen.main_part_ / options->global_shortest_) * 0.8;
65     }
66   else
67     {
68       Moment dt = Paper_column::when_mom (r) - Paper_column::when_mom (l);
69
70       if (dt == Moment (0, 0))
71         {
72           /*
73             In this case, Staff_spacing should handle the job,
74             using dt when it is 0 is silly.
75           */
76           *space = *fixed + 0.5;
77         }
78       else
79         {
80           bool dummy;
81           *space = *fixed + options->get_duration_space (dt.main_part_, &dummy);
82         }
83     }
84 }
85
86 Moment *
87 get_measure_length (Grob *column)
88 {
89   Grob * sys = column->get_parent (X_AXIS);
90
91   extract_grob_set (sys, "columns", cols);
92
93   vsize col_idx = Paper_column::get_rank (column);
94   
95   do
96     {
97       if (Moment *len = unsmob_moment (cols[col_idx]->get_property ("measure-length")))
98         {
99           return len;
100         }
101     }
102   while (col_idx-- != 0);
103   
104   return 0;
105 }
106
107 Real
108 Spacing_spanner::note_spacing (Grob *me, Grob *lc, Grob *rc,
109                                Spacing_options const *options,
110                                bool *expand_only)
111 {
112   (void) me;
113   
114   Moment shortest_playing_len = 0;
115   SCM s = lc->get_property ("shortest-playing-duration");
116
117   if (unsmob_moment (s))
118     shortest_playing_len = *unsmob_moment (s);
119
120   if (! shortest_playing_len.to_bool ())
121     {
122       programming_error ("cannot find a ruling note at: " + Paper_column::when_mom (lc).to_string ());
123       shortest_playing_len = 1;
124     }
125
126   Moment lwhen = Paper_column::when_mom (lc);
127   Moment rwhen = Paper_column::when_mom (rc);
128
129   Moment delta_t = rwhen - lwhen;
130
131   /*
132     when toying with mmrests, it is possible to have musical
133     column on the left and non-musical on the right, spanning
134     several measures.
135
136     TODO: efficiency: measure length can be cached, or stored as
137     property in paper-column.
138   */
139
140   if (Moment *measure_len = get_measure_length (lc))
141     {
142       delta_t = min (delta_t, *measure_len);
143
144       /*
145         The following is an extra safety measure, such that
146         the length of a mmrest event doesn't cause havoc.
147       */
148       shortest_playing_len = min (shortest_playing_len, *measure_len);
149     }
150
151   Real dist = 0.0;
152   if (delta_t.main_part_ && !lwhen.grace_part_)
153     {
154       dist = options->get_duration_space (shortest_playing_len.main_part_,
155                                           expand_only);
156       dist *= double (delta_t.main_part_ / shortest_playing_len.main_part_);
157     }
158   else if (delta_t.grace_part_)
159     {
160       /*
161         Crude hack for spacing graces: we take the shortest space
162         available (namely the space for the global shortest note), and
163         multiply that by grace-space-factor
164       */
165       dist = options->get_duration_space (options->global_shortest_, expand_only) / 2.0;
166       Grob *grace_spacing = unsmob_grob (lc->get_object ("grace-spacing"));
167       if (grace_spacing)
168         {
169           Spacing_options grace_opts;
170           grace_opts.init_from_grob (grace_spacing);
171           bool bla;
172           dist = grace_opts.get_duration_space (delta_t.grace_part_, &bla);
173         }
174       
175     }
176
177   return dist;
178 }
179