]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.52
[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   Score_element *sc = unsmob_element (src);
347   if (sc)
348     {
349       if (gh_number_p (criterion))
350         {
351           Item * i = dynamic_cast<Item*> (sc);
352           Direction d = to_dir (criterion);
353           if (i && i->break_status_dir () != d)
354             {
355               Item *br = i->find_prebroken_piece (d);
356               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
357             }
358         }
359       else
360         {
361           Line_of_score * line = dynamic_cast<Line_of_score*> (unsmob_element ( criterion));
362           Line_of_score * dep_line = sc->line_l ();
363           if (dep_line != line)
364             {
365               Score_element * br = sc->find_broken_piece (line);
366               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
367             }
368           if (line
369               &&  (!dep_line
370                    || !sc->common_refpoint (line, X_AXIS)
371                    || !sc->common_refpoint (line, Y_AXIS)))
372             {
373               return SCM_UNDEFINED;
374             }
375         }
376     }
377   else if (gh_pair_p (src))
378     {
379       SCM oldcar =gh_car (src);
380       /*
381         UGH! breaks on circular lists.
382       */
383       SCM newcar = handle_broken_smobs (oldcar, criterion);
384       SCM oldcdr = gh_cdr (src);
385       
386       if (newcar == SCM_UNDEFINED
387           && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
388         {
389           /*
390             This is tail-recursion, ie. 
391             
392             return handle_broken_smobs (cdr, criterion);
393
394             We don't want to rely on the compiler to do this.  Without
395             tail-recursion, this easily crashes with a stack overflow.  */
396           src =  oldcdr;
397           goto again;
398         }
399
400       SCM newcdr = handle_broken_smobs (oldcdr, criterion);
401       return gh_cons (newcar, newcdr);
402     }
403   else
404     return src;
405
406   return src;
407 }
408
409 void
410 Score_element::handle_broken_dependencies()
411 {
412   Spanner * s= dynamic_cast<Spanner*> (this);
413   if (original_l_ && s)
414     return;
415
416   if (s)
417     {
418       for (int i = 0;  i< s->broken_into_l_arr_ .size (); i++)
419         {
420           Score_element * sc = s->broken_into_l_arr_[i];
421           Line_of_score * l = sc->line_l ();
422           s->broken_into_l_arr_[i]->element_property_alist_ =
423             handle_broken_smobs (element_property_alist_,
424                                  l ? l->self_scm_ : SCM_UNDEFINED);
425         }
426     }
427
428
429   Line_of_score *line = line_l();
430
431   if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
432     {
433       element_property_alist_
434         = handle_broken_smobs (element_property_alist_,
435                                line ? line->self_scm_ : SCM_UNDEFINED);
436     }
437   else if (!dynamic_cast <Line_of_score*> (this))
438     {
439       /*
440         This element is `invalid'; it has been removed from all
441         dependencies, so let's junk the element itself.
442
443         do not do this for Line_of_score , since that would free
444         up originals of score-elts (a bad thing.)
445       */
446       
447       element_property_alist_ = SCM_EOL;
448       set_extent_callback (0, Y_AXIS);
449       set_extent_callback (0, X_AXIS);
450     }
451 }
452
453 void
454 Score_element::handle_prebroken_dependencies()
455 {
456 }
457
458
459 bool
460 Score_element::linked_b() const
461 {
462   return used_b_;
463 }
464
465 void
466 Score_element::do_print () const
467 {
468 }
469
470 Score_element*
471 Score_element::find_broken_piece (Line_of_score*) const
472 {
473   return 0;
474 }
475
476 void
477 Score_element::translate_axis (Real y, Axis a)
478 {
479   dim_cache_[a]->translate (y);
480 }  
481
482 Real
483 Score_element::relative_coordinate (Score_element const*e, Axis a) const
484 {
485   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
486 }
487
488 Score_element * 
489 Score_element::common_refpoint (Score_element const* s, Axis a) const
490 {
491   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
492   return  dim ? dim->element_l () : 0;
493 }
494
495 bool
496 Score_element::empty_b (Axis a)const
497 {
498   return !dim_cache_[a]->extent_callback_l_;
499 }
500
501 Interval
502 Score_element::extent (Axis a) const
503 {
504   Dimension_cache const * d = dim_cache_[a];
505   Interval ext = d->get_dim ();
506
507   if (empty_b (a)) 
508     return ext;
509
510   SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
511                                 : "extra-extent-Y");
512
513   /*
514     signs ?
515    */
516   Real s = paper_l ()->get_var ("staffspace");
517   if (gh_pair_p (extra))
518     {
519       ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
520       ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
521     }
522   
523   extra = get_elt_property (a == X_AXIS
524                                 ? "minimum-extent-X"
525                                 : "minimum-extent-Y");
526   if (gh_pair_p (extra))
527     {
528       ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
529                            s * gh_scm2double (gh_cdr (extra))));
530     }
531   
532   return ext;
533 }
534
535
536 Score_element*
537 Score_element::parent_l (Axis a) const
538 {
539   Dimension_cache*d= dim_cache_[a]->parent_l_;
540   return d ? d->elt_l_ : 0;
541 }
542
543 Score_element *
544 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
545 {
546   Dimension_cache * common = dim_cache_[a];
547   for (int i=0; i < gs.size (); i++)
548     {
549       common = common->common_refpoint (gs[i]->dim_cache_[a]);
550     }
551
552   return common->element_l ();
553 }
554
555 char const *
556 Score_element::name () const
557 {
558   return classname (this);
559 }
560
561 void
562 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
563 {
564   dim_cache_[a]->off_callbacks_.push (cb);
565 }
566
567 bool
568 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
569 {
570   for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
571     {
572       if (dim_cache_[a]->off_callbacks_[i] == cb)
573         return true;
574     }
575   return false;
576 }
577
578 void
579 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
580 {
581   dim_cache_[a]->extent_callback_l_ = dc ;
582 }
583
584                                     
585 void
586 Score_element::set_parent (Score_element *g, Axis a)
587 {
588   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
589 }
590
591 void
592 Score_element::fixup_refpoint ()
593 {
594   for (int a = X_AXIS; a < NO_AXES; a ++)
595     {
596       Axis ax = (Axis)a;
597       Score_element * parent = parent_l (ax);
598
599       if (!parent)
600         continue;
601       
602       if (parent->line_l () != line_l () && line_l ())
603         {
604           Score_element * newparent = parent->find_broken_piece (line_l ());
605           set_parent (newparent, ax);
606         }
607
608       if (Item * i  = dynamic_cast<Item*> (this))
609         {
610           Item *parenti = dynamic_cast<Item*> (parent);
611
612           if (parenti && i)
613             {
614               Direction  my_dir = i->break_status_dir () ;
615               if (my_dir!= parenti->break_status_dir())
616                 {
617                   Item *newparent =  parenti->find_prebroken_piece (my_dir);
618                   set_parent (newparent, ax);
619                 }
620             }
621         }
622     }
623 }
624
625
626
627 /****************************************************
628   SMOB funcs
629  ****************************************************/
630
631 #include "ly-smobs.icc"
632
633 IMPLEMENT_UNSMOB(Score_element, element);
634 IMPLEMENT_SMOBS(Score_element);
635
636 SCM
637 Score_element::mark_smob (SCM ses)
638 {
639   Score_element * s = SMOB_TO_TYPE (Score_element, ses);
640   if (s->self_scm_ != ses)
641     {
642       programming_error ("SMOB marking gone awry");
643       return SCM_EOL;
644     }
645   return s->element_property_alist_;
646 }
647
648 int
649 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
650 {
651   Score_element *sc = (Score_element *) gh_cdr (s);
652      
653   scm_puts ("#<Score_element ", port);
654   scm_puts ((char *)sc->name (), port);
655
656   /*
657     don't try to print properties, that is too much hassle.
658    */
659   scm_puts (" >", port);
660   return 1;
661 }
662
663 void
664 Score_element::do_smobify_self ()
665 {
666 }
667
668 SCM
669 Score_element::equal_p (SCM a, SCM b)
670 {
671   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
672 }
673
674
675 SCM
676 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
677 {
678   Score_element * sc = unsmob_element (elt);
679
680   if (!gh_symbol_p (sym))
681     {
682       error ("Not a symbol");
683       ly_display_scm (sym);
684       return SCM_UNDEFINED;
685     }
686
687   if (sc)
688     {
689       sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
690     }
691   else
692     {
693       error ("Not a score element");
694       ly_display_scm (elt);
695     }
696
697   return SCM_UNDEFINED;
698 }
699
700
701 SCM
702 Score_element::ly_get_elt_property (SCM elt, SCM sym)
703 {
704   Score_element * sc = unsmob_element (elt);
705   
706   if (sc)
707     {
708       SCM s = scm_assq(sym, sc->element_property_alist_);
709
710       if (s != SCM_BOOL_F)
711         return gh_cdr (s); 
712       else
713         return SCM_UNDEFINED;
714     }
715   else
716     {
717       error ("Not a score element");
718       ly_display_scm (elt);
719     }
720   return SCM_UNDEFINED;
721 }
722
723
724 static void
725 init_functions ()
726 {
727   scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
728   scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
729 }
730
731 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
732
733 void
734 Score_element::do_breakable_col_processing ()
735 {
736 }