]> git.donarmstrong.com Git - lilypond.git/blob - lily/spaceable-grob.cc
use springs instead of fixed/distance pairs
[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--2007 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 #include "international.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     programming_error ("Adding reverse rod");
53
54   mins = scm_cons (scm_cons (p->self_scm (), newdist), mins);
55   me->set_object ("minimum-distances", mins);
56 }
57
58 void
59 Spaceable_grob::add_spring (Grob *me, Grob *other,
60                             Real distance, Real inverse_strength)
61 {
62 #ifndef NDEBUG
63   SCM mins = me->get_object ("ideal-distances");
64   for (SCM s = mins; scm_is_pair (s); s = scm_cdr (s))
65     {
66       Spring *sp = unsmob_spring (scm_car (s));
67       if (sp->other_ == other)
68         {
69           programming_error ("already have that spring");
70           return;
71         }
72     }
73 #endif
74
75   Spring spring;
76   spring.set_inverse_stretch_strength (inverse_strength);
77   spring.set_inverse_compress_strength (inverse_strength);
78   spring.set_distance (distance);
79   spring.other_ = other;
80
81   SCM ideal = me->get_object ("ideal-distances");
82   ideal = scm_cons (spring.smobbed_copy (), ideal);
83   me->set_object ("ideal-distances", ideal);
84 }
85
86 void
87 Spaceable_grob::add_spring (Grob *me, Grob *other, Spring sp)
88 {
89   SCM ideal = me->get_object ("ideal-distances");
90   sp.other_ = other;
91   ideal = scm_cons (sp.smobbed_copy (), ideal);
92   me->set_object ("ideal-distances", ideal);
93 }
94
95 void
96 Spaceable_grob::get_spring (Grob *this_col, Grob *next_col, Real *dist, Real *inv_strength)
97 {
98   Spring *spring = 0;
99
100   for (SCM s = this_col->get_object ("ideal-distances");
101        !spring && scm_is_pair (s);
102        s = scm_cdr (s))
103     {
104       Spring *sp = unsmob_spring (scm_car (s));
105
106       if (sp && sp->other_ == next_col)
107         spring = sp;
108     }
109
110   if (!spring)
111     programming_error (_f ("No spring between column %d and next one",
112                            Paper_column::get_rank (this_col)));
113
114   *dist = (spring) ? spring->distance () : 5.0;
115   *inv_strength = (spring) ? spring->inverse_stretch_strength () : 1.0;
116 }
117
118
119
120 ADD_INTERFACE (Spaceable_grob,
121                "A layout object that takes part in the spacing problem. ",
122                
123
124                /* properties */
125                "allow-loose-spacing "
126                "ideal-distances "
127                "keep-inside-line "
128                "left-neighbors "
129                "measure-length "
130                "minimum-distances "
131                "right-neighbors "
132                "spacing-wishes "
133
134                );
135