]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-basic.cc
add 2007 to (c) year.
[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       if (!Paper_column::is_musical (cols[d]))
39         {
40           /*
41             Tied accidentals over barlines cause problems, so lets see
42             what happens if we do this for non musical columns only.
43           */
44           Interval lext = cols[d]->extent (cols [d], X_AXIS);
45           if (!lext.is_empty ())
46             *fixed += -d * lext[-d];
47         }
48     }
49   while (flip (&d) != LEFT);
50
51   if (Paper_column::is_breakable (l) && Paper_column::is_breakable (r))
52     {
53       Moment *dt = unsmob_moment (l->get_property ("measure-length"));
54       Moment mlen (1);
55       if (dt)
56         mlen = *dt;
57
58       Real incr = robust_scm2double (me->get_property ("spacing-increment"), 1);
59
60       *space = *fixed + incr * double (mlen.main_part_ / options->global_shortest_) * 0.8;
61     }
62   else
63     {
64       Moment dt = Paper_column::when_mom (r) - Paper_column::when_mom (l);
65
66       if (dt == Moment (0, 0))
67         {
68           /*
69             In this case, Staff_spacing should handle the job,
70             using dt when it is 0 is silly.
71           */
72           *space = *fixed + 0.5;
73         }
74       else
75         {
76           bool dummy;
77           *space = *fixed + options->get_duration_space (dt.main_part_, &dummy);
78         }
79     }
80 }
81
82 Moment *
83 get_measure_length (Grob *column)
84 {
85   Grob * sys = column->get_parent (X_AXIS);
86
87   extract_grob_set (sys, "columns", cols);
88
89   vsize col_idx = Paper_column::get_rank (column);
90   
91   do
92     {
93       if (Moment *len = unsmob_moment (cols[col_idx]->get_property ("measure-length")))
94         {
95           return len;
96         }
97     }
98   while (col_idx-- != 0);
99   
100   return 0;
101 }
102
103 Real
104 Spacing_spanner::note_spacing (Grob *me, Grob *lc, Grob *rc,
105                                Spacing_options const *options,
106                                bool *expand_only)
107 {
108   (void) me;
109   
110   Moment shortest_playing_len = 0;
111   SCM s = lc->get_property ("shortest-playing-duration");
112
113   if (unsmob_moment (s))
114     shortest_playing_len = *unsmob_moment (s);
115
116   if (! shortest_playing_len.to_bool ())
117     {
118       programming_error ("cannot find a ruling note at: " + Paper_column::when_mom (lc).to_string ());
119       shortest_playing_len = 1;
120     }
121
122   Moment lwhen = Paper_column::when_mom (lc);
123   Moment rwhen = Paper_column::when_mom (rc);
124
125   Moment delta_t = rwhen - lwhen;
126
127   /*
128     when toying with mmrests, it is possible to have musical
129     column on the left and non-musical on the right, spanning
130     several measures.
131
132     TODO: efficiency: measure length can be cached, or stored as
133     property in paper-column.
134   */
135
136   if (Moment *measure_len = get_measure_length (lc))
137     {
138       delta_t = min (delta_t, *measure_len);
139
140       /*
141         The following is an extra safety measure, such that
142         the length of a mmrest event doesn't cause havoc.
143       */
144       shortest_playing_len = min (shortest_playing_len, *measure_len);
145     }
146
147   Real dist = 0.0;
148   if (delta_t.main_part_ && !lwhen.grace_part_)
149     {
150       dist = options->get_duration_space (shortest_playing_len.main_part_,
151                                           expand_only);
152       dist *= double (delta_t.main_part_ / shortest_playing_len.main_part_);
153     }
154   else if (delta_t.grace_part_)
155     {
156       /*
157         Crude hack for spacing graces: we take the shortest space
158         available (namely the space for the global shortest note), and
159         multiply that by grace-space-factor
160       */
161       dist = options->get_duration_space (options->global_shortest_, expand_only) / 2.0;
162       Grob *grace_spacing = unsmob_grob (lc->get_object ("grace-spacing"));
163       if (grace_spacing)
164         {
165           Spacing_options grace_opts;
166           grace_opts.init_from_grob (grace_spacing);
167           bool bla;
168           dist = grace_opts.get_duration_space (delta_t.grace_part_, &bla);
169         }
170       
171     }
172
173   return dist;
174 }
175