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