]> 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
19 SCM
20 Spaceable_grob::get_minimum_distances (Grob *me)
21 {
22   return me->get_object ("minimum-distances");
23 }
24
25 /*todo: merge code of spring & rod?
26  */
27 void
28 Spaceable_grob::add_rod (Grob *me, Grob *p, Real d)
29 {
30   //  printf ("rod %lf\n", d);
31   if (d < 0)
32     return;
33
34   if (isinf (d))
35     programming_error ("infinite rod");
36
37   SCM mins = get_minimum_distances (me);
38   SCM newdist = scm_from_double (d);
39   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
40     {
41       SCM dist = scm_car (s);
42       if (scm_car (dist) == p->self_scm ())
43         {
44           scm_set_cdr_x (dist, scm_max (scm_cdr (dist),
45                                         newdist));
46           return;
47         }
48     }
49
50   if (Paper_column::get_rank (p) < Paper_column::get_rank (me))
51     programming_error ("Adding reverse rod");
52
53   mins = scm_cons (scm_cons (p->self_scm (), newdist), mins);
54   me->set_object ("minimum-distances", mins);
55 }
56
57 void
58 Spaceable_grob::add_spring (Grob *me, Grob *other,
59                             Real distance, Real inverse_strength)
60 {
61   if (distance <= 0.0 || inverse_strength < 0.0)
62     {
63       programming_error ("adding reverse spring, setting to unit");
64       distance = 1.0;
65       inverse_strength = 1.0;
66     }
67
68   if (isinf (distance) || isnan (distance)
69       || isnan (inverse_strength))
70     {
71       /* strength == INF is possible. It means fixed distance.  */
72       programming_error ("insane distance found");
73       distance = 1.0;
74       inverse_strength = 1.0;
75     }
76
77 #ifndef NDEBUG
78   SCM mins = me->get_object ("ideal-distances");
79   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
80     {
81       Spring_smob *sp = unsmob_spring (scm_car (s));
82       if (sp->other_ == other)
83         {
84           programming_error ("already have that spring");
85           return;
86         }
87     }
88 #endif
89
90   Spring_smob spring;
91   spring.inverse_strength_ = inverse_strength;
92   spring.distance_ = distance;
93   spring.other_ = other;
94
95   SCM ideal = me->get_object ("ideal-distances");
96   ideal = scm_cons (spring.smobbed_copy (), ideal);
97   me->set_object ("ideal-distances", ideal);
98 }
99
100 void
101 Spaceable_grob::get_spring (Grob *me, Grob *other, Real *dist, Real *inv_strength)
102 {
103   for (SCM s = me->get_object ("ideal-distances");
104        scm_is_pair (s); s = scm_cdr (s))
105     {
106       Spring_smob *spring = unsmob_spring (scm_car (s));
107       if (spring && spring->other_ == other)
108         {
109           *dist = spring->distance_;
110           *inv_strength = spring->inverse_strength_;
111         }
112     }
113 }
114
115 void
116 Spaceable_grob::remove_interface (Grob *me)
117 {
118   me->set_object ("minimum-distances", SCM_EOL);
119   me->set_object ("spacing-wishes", SCM_EOL);
120   me->set_object ("ideal-distances", SCM_EOL);
121 }
122
123 ADD_INTERFACE (Spaceable_grob,
124                "A layout object that takes part in the spacing problem. ",
125                
126
127                /* properties */
128                "allow-loose-spacing "
129                "ideal-distances "
130                "keep-inside-line "
131                "left-neighbors "
132                "measure-length "
133                "minimum-distances "
134                "right-neighbors "
135                "spacing-wishes "
136
137                );
138