]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
release: 1.3.15
[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 gh_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 = (gh_number_p (sz))
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  again:
371
372   
373   Score_element *sc = unsmob_element ( s);
374   if (sc)
375     {
376       if (criterion == SCM_UNDEFINED)
377         return SCM_UNDEFINED;
378       else if (gh_number_p (criterion))
379         {
380           Item * i = dynamic_cast<Item*> (sc);
381           Direction d = to_dir (criterion);
382           if (i && i->break_status_dir () != d)
383             {
384               Item *br = i->find_broken_piece (d);
385               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
386             }
387         }
388       else
389         {
390           Score_element * ln = unsmob_element ( criterion);
391           Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
392           Score_element * br =0;
393           Line_of_score * dep_line = sc->line_l ();
394           if (dep_line != line)
395             {
396               br = sc->find_broken_piece (line);
397               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
398             }
399           if (!dep_line)
400             return SCM_UNDEFINED;
401         }
402     }
403   else if (gh_pair_p (s))
404     {
405       /*
406         UGH! breaks on circular lists.
407       */
408       SCM car = handle_broken_smobs (gh_car (s), criterion);
409       SCM cdr = gh_cdr (s);
410       
411       if (car == SCM_UNDEFINED
412           && (gh_pair_p (cdr) || cdr == SCM_EOL))
413         {
414           /*
415             This is tail-recursion, ie. 
416             
417             return handle_broken_smobs (cdr, criterion);
418
419             We don't want to rely on the compiler to do this.  */
420           s =  cdr;     
421           goto again;
422         }
423
424       gh_set_car_x (s, car);
425       gh_set_cdr_x (s, handle_broken_smobs (cdr, criterion));
426       return s;
427     }
428   return s;
429 }
430
431 void
432 Score_element::handle_broken_dependencies()
433 {
434   Line_of_score *line  = line_l();
435   element_property_alist_ = handle_broken_smobs (element_property_alist_,
436                                                  line ? line->self_scm_ : SCM_UNDEFINED);
437
438   if (!line)
439     return;
440 }
441
442
443 /*
444   TODO: cleanify.
445  */
446 void
447 Score_element::handle_prebroken_dependencies()
448 {
449   if (Item*i =dynamic_cast<Item*> (this))
450     {
451       element_property_alist_
452         = handle_broken_smobs (element_property_alist_,
453                                gh_int2scm (i->break_status_dir ()));
454     }
455 }
456
457
458
459
460
461 Link_array<Score_element>
462 Score_element::get_extra_dependencies() const
463 {
464   Link_array<Score_element> empty;
465   return empty;
466 }
467
468 bool
469 Score_element::linked_b() const
470 {
471   return used_b_;
472 }
473
474 void
475 Score_element::do_print () const
476 {
477 }
478
479 Score_element*
480 Score_element::find_broken_piece (Line_of_score*) const
481 {
482   return 0;
483 }
484
485 SCM
486 Score_element::mark_smob (SCM ses)
487 {
488   void * mp = (void*) gh_cdr(ses);
489   Score_element * s = (Score_element*) mp;
490
491   if (s->self_scm_ != ses)
492     {
493       programming_error ("SMOB marking gone awry");
494       return SCM_EOL;
495     }
496   return s->element_property_alist_;
497 }
498
499
500 int
501 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
502 {
503   Score_element *sc = (Score_element *) gh_cdr (s);
504      
505   scm_puts ("#<Score_element ", port);
506   scm_puts ((char *)sc->name (), port);
507
508   // scm_puts (" properties = ", port);
509   // scm_display (sc->element_property_alist_, port);
510   scm_puts (" >", port);
511   return 1;
512 }
513
514 void
515 Score_element::do_smobify_self ()
516 {
517   scm_unprotect_object (element_property_alist_); // ugh
518 }
519 #include "ly-smobs.icc"
520 IMPLEMENT_SMOBS(Score_element);
521
522 SCM
523 Score_element::equal_p (SCM a, SCM b)
524 {
525   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
526 }
527
528 void
529 Score_element::translate_axis (Real y, Axis a)
530 {
531   dim_cache_[a]->translate (y);
532 }  
533
534 Real
535 Score_element::relative_coordinate (Score_element const*e, Axis a) const
536 {
537   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
538 }
539
540 Score_element * 
541 Score_element::common_refpoint (Score_element const* s, Axis a) const
542 {
543   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
544   if (!dim)
545     programming_error ("No  common reference point");
546   return  dim ? dim->element_l () : 0;
547 }
548
549 void
550 Score_element::set_empty (Axis a)
551 {
552   dim_cache_[a]->callback_l_ =0;
553 }
554
555 bool
556 Score_element::empty_b (Axis a)const
557 {
558   return !dim_cache_[a]->callback_l_;
559 }
560
561 Interval
562 Score_element::extent (Axis a) const
563 {
564   Dimension_cache const * d = dim_cache_[a];
565
566   return d->get_dim ();
567 }
568
569 Score_element*
570 unsmob_element (SCM s)
571 {
572   if (SMOB_IS_TYPE_B (Score_element, s))
573     return SMOB_TO_TYPE(Score_element,s);
574   else
575     return 0;
576 }
577
578
579 Score_element*
580 Score_element::parent_l (Axis a) const
581 {
582   Dimension_cache*d= dim_cache_[a]->parent_l_;
583   return d ? d->elt_l_ : 0;
584 }
585
586 Score_element *
587 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
588 {
589   Dimension_cache * common = dim_cache_[a];
590   for (int i=0; i < gs.size (); i++)
591     {
592       common = common->common_refpoint (gs[i]->dim_cache_[a]);
593     }
594
595   return common->element_l ();
596 }
597
598 char const *
599 Score_element::name () const
600 {
601   return classname (this);
602 }
603
604
605 void
606 Score_element::set_parent (Score_element *g, Axis a)
607 {
608   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
609 }
610
611 void
612 Score_element::fixup_refpoint ()
613 {
614   for (int a = X_AXIS; a < NO_AXES; a ++)
615     {
616       Axis ax = (Axis)a;
617       Score_element * par = parent_l (ax);
618
619       if (!par)
620         continue;
621       
622       if (par->line_l () != line_l ())
623         {
624           Score_element * newpar = par->find_broken_piece (line_l ());
625           set_parent (newpar, ax);
626         }
627
628       if (Item * i  = dynamic_cast<Item*> (this))
629         {
630           Item *pari = dynamic_cast<Item*> (par);
631
632           if (pari && i)
633             {
634               Direction  my_dir = i->break_status_dir () ;
635               if (my_dir!= pari->break_status_dir())
636                 {
637                   Item *newpar =  pari->find_broken_piece (my_dir);
638                   set_parent (newpar, ax);
639                 }
640             }
641         }
642     }
643 }