]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-basic.cc
bed086a72cefab0672589b0042cfb0e5f3bb1f14
[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 #include "spacing-interface.hh"
18 #include "spring.hh"
19
20 /*
21   LilyPond spaces by taking a simple-minded spacing algorithm, and
22   adding subtle adjustments to that. This file does the simple-minded
23   spacing routines.
24 */
25 /*
26   The one-size-fits all spacing. It doesn't take into account
27   different spacing wishes from one to the next column.
28 */
29 Spring
30 Spacing_spanner::standard_breakable_column_spacing (Grob *me, Item *l, Item *r, Spacing_options const *options)
31 {
32   Real min_dist = max (0.0, Paper_column::minimum_distance (l, r));
33   Real ideal;
34
35   if (Paper_column::is_breakable (l) && Paper_column::is_breakable (r))
36     {
37       Moment *dt = unsmob_moment (l->get_property ("measure-length"));
38       Moment mlen (1);
39       if (dt)
40         mlen = *dt;
41
42       Real incr = robust_scm2double (me->get_property ("spacing-increment"), 1);
43
44       ideal = min_dist + incr * double (mlen.main_part_ / options->global_shortest_) * 0.8;
45     }
46   else
47     {
48       Moment dt = Paper_column::when_mom (r) - Paper_column::when_mom (l);
49
50       if (dt == Moment (0, 0))
51         {
52           /*
53             In this case, Staff_spacing should handle the job,
54             using dt when it is 0 is silly.
55           */
56           ideal = min_dist + 0.5;
57         }
58       else
59         ideal = min_dist + options->get_duration_space (dt.main_part_);
60     }
61   return Spring (ideal, min_dist);
62 }
63
64 Moment *
65 get_measure_length (Grob *column)
66 {
67   Grob * sys = column->get_parent (X_AXIS);
68
69   extract_grob_set (sys, "columns", cols);
70
71   vsize col_idx = Paper_column::get_rank (column);
72   
73   do
74     {
75       if (Moment *len = unsmob_moment (cols[col_idx]->get_property ("measure-length")))
76         {
77           return len;
78         }
79     }
80   while (col_idx-- != 0);
81   
82   return 0;
83 }
84
85 Real
86 Spacing_spanner::note_spacing (Grob *me, Grob *lc, Grob *rc,
87                                Spacing_options const *options)
88 {
89   (void) me;
90   
91   Moment shortest_playing_len = 0;
92   SCM s = lc->get_property ("shortest-playing-duration");
93
94   if (unsmob_moment (s))
95     shortest_playing_len = *unsmob_moment (s);
96
97   if (! shortest_playing_len.to_bool ())
98     {
99       programming_error ("cannot find a ruling note at: " + Paper_column::when_mom (lc).to_string ());
100       shortest_playing_len = 1;
101     }
102
103   Moment lwhen = Paper_column::when_mom (lc);
104   Moment rwhen = Paper_column::when_mom (rc);
105
106   Moment delta_t = rwhen - lwhen;
107
108   /*
109     when toying with mmrests, it is possible to have musical
110     column on the left and non-musical on the right, spanning
111     several measures.
112
113     TODO: efficiency: measure length can be cached, or stored as
114     property in paper-column.
115   */
116
117   if (Moment *measure_len = get_measure_length (lc))
118     {
119       delta_t = min (delta_t, *measure_len);
120
121       /*
122         The following is an extra safety measure, such that
123         the length of a mmrest event doesn't cause havoc.
124       */
125       shortest_playing_len = min (shortest_playing_len, *measure_len);
126     }
127
128   Real dist = 0.0;
129   if (delta_t.main_part_ && !lwhen.grace_part_)
130     {
131       dist = options->get_duration_space (shortest_playing_len.main_part_);
132       dist *= double (delta_t.main_part_ / shortest_playing_len.main_part_);
133     }
134   else if (delta_t.grace_part_)
135     {
136       /*
137         Crude hack for spacing graces: we take the shortest space
138         available (namely the space for the global shortest note), and
139         multiply that by grace-space-factor
140       */
141       dist = options->get_duration_space (options->global_shortest_) / 2.0;
142       Grob *grace_spacing = unsmob_grob (lc->get_object ("grace-spacing"));
143       if (grace_spacing)
144         {
145           Spacing_options grace_opts;
146           grace_opts.init_from_grob (grace_spacing);
147           dist = grace_opts.get_duration_space (delta_t.grace_part_);
148         }
149       
150     }
151
152   return dist;
153 }
154