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