]> git.donarmstrong.com Git - lilypond.git/blob - lily/separating-group-spanner.cc
patch::: 1.5.16.jcn1
[lilypond.git] / lily / separating-group-spanner.cc
1 /*   
2   separating-group-spanner.cc --  implement Separating_group_spanner
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "separating-group-spanner.hh"
11 #include "separation-item.hh"
12 #include "paper-column.hh"
13 #include "paper-def.hh"
14 #include "dimensions.hh"
15 #include "group-interface.hh"
16
17 void
18 Separating_group_spanner::find_rods (Item * r, SCM next)
19 {
20   Interval ri (Separation_item::my_width (r));
21   if (ri.empty_b ())
22     return;
23
24   /*
25     This is an inner loop, however, in most cases, the interesting L
26     will just be the first entry of NEXT, making it linear in most of
27     the cases.  */
28   for(; gh_pair_p (next); next = ly_cdr (next))
29     {
30       Item *l = dynamic_cast<Item*> (unsmob_grob (ly_car( next)));
31       Item *lb = l->find_prebroken_piece (RIGHT);
32
33       if (lb)
34         {
35           Interval li (Separation_item::my_width (lb));
36
37           if (!li.empty_b ())
38             {
39               Rod rod;
40
41               rod.item_l_drul_[LEFT] = lb;
42               rod.item_l_drul_[RIGHT] = r;
43
44               rod.distance_f_ = li[RIGHT] - ri[LEFT];
45         
46               rod.columnize ();
47               rod.add_to_cols ();
48             }
49         }
50
51       Interval li (Separation_item::my_width (l));
52       if (!li.empty_b ())
53         {
54           Rod rod;
55
56           rod.item_l_drul_[LEFT] =l;
57           rod.item_l_drul_[RIGHT]=r;
58
59           rod.distance_f_ = li[RIGHT] - ri[LEFT];
60         
61           rod.columnize ();
62           rod.add_to_cols ();
63
64           break;
65         }
66       else
67         /*
68           this grob doesn't cause a constraint. We look further until we
69           find one that does.  */
70         ;
71     }
72 }
73
74 MAKE_SCHEME_CALLBACK (Separating_group_spanner,set_spacing_rods,1);
75 SCM
76 Separating_group_spanner::set_spacing_rods (SCM smob)
77 {
78   Grob*me = unsmob_grob (smob);
79   
80   for (SCM s = me->get_grob_property ("elements"); gh_pair_p (s) && gh_pair_p (ly_cdr (s)); s = ly_cdr (s))
81     {
82       /*
83         Order of elements is reversed!
84        */
85       SCM elt = ly_car (s);
86       Item *r = dynamic_cast<Item*> (unsmob_grob (elt));
87
88       if (!r)
89         continue;
90
91       Item *rb
92         = dynamic_cast<Item*> (r->find_prebroken_piece (LEFT));
93       
94       find_rods (r, ly_cdr (s));
95       if (rb)
96         find_rods (rb, ly_cdr (s));
97     }
98   find_musical_sequences (me);
99 #if 0
100   /*
101     TODO; restore this.
102    */
103   /*
104     We've done our job, so we get lost. 
105    */
106   for (SCM s = me->get_grob_property ("elements"); gh_pair_p (s); s = ly_cdr (s))
107     {
108       Item * it =dynamic_cast<Item*> (unsmob_grob (ly_car (s)));
109       if (it && it->broken_b ())
110         {
111           it->find_prebroken_piece (LEFT) ->suicide ();
112           it->find_prebroken_piece (RIGHT)->suicide ();
113         }
114       it->suicide ();
115     }
116   me->suicide ();
117 #endif
118   return SCM_UNSPECIFIED ;
119 }
120
121 void
122 Separating_group_spanner::add_spacing_unit (Grob* me ,Item*i)
123 {
124   Pointer_group_interface::add_element (me, "elements",i);
125   me->add_dependency (i);
126 }
127
128
129 void
130 Separating_group_spanner::find_musical_sequences (Grob *me)
131 {
132   Item *last = 0;
133   Item *llast = 0;
134   for (SCM s = me->get_grob_property ("elements");
135        gh_pair_p (s); s = ly_cdr (s))
136     {
137       Item *it = dynamic_cast<Item*> (unsmob_grob (ly_car (s)));
138       if (last)
139         {       
140           Item *lcol = last->column_l ();
141           Item *col = it->column_l ();
142
143           int lrank = Paper_column::rank_i (lcol);
144           int rank = Paper_column ::rank_i (col);
145
146           bool mus = Paper_column::musical_b (col);
147           bool lmus = Paper_column::musical_b (lcol);
148
149           if ((lrank - rank == 2) && lmus && mus)
150             {
151               SCM seq = col->get_grob_property ("spacing-sequence");
152               col->set_grob_property ("spacing-sequence",
153                                       gh_cons (gh_cons (it->self_scm (), last->self_scm ()), seq));
154             }
155
156           if (llast && !Paper_column::breakable_b (last))
157             {
158               Item *llcol = llast->column_l ();
159               int llrank = Paper_column::rank_i (llcol);
160               bool llmus= Paper_column::musical_b (llcol);
161               if (llrank - lrank == 1
162                   && lrank - rank == 1
163                   && llmus && !lmus && mus)
164                 {
165                   SCM seq = col->get_grob_property ("spacing-sequence");
166                   col->set_grob_property ("spacing-sequence",
167                                           gh_cons (gh_cons (it->self_scm (), last->self_scm ()), seq));
168                 }
169               else if (!lmus)
170                 {
171                   SCM between = lcol->get_grob_property ("between-cols");
172
173                   if (!gh_pair_p (between))
174                     {
175                       between = gh_cons (it->self_scm (), llast->self_scm ());
176                       lcol ->set_grob_property ("between-cols", between);
177                     }
178
179                   Item * left
180                     = dynamic_cast<Item*> (unsmob_grob (ly_car (between)));
181                   if(Paper_column::rank_i (left->column_l ()) < rank)
182                     gh_set_car_x (between, col->self_scm());
183                   
184                   Item * right
185                     = dynamic_cast<Item*> (unsmob_grob (ly_cdr (between)));
186                   if (Paper_column::rank_i (right->column_l ()) > llrank )
187                     gh_set_cdr_x (between, llcol->self_scm ());
188                 }
189             }
190         }
191
192       llast = last;
193       last = it;
194     }
195 }
196
197 #if 0
198 void
199 Separating_group_spanner::set_loose_rods ()
200 {
201   // loose columns should  also generate minimum distances.
202   // TODO
203 }
204 #endif
205
206
207 void
208 Separating_group_spanner::set_interface (Grob*)
209 {
210 }
211
212 bool
213 Separating_group_spanner::has_interface (Grob*)
214 {//todo
215   assert (false);
216 }