]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
a0a13eb5afa1532809d992cea194c95c4d35125b
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <string.h>
11 #include <math.h>
12
13 #include "libc-extension.hh"
14 #include "group-interface.hh"
15 #include "misc.hh"
16 #include "paper-score.hh"
17 #include "paper-def.hh"
18 #include "lookup.hh"
19 #include "molecule.hh"
20 #include "score-element.hh"
21 #include "debug.hh"
22 #include "spanner.hh"
23 #include "line-of-score.hh"
24 #include "item.hh"
25 #include "paper-column.hh"
26 #include "molecule.hh"
27 #include "misc.hh"
28 #include "paper-outputter.hh"
29 #include "dimension-cache.hh"
30 #include "side-position-interface.hh"
31 #include "item.hh"
32
33 /*
34 TODO:
35
36 remove dynamic_cast<Spanner,Item> and put this code into respective
37   subclass.
38 */
39
40
41 #define INFINITY_MSG "Infinity or NaN encountered"
42
43 Score_element::Score_element(SCM basicprops)
44 {
45   set_extent_callback (molecule_extent, X_AXIS);
46   set_extent_callback (molecule_extent, Y_AXIS);    
47
48   pscore_l_=0;
49   lookup_l_ =0;
50   status_i_ = 0;
51   self_scm_ = SCM_EOL;
52   original_l_ = 0;
53   property_alist_ = basicprops;
54   pointer_alist_ = SCM_EOL;
55   
56   smobify_self ();
57   set_elt_pointer ("dependencies", SCM_EOL);
58   set_elt_property ("interfaces", SCM_EOL);
59 }
60
61
62 Score_element::Score_element (Score_element const&s)
63    : dim_cache_ (s.dim_cache_)
64 {
65   self_scm_ = SCM_EOL;
66   original_l_ =(Score_element*) &s;
67   property_alist_ = s.property_alist_;
68
69   pointer_alist_ = SCM_EOL;
70   
71   status_i_ = s.status_i_;
72   lookup_l_ = s.lookup_l_;
73   pscore_l_ = s.pscore_l_;
74
75   smobify_self ();
76 }
77
78 Score_element::~Score_element()
79 {
80   // this goes awry when score-elements are copied...
81
82   /*
83      Kijk goed naar hoe smobify en unsmobify werken.  unsmobify_self
84     is te gebruiken wanneer C++ geheugen beheer weer overneemt van
85     GUILE. --hwn */
86 }
87
88
89 SCM
90 Score_element::get_elt_pointer (const char *nm) const
91 {
92   SCM sym =  ly_symbol2scm (nm);
93   SCM s = scm_assq(sym, pointer_alist_);
94
95   return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s); 
96 }
97
98 // should also have one that takes SCM arg. 
99 SCM
100 Score_element::get_elt_property (String nm) const
101 {
102   SCM sym =  ly_symbol2scm (nm.ch_C());
103   SCM s = scm_assq(sym, property_alist_);
104
105   if (s != SCM_BOOL_F)
106     return gh_cdr (s); 
107
108   return SCM_UNDEFINED;
109 }
110
111 /*
112   Remove the value associated with KEY, and return it. The result is
113   that a next call will yield SCM_UNDEFINED (and not the underlying
114   `basic' property.
115 */
116 SCM
117 Score_element::remove_elt_property (const char* key)
118 {
119   SCM val = get_elt_property (key);
120   if (val != SCM_UNDEFINED)
121     set_elt_property (key, SCM_UNDEFINED);
122   return val;
123 }
124
125 void
126 Score_element::set_elt_property (String k, SCM val)
127 {
128   SCM sym = ly_symbol2scm (k.ch_C ());
129   property_alist_ = gh_cons (gh_cons (sym, val), property_alist_);
130 }
131
132 void
133 Score_element::set_elt_pointer (const char* k, SCM v)
134 {
135   SCM s = ly_symbol2scm (k);
136   pointer_alist_ = scm_assq_set_x (pointer_alist_, s, v);
137 }
138
139
140 Interval
141 Score_element::molecule_extent (Score_element const *s, Axis a )
142 {
143   Molecule m = s->get_molecule ();
144   return m.extent(a);
145 }
146
147 Interval
148 Score_element::preset_extent (Score_element const *s , Axis a )
149 {
150   SCM ext = s->get_elt_property ((a == X_AXIS)
151                                  ? "extent-X"
152                                  : "extent-Y");
153   
154   if (gh_pair_p (ext))
155     {
156       Real l = gh_scm2double (gh_car (ext));
157       Real r = gh_scm2double (gh_cdr (ext));
158       l *= s->paper_l ()->get_var ("staffspace");
159       r *= s->paper_l ()->get_var ("staffspace");
160       return Interval (l, r);
161     }
162   
163   return Interval ();
164 }
165
166
167
168 Paper_def*
169 Score_element::paper_l ()  const
170 {
171  return pscore_l_ ? pscore_l_->paper_l_ : 0;
172 }
173
174 Lookup const *
175 Score_element::lookup_l () const
176 {
177   if (!lookup_l_)
178     {
179       Score_element * urg = (Score_element*)this;
180       SCM sz = urg->remove_elt_property ("fontsize");
181       int i = (gh_number_p (sz))
182         ? gh_scm2int  (sz)
183         : 0;
184
185       urg->lookup_l_ =  (Lookup*)pscore_l_->paper_l_->lookup_l (i);
186     }
187   return lookup_l_;
188 }
189
190 void
191 Score_element::add_processing()
192 {
193   assert (status_i_ >=0);
194   if (status_i_)
195     return;
196   status_i_ ++;
197
198   do_add_processing();
199 }
200
201 void
202 Score_element::calculate_dependencies (int final, int busy,
203                                        Score_element_method_pointer funcptr)
204 {
205   assert (status_i_ >=0);
206
207   if (status_i_ >= final)
208     return;
209
210   if (status_i_== busy)
211     {
212       programming_error ("Element is busy, come back later");
213       return;
214     }
215   
216   status_i_= busy;
217
218   for (SCM d=  get_elt_pointer ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
219     {
220       unsmob_element (gh_car (d))
221         ->calculate_dependencies (final, busy, funcptr);
222     }
223
224   (this->*funcptr)();
225   status_i_= final;
226 }
227
228 Molecule
229 Score_element::get_molecule ()  const
230 {
231   SCM proc = get_elt_property ("molecule-callback");
232   if (gh_procedure_p (proc)) 
233     return create_molecule (gh_apply (proc, gh_list (this->self_scm_, SCM_UNDEFINED)));
234
235   return Molecule ();
236 }
237
238
239 /*
240   
241   VIRTUAL STUBS
242
243  */
244 void
245 Score_element::do_break_processing()
246 {
247 }
248
249 void
250 Score_element::after_line_breaking ()
251 {
252 }
253
254
255 void
256 Score_element::before_line_breaking ()
257 {
258 }
259
260 void
261 Score_element::do_space_processing ()
262 {
263 }
264
265 void
266 Score_element::do_add_processing()
267 {
268 }
269
270
271 MAKE_SCHEME_SCORE_ELEMENT_NON_DEFAULT_CALLBACKS(Score_element)
272
273 /*
274   ugh.
275  */  
276 SCM
277 Score_element::scheme_molecule (SCM smob) 
278 {
279   Score_element * sc = unsmob_element (smob);
280   SCM glyph = sc->get_elt_property ("glyph");
281   if (gh_string_p (glyph))
282     {
283       return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
284     }
285   else
286     {
287       return SCM_EOL;
288     }
289 }
290
291
292 Line_of_score *
293 Score_element::line_l() const
294 {
295   return 0;
296 }
297
298 void
299 Score_element::add_dependency (Score_element*e)
300 {
301   if (e)
302     {
303       Pointer_group_interface gi (this, "dependencies");
304       gi.add_element (e);
305     }
306   else
307     programming_error ("Null dependency added");
308 }
309
310
311
312
313 /**
314       Do break substitution in S, using CRITERION. Return new value.
315       CRITERION is either a SMOB pointer to the desired line, or a number
316       representing the break direction. Do not modify SRC.
317 */
318 SCM
319 Score_element::handle_broken_smobs (SCM src, SCM criterion)
320 {
321  again:
322   Score_element *sc = unsmob_element (src);
323   if (sc)
324     {
325       if (gh_number_p (criterion))
326         {
327           Item * i = dynamic_cast<Item*> (sc);
328           Direction d = to_dir (criterion);
329           if (i && i->break_status_dir () != d)
330             {
331               Item *br = i->find_prebroken_piece (d);
332               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
333             }
334         }
335       else
336         {
337           Line_of_score * line
338             = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
339           if (sc->line_l () != line)
340             {
341               sc = sc->find_broken_piece (line);
342
343             }
344
345           /* now: !sc || (sc && sc->line_l () == line) */
346           if (!sc)
347             return SCM_UNDEFINED;
348
349           /* now: sc && sc->line_l () == line */
350           if (!line
351               || (sc->common_refpoint (line, X_AXIS)
352                   && sc->common_refpoint (line, Y_AXIS)))
353             {
354               return sc->self_scm_;
355             }
356           return SCM_UNDEFINED;
357         }
358     }
359   else if (gh_pair_p (src))
360     {
361       SCM oldcar =gh_car (src);
362       /*
363         UGH! breaks on circular lists.
364       */
365       SCM newcar = handle_broken_smobs (oldcar, criterion);
366       SCM oldcdr = gh_cdr (src);
367       
368       if (newcar == SCM_UNDEFINED
369           && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
370         {
371           /*
372             This is tail-recursion, ie. 
373             
374             return handle_broken_smobs (cdr, criterion);
375
376             We don't want to rely on the compiler to do this.  Without
377             tail-recursion, this easily crashes with a stack overflow.  */
378           src =  oldcdr;
379           goto again;
380         }
381
382       SCM newcdr = handle_broken_smobs (oldcdr, criterion);
383       return gh_cons (newcar, newcdr);
384     }
385   else
386     return src;
387
388   return src;
389 }
390
391 void
392 Score_element::handle_broken_dependencies()
393 {
394   Spanner * s= dynamic_cast<Spanner*> (this);
395   if (original_l_ && s)
396     return;
397
398   if (s)
399     {
400       for (int i = 0;  i< s->broken_into_l_arr_ .size (); i++)
401         {
402           Score_element * sc = s->broken_into_l_arr_[i];
403           Line_of_score * l = sc->line_l ();
404           sc->pointer_alist_ =
405             handle_broken_smobs (pointer_alist_,
406                                  l ? l->self_scm_ : SCM_UNDEFINED);
407         }
408     }
409
410
411   Line_of_score *line = line_l();
412
413   if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
414     {
415       pointer_alist_
416         = handle_broken_smobs (pointer_alist_,
417                                line ? line->self_scm_ : SCM_UNDEFINED);
418     }
419   else if (dynamic_cast <Line_of_score*> (this))
420     {
421       pointer_alist_ = handle_broken_smobs (pointer_alist_,
422                                             SCM_UNDEFINED);
423     }
424   else
425     {
426       /*
427         This element is `invalid'; it has been removed from all
428         dependencies, so let's junk the element itself.
429
430         do not do this for Line_of_score, since that would remove
431         references to the originals of score-elts, which get then GC'd
432         (a bad thing.)
433       */
434       suicide();
435     }
436 }
437
438 /*
439  Note that we still want references to this element to be
440  rearranged, and not silently thrown away, so we keep pointers
441  like {broken_into_{drul,array}, original}
442 */
443 void
444 Score_element::suicide ()
445 {
446   property_alist_ = SCM_EOL;
447   pointer_alist_ = SCM_EOL;
448   set_extent_callback (0, Y_AXIS);
449   set_extent_callback (0, X_AXIS);
450
451   for (int a= X_AXIS; a <= Y_AXIS; a++)
452     {
453       dim_cache_[a].off_callbacks_.clear ();
454     }
455 }
456
457 void
458 Score_element::handle_prebroken_dependencies()
459 {
460 }
461
462 Score_element*
463 Score_element::find_broken_piece (Line_of_score*) const
464 {
465   return 0;
466 }
467
468 void
469 Score_element::translate_axis (Real y, Axis a)
470 {
471   if (isinf (y) || isnan (y))
472     programming_error (_(INFINITY_MSG));
473   else
474     {
475       dim_cache_[a].offset_ += y;
476     }
477 }  
478
479 Real
480 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
481 {
482   if (refp == this)
483     return 0.0;
484
485   /*
486     We catch PARENT_L_ == nil case with this, but we crash if we did
487     not ask for the absolute coordinate (ie. REFP == nil.)
488     
489    */
490   if (refp == dim_cache_[a].parent_l_)
491     return get_offset (a);
492   else
493     return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
494 }
495
496 Real
497 Score_element::get_offset (Axis a) const
498 {
499   Score_element *me = (Score_element*) this;
500   while (dim_cache_[a].off_callbacks_.size ())
501     {
502       Offset_callback c = dim_cache_[a].off_callbacks_[0];
503       me->dim_cache_[a].off_callbacks_.del (0);
504       Real r =  (*c) (me,a );
505       if (isinf (r) || isnan (r))
506         {
507           r = 0.0;
508           programming_error (INFINITY_MSG);
509         }
510       me->dim_cache_[a].offset_ +=r;
511     }
512   return dim_cache_[a].offset_;
513 }
514
515
516 Interval
517 Score_element::point_dimension_callback (Score_element const* , Axis)
518 {
519   return Interval (0,0);
520 }
521
522 bool
523 Score_element::empty_b (Axis a)const
524 {
525   return !dim_cache_[a].extent_callback_l_;
526 }
527
528 Interval
529 Score_element::extent (Axis a) const
530 {
531   Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
532   if (!d->extent_callback_l_)
533     {
534       d->dim_.set_empty ();
535     }
536   else if (!d->valid_b_)
537     {
538       d->dim_= (*d->extent_callback_l_ ) (this, a);
539       d->valid_b_ = true;
540     }
541
542   Interval ext = d->dim_;
543   
544   if (empty_b (a)) 
545     return ext;
546
547   SCM extra = get_elt_property (a == X_AXIS
548                                 ? "extra-extent-X"
549                                 : "extra-extent-Y");
550
551   /*
552     signs ?
553    */
554   Real s = paper_l ()->get_var ("staffspace");
555   if (gh_pair_p (extra))
556     {
557       ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
558       ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
559     }
560   
561   extra = get_elt_property (a == X_AXIS
562                                 ? "minimum-extent-X"
563                                 : "minimum-extent-Y");
564   if (gh_pair_p (extra))
565     {
566       ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
567                            s * gh_scm2double (gh_cdr (extra))));
568     }
569   
570   return ext;
571 }
572
573
574 Score_element*
575 Score_element::parent_l (Axis a) const
576 {
577   return  dim_cache_[a].parent_l_;
578 }
579
580 Score_element * 
581 Score_element::common_refpoint (Score_element const* s, Axis a) const
582 {
583   /*
584     I don't like the quadratic aspect of this code, but I see no other
585     way. The largest chain of parents might be 10 high or so, so
586     it shouldn't be a real issue. */
587   for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
588     for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
589       if (d == c)
590         return (Score_element*)d;
591
592   return 0;
593 }
594
595
596 Score_element *
597 Score_element::common_refpoint (SCM elist, Axis a) const
598 {
599   Score_element * common = (Score_element*) this;
600   for (; gh_pair_p (elist); elist = gh_cdr (elist))
601     {
602       Score_element * s = unsmob_element (gh_car (elist));
603       if (s)
604         common = common->common_refpoint (s, a);
605     }
606
607   return common;
608 }
609
610 char const *
611 Score_element::name () const
612 {
613   return classname (this);
614 }
615
616 void
617 Score_element::add_offset_callback (Offset_callback cb, Axis a)
618 {
619   dim_cache_[a].off_callbacks_.push (cb);
620 }
621
622 bool
623 Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
624 {
625   return cb == dim_cache_[a].extent_callback_l_;
626 }
627
628 bool
629 Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
630 {
631   for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
632     {
633       if (dim_cache_[a].off_callbacks_[i] == cb)
634         return true;
635     }
636   return false;
637 }
638
639 void
640 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
641 {
642   dim_cache_[a].extent_callback_l_ = dc ;
643 }
644
645                                     
646 void
647 Score_element::set_parent (Score_element *g, Axis a)
648 {
649   dim_cache_[a].parent_l_ = g;
650 }
651
652 void
653 Score_element::fixup_refpoint ()
654 {
655   for (int a = X_AXIS; a < NO_AXES; a ++)
656     {
657       Axis ax = (Axis)a;
658       Score_element * parent = parent_l (ax);
659
660       if (!parent)
661         continue;
662       
663       if (parent->line_l () != line_l () && line_l ())
664         {
665           Score_element * newparent = parent->find_broken_piece (line_l ());
666           set_parent (newparent, ax);
667         }
668
669       if (Item * i  = dynamic_cast<Item*> (this))
670         {
671           Item *parenti = dynamic_cast<Item*> (parent);
672
673           if (parenti && i)
674             {
675               Direction  my_dir = i->break_status_dir () ;
676               if (my_dir!= parenti->break_status_dir())
677                 {
678                   Item *newparent =  parenti->find_prebroken_piece (my_dir);
679                   set_parent (newparent, ax);
680                 }
681             }
682         }
683     }
684 }
685
686
687
688 /****************************************************
689   SMOB funcs
690  ****************************************************/
691
692 #include "ly-smobs.icc"
693
694 IMPLEMENT_UNSMOB(Score_element, element);
695 IMPLEMENT_SMOBS(Score_element);
696
697 SCM
698 Score_element::mark_smob (SCM ses)
699 {
700   Score_element * s = SMOB_TO_TYPE (Score_element, ses);
701   if (s->self_scm_ != ses)
702     {
703       programming_error ("SMOB marking gone awry");
704       return SCM_EOL;
705     }
706   scm_gc_mark (s->pointer_alist_);
707
708   s->do_derived_mark ();
709   if (s->parent_l (Y_AXIS))
710     scm_gc_mark (s->parent_l (Y_AXIS)->self_scm_);
711   if (s->parent_l (X_AXIS))
712     scm_gc_mark (s->parent_l (X_AXIS)->self_scm_);
713
714   return s->property_alist_;
715 }
716
717 int
718 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
719 {
720   Score_element *sc = (Score_element *) gh_cdr (s);
721      
722   scm_puts ("#<Score_element ", port);
723   scm_puts ((char *)sc->name (), port);
724
725   /*
726     don't try to print properties, that is too much hassle.
727    */
728   scm_puts (" >", port);
729   return 1;
730 }
731
732 void
733 Score_element::do_derived_mark ()
734 {
735 }
736
737 void
738 Score_element::do_smobify_self ()
739 {
740 }
741
742 SCM
743 Score_element::equal_p (SCM a, SCM b)
744 {
745   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
746 }
747
748
749 SCM
750 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
751 {
752   Score_element * sc = unsmob_element (elt);
753
754   if (!gh_symbol_p (sym))
755     {
756       error ("Not a symbol");
757       ly_display_scm (sym);
758       return SCM_UNDEFINED;
759     }
760
761   if (sc)
762     {
763       sc->property_alist_ = scm_assq_set_x (sc->property_alist_, sym, val);
764     }
765   else
766     {
767       error ("Not a score element");
768       ly_display_scm (elt);
769     }
770
771   return SCM_UNDEFINED;
772 }
773
774
775 SCM
776 Score_element::ly_get_elt_property (SCM elt, SCM sym)
777 {
778   Score_element * sc = unsmob_element (elt);
779   
780   if (sc)
781     {
782       SCM s = scm_assq(sym, sc->property_alist_);
783
784       if (s != SCM_BOOL_F)
785         return gh_cdr (s); 
786       else
787         return SCM_UNDEFINED;
788     }
789   else
790     {
791       error ("Not a score element");
792       ly_display_scm (elt);
793     }
794   return SCM_UNDEFINED;
795 }
796
797
798 static void
799 init_functions ()
800 {
801   scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
802   scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
803 }
804
805 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
806
807 void
808 Score_element::discretionary_processing()
809 {
810 }