]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.62
[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, SCM funcname)
203 {
204   assert (status_i_ >=0);
205
206   if (status_i_ >= final)
207     return;
208
209   if (status_i_== busy)
210     {
211       programming_error ("Element is busy, come back later");
212       return;
213     }
214   
215   status_i_= busy;
216
217   for (SCM d=  get_elt_pointer ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
218     {
219       unsmob_element (gh_car (d))
220         ->calculate_dependencies (final, busy, funcname);
221     }
222
223   // ughugh.
224   String s = ly_symbol2string (funcname);
225   SCM proc = get_elt_property (s.ch_C());
226   if (gh_procedure_p (proc))
227     gh_call1 (proc, this->self_scm_);
228   
229   status_i_= final;
230
231 }
232
233 Molecule
234 Score_element::get_molecule ()  const
235 {
236   SCM proc = get_elt_property ("molecule-callback");
237   if (gh_procedure_p (proc)) 
238     return create_molecule (gh_apply (proc, gh_list (this->self_scm_, SCM_UNDEFINED)));
239
240   return Molecule ();
241 }
242
243
244 /*
245   
246   VIRTUAL STUBS
247
248  */
249 void
250 Score_element::do_break_processing()
251 {
252 }
253
254 void
255 Score_element::do_space_processing ()
256 {
257 }
258
259 void
260 Score_element::do_add_processing()
261 {
262 }
263
264
265 MAKE_SCHEME_SCORE_ELEMENT_CALLBACK(Score_element,brew_molecule)
266
267 /*
268   ugh.
269  */  
270 SCM
271 Score_element::brew_molecule (SCM smob) 
272 {
273   Score_element * sc = unsmob_element (smob);
274   SCM glyph = sc->get_elt_property ("glyph");
275   if (gh_string_p (glyph))
276     {
277       return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
278     }
279   else
280     {
281       return SCM_EOL;
282     }
283 }
284
285
286 Line_of_score *
287 Score_element::line_l() const
288 {
289   return 0;
290 }
291
292 void
293 Score_element::add_dependency (Score_element*e)
294 {
295   if (e)
296     {
297       Pointer_group_interface gi (this, "dependencies");
298       gi.add_element (e);
299     }
300   else
301     programming_error ("Null dependency added");
302 }
303
304
305
306
307 /**
308       Do break substitution in S, using CRITERION. Return new value.
309       CRITERION is either a SMOB pointer to the desired line, or a number
310       representing the break direction. Do not modify SRC.
311 */
312 SCM
313 Score_element::handle_broken_smobs (SCM src, SCM criterion)
314 {
315  again:
316   Score_element *sc = unsmob_element (src);
317   if (sc)
318     {
319       if (gh_number_p (criterion))
320         {
321           Item * i = dynamic_cast<Item*> (sc);
322           Direction d = to_dir (criterion);
323           if (i && i->break_status_dir () != d)
324             {
325               Item *br = i->find_prebroken_piece (d);
326               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
327             }
328         }
329       else
330         {
331           Line_of_score * line
332             = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
333           if (sc->line_l () != line)
334             {
335               sc = sc->find_broken_piece (line);
336
337             }
338
339           /* now: !sc || (sc && sc->line_l () == line) */
340           if (!sc)
341             return SCM_UNDEFINED;
342
343           /* now: sc && sc->line_l () == line */
344           if (!line
345               || (sc->common_refpoint (line, X_AXIS)
346                   && sc->common_refpoint (line, Y_AXIS)))
347             {
348               return sc->self_scm_;
349             }
350           return SCM_UNDEFINED;
351         }
352     }
353   else if (gh_pair_p (src))
354     {
355       SCM oldcar =gh_car (src);
356       /*
357         UGH! breaks on circular lists.
358       */
359       SCM newcar = handle_broken_smobs (oldcar, criterion);
360       SCM oldcdr = gh_cdr (src);
361       
362       if (newcar == SCM_UNDEFINED
363           && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
364         {
365           /*
366             This is tail-recursion, ie. 
367             
368             return handle_broken_smobs (cdr, criterion);
369
370             We don't want to rely on the compiler to do this.  Without
371             tail-recursion, this easily crashes with a stack overflow.  */
372           src =  oldcdr;
373           goto again;
374         }
375
376       SCM newcdr = handle_broken_smobs (oldcdr, criterion);
377       return gh_cons (newcar, newcdr);
378     }
379   else
380     return src;
381
382   return src;
383 }
384
385 void
386 Score_element::handle_broken_dependencies()
387 {
388   Spanner * s= dynamic_cast<Spanner*> (this);
389   if (original_l_ && s)
390     return;
391
392   if (s)
393     {
394       for (int i = 0;  i< s->broken_into_l_arr_ .size (); i++)
395         {
396           Score_element * sc = s->broken_into_l_arr_[i];
397           Line_of_score * l = sc->line_l ();
398           sc->pointer_alist_ =
399             handle_broken_smobs (pointer_alist_,
400                                  l ? l->self_scm_ : SCM_UNDEFINED);
401         }
402     }
403
404
405   Line_of_score *line = line_l();
406
407   if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
408     {
409       pointer_alist_
410         = handle_broken_smobs (pointer_alist_,
411                                line ? line->self_scm_ : SCM_UNDEFINED);
412     }
413   else if (dynamic_cast <Line_of_score*> (this))
414     {
415       pointer_alist_ = handle_broken_smobs (pointer_alist_,
416                                             SCM_UNDEFINED);
417     }
418   else
419     {
420       /*
421         This element is `invalid'; it has been removed from all
422         dependencies, so let's junk the element itself.
423
424         do not do this for Line_of_score, since that would remove
425         references to the originals of score-elts, which get then GC'd
426         (a bad thing.)
427       */
428       suicide();
429     }
430 }
431
432 /*
433  Note that we still want references to this element to be
434  rearranged, and not silently thrown away, so we keep pointers
435  like {broken_into_{drul,array}, original}
436 */
437 void
438 Score_element::suicide ()
439 {
440   property_alist_ = SCM_EOL;
441   pointer_alist_ = SCM_EOL;
442   set_extent_callback (0, Y_AXIS);
443   set_extent_callback (0, X_AXIS);
444
445   for (int a= X_AXIS; a <= Y_AXIS; a++)
446     {
447       dim_cache_[a].off_callbacks_.clear ();
448     }
449 }
450
451 void
452 Score_element::handle_prebroken_dependencies()
453 {
454 }
455
456 Score_element*
457 Score_element::find_broken_piece (Line_of_score*) const
458 {
459   return 0;
460 }
461
462 void
463 Score_element::translate_axis (Real y, Axis a)
464 {
465   if (isinf (y) || isnan (y))
466     programming_error (_(INFINITY_MSG));
467   else
468     {
469       dim_cache_[a].offset_ += y;
470     }
471 }  
472
473 Real
474 Score_element::relative_coordinate (Score_element const*refp, Axis a) const
475 {
476   if (refp == this)
477     return 0.0;
478
479   /*
480     We catch PARENT_L_ == nil case with this, but we crash if we did
481     not ask for the absolute coordinate (ie. REFP == nil.)
482     
483    */
484   if (refp == dim_cache_[a].parent_l_)
485     return get_offset (a);
486   else
487     return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
488 }
489
490 Real
491 Score_element::get_offset (Axis a) const
492 {
493   Score_element *me = (Score_element*) this;
494   while (dim_cache_[a].off_callbacks_.size ())
495     {
496       Offset_callback c = dim_cache_[a].off_callbacks_[0];
497       me->dim_cache_[a].off_callbacks_.del (0);
498       Real r =  (*c) (me,a );
499       if (isinf (r) || isnan (r))
500         {
501           r = 0.0;
502           programming_error (INFINITY_MSG);
503         }
504       me->dim_cache_[a].offset_ +=r;
505     }
506   return dim_cache_[a].offset_;
507 }
508
509
510 Interval
511 Score_element::point_dimension_callback (Score_element const* , Axis)
512 {
513   return Interval (0,0);
514 }
515
516 bool
517 Score_element::empty_b (Axis a)const
518 {
519   return !dim_cache_[a].extent_callback_l_;
520 }
521
522 Interval
523 Score_element::extent (Axis a) const
524 {
525   Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
526   if (!d->extent_callback_l_)
527     {
528       d->dim_.set_empty ();
529     }
530   else if (!d->valid_b_)
531     {
532       d->dim_= (*d->extent_callback_l_ ) (this, a);
533       d->valid_b_ = true;
534     }
535
536   Interval ext = d->dim_;
537   
538   if (empty_b (a)) 
539     return ext;
540
541   SCM extra = get_elt_property (a == X_AXIS
542                                 ? "extra-extent-X"
543                                 : "extra-extent-Y");
544
545   /*
546     signs ?
547    */
548   Real s = paper_l ()->get_var ("staffspace");
549   if (gh_pair_p (extra))
550     {
551       ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
552       ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
553     }
554   
555   extra = get_elt_property (a == X_AXIS
556                                 ? "minimum-extent-X"
557                                 : "minimum-extent-Y");
558   if (gh_pair_p (extra))
559     {
560       ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
561                            s * gh_scm2double (gh_cdr (extra))));
562     }
563   
564   return ext;
565 }
566
567
568 Score_element*
569 Score_element::parent_l (Axis a) const
570 {
571   return  dim_cache_[a].parent_l_;
572 }
573
574 Score_element * 
575 Score_element::common_refpoint (Score_element const* s, Axis a) const
576 {
577   /*
578     I don't like the quadratic aspect of this code, but I see no other
579     way. The largest chain of parents might be 10 high or so, so
580     it shouldn't be a real issue. */
581   for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
582     for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
583       if (d == c)
584         return (Score_element*)d;
585
586   return 0;
587 }
588
589
590 Score_element *
591 Score_element::common_refpoint (SCM elist, Axis a) const
592 {
593   Score_element * common = (Score_element*) this;
594   for (; gh_pair_p (elist); elist = gh_cdr (elist))
595     {
596       Score_element * s = unsmob_element (gh_car (elist));
597       if (s)
598         common = common->common_refpoint (s, a);
599     }
600
601   return common;
602 }
603
604 char const *
605 Score_element::name () const
606 {
607   return classname (this);
608 }
609
610 void
611 Score_element::add_offset_callback (Offset_callback cb, Axis a)
612 {
613   dim_cache_[a].off_callbacks_.push (cb);
614 }
615
616 bool
617 Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
618 {
619   return cb == dim_cache_[a].extent_callback_l_;
620 }
621
622 bool
623 Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
624 {
625   for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
626     {
627       if (dim_cache_[a].off_callbacks_[i] == cb)
628         return true;
629     }
630   return false;
631 }
632
633 void
634 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
635 {
636   dim_cache_[a].extent_callback_l_ = dc ;
637 }
638
639                                     
640 void
641 Score_element::set_parent (Score_element *g, Axis a)
642 {
643   dim_cache_[a].parent_l_ = g;
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 * parent = parent_l (ax);
653
654       if (!parent)
655         continue;
656       
657       if (parent->line_l () != line_l () && line_l ())
658         {
659           Score_element * newparent = parent->find_broken_piece (line_l ());
660           set_parent (newparent, ax);
661         }
662
663       if (Item * i  = dynamic_cast<Item*> (this))
664         {
665           Item *parenti = dynamic_cast<Item*> (parent);
666
667           if (parenti && i)
668             {
669               Direction  my_dir = i->break_status_dir () ;
670               if (my_dir!= parenti->break_status_dir())
671                 {
672                   Item *newparent =  parenti->find_prebroken_piece (my_dir);
673                   set_parent (newparent, ax);
674                 }
675             }
676         }
677     }
678 }
679
680
681
682 /****************************************************
683   SMOB funcs
684  ****************************************************/
685
686 #include "ly-smobs.icc"
687
688 IMPLEMENT_UNSMOB(Score_element, element);
689 IMPLEMENT_SMOBS(Score_element);
690
691 SCM
692 Score_element::mark_smob (SCM ses)
693 {
694   Score_element * s = SMOB_TO_TYPE (Score_element, ses);
695   if (s->self_scm_ != ses)
696     {
697       programming_error ("SMOB marking gone awry");
698       return SCM_EOL;
699     }
700   scm_gc_mark (s->pointer_alist_);
701
702   s->do_derived_mark ();
703   if (s->parent_l (Y_AXIS))
704     scm_gc_mark (s->parent_l (Y_AXIS)->self_scm_);
705   if (s->parent_l (X_AXIS))
706     scm_gc_mark (s->parent_l (X_AXIS)->self_scm_);
707
708   return s->property_alist_;
709 }
710
711 int
712 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
713 {
714   Score_element *sc = (Score_element *) gh_cdr (s);
715      
716   scm_puts ("#<Score_element ", port);
717   scm_puts ((char *)sc->name (), port);
718
719   /*
720     don't try to print properties, that is too much hassle.
721    */
722   scm_puts (" >", port);
723   return 1;
724 }
725
726 void
727 Score_element::do_derived_mark ()
728 {
729 }
730
731 void
732 Score_element::do_smobify_self ()
733 {
734 }
735
736 SCM
737 Score_element::equal_p (SCM a, SCM b)
738 {
739   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
740 }
741
742
743 SCM
744 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
745 {
746   Score_element * sc = unsmob_element (elt);
747
748   if (!gh_symbol_p (sym))
749     {
750       error ("Not a symbol");
751       ly_display_scm (sym);
752       return SCM_UNDEFINED;
753     }
754
755   if (sc)
756     {
757       sc->property_alist_ = scm_assq_set_x (sc->property_alist_, sym, val);
758     }
759   else
760     {
761       error ("Not a score element");
762       ly_display_scm (elt);
763     }
764
765   return SCM_UNDEFINED;
766 }
767
768
769 SCM
770 Score_element::ly_get_elt_property (SCM elt, SCM sym)
771 {
772   Score_element * sc = unsmob_element (elt);
773   
774   if (sc)
775     {
776       SCM s = scm_assq(sym, sc->property_alist_);
777
778       if (s != SCM_BOOL_F)
779         return gh_cdr (s); 
780       else
781         return SCM_UNDEFINED;
782     }
783   else
784     {
785       error ("Not a score element");
786       ly_display_scm (elt);
787     }
788   return SCM_UNDEFINED;
789 }
790
791
792 static void
793 init_functions ()
794 {
795   scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
796   scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
797 }
798
799 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
800
801 void
802 Score_element::discretionary_processing()
803 {
804 }