]> git.donarmstrong.com Git - lilypond.git/blob - lily/spaceable-grob.cc
*** empty log message ***
[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 "group-interface.hh"
17
18 SCM
19 Spaceable_grob::get_minimum_distances (Grob *me)
20 {
21   return me->get_property ("minimum-distances");
22 }
23
24 /*todo: merge code of spring & rod?
25  */
26 void
27 Spaceable_grob::add_rod (Grob *me, Grob *p, Real d)
28 {
29   //  printf ("rod %lf\n", d);
30   if (d < 0)
31     return;
32   
33   if (isinf (d))
34     programming_error ("infinite rod");
35
36   SCM mins = get_minimum_distances (me);
37   SCM newdist = scm_make_real (d);
38   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
39     {
40       SCM dist = scm_car (s);
41       if (scm_car (dist) == p->self_scm ())
42         {
43           scm_set_cdr_x (dist, scm_max (scm_cdr (dist),
44                                         newdist));
45           return;
46         }
47     }
48
49   mins = scm_cons (scm_cons (p->self_scm (), newdist), mins);
50   me->set_property ("minimum-distances", mins);
51 }
52
53 void
54 Spaceable_grob::add_spring (Grob *me, Grob *p, Real d, Real inverse_strength)
55 {
56   if (d <= 0.0 || inverse_strength < 0.0)
57     {
58       programming_error ("adding reverse spring, setting to unit");
59       d = 1.0;
60       inverse_strength = 1.0;
61     }
62
63   if (isinf (d) || isnan (d)
64       || isnan (inverse_strength))
65     {
66       /* strength == INF is possible. It means fixed distance.  */
67       programming_error ("insane distance found");
68       d = 1.0;
69       inverse_strength = 1.0;
70     }
71
72 #ifndef NDEBUG
73   SCM mins = me->get_property ("ideal-distances");
74   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
75     {
76       Spring_smob *sp = unsmob_spring (scm_car (s));
77       if (sp->other_ == p)
78         {
79           programming_error ("already have that spring");
80           return;
81         }
82     }
83 #endif
84
85   Spring_smob spring;
86   spring.inverse_strength_ = inverse_strength;
87   spring.distance_ = d;
88   spring.other_ = p;
89
90   Group_interface::add_thing (me, ly_symbol2scm ("ideal-distances"), spring.smobbed_copy ());
91 }
92
93 void
94 Spaceable_grob::remove_interface (Grob *me)
95 {
96   me->set_property ("minimum-distances", SCM_EOL);
97   me->set_property ("spacing-wishes", SCM_EOL);
98   me->set_property ("ideal-distances", SCM_EOL);
99 }
100
101 ADD_INTERFACE (Spaceable_grob, "spaceable-grob-interface",
102                "A layout object that takes part in the spacing problem. ",
103                "measure-length spacing-wishes penalty minimum-distances ideal-distances "
104                "allow-outside-line left-neighbors right-neighbors");
105