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