]> git.donarmstrong.com Git - lilypond.git/blob - lily/spaceable-grob.cc
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[lilypond.git] / lily / spaceable-grob.cc
1 /*
2   spaceable-grob.cc -- implement Spaceable_grob
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "spaceable-grob.hh"
10
11 #include <cstdio>
12
13 #include "warn.hh"
14 #include "spring.hh"
15 #include "pointer-group-interface.hh"
16 #include "grob.hh"
17 #include "paper-column.hh"
18 #include "international.hh"
19
20 SCM
21 Spaceable_grob::get_minimum_distances (Grob *me)
22 {
23   return me->get_object ("minimum-distances");
24 }
25
26 /*todo: merge code of spring & rod?
27  */
28 void
29 Spaceable_grob::add_rod (Grob *me, Grob *p, Real d)
30 {
31   //  printf ("rod %lf\n", d);
32   if (d < 0)
33     return;
34
35   if (isinf (d))
36     programming_error ("infinite rod");
37
38   SCM mins = get_minimum_distances (me);
39   SCM newdist = scm_from_double (d);
40   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
41     {
42       SCM dist = scm_car (s);
43       if (scm_car (dist) == p->self_scm ())
44         {
45           scm_set_cdr_x (dist, scm_max (scm_cdr (dist),
46                                         newdist));
47           return;
48         }
49     }
50
51   if (Paper_column::get_rank (p) < Paper_column::get_rank (me))
52     programming_error ("Adding reverse rod");
53
54   mins = scm_cons (scm_cons (p->self_scm (), newdist), mins);
55   me->set_object ("minimum-distances", mins);
56 }
57
58 void
59 Spaceable_grob::add_spring (Grob *me, Grob *other,
60                             Real distance, Real inverse_strength)
61 {
62   if (distance <= 0.0 || inverse_strength < 0.0)
63     {
64       programming_error ("adding reverse spring, setting to unit");
65       distance = 1.0;
66       inverse_strength = 1.0;
67     }
68
69   if (isinf (distance) || isnan (distance)
70       || isnan (inverse_strength))
71     {
72       /* strength == INF is possible. It means fixed distance.  */
73       programming_error ("insane distance found");
74       distance = 1.0;
75       inverse_strength = 1.0;
76     }
77
78 #ifndef NDEBUG
79   SCM mins = me->get_object ("ideal-distances");
80   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
81     {
82       Spring_smob *sp = unsmob_spring (scm_car (s));
83       if (sp->other_ == other)
84         {
85           programming_error ("already have that spring");
86           return;
87         }
88     }
89 #endif
90
91   Spring_smob spring;
92   spring.inverse_strength_ = inverse_strength;
93   spring.distance_ = distance;
94   spring.other_ = other;
95
96   SCM ideal = me->get_object ("ideal-distances");
97   ideal = scm_cons (spring.smobbed_copy (), ideal);
98   me->set_object ("ideal-distances", ideal);
99 }
100
101 void
102 Spaceable_grob::get_spring (Grob *this_col, Grob *next_col, Real *dist, Real *inv_strength)
103 {
104   Spring_smob *spring = 0;
105
106   for (SCM s = this_col->get_object ("ideal-distances");
107        !spring && scm_is_pair (s);
108        s = scm_cdr (s))
109     {
110       Spring_smob *sp = unsmob_spring (scm_car (s));
111
112       if (sp && sp->other_ == next_col)
113         spring = sp;
114     }
115
116   if (!spring)
117     programming_error (_f ("No spring between column %d and next one",
118                            Paper_column::get_rank (this_col)));
119
120   *dist = (spring) ? spring->distance_ : 5.0;
121   *inv_strength = (spring) ? spring->inverse_strength_ : 1.0;
122 }
123
124
125 void
126 Spaceable_grob::remove_interface (Grob *me)
127 {
128   me->set_object ("minimum-distances", SCM_EOL);
129   me->set_object ("spacing-wishes", SCM_EOL);
130   me->set_object ("ideal-distances", SCM_EOL);
131 }
132
133 ADD_INTERFACE (Spaceable_grob,
134                "A layout object that takes part in the spacing problem. ",
135                
136
137                /* properties */
138                "allow-loose-spacing "
139                "ideal-distances "
140                "keep-inside-line "
141                "left-neighbors "
142                "measure-length "
143                "minimum-distances "
144                "right-neighbors "
145                "spacing-wishes "
146
147                );
148