]> git.donarmstrong.com Git - lilypond.git/blob - lily/spanner.cc
release: 1.1.62
[lilypond.git] / lily / spanner.cc
1 /*
2   spanner.cc -- implement Spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "debug.hh"
10 #include "spanner.hh"
11 #include "paper-column.hh"
12 #include "paper-score.hh"
13 #include "molecule.hh"
14 #include "paper-outputter.hh"
15
16 void
17 Spanner::do_print() const
18 {
19 #ifndef NPRINT
20   DOUT << "Between " << classname (spanned_drul_[LEFT])
21        << " and " << classname (spanned_drul_[RIGHT]) << '\n';
22
23   if (broken_b ())
24     DOUT << "Broken in " << to_str (broken_into_l_arr_.size ()) << " pieces";
25 #endif
26 }
27
28 void
29 Spanner::break_into_pieces ()
30 {
31   if (broken_b ())
32     return; 
33          
34   Item * left = spanned_drul_[LEFT];
35   Item * right = spanned_drul_[RIGHT];
36   
37   if  (left == right)
38     {
39       warning (_ ("left spanpoint is right spanpoint\n"));
40       return;
41     }
42   
43   Link_array<Item> break_points = pscore_l_->broken_col_range (left,right);
44
45   break_points.insert (left,0);
46   break_points.push (right);
47
48   for (int i=1; i < break_points.size(); i++) 
49     {
50       Breaking_information info;
51       info.bounds_[LEFT] = break_points[i-1];
52       info.bounds_[RIGHT] = break_points[i];
53       Direction d = LEFT;
54       do
55         {
56           Item *&pc_l = info.bounds_[d] ;
57           if (!pc_l->line_l())
58             pc_l =  pc_l->find_prebroken_piece(- d);
59           
60           assert (pc_l);
61           if (!info.line_l_)
62             info.line_l_ = pc_l-> line_l ();
63           else
64             assert( info.line_l_ = pc_l->line_l ());
65           
66         }
67       while ((flip(&d))!= LEFT);
68
69       Spanner *span_p = dynamic_cast<Spanner*>(clone ());
70       span_p->set_bounds(LEFT,info.bounds_[LEFT]);
71       span_p->set_bounds(RIGHT,info.bounds_[RIGHT]);
72       pscore_l_->typeset_element (span_p);
73               
74       info.broken_spanner_l_ = span_p;
75       span_p->handle_broken_dependencies();
76
77       broken_into_l_arr_.push (span_p);
78     }
79 }
80
81 void
82 Spanner::set_my_columns()
83 {
84   Direction i = (Direction)1;
85   do 
86     {
87       if (!spanned_drul_[i]->line_l())
88         set_bounds(i,spanned_drul_[i]->find_prebroken_piece((Direction)-i));
89     } 
90   while (flip(&i) != 1);
91 }       
92
93 void
94 Spanner::set_bounds(Direction d, Item*i)
95 {
96   spanned_drul_[d] =i;
97   if (i)
98     {
99       i->used_b_ = true;
100     }
101
102   if (d== LEFT)
103     {
104       dim_cache_[X_AXIS]->parent_l_ = i->dim_cache_[X_AXIS];
105     }
106   
107   if  (spanned_drul_[Direction(-d)] == spanned_drul_[d]
108        && i)
109     warning (_f ("Spanner `%s\' with equal left and right spanpoints", classname (this)));
110 }
111
112 void
113 Spanner::do_break_processing()
114 {
115   if (!line_l())
116     break_into_pieces ();
117   else 
118     handle_broken_dependencies();
119 }
120
121 Spanner::Spanner ()
122 {
123   spanned_drul_[LEFT]=0;
124   spanned_drul_[RIGHT]=0;
125 }
126
127 Spanner::Spanner (Spanner const &s)
128   : Score_element (s)
129 {
130   spanned_drul_[LEFT] = spanned_drul_[RIGHT] =0;
131 }
132
133
134 Interval
135 Spanner::do_width() const
136 {  
137   Real l = spanned_drul_[LEFT]->absolute_coordinate (X_AXIS);
138   Real r = spanned_drul_[RIGHT]->absolute_coordinate (X_AXIS);
139
140   if (r< l)
141     warning ("Spanner with negative length");
142         
143   return Interval (0, r-l);
144 }
145
146 Line_of_score *
147 Spanner::line_l() const
148 {
149   if (!spanned_drul_[LEFT] || !spanned_drul_[RIGHT])
150     return 0;
151   if (spanned_drul_[LEFT]->line_l() != spanned_drul_[RIGHT]->line_l())
152     return 0;
153   return spanned_drul_[LEFT]->line_l();
154 }
155
156
157 Spanner*
158 Spanner::find_broken_piece (Line_of_score*l) const
159 {
160   for (int i=0; i < broken_into_l_arr_.size (); i++)
161     {
162       if (broken_into_l_arr_[i]->line_l () == l)
163         return broken_into_l_arr_[i];
164     }
165
166   return 0;                                
167 }
168
169 bool
170 Spanner::broken_b() const
171 {
172   return broken_into_l_arr_.size();
173 }
174
175 Array<Rod>
176 Spanner::get_rods () const
177 {
178   Array<Rod> r;
179   return r;
180 }
181
182 Array<Spring>
183 Spanner::get_springs () const
184 {
185   Array<Spring> s;
186   return s;    
187 }
188
189 void
190 Spanner::do_space_processing ()
191 {
192   Array<Rod> rs (get_rods ());
193   for (int i=0; i < rs.size (); i++)
194     {
195       rs[i].add_to_cols ();
196     }
197
198   Array<Spring> ss (get_springs ());
199   for (int i=0; i < ss.size (); i++)
200     {
201       ss[i].add_to_cols ();
202     }
203 }
204
205 /*
206   UGH.
207  */
208 void
209 Spanner::handle_broken_dependents ()
210 {
211   Spanner *unbrok = dynamic_cast<Spanner*> (original_l_);
212   if (!unbrok || dim_cache_[Y_AXIS]->parent_l_)
213     return;
214   
215   Spanner *refpoint = dynamic_cast<Spanner*> (unbrok->parent_l (Y_AXIS));
216   
217   if (refpoint)
218     {
219       Spanner * broken_refpoint = refpoint->find_broken_piece (line_l ());
220       if (broken_refpoint)
221         dim_cache_[Y_AXIS]->parent_l_ = broken_refpoint->dim_cache_[Y_AXIS];
222       else
223         programming_error ("Spanner y -refpoint lost.");
224     }
225 }