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