]> git.donarmstrong.com Git - lilypond.git/blob - lily/spaceable-grob.cc
(parse_symbol_list): Bugfix.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "spaceable-grob.hh"
10
11 #include <cstdio>
12 #include <math.h>
13
14 #include "warn.hh"
15 #include "spring.hh"
16 #include "pointer-group-interface.hh"
17 #include "grob.hh"
18 #include "paper-column.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     {
53       programming_error ("Adding reverse rod");
54     }
55
56   mins = scm_cons (scm_cons (p->self_scm (), newdist), mins);
57   me->set_object ("minimum-distances", mins);
58 }
59
60 void
61 Spaceable_grob::add_spring (Grob *me, Grob *p, Real d, Real inverse_strength)
62 {
63   if (d <= 0.0 || inverse_strength < 0.0)
64     {
65       programming_error ("adding reverse spring, setting to unit");
66       d = 1.0;
67       inverse_strength = 1.0;
68     }
69
70   if (isinf (d) || isnan (d)
71       || isnan (inverse_strength))
72     {
73       /* strength == INF is possible. It means fixed distance.  */
74       programming_error ("insane distance found");
75       d = 1.0;
76       inverse_strength = 1.0;
77     }
78
79 #ifndef NDEBUG
80   SCM mins = me->get_object ("ideal-distances");
81   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
82     {
83       Spring_smob *sp = unsmob_spring (scm_car (s));
84       if (sp->other_ == p)
85         {
86           programming_error ("already have that spring");
87           return;
88         }
89     }
90 #endif
91
92   Spring_smob spring;
93   spring.inverse_strength_ = inverse_strength;
94   spring.distance_ = d;
95   spring.other_ = p;
96
97   SCM ideal = me->get_object ("ideal-distances");
98   ideal = scm_cons (spring.smobbed_copy (), ideal);
99   me->set_object ("ideal-distances", ideal);
100 }
101
102 void
103 Spaceable_grob::get_spring (Grob *me, Grob *other, Real *dist, Real *inv_strength)
104 {
105   for (SCM s = me->get_object ("ideal-distances");
106        scm_is_pair (s); s = scm_cdr (s))
107     {
108       Spring_smob *spring = unsmob_spring (scm_car (s));
109       if (spring && spring->other_ == other)
110         {
111           *dist = spring->distance_;
112           *inv_strength = spring->inverse_strength_;
113         }
114     }
115 }
116
117 void
118 Spaceable_grob::remove_interface (Grob *me)
119 {
120   me->set_object ("minimum-distances", SCM_EOL);
121   me->set_object ("spacing-wishes", SCM_EOL);
122   me->set_object ("ideal-distances", SCM_EOL);
123 }
124
125 ADD_INTERFACE (Spaceable_grob, "spaceable-grob-interface",
126                "A layout object that takes part in the spacing problem. ",
127                "measure-length spacing-wishes penalty minimum-distances ideal-distances "
128                "keep-inside-line left-neighbors right-neighbors");
129