]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.14
[lilypond.git] / lily / score-element.cc
1 /*
2   score-elem.cc -- implement Score_element
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <string.h>
11
12 #include "group-interface.hh"
13 #include "misc.hh"
14 #include "paper-score.hh"
15 #include "paper-def.hh"
16 #include "lookup.hh"
17 #include "molecule.hh"
18 #include "score-element.hh"
19 #include "debug.hh"
20 #include "spanner.hh"
21 #include "line-of-score.hh"
22 #include "item.hh"
23 #include "paper-column.hh"
24 #include "molecule.hh"
25 #include "misc.hh"
26 #include "paper-outputter.hh"
27 #include "dimension-cache.hh"
28 #include "side-position-interface.hh"
29 #include "item.hh"
30
31 Score_element::Score_element()
32 {
33   output_p_ =0;
34   dim_cache_[X_AXIS] = new Dimension_cache;
35   dim_cache_[Y_AXIS] = new Dimension_cache;
36   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
37   
38   used_b_ = false;
39
40   dim_cache_[X_AXIS]->set_callback (molecule_extent);
41   dim_cache_[Y_AXIS]->set_callback (molecule_extent); 
42   used_b_ = false;
43   pscore_l_=0;
44   lookup_l_ =0;
45   status_i_ = 0;
46   self_scm_ = SCM_EOL;
47   original_l_ = 0;
48   element_property_alist_ = SCM_EOL;
49
50   smobify_self ();
51
52
53   set_elt_property ("dependencies", SCM_EOL);
54 }
55
56 SCM ly_deep_copy (SCM);
57
58 SCM
59 ly_deep_copy (SCM l)
60 {
61   if (gh_pair_p (l))
62     {
63       return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
64     }
65   else
66     return l;
67 }
68
69
70 Score_element::Score_element (Score_element const&s)
71 {
72   dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
73   dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
74   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
75   
76   self_scm_ = SCM_EOL;
77   used_b_ = true;
78   original_l_ =(Score_element*) &s;
79
80   /*
81     should protect because smobify_self () might trigger GC.
82    */
83   element_property_alist_ = scm_protect_object (ly_deep_copy (s.element_property_alist_));
84
85   output_p_ =0;
86   status_i_ = s.status_i_;
87   lookup_l_ = s.lookup_l_;
88   pscore_l_ = s.pscore_l_;
89
90   smobify_self ();
91 }
92
93 Score_element::~Score_element()
94 {
95   assert (!output_p_);
96   assert (status_i_ >=0);
97   status_i_  = -1;
98
99   delete dim_cache_[X_AXIS];
100   delete dim_cache_[Y_AXIS];  
101 }
102
103
104 Real
105 Score_element::get_real (String s) const
106 {
107   return gh_scm2double (get_elt_property (s));
108 }
109
110 void
111 Score_element::set_real (String s, Real r)
112 {
113   set_elt_property (s, gh_double2scm (r));
114 }
115
116 // should also have one that takes SCM arg. 
117 SCM
118 Score_element::get_elt_property (String nm) const
119 {
120   SCM sym =  ly_symbol2scm (nm.ch_C());
121   SCM s = scm_assq(sym, element_property_alist_);
122
123   if (s != SCM_BOOL_F)
124     return gh_cdr (s); 
125   
126   if (pscore_l_)
127     {
128       SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
129       SCM val;
130       
131       // should probably check for Type::sym as well.
132       Paper_def * p= pscore_l_->paper_l_;
133       if (p->default_properties_.try_retrieve (sym2, &val))
134         return val;
135       else if (p->default_properties_.try_retrieve (sym, &val))
136         return val;
137     }
138   
139   return SCM_UNDEFINED;
140 }
141
142 SCM
143 Score_element::remove_elt_property (String key)
144 {
145   SCM s = get_elt_property (key); 
146   SCM sym = ly_symbol2scm (key.ch_C());
147   element_property_alist_ =  scm_assq_remove_x (element_property_alist_, sym);
148   return s;
149 }
150
151 /*
152   UGH. assoc vs. assq
153  */
154 void
155 Score_element::set_elt_property (String k, SCM v)
156 {
157   SCM s = ly_symbol2scm (k.ch_C( ));
158   element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
159 }
160
161 Interval
162 Score_element::molecule_extent(Dimension_cache const *c)
163 {
164   Score_element *s = dynamic_cast<Score_element*>(c->element_l());
165   Molecule*m = s->do_brew_molecule_p();
166   return  m->extent()[c->axis ()];
167 }
168
169
170 void
171 Score_element::print() const
172 {
173 #ifndef NPRINT
174   DEBUG_OUT << classname(this) << "{\n";
175   
176   
177   if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
178     ly_display_scm (element_property_alist_);
179
180   if (original_l_)
181     DEBUG_OUT << "Copy ";
182   do_print();
183   
184   DEBUG_OUT <<  "}\n";
185 #endif
186 }
187
188 Paper_def*
189 Score_element::paper_l ()  const
190 {
191  return pscore_l_ ? pscore_l_->paper_l_ : 0;
192 }
193
194 Lookup const *
195 Score_element::lookup_l () const
196 {
197   if (!lookup_l_)
198     {
199       Score_element * urg = (Score_element*)this;
200       SCM sz = urg->remove_elt_property ("fontsize");
201       int i = (sz != SCM_UNDEFINED)
202         ? gh_scm2int  (sz)
203         : 0;
204
205       urg->lookup_l_ =  (Lookup*)pscore_l_->paper_l_->lookup_l (i);
206     }
207   return lookup_l_;
208 }
209
210 void
211 Score_element::add_processing()
212 {
213   assert (status_i_ >=0);
214   if (status_i_)
215     return;
216   status_i_ ++;
217
218 #if 0
219     /*
220     UGH. UGH. UGH.
221    */
222   if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
223       && !dim_cache_[X_AXIS]->off_callback_l_)
224     {
225       dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::self_alignment);
226     }
227   
228   if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
229       && !dim_cache_[X_AXIS]->off_callback_l_)
230       
231     {
232       dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::self_alignment);
233     }
234 #endif
235   
236   do_add_processing();
237 }
238
239 void
240 Score_element::calculate_dependencies (int final, int busy,
241                                        Score_element_method_pointer funcptr)
242 {
243   assert (status_i_ >=0);
244
245   if (status_i_ >= final)
246     return;
247
248   assert (status_i_!= busy);
249   status_i_= busy;
250
251   Link_array<Score_element> dependency_arr =
252     Group_interface__extract_elements (this, (Score_element*)0, "dependencies");
253   
254   for (int i=0; i < dependency_arr.size(); i++)
255     dependency_arr[i]->calculate_dependencies (final, busy, funcptr);
256
257   Link_array<Score_element> extra (get_extra_dependencies());
258   for (int i=0; i < extra.size(); i++)
259     extra[i]->calculate_dependencies (final, busy, funcptr);
260   
261   (this->*funcptr)();
262   status_i_= final;
263 }
264
265 void
266 Score_element::output_processing () 
267 {
268   if (get_elt_property ("transparent") != SCM_UNDEFINED)
269     return;
270
271   // we're being silly here. 
272   if (output_p_)
273     delete output_p_;
274   
275   output_p_ = do_brew_molecule_p ();
276   Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
277
278   SCM s = get_elt_property ("extra-offset");
279   if (gh_pair_p (s))
280     {
281       Real il = paper_l ()->get_var ("interline");
282       o[X_AXIS] += il * gh_scm2double (gh_car (s));
283       o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
284     }
285   
286   pscore_l_->outputter_l_->output_molecule (output_p_,
287                                             o,
288                                             classname(this));
289
290   delete output_p_;
291   output_p_ =0;
292 }
293
294 /*
295   
296   VIRTUAL STUBS
297
298  */
299 void
300 Score_element::do_break_processing()
301 {
302 }
303
304 void
305 Score_element::do_post_processing()
306 {
307 }
308
309 void
310 Score_element::do_breakable_col_processing()
311 {
312   handle_prebroken_dependencies();
313 }
314
315 void
316 Score_element::do_pre_processing()
317 {
318 }
319
320 void
321 Score_element::do_space_processing ()
322 {
323 }
324
325 void
326 Score_element::do_add_processing()
327 {
328 }
329
330
331
332 Molecule*
333 Score_element::do_brew_molecule_p() const
334 {
335   Interval emp;
336   emp.set_empty ();
337   Molecule a (lookup_l ()->fill (Box (emp,emp)));
338   return new Molecule (a);
339 }
340
341
342 Line_of_score *
343 Score_element::line_l() const
344 {
345   return 0;
346 }
347
348 void
349 Score_element::add_dependency (Score_element*e)
350 {
351   if (e)
352     {
353       Group_interface gi (this, "dependencies");
354       gi.add_element (e);
355     }
356   else
357     programming_error ("Null dependency added");
358 }
359
360
361
362
363 /**
364       Do break substitution in S, using CRITERION. Return new value.
365          CRITERION is either a SMOB pointer to the desired line, or a number
366          representing the break direction.  */
367 SCM
368 Score_element::handle_broken_smobs (SCM s, SCM criterion)
369 {
370   Score_element *sc = unsmob_element ( s);
371   if (sc)
372     {
373       if (criterion == SCM_UNDEFINED)
374         return SCM_UNDEFINED;
375       else if (gh_number_p (criterion))
376         {
377           Item * i = dynamic_cast<Item*> (sc);
378           Direction d = to_dir (criterion);
379           if (i && i->break_status_dir () != d)
380             {
381               Item *br = i->find_broken_piece (d);
382               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
383             }
384         }
385       else
386         {
387           Score_element * ln = unsmob_element ( criterion);
388           Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
389           Score_element * br =0;
390           Line_of_score * dep_line = sc->line_l ();
391           if (dep_line != line)
392             {
393               br = sc->find_broken_piece (line);
394               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
395             }
396           if (!dep_line)
397             return SCM_UNDEFINED;
398         }
399     }
400   else if (gh_pair_p (s))
401     {
402       /*
403         UGH! breaks on circular lists.
404       */
405       gh_set_car_x (s, handle_broken_smobs (gh_car (s), criterion));
406       gh_set_cdr_x (s, handle_broken_smobs (gh_cdr (s), criterion));
407
408       SCM c = gh_cdr(s);
409
410       // gh_list_p () is linear, this is O(1)  
411       bool list = gh_pair_p (c) || c == SCM_EOL;
412       
413       if (gh_car (s) == SCM_UNDEFINED && list)
414         return c;
415     }
416   return s;
417 }
418
419 void
420 Score_element::handle_broken_dependencies()
421 {
422   Line_of_score *line  = line_l();
423   element_property_alist_ = handle_broken_smobs (element_property_alist_,
424                                                  line ? line->self_scm_ : SCM_UNDEFINED);
425
426   if (!line)
427     return;
428 }
429
430
431 /*
432   TODO: cleanify.
433  */
434 void
435 Score_element::handle_prebroken_dependencies()
436 {
437   if (Item*i =dynamic_cast<Item*> (this))
438     {
439       element_property_alist_
440         = handle_broken_smobs (element_property_alist_,
441                                gh_int2scm (i->break_status_dir ()));
442     }
443 }
444
445
446
447
448
449 Link_array<Score_element>
450 Score_element::get_extra_dependencies() const
451 {
452   Link_array<Score_element> empty;
453   return empty;
454 }
455
456 bool
457 Score_element::linked_b() const
458 {
459   return used_b_;
460 }
461
462 void
463 Score_element::do_print () const
464 {
465 }
466
467 Score_element*
468 Score_element::find_broken_piece (Line_of_score*) const
469 {
470   return 0;
471 }
472
473 SCM
474 Score_element::mark_smob (SCM ses)
475 {
476   void * mp = (void*) gh_cdr(ses);
477   Score_element * s = (Score_element*) mp;
478
479   if (s->self_scm_ != ses)
480     {
481       programming_error ("SMOB marking gone awry");
482       return SCM_EOL;
483     }
484   return s->element_property_alist_;
485 }
486
487
488 int
489 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
490 {
491   Score_element *sc = (Score_element *) gh_cdr (s);
492      
493   scm_puts ("#<Score_element ", port);
494   scm_puts ((char *)sc->name (), port);
495
496   // scm_puts (" properties = ", port);
497   // scm_display (sc->element_property_alist_, port);
498   scm_puts (" >", port);
499   return 1;
500 }
501
502 void
503 Score_element::do_smobify_self ()
504 {
505   scm_unprotect_object (element_property_alist_); // ugh
506 }
507 #include "ly-smobs.icc"
508 IMPLEMENT_SMOBS(Score_element);
509
510 SCM
511 Score_element::equal_p (SCM a, SCM b)
512 {
513   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
514 }
515
516 void
517 Score_element::translate_axis (Real y, Axis a)
518 {
519   dim_cache_[a]->translate (y);
520 }  
521
522 Real
523 Score_element::relative_coordinate (Score_element const*e, Axis a) const
524 {
525   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
526 }
527
528 Score_element * 
529 Score_element::common_refpoint (Score_element const* s, Axis a) const
530 {
531   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
532   if (!dim)
533     programming_error ("No  common reference point");
534   return  dim ? dim->element_l () : 0;
535 }
536
537 void
538 Score_element::set_empty (Axis a)
539 {
540   dim_cache_[a]->callback_l_ =0;
541 }
542
543 bool
544 Score_element::empty_b (Axis a)const
545 {
546   return !dim_cache_[a]->callback_l_;
547 }
548
549 Interval
550 Score_element::extent (Axis a) const
551 {
552   Dimension_cache const * d = dim_cache_[a];
553
554   return d->get_dim ();
555 }
556
557 Score_element*
558 unsmob_element (SCM s)
559 {
560   if (SMOB_IS_TYPE_B (Score_element, s))
561     return SMOB_TO_TYPE(Score_element,s);
562   else
563     return 0;
564 }
565
566
567 Score_element*
568 Score_element::parent_l (Axis a) const
569 {
570   Dimension_cache*d= dim_cache_[a]->parent_l_;
571   return d ? d->elt_l_ : 0;
572 }
573
574 Score_element *
575 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
576 {
577   Dimension_cache * common = dim_cache_[a];
578   for (int i=0; i < gs.size (); i++)
579     {
580       common = common->common_refpoint (gs[i]->dim_cache_[a]);
581     }
582
583   return common->element_l ();
584 }
585
586 char const *
587 Score_element::name () const
588 {
589   return classname (this);
590 }
591
592
593 void
594 Score_element::set_parent (Score_element *g, Axis a)
595 {
596   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
597 }
598
599 void
600 Score_element::fixup_refpoint ()
601 {
602   for (int a = X_AXIS; a < NO_AXES; a ++)
603     {
604       Axis ax = (Axis)a;
605       Score_element * par = parent_l (ax);
606
607       if (!par)
608         continue;
609       
610       if (par->line_l () != line_l ())
611         {
612           Score_element * newpar = par->find_broken_piece (line_l ());
613           set_parent (newpar, ax);
614         }
615
616       if (Item * i  = dynamic_cast<Item*> (this))
617         {
618           Item *pari = dynamic_cast<Item*> (par);
619
620           if (pari && i)
621             {
622               Direction  my_dir = i->break_status_dir () ;
623               if (my_dir!= pari->break_status_dir())
624                 {
625                   Item *newpar =  pari->find_broken_piece (my_dir);
626                   set_parent (newpar, ax);
627                 }
628             }
629         }
630     }
631 }