]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.44
[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 #if 0
213     /*
214     UGH. UGH. UGH.
215    */
216   if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
217       && !dim_cache_[X_AXIS]->off_callback_l_)
218     {
219       add_offset_callback (Side_position_interface::aligned_on_self,X_AXIS);
220     }
221   
222   if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
223       && !dim_cache_[X_AXIS]->off_callback_l_)
224       
225     {
226       add_offset_callback (Side_position_interface::aligned_on_self, Y_AXIS);
227     }
228 #endif
229   
230   do_add_processing();
231 }
232
233 void
234 Score_element::calculate_dependencies (int final, int busy,
235                                        Score_element_method_pointer funcptr)
236 {
237   assert (status_i_ >=0);
238
239   if (status_i_ >= final)
240     return;
241
242   if (status_i_== busy)
243     {
244       programming_error ("Element is busy, come back later");
245       return;
246     }
247   
248   status_i_= busy;
249
250   for (SCM d=  get_elt_property ("dependencies"); d != SCM_EOL; d = gh_cdr (d))
251     {
252       unsmob_element (gh_car (d))
253         ->calculate_dependencies (final, busy, funcptr);
254     }
255
256   (this->*funcptr)();
257   status_i_= final;
258 }
259
260 void
261 Score_element::output_processing () 
262 {
263   if (to_boolean (get_elt_property ("transparent")))
264     return;
265
266   Molecule m (do_brew_molecule ());
267   Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
268
269   SCM s = get_elt_property ("extra-offset");
270   if (gh_pair_p (s))
271     {
272       Real il = paper_l ()->get_var ("interline");
273       o[X_AXIS] += il * gh_scm2double (gh_car (s));
274       o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
275     }
276   
277   line_l ()->output_molecule (m.expr_, o);
278 }
279
280 /*
281   
282   VIRTUAL STUBS
283
284  */
285 void
286 Score_element::do_break_processing()
287 {
288 }
289
290 void
291 Score_element::after_line_breaking ()
292 {
293 }
294
295
296 void
297 Score_element::before_line_breaking ()
298 {
299 }
300
301 void
302 Score_element::do_space_processing ()
303 {
304 }
305
306 void
307 Score_element::do_add_processing()
308 {
309 }
310
311
312
313 Molecule 
314 Score_element::do_brew_molecule() const
315 {
316   SCM glyph = get_elt_property ("glyph");
317   if (gh_string_p (glyph))
318     {
319       return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
320       
321     }
322   else
323     {
324       Molecule m ;
325       m.set_empty (true);
326       return m;
327     }
328 }
329
330
331 Line_of_score *
332 Score_element::line_l() const
333 {
334   return 0;
335 }
336
337 void
338 Score_element::add_dependency (Score_element*e)
339 {
340   if (e)
341     {
342       Group_interface gi (this, "dependencies");
343       gi.add_element (e);
344     }
345   else
346     programming_error ("Null dependency added");
347 }
348
349
350
351
352 /**
353       Do break substitution in S, using CRITERION. Return new value.
354       CRITERION is either a SMOB pointer to the desired line, or a number
355       representing the break direction. Do not modify SRC.
356 */
357 SCM
358 Score_element::handle_broken_smobs (SCM src, SCM criterion)
359 {
360  again:
361
362   
363   Score_element *sc = unsmob_element (src);
364   if (sc)
365     {
366       if (criterion == SCM_UNDEFINED)
367         return SCM_UNDEFINED;
368       else if (gh_number_p (criterion))
369         {
370           Item * i = dynamic_cast<Item*> (sc);
371           Direction d = to_dir (criterion);
372           if (i && i->break_status_dir () != d)
373             {
374               Item *br = i->find_broken_piece (d);
375               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
376             }
377         }
378       else
379         {
380           Score_element * ln = unsmob_element ( criterion);
381           Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
382           Score_element * br =0;
383           Line_of_score * dep_line = sc->line_l ();
384           if (dep_line != line)
385             {
386               br = sc->find_broken_piece (line);
387               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
388             }
389           if (!dep_line)
390             return SCM_UNDEFINED;
391         }
392     }
393   else if (gh_pair_p (src))
394     {
395       /*
396         UGH! breaks on circular lists.
397       */
398       SCM car = handle_broken_smobs (gh_car (src), criterion);
399       SCM cdr = gh_cdr (src);
400       
401       if (car == SCM_UNDEFINED
402           && (gh_pair_p (cdr) || cdr == SCM_EOL))
403         {
404           /*
405             This is tail-recursion, ie. 
406             
407             return handle_broken_smobs (cdr, criterion);
408
409             We don't want to rely on the compiler to do this.  */
410           src =  cdr;   
411           goto again;
412         }
413
414       return gh_cons (car, handle_broken_smobs (cdr, criterion));
415     }
416   else
417     return src;
418
419   return src;
420 }
421
422 void
423 Score_element::handle_broken_dependencies()
424 {
425   Spanner * s= dynamic_cast<Spanner*> (this);
426   if (original_l_ && s)
427     return;
428
429   if (s)
430     {
431       for (int i = 0;  i< s->broken_into_l_arr_ .size (); i++)
432         {
433           Score_element * sc = s->broken_into_l_arr_[i];
434           Line_of_score * l = sc->line_l ();
435           s->broken_into_l_arr_[i]->element_property_alist_ =
436             handle_broken_smobs (element_property_alist_,
437                                  l ? l->self_scm_ : SCM_UNDEFINED);
438         }
439     }
440
441   Line_of_score *line = line_l();
442   element_property_alist_
443     = handle_broken_smobs (element_property_alist_,
444                            line ? line->self_scm_ : SCM_UNDEFINED);
445 }
446
447
448 /*
449   TODO: cleanify.
450  */
451 void
452 Score_element::handle_prebroken_dependencies()
453 {
454   if (Item*i =dynamic_cast<Item*> (this))
455     {
456       if (original_l_)
457         {
458           element_property_alist_
459             = handle_broken_smobs (original_l_->element_property_alist_,
460                                gh_int2scm (i->break_status_dir ()));
461         }
462     }
463 }
464
465 bool
466 Score_element::linked_b() const
467 {
468   return used_b_;
469 }
470
471 void
472 Score_element::do_print () const
473 {
474 }
475
476 Score_element*
477 Score_element::find_broken_piece (Line_of_score*) const
478 {
479   return 0;
480 }
481
482 void
483 Score_element::translate_axis (Real y, Axis a)
484 {
485   dim_cache_[a]->translate (y);
486 }  
487
488 Real
489 Score_element::relative_coordinate (Score_element const*e, Axis a) const
490 {
491   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
492 }
493
494 Score_element * 
495 Score_element::common_refpoint (Score_element const* s, Axis a) const
496 {
497   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
498   return  dim ? dim->element_l () : 0;
499 }
500
501 bool
502 Score_element::empty_b (Axis a)const
503 {
504   return !dim_cache_[a]->extent_callback_l_;
505 }
506
507 Interval
508 Score_element::extent (Axis a) const
509 {
510   Dimension_cache const * d = dim_cache_[a];
511   Interval ext = d->get_dim ();
512
513   if (empty_b (a)) 
514     return ext;
515
516   SCM extra = get_elt_property (a == X_AXIS ? "extra-extent-X"
517                                 : "extra-extent-Y");
518
519   /*
520     signs ?
521    */
522   Real s = paper_l ()->get_var ("staffspace");
523   if (gh_pair_p (extra))
524     {
525       ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
526       ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
527     }
528   
529   extra = get_elt_property (a == X_AXIS
530                                 ? "minimum-extent-X"
531                                 : "minimum-extent-Y");
532   if (gh_pair_p (extra))
533     {
534       ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
535                            s * gh_scm2double (gh_cdr (extra))));
536     }
537   
538   return ext;
539 }
540
541
542 Score_element*
543 Score_element::parent_l (Axis a) const
544 {
545   Dimension_cache*d= dim_cache_[a]->parent_l_;
546   return d ? d->elt_l_ : 0;
547 }
548
549 Score_element *
550 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
551 {
552   Dimension_cache * common = dim_cache_[a];
553   for (int i=0; i < gs.size (); i++)
554     {
555       common = common->common_refpoint (gs[i]->dim_cache_[a]);
556     }
557
558   return common->element_l ();
559 }
560
561 char const *
562 Score_element::name () const
563 {
564   return classname (this);
565 }
566
567 void
568 Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
569 {
570   dim_cache_[a]->off_callbacks_.push (cb);
571 }
572
573 bool
574 Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
575 {
576   for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
577     {
578       if (dim_cache_[a]->off_callbacks_[i] == cb)
579         return true;
580     }
581   return false;
582 }
583
584 void
585 Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
586 {
587   dim_cache_[a]->extent_callback_l_ = dc ;
588 }
589
590                                     
591 void
592 Score_element::set_parent (Score_element *g, Axis a)
593 {
594   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
595 }
596
597 void
598 Score_element::fixup_refpoint ()
599 {
600   for (int a = X_AXIS; a < NO_AXES; a ++)
601     {
602       Axis ax = (Axis)a;
603       Score_element * parent = parent_l (ax);
604
605       if (!parent)
606         continue;
607       
608       if (parent->line_l () != line_l () && line_l ())
609         {
610           Score_element * newparent = parent->find_broken_piece (line_l ());
611           set_parent (newparent, ax);
612           if (!newparent)
613             {
614               programming_error ("Orphaned score-element.");
615             }
616         }
617
618       if (Item * i  = dynamic_cast<Item*> (this))
619         {
620           Item *parenti = dynamic_cast<Item*> (parent);
621
622           if (parenti && i)
623             {
624               Direction  my_dir = i->break_status_dir () ;
625               if (my_dir!= parenti->break_status_dir())
626                 {
627                   Item *newparent =  parenti->find_broken_piece (my_dir);
628                   set_parent (newparent, ax);
629                 }
630             }
631         }
632     }
633 }
634
635
636
637 /****************************************************
638   SMOB funcs
639  ****************************************************/
640
641
642 #include "ly-smobs.icc"
643
644 IMPLEMENT_UNSMOB(Score_element, element);
645 IMPLEMENT_SMOBS(Score_element);
646 SCM
647 Score_element::mark_smob (SCM ses)
648 {
649   Score_element * s = SMOB_TO_TYPE (Score_element, ses);
650   if (s->self_scm_ != ses)
651     {
652       programming_error ("SMOB marking gone awry");
653       return SCM_EOL;
654     }
655   return s->element_property_alist_;
656 }
657
658 int
659 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
660 {
661   Score_element *sc = (Score_element *) gh_cdr (s);
662      
663   scm_puts ("#<Score_element ", port);
664   scm_puts ((char *)sc->name (), port);
665
666   /*
667     don't try to print properties, that is too much hassle.
668    */
669   scm_puts (" >", port);
670   return 1;
671 }
672
673 void
674 Score_element::do_smobify_self ()
675 {
676 }
677
678 SCM
679 Score_element::equal_p (SCM a, SCM b)
680 {
681   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
682 }
683
684
685 SCM
686 Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
687 {
688   Score_element * sc = unsmob_element (elt);
689
690   if (!gh_symbol_p (sym))
691     {
692       error ("Not a symbol");
693       ly_display_scm (sym);
694       return SCM_UNDEFINED;
695     }
696
697   if (sc)
698     {
699       sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
700     }
701   else
702     {
703       error ("Not a score element");
704       ly_display_scm (elt);
705     }
706
707   return SCM_UNDEFINED;
708 }
709
710
711 SCM
712 Score_element::ly_get_elt_property (SCM elt, SCM sym)
713 {
714   Score_element * sc = unsmob_element (elt);
715   
716   if (sc)
717     {
718       SCM s = scm_assq(sym, sc->element_property_alist_);
719
720       if (s != SCM_BOOL_F)
721         return gh_cdr (s); 
722       else
723         return SCM_UNDEFINED;
724     }
725   else
726     {
727       error ("Not a score element");
728       ly_display_scm (elt);
729     }
730   return SCM_UNDEFINED;
731 }
732
733
734 static void
735 init_functions ()
736 {
737   scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
738   scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
739 }
740
741 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
742
743 void
744 Score_element::do_breakable_col_processing ()
745 {
746 }