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