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