]> git.donarmstrong.com Git - lilypond.git/blob - lily/spaceable-grob.cc
``slikken kreng''
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <stdio.h> 
10 #include <math.h>
11
12 #include "spaceable-grob.hh"
13 #include "grob.hh"
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_grob_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
31   
32   SCM mins = get_minimum_distances (me);
33   SCM newdist = gh_double2scm (d);
34   for (SCM s = mins; gh_pair_p (s); s = ly_cdr (s))
35     {
36       SCM dist = ly_car (s);
37       if (ly_car (dist) == p->self_scm ())
38         {
39           gh_set_cdr_x (dist, scm_max (ly_cdr (dist),
40                                        newdist));
41           return ;
42         }
43     }
44
45   mins = gh_cons (gh_cons (p->self_scm (), newdist), mins);
46   me->set_grob_property ("minimum-distances", mins);
47 }
48
49 void
50 Spaceable_grob::add_spring (Grob*me, Grob * p, Real d, Real strength, bool expand_only)
51 {
52   //  printf ("dist %lf, str %lf\n", d, strength); 
53   if (d <= 0.0 || strength <= 0.0)
54     {
55       programming_error ("Adding reverse spring! Setting to unit spring");
56       d = 1.0;
57       strength = 1.0;
58     }
59   
60   if (isinf (d) || isnan(d)
61       || isnan (strength))
62     {
63       /*
64         strength == INF is possible. It means fixed distance.
65        */
66       programming_error ("Insane distance found.");
67       d = 1.0;
68       strength = 1.0;
69     }
70     
71 #ifndef NDEBUG
72   SCM mins = me->get_grob_property ("ideal-distances");
73   for (SCM s = mins; gh_pair_p (s); s = ly_cdr (s))
74     {
75       Spring_smob * sp = unsmob_spring(ly_car (s));
76       if (sp->other_ == p)
77         {
78           programming_error ("already have that spring");
79           return ;
80         }
81     }
82 #endif
83   
84   Spring_smob spring;
85   spring.strength_ = strength;
86   spring.distance_ = d;
87   spring.expand_only_b_ = expand_only;
88   spring.other_ = p;
89   
90   Group_interface::add_thing (me, ly_symbol2scm ("ideal-distances"), spring.smobbed_copy ());
91 }
92
93
94 void
95 Spaceable_grob::remove_interface (Grob*me)
96 {
97   me->remove_grob_property ("minimum-distances");
98   me->remove_grob_property ("spacing-wishes");
99   me->remove_grob_property ("ideal-distances");
100 }
101
102
103
104 ADD_INTERFACE (Spaceable_grob,"spaceable-grob-interface",
105   "A grob (a Paper_column) that takes part in the
106 spacing problem. ",
107   "measure-length spacing-wishes penalty minimum-distances ideal-distances
108 left-neighbors right-neighbors");
109