]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.51
[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
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 /*
32 TODO:
33
34 remove dynamic_cast<Spanner,Item> and put this code into respective
35   subclass.
36 */
37
38 Score_element::Score_element()
39 {
40   dim_cache_[X_AXIS] = new Dimension_cache;
41   dim_cache_[Y_AXIS] = new Dimension_cache;
42   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
43
44   // junkme.
45   used_b_ = false;
46
47   dim_cache_[X_AXIS]->set_extent_callback (molecule_extent);
48   dim_cache_[Y_AXIS]->set_extent_callback (molecule_extent); 
49   used_b_ = false;
50   pscore_l_=0;
51   lookup_l_ =0;
52   status_i_ = 0;
53   self_scm_ = SCM_EOL;
54   original_l_ = 0;
55   element_property_alist_ = SCM_EOL;
56
57   smobify_self ();
58
59
60   set_elt_property ("dependencies", SCM_EOL);
61   set_elt_property ("interfaces", SCM_EOL);
62 }
63
64
65 Score_element::Score_element (Score_element const&s)
66 {
67   dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
68   dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
69   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
70   
71   self_scm_ = SCM_EOL;
72   used_b_ = true;
73   original_l_ =(Score_element*) &s;
74   element_property_alist_ = SCM_EOL; // onstack;
75
76   status_i_ = s.status_i_;
77   lookup_l_ = s.lookup_l_;
78   pscore_l_ = s.pscore_l_;
79
80   smobify_self ();
81 }
82
83 Score_element::~Score_element()
84 {
85   assert (status_i_ >=0);
86   status_i_  = -1;
87
88   delete dim_cache_[X_AXIS];
89   delete dim_cache_[Y_AXIS];  
90 }
91
92 // should also have one that takes SCM arg. 
93 SCM
94 Score_element::get_elt_property (String nm) const
95 {
96   SCM sym =  ly_symbol2scm (nm.ch_C());
97   SCM s = scm_assq(sym, element_property_alist_);
98
99   if (s != SCM_BOOL_F)
100     return gh_cdr (s); 
101   
102   if (pscore_l_)
103     {
104       SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
105       SCM val;
106       
107       // should probably check for Type::sym as well.
108       Paper_def * p= pscore_l_->paper_l_;
109       if (p->default_properties_.try_retrieve (sym2, &val))
110         return val;
111       else if (p->default_properties_.try_retrieve (sym, &val))
112         return val;
113     }
114   
115   return SCM_UNDEFINED;
116 }
117
118 SCM
119 Score_element::remove_elt_property (String key)
120 {
121   SCM s = get_elt_property (key); 
122   SCM sym = ly_symbol2scm (key.ch_C());
123   element_property_alist_ =  scm_assq_remove_x (element_property_alist_, sym);
124   return s;
125 }
126
127 /*
128   UGH. assoc vs. assq
129  */
130 void
131 Score_element::set_elt_property (String k, SCM v)
132 {
133   SCM s = ly_symbol2scm (k.ch_C( ));
134   element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
135 }
136
137 Interval
138 Score_element::molecule_extent (Dimension_cache const *c)
139 {
140   Score_element *s = dynamic_cast<Score_element*>(c->element_l());
141   Molecule m = s->do_brew_molecule();
142   return m.extent()[c->axis ()];
143 }
144
145 Interval
146 Score_element::preset_extent (Dimension_cache const *c)
147 {
148   Score_element *s = dynamic_cast<Score_element*>(c->element_l());
149   SCM ext = s->get_elt_property ((c->axis () == X_AXIS)
150                                  ? "extent-X"
151                                  : "extent-Y");
152   
153   if (gh_pair_p (ext))
154     {
155       Real l = gh_scm2double (gh_car (ext));
156       Real r = gh_scm2double (gh_cdr (ext));
157       l *= s->paper_l ()->get_var ("staffspace");
158       r *= s->paper_l ()->get_var ("staffspace");
159       return Interval (l, r);
160     }
161   
162   return Interval ();
163 }
164
165
166 void
167 Score_element::print() const
168 {
169 #ifndef NPRINT
170   DEBUG_OUT << classname(this) << "{\n";
171     
172   if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
173     ly_display_scm (element_property_alist_);
174
175   if (original_l_)
176     DEBUG_OUT << "Copy ";
177   do_print();
178   
179   DEBUG_OUT <<  "}\n";
180 #endif
181 }
182
183 Paper_def*
184 Score_element::paper_l ()  const
185 {
186  return pscore_l_ ? pscore_l_->paper_l_ : 0;
187 }
188
189 Lookup const *
190 Score_element::lookup_l () const
191 {
192   if (!lookup_l_)
193     {
194       Score_element * urg = (Score_element*)this;
195       SCM sz = urg->remove_elt_property ("fontsize");
196       int i = (gh_number_p (sz))
197         ? gh_scm2int  (sz)
198         : 0;
199
200       urg->lookup_l_ =  (Lookup*)pscore_l_->paper_l_->lookup_l (i);
201     }
202   return lookup_l_;
203 }
204
205 void
206 Score_element::add_processing()
207 {
208   assert (status_i_ >=0);
209   if (status_i_)
210     return;
211   status_i_ ++;
212
213   do_add_processing();
214 }
215
216 void
217 Score_element::calculate_dependencies (int final, int busy,
218                                        Score_element_method_pointer funcptr)
219 {
220   assert (status_i_ >=0);
221
222   if (status_i_ >= final)
223     return;
224
225   if (status_i_== busy)
226     {
227       programming_error ("Element is busy, come back later");
228       return;
229     }
230   
231   status_i_= busy;
232
233   for (SCM d=  get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
234     {
235       unsmob_element (gh_car (d))
236         ->calculate_dependencies (final, busy, funcptr);
237     }
238
239   (this->*funcptr)();
240   status_i_= final;
241 }
242
243 void
244 Score_element::output_processing () 
245 {
246   if (to_boolean (get_elt_property ("transparent")))
247     return;
248
249   Molecule m (do_brew_molecule ());
250   Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
251
252   SCM s = get_elt_property ("extra-offset");
253   if (gh_pair_p (s))
254     {
255       Real il = paper_l ()->get_var ("interline");
256       o[X_AXIS] += il * gh_scm2double (gh_car (s));
257       o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
258     }
259   
260   line_l ()->output_molecule (m.expr_, o);
261 }
262
263 /*
264   
265   VIRTUAL STUBS
266
267  */
268 void
269 Score_element::do_break_processing()
270 {
271 }
272
273 void
274 Score_element::after_line_breaking ()
275 {
276 }
277
278
279 void
280 Score_element::before_line_breaking ()
281 {
282 }
283
284 void
285 Score_element::do_space_processing ()
286 {
287 }
288
289 void
290 Score_element::do_add_processing()
291 {
292 }
293
294
295 /*
296   ugh.
297  */  
298 Molecule 
299 Score_element::do_brew_molecule() const
300 {
301   SCM glyph = get_elt_property ("glyph");
302   if (gh_string_p (glyph))
303     {
304       return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
305       
306     }
307   else
308     {
309       Molecule m ;
310       m.set_empty (true);
311       return m;
312     }
313 }
314
315
316 Line_of_score *
317 Score_element::line_l() const
318 {
319   return 0;
320 }
321
322 void
323 Score_element::add_dependency (Score_element*e)
324 {
325   if (e)
326     {
327       Group_interface gi (this, "dependencies");
328       gi.add_element (e);
329     }
330   else
331     programming_error ("Null dependency added");
332 }
333
334
335
336
337 /**
338       Do break substitution in S, using CRITERION. Return new value.
339       CRITERION is either a SMOB pointer to the desired line, or a number
340       representing the break direction. Do not modify SRC.
341 */
342 SCM
343 Score_element::handle_broken_smobs (SCM src, SCM criterion)
344 {
345  again:
346
347   
348   Score_element *sc = unsmob_element (src);
349   if (sc)
350     {
351       if (criterion == SCM_UNDEFINED)
352         return SCM_UNDEFINED;
353       else if (gh_number_p (criterion))
354         {
355           Item * i = dynamic_cast<Item*> (sc);
356           Direction d = to_dir (criterion);
357           if (i && i->break_status_dir () != d)
358             {
359               Item *br = i->find_prebroken_piece (d);
360               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
361             }
362         }
363       else
364         {
365           Line_of_score * line = dynamic_cast<Line_of_score*> (unsmob_element ( criterion));
366           Line_of_score * dep_line = sc->line_l ();
367           if (dep_line != line)
368             {
369               Score_element * br = sc->find_broken_piece (line);
370               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
371             }
372           if (!dep_line)
373             return SCM_UNDEFINED;
374
375           if (!sc->common_refpoint (line, X_AXIS)
376               || !sc->common_refpoint (line, Y_AXIS))
377             {
378               return SCM_UNDEFINED;
379             }
380         }
381     }
382   else if (gh_pair_p (src))
383     {
384       /*
385         UGH! breaks on circular lists.
386       */
387       SCM car = handle_broken_smobs (gh_car (src), criterion);
388       SCM cdr = gh_cdr (src);
389       
390       if (car == SCM_UNDEFINED
391           && (gh_pair_p (cdr) || cdr == SCM_EOL))
392         {
393           /*
394             This is tail-recursion, ie. 
395             
396             return handle_broken_smobs (cdr, criterion);
397
398             We don't want to rely on the compiler to do this.  Without
399             tail-recursion, this easily crashes with a stack overflow.  */
400           src =  cdr;   
401           goto again;
402         }
403
404       return gh_cons (car, handle_broken_smobs (cdr, criterion));
405     }
406   else
407     return src;
408
409   return src;
410 }
411
412 void
413 Score_element::handle_broken_dependencies()
414 {
415   Spanner * s= dynamic_cast<Spanner*> (this);
416   if (original_l_ && s)
417     return;
418
419   if (s)
420     {
421       for (int i = 0;  i< s->broken_into_l_arr_ .size (); i++)
422         {
423           Score_element * sc = s->broken_into_l_arr_[i];
424           Line_of_score * l = sc->line_l ();
425           s->broken_into_l_arr_[i]->element_property_alist_ =
426             handle_broken_smobs (element_property_alist_,
427                                  l ? l->self_scm_ : SCM_UNDEFINED);
428         }
429     }
430
431
432   Line_of_score *line = line_l();
433
434   if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
435     {
436       element_property_alist_
437         = handle_broken_smobs (element_property_alist_,
438                                line ? line->self_scm_ : SCM_UNDEFINED);
439     }
440   else
441     {
442       /*
443         This element is `invalid'; it has been removed from all dependencies, so
444         let's junk the element itself.
445       */
446       element_property_alist_ = SCM_EOL;
447       set_extent_callback (0, Y_AXIS);
448       set_extent_callback (0, X_AXIS);
449     }
450 }
451
452 void
453 Score_element::handle_prebroken_dependencies()
454 {
455 }
456
457
458 bool
459 Score_element::linked_b() const
460 {
461   return used_b_;
462 }
463
464 void
465 Score_element::do_print () const
466 {
467 }
468
469 Score_element*
470 Score_element::find_broken_piece (Line_of_score*) const
471 {
472   return 0;
473 }
474
475 void
476 Score_element::translate_axis (Real y, Axis a)
477 {
478   dim_cache_[a]->translate (y);
479 }  
480
481 Real
482 Score_element::relative_coordinate (Score_element const*e, Axis a) const
483 {
484   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
485 }
486
487 Score_element * 
488 Score_element::common_refpoint (Score_element const* s, Axis a) const
489 {
490   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
491   return  dim ? dim->element_l () : 0;
492 }
493
494 bool
495 Score_element::empty_b (Axis a)const
496 {
497   return !dim_cache_[a]->extent_callback_l_;
498 }
499
500 Interval
501 Score_element::extent (Axis a) const
502 {
503   Dimension_cache const * d = dim_cache_[a];
504   Interval ext = d->get_dim ();
505
506   if (empty_b (a)) 
507     return ext;
508
509   SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
510                                 : "extra-extent-Y");
511
512   /*
513     signs ?
514    */
515   Real s = paper_l ()->get_var ("staffspace");
516   if (gh_pair_p (extra))
517     {
518       ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
519       ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
520     }
521   
522   extra = get_elt_property (a == X_AXIS
523                                 ? "minimum-extent-X"
524                                 : "minimum-extent-Y");
525   if (gh_pair_p (extra))
526     {
527       ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
528                            s * gh_scm2double (gh_cdr (extra))));
529     }
530   
531   return ext;
532 }
533
534
535 Score_element*
536 Score_element::parent_l (Axis a) const
537 {
538   Dimension_cache*d= dim_cache_[a]->parent_l_;
539   return d ? d->elt_l_ : 0;
540 }
541
542 Score_element *
543 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
544 {
545   Dimension_cache * common = dim_cache_[a];
546   for (int i=0; i < gs.size (); i++)
547     {
548       common = common->common_refpoint (gs[i]->dim_cache_[a]);
549     }
550
551   return common->element_l ();
552 }
553
554 char const *
555 Score_element::name () const
556 {
557   return classname (this);
558 }
559
560 void
561 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
562 {
563   dim_cache_[a]->off_callbacks_.push (cb);
564 }
565
566 bool
567 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
568 {
569   for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
570     {
571       if (dim_cache_[a]->off_callbacks_[i] == cb)
572         return true;
573     }
574   return false;
575 }
576
577 void
578 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
579 {
580   dim_cache_[a]->extent_callback_l_ = dc ;
581 }
582
583                                     
584 void
585 Score_element::set_parent (Score_element *g, Axis a)
586 {
587   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
588 }
589
590 void
591 Score_element::fixup_refpoint ()
592 {
593   for (int a = X_AXIS; a < NO_AXES; a ++)
594     {
595       Axis ax = (Axis)a;
596       Score_element * parent = parent_l (ax);
597
598       if (!parent)
599         continue;
600       
601       if (parent->line_l () != line_l () && line_l ())
602         {
603           Score_element * newparent = parent->find_broken_piece (line_l ());
604           set_parent (newparent, ax);
605         }
606
607       if (Item * i  = dynamic_cast<Item*> (this))
608         {
609           Item *parenti = dynamic_cast<Item*> (parent);
610
611           if (parenti && i)
612             {
613               Direction  my_dir = i->break_status_dir () ;
614               if (my_dir!= parenti->break_status_dir())
615                 {
616                   Item *newparent =  parenti->find_prebroken_piece (my_dir);
617                   set_parent (newparent, ax);
618                 }
619             }
620         }
621     }
622 }
623
624
625
626 /****************************************************
627   SMOB funcs
628  ****************************************************/
629
630 #include "ly-smobs.icc"
631
632 IMPLEMENT_UNSMOB(Score_element, element);
633 IMPLEMENT_SMOBS(Score_element);
634 SCM
635 Score_element::mark_smob (SCM ses)
636 {
637   Score_element * s = SMOB_TO_TYPE (Score_element, ses);
638   if (s->self_scm_ != ses)
639     {
640       programming_error ("SMOB marking gone awry");
641       return SCM_EOL;
642     }
643   return s->element_property_alist_;
644 }
645
646 int
647 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
648 {
649   Score_element *sc = (Score_element *) gh_cdr (s);
650      
651   scm_puts ("#<Score_element ", port);
652   scm_puts ((char *)sc->name (), port);
653
654   /*
655     don't try to print properties, that is too much hassle.
656    */
657   scm_puts (" >", port);
658   return 1;
659 }
660
661 void
662 Score_element::do_smobify_self ()
663 {
664 }
665
666 SCM
667 Score_element::equal_p (SCM a, SCM b)
668 {
669   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
670 }
671
672
673 SCM
674 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
675 {
676   Score_element * sc = unsmob_element (elt);
677
678   if (!gh_symbol_p (sym))
679     {
680       error ("Not a symbol");
681       ly_display_scm (sym);
682       return SCM_UNDEFINED;
683     }
684
685   if (sc)
686     {
687       sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
688     }
689   else
690     {
691       error ("Not a score element");
692       ly_display_scm (elt);
693     }
694
695   return SCM_UNDEFINED;
696 }
697
698
699 SCM
700 Score_element::ly_get_elt_property (SCM elt, SCM sym)
701 {
702   Score_element * sc = unsmob_element (elt);
703   
704   if (sc)
705     {
706       SCM s = scm_assq(sym, sc->element_property_alist_);
707
708       if (s != SCM_BOOL_F)
709         return gh_cdr (s); 
710       else
711         return SCM_UNDEFINED;
712     }
713   else
714     {
715       error ("Not a score element");
716       ly_display_scm (elt);
717     }
718   return SCM_UNDEFINED;
719 }
720
721
722 static void
723 init_functions ()
724 {
725   scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
726   scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
727 }
728
729 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
730
731 void
732 Score_element::do_breakable_col_processing ()
733 {
734 }