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