]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.11
[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--1999 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   output_p_ =0;
34   dim_cache_[X_AXIS] = new Dimension_cache;
35   dim_cache_[Y_AXIS] = new Dimension_cache;
36   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
37   
38   used_b_ = false;
39
40   dim_cache_[X_AXIS]->set_callback (molecule_extent);
41   dim_cache_[Y_AXIS]->set_callback (molecule_extent); 
42   used_b_ = false;
43   pscore_l_=0;
44   lookup_l_ =0;
45   status_i_ = 0;
46   self_scm_ = SCM_EOL;
47   original_l_ = 0;
48   element_property_alist_ = SCM_EOL;
49
50   smobify_self ();
51
52
53   set_elt_property ("dependencies", SCM_EOL);
54 }
55
56 SCM ly_deep_copy (SCM);
57
58 SCM
59 ly_deep_copy (SCM l)
60 {
61   if (gh_pair_p (l))
62     {
63       return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
64     }
65   else
66     return l;
67 }
68
69
70 Score_element::Score_element (Score_element const&s)
71 {
72   dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
73   dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
74   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
75   
76   self_scm_ = SCM_EOL;
77   used_b_ = true;
78   original_l_ =(Score_element*) &s;
79
80   /*
81     should protect because smobify_self () might trigger GC.
82    */
83   element_property_alist_ = scm_protect_object (ly_deep_copy (s.element_property_alist_));
84
85   output_p_ =0;
86   status_i_ = s.status_i_;
87   lookup_l_ = s.lookup_l_;
88   pscore_l_ = s.pscore_l_;
89
90   smobify_self ();
91 }
92
93 Score_element::~Score_element()
94 {
95   assert (!output_p_);
96   assert (status_i_ >=0);
97   status_i_  = -1;
98
99   delete dim_cache_[X_AXIS];
100   delete dim_cache_[Y_AXIS];  
101 }
102
103
104 Real
105 Score_element::get_real (String s) const
106 {
107   return gh_scm2double (get_elt_property (s));
108 }
109
110 void
111 Score_element::set_real (String s, Real r)
112 {
113   set_elt_property (s, gh_double2scm (r));
114 }
115
116 // should also have one that takes SCM arg. 
117 SCM
118 Score_element::get_elt_property (String nm) const
119 {
120   SCM sym =  ly_symbol2scm (nm.ch_C());
121   SCM s = scm_assq(sym, element_property_alist_);
122
123   if (s != SCM_BOOL_F)
124     return SCM_CDR (s); 
125   
126   if (pscore_l_)
127     {
128       SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
129       SCM val;
130       
131       // should probably check for Type::sym as well.
132       Paper_def * p= pscore_l_->paper_l_;
133       if (p->default_properties_.try_retrieve (sym2, &val))
134         return val;
135       else if (p->default_properties_.try_retrieve (sym, &val))
136         return val;
137     }
138   
139   return SCM_UNDEFINED;
140 }
141
142 SCM
143 Score_element::remove_elt_property (String key)
144 {
145   SCM s = get_elt_property (key); 
146   SCM sym = ly_symbol2scm (key.ch_C());
147   element_property_alist_ =  scm_assq_remove_x (element_property_alist_, sym);
148   return s;
149 }
150
151 /*
152   UGH. assoc vs. assq
153  */
154 void
155 Score_element::set_elt_property (String k, SCM v)
156 {
157   SCM s = ly_symbol2scm (k.ch_C( ));
158   element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
159 }
160
161 Interval
162 Score_element::molecule_extent(Dimension_cache const *c)
163 {
164   Score_element *s = dynamic_cast<Score_element*>(c->element_l());
165   Molecule*m = s->do_brew_molecule_p();
166   return  m->extent()[c->axis ()];
167 }
168
169
170 void
171 Score_element::print() const
172 {
173 #ifndef NPRINT
174   DEBUG_OUT << classname(this) << "{\n";
175   
176   
177   if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
178     ly_display_scm (element_property_alist_);
179
180   if (original_l_)
181     DEBUG_OUT << "Copy ";
182   do_print();
183   
184   DEBUG_OUT <<  "}\n";
185 #endif
186 }
187
188 Paper_def*
189 Score_element::paper_l ()  const
190 {
191  return pscore_l_ ? pscore_l_->paper_l_ : 0;
192 }
193
194 Lookup const *
195 Score_element::lookup_l () const
196 {
197   if (!lookup_l_)
198     {
199       Score_element * urg = (Score_element*)this;
200       SCM sz = urg->remove_elt_property ("fontsize");
201       int i = (sz != SCM_UNDEFINED)
202         ? gh_scm2int  (sz)
203         : 0;
204
205       urg->lookup_l_ =  (Lookup*)pscore_l_->paper_l_->lookup_l (i);
206     }
207   return lookup_l_;
208 }
209
210 void
211 Score_element::add_processing()
212 {
213   assert (status_i_ >=0);
214   if (status_i_)
215     return;
216   status_i_ ++;
217
218 #if 0
219     /*
220     UGH. UGH. UGH.
221    */
222   if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
223       && !dim_cache_[X_AXIS]->off_callback_l_)
224     {
225       dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::self_alignment);
226     }
227   
228   if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
229       && !dim_cache_[X_AXIS]->off_callback_l_)
230       
231     {
232       dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::self_alignment);
233     }
234 #endif
235   
236   do_add_processing();
237 }
238
239 void
240 Score_element::calculate_dependencies (int final, int busy,
241                                        Score_element_method_pointer funcptr)
242 {
243   assert (status_i_ >=0);
244
245   if (status_i_ >= final)
246     return;
247
248   assert (status_i_!= busy);
249   status_i_= busy;
250
251   Link_array<Score_element> dependency_arr =
252     Group_interface__extract_elements (this, (Score_element*)0, "dependencies");
253   
254   for (int i=0; i < dependency_arr.size(); i++)
255     dependency_arr[i]->calculate_dependencies (final, busy, funcptr);
256
257   Link_array<Score_element> extra (get_extra_dependencies());
258   for (int i=0; i < extra.size(); i++)
259     extra[i]->calculate_dependencies (final, busy, funcptr);
260   
261   (this->*funcptr)();
262   status_i_= final;
263 }
264
265 void
266 Score_element::output_processing () 
267 {
268   if (get_elt_property ("transparent") != SCM_UNDEFINED)
269     return;
270
271   // we're being silly here. 
272   if (output_p_)
273     delete output_p_;
274   
275   output_p_ = do_brew_molecule_p ();
276   Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
277
278   SCM s = get_elt_property ("extra-offset");
279   if (gh_pair_p (s))
280     {
281       Real il = paper_l ()->get_var ("interline");
282       o[X_AXIS] += il * gh_scm2double (gh_car (s));
283       o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
284     }
285   
286   pscore_l_->outputter_l_->output_molecule (output_p_,
287                                             o,
288                                             classname(this));
289
290   delete output_p_;
291   output_p_ =0;
292 }
293
294 /*
295   
296   VIRTUAL STUBS
297
298  */
299 void
300 Score_element::do_break_processing()
301 {
302 }
303
304 void
305 Score_element::do_post_processing()
306 {
307 }
308
309 void
310 Score_element::do_breakable_col_processing()
311 {
312   handle_prebroken_dependencies();
313 }
314
315 void
316 Score_element::do_pre_processing()
317 {
318 }
319
320 void
321 Score_element::do_space_processing ()
322 {
323 }
324
325 void
326 Score_element::do_add_processing()
327 {
328 }
329
330
331
332 Molecule*
333 Score_element::do_brew_molecule_p() const
334 {
335   Interval emp;
336   emp.set_empty ();
337   Molecule a (lookup_l ()->fill (Box (emp,emp)));
338   return new Molecule (a);
339 }
340
341
342 Line_of_score *
343 Score_element::line_l() const
344 {
345   return 0;
346 }
347
348 void
349 Score_element::add_dependency (Score_element*e)
350 {
351   if (e)
352     {
353       Group_interface gi (this, "dependencies");
354       gi.add_element (e);
355     }
356   else
357     programming_error ("Null dependency added");
358 }
359
360
361
362
363 /**
364       Do break substitution in S, using CRITERION. Return new value.
365          CRITERION is either a SMOB pointer to the desired line, or a number
366          representing the break direction.  */
367 SCM
368 Score_element::handle_broken_smobs (SCM s, SCM criterion)
369 {
370   Score_element *sc = unsmob_element ( s);
371   if (sc)
372     {
373       if (criterion == SCM_UNDEFINED)
374         return SCM_UNDEFINED;
375       else if (gh_number_p (criterion))
376         {
377           Item * i = dynamic_cast<Item*> (sc);
378           Direction d = to_dir (criterion);
379           if (i && i->break_status_dir () != d)
380             {
381               Item *br = i->find_broken_piece (d);
382               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
383             }
384         }
385       else
386         {
387           Score_element * ln = unsmob_element ( criterion);
388           Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
389           Score_element * br =0;
390           Line_of_score * dep_line = sc->line_l ();
391           if (dep_line != line)
392             {
393               br = sc->find_broken_piece (line);
394               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
395             }
396           if (!dep_line)
397             return SCM_UNDEFINED;
398         }
399     }
400   else if (gh_pair_p (s))
401     {
402       /*
403         UGH! breaks on circular lists.
404       */
405       gh_set_car_x (s, handle_broken_smobs (gh_car (s), criterion));
406       gh_set_cdr_x (s, handle_broken_smobs (gh_cdr (s), criterion));
407
408       SCM c = gh_cdr(s);
409
410       // gh_list_p () is linear, this is O(1)  
411       bool list = gh_pair_p (c) || c == SCM_EOL;
412       
413       if (gh_car (s) == SCM_UNDEFINED && list)
414         return c;
415     }
416   return s;
417 }
418 #if 0
419 void
420 Score_element::recurse_into_smobs (SCM s, void (Score_element::*meth_ptr)())
421 {
422   Score_element * sc = unsmob_element ( s);
423   if (sc)
424     {
425       (sc->*meth_ptr) ();
426     }
427   else if (gh_pair_p (s))
428     {
429       recurse_into_smobs (gh_car (s), meth_ptr);
430       recurse_into_smobs (gh_cdr (s), meth_ptr);      
431     }
432 }
433 #endif
434
435 void
436 Score_element::handle_broken_dependencies()
437 {
438   Line_of_score *line  = line_l();
439   
440   SCM rec = get_elt_property ("handle-broken-deps");
441   if (gh_boolean_p (rec) && gh_scm2bool (rec))
442     return;
443   
444   set_elt_property ("handle-broken-deps", SCM_BOOL_T);
445   element_property_alist_ = handle_broken_smobs (element_property_alist_,
446                                                  line ? line->self_scm_ : SCM_UNDEFINED);
447
448
449   if (!line)
450     return;
451 }
452
453
454 /*
455   TODO: cleanify.
456  */
457 void
458 Score_element::handle_prebroken_dependencies()
459 {
460   if (Item*i =dynamic_cast<Item*> (this))
461     {
462       element_property_alist_
463         = handle_broken_smobs (element_property_alist_,
464                                gh_int2scm (i->break_status_dir ()));
465     }
466 }
467
468
469
470
471
472 Link_array<Score_element>
473 Score_element::get_extra_dependencies() const
474 {
475   Link_array<Score_element> empty;
476   return empty;
477 }
478
479 bool
480 Score_element::linked_b() const
481 {
482   return used_b_;
483 }
484
485 void
486 Score_element::do_print () const
487 {
488 }
489
490 Score_element*
491 Score_element::find_broken_piece (Line_of_score*) const
492 {
493   return 0;
494 }
495
496 SCM
497 Score_element::mark_smob (SCM ses)
498 {
499   void * mp = (void*) SCM_CDR(ses);
500   Score_element * s = (Score_element*) mp;
501
502   assert (s->self_scm_ == ses);
503   return s->element_property_alist_;
504 }
505
506
507 int
508 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
509 {
510   Score_element *sc = (Score_element *) SCM_CDR (s);
511      
512   scm_puts ("#<Score_element ", port);
513   scm_puts ((char *)sc->name (), port);
514
515   // scm_puts (" properties = ", port);
516   // scm_display (sc->element_property_alist_, port);
517   scm_puts (" >", port);
518   return 1;
519 }
520
521 void
522 Score_element::do_smobify_self ()
523 {
524   scm_unprotect_object (element_property_alist_); // ugh
525 }
526 #include "ly-smobs.icc"
527 IMPLEMENT_SMOBS(Score_element);
528
529 SCM
530 Score_element::equal_p (SCM a, SCM b)
531 {
532   return SCM_CDR(a) == SCM_CDR(b) ? SCM_BOOL_T : SCM_BOOL_F;
533 }
534
535 void
536 Score_element::translate_axis (Real y, Axis a)
537 {
538   dim_cache_[a]->translate (y);
539 }  
540
541 Real
542 Score_element::relative_coordinate (Score_element const*e, Axis a) const
543 {
544   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
545 }
546
547 Score_element * 
548 Score_element::common_refpoint (Score_element const* s, Axis a) const
549 {
550   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
551   if (!dim)
552     programming_error ("No  common reference point");
553   return  dim ? dim->element_l () : 0;
554 }
555
556 void
557 Score_element::set_empty (Axis a)
558 {
559   dim_cache_[a]->callback_l_ =0;
560 }
561
562 bool
563 Score_element::empty_b (Axis a)const
564 {
565   return !dim_cache_[a]->callback_l_;
566 }
567
568 Interval
569 Score_element::extent (Axis a) const
570 {
571   Dimension_cache const * d = dim_cache_[a];
572
573   return d->get_dim ();
574 }
575
576 Score_element*
577 unsmob_element (SCM s)
578 {
579   if (SMOB_IS_TYPE_B (Score_element, s))
580     return SMOB_TO_TYPE(Score_element,s);
581   else
582     return 0;
583 }
584
585
586 /*
587   JUNKME
588  */
589 void
590 Score_element::invalidate_cache (Axis a)
591 {
592   //  dim_cache_[a]->invalidate ();
593 }
594
595 Score_element*
596 Score_element::parent_l (Axis a) const
597 {
598   Dimension_cache*d= dim_cache_[a]->parent_l_;
599   return d ? d->elt_l_ : 0;
600 }
601
602 Score_element *
603 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
604 {
605   Dimension_cache * common = dim_cache_[a];
606   for (int i=0; i < gs.size (); i++)
607     {
608       common = common->common_refpoint (gs[i]->dim_cache_[a]);
609     }
610
611   return common->element_l ();
612 }
613
614 char const *
615 Score_element::name () const
616 {
617   return classname (this);
618 }
619
620
621 void
622 Score_element::set_parent (Score_element *g, Axis a)
623 {
624   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
625 }
626
627 void
628 Score_element::fixup_refpoint ()
629 {
630   for (int a = X_AXIS; a < NO_AXES; a ++)
631     {
632       Axis ax = (Axis)a;
633       Score_element * par = parent_l (ax);
634
635       if (!par)
636         continue;
637       
638       if (par->line_l () != line_l ())
639         {
640           Score_element * newpar = par->find_broken_piece (line_l ());
641           set_parent (newpar, ax);
642         }
643
644       if (Item * i  = dynamic_cast<Item*> (this))
645         {
646           Item *pari = dynamic_cast<Item*> (par);
647
648           if (pari && i)
649             {
650               Direction  my_dir = i->break_status_dir () ;
651               if (my_dir!= pari->break_status_dir())
652                 {
653                   Item *newpar =  pari->find_broken_piece (my_dir);
654                   set_parent (newpar, ax);
655                 }
656             }
657         }
658     }
659 }