]> 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 (isinf(d) || d < 0)
31     {
32       programming_error ("Weird rod");
33     }
34
35   SCM mins = get_minimum_distances (me);
36   SCM newdist = scm_make_real (d);
37   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
38     {
39       SCM dist = scm_car (s);
40       if (scm_car (dist) == p->self_scm ())
41         {
42           scm_set_cdr_x (dist, scm_max (scm_cdr (dist),
43                                         newdist));
44           return;
45         }
46     }
47
48   mins = scm_cons (scm_cons (p->self_scm (), newdist), mins);
49   me->set_property ("minimum-distances", mins);
50 }
51
52 void
53 Spaceable_grob::add_spring (Grob *me, Grob *p, Real d, Real strength)
54 {
55   //  printf ("dist %lf, str %lf\n", d, strength); 
56   if (d <= 0.0 || strength <= 0.0)
57     {
58       programming_error ("Adding reverse spring! Setting to unit spring");
59       d = 1.0;
60       strength = 1.0;
61     }
62
63   if (isinf (d) || isnan (d)
64       || isnan (strength))
65     {
66       /*
67         strength == INF is possible. It means fixed distance.
68       */
69       programming_error ("Insane distance found.");
70       d = 1.0;
71       strength = 1.0;
72     }
73
74 #ifndef NDEBUG
75   SCM mins = me->get_property ("ideal-distances");
76   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
77     {
78       Spring_smob *sp = unsmob_spring (scm_car (s));
79       if (sp->other_ == p)
80         {
81           programming_error ("already have that spring");
82           return;
83         }
84     }
85 #endif
86
87   Spring_smob spring;
88   spring.strength_ = strength;
89   spring.distance_ = d;
90   spring.other_ = p;
91
92   Group_interface::add_thing (me, ly_symbol2scm ("ideal-distances"), spring.smobbed_copy ());
93 }
94
95 void
96 Spaceable_grob::remove_interface (Grob *me)
97 {
98   me->set_property ("minimum-distances", SCM_EOL);
99   me->set_property ("spacing-wishes", SCM_EOL);
100   me->set_property ("ideal-distances", SCM_EOL);
101 }
102
103 ADD_INTERFACE (Spaceable_grob, "spaceable-grob-interface",
104                "A layout object that takes part in the spacing problem. ",
105                "measure-length spacing-wishes penalty minimum-distances ideal-distances "
106                "allow-outside-line left-neighbors right-neighbors");
107