]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-engraver.cc
* scm/output-ps.scm (output-scopes): use ly:paper-lookup for font
[lilypond.git] / lily / tie-engraver.cc
1 /*   
2   new-tie-engraver.cc --  implement Tie_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "event.hh"
11 #include "tie.hh"
12 #include "context.hh"
13
14 #include "spanner.hh"
15 #include "tie-column.hh"
16 #include "engraver.hh"
17 #include "item.hh"
18 #include "grob-pitch-tuple.hh"
19 #include "warn.hh"
20 #include "note-head.hh"
21 #include "staff-symbol-referencer.hh"
22
23 /**
24    Manufacture ties.  Acknowledge noteheads, and put them into a
25    priority queue. If we have a TieEvent, connect the notes that finish
26    just at this time, and note that start at this time.
27
28    TODO: Remove the dependency on musical info. We should tie on the
29    basis of position and duration-log of the heads (not of the events).
30
31    TODO: support sparseTies.
32 */
33 class Tie_engraver : public Engraver
34 {
35   Music *event_;
36   Music *last_event_;
37   Link_array<Grob> now_heads_;
38   Link_array<Grob> heads_to_tie_;
39   Link_array<Grob> ties_;
40   
41   Spanner * tie_column_;
42   
43 protected:
44   virtual void stop_translation_timestep ();
45   virtual void start_translation_timestep ();
46   virtual void acknowledge_grob (Grob_info);
47   virtual bool try_music (Music*);
48   virtual void process_music ();
49   void typeset_tie (Grob*);
50 public:
51   TRANSLATOR_DECLARATIONS(Tie_engraver);
52 };
53
54
55
56 Tie_engraver::Tie_engraver ()
57 {
58   event_ = 0;
59   last_event_  = 0;
60   tie_column_ = 0;
61 }
62
63
64 bool
65 Tie_engraver::try_music (Music *mus)
66 {
67   if (mus->is_mus_type ("tie-event"))
68     {
69       event_ = mus;
70     }
71   
72   return true;
73 }
74
75 void
76 Tie_engraver::process_music ()
77 {
78   if (event_)
79     daddy_context_->set_property ("tieMelismaBusy", SCM_BOOL_T);
80 }
81
82 void
83 Tie_engraver::acknowledge_grob (Grob_info i)
84 {
85   if (Note_head::has_interface (i.grob_))
86     {
87       Grob * h  = i.grob_;
88       now_heads_.push (h);
89       for  (int i = heads_to_tie_.size (); i--;)
90         {
91           Grob *th =  heads_to_tie_[i];
92           Music * right_mus = unsmob_music (h->get_property ("cause"));
93           Music * left_mus = unsmob_music (th->get_property ("cause"));
94
95           /*
96             maybe should check positions too.
97            */
98           if (right_mus && left_mus
99               && gh_equal_p (right_mus->get_property ("pitch"),
100                              left_mus->get_property ("pitch")))
101             {
102               Grob * p = make_spanner ("Tie");
103               Tie::set_interface (p); // cannot remove yet!
104           
105               Tie::set_head (p, LEFT, th);
106               Tie::set_head (p, RIGHT, h);
107           
108               ties_.push (p);
109               announce_grob(p, last_event_->self_scm());
110             }
111         }
112
113       if (ties_.size () && ! tie_column_)
114         {
115           tie_column_ = make_spanner ("TieColumn");
116           announce_grob(tie_column_, SCM_EOL);
117         }
118
119       if (tie_column_)
120         for (int i = ties_.size (); i--;)
121           Tie_column::add_tie (tie_column_,ties_ [i]);
122     }
123 }
124
125 void
126 Tie_engraver::start_translation_timestep ()
127 {
128   daddy_context_->set_property ("tieMelismaBusy",
129                               gh_bool2scm (heads_to_tie_.size ()));
130       
131 }
132
133 void
134 Tie_engraver::stop_translation_timestep ()
135 {
136   if (ties_.size ())
137     {
138       heads_to_tie_.clear ();
139       for (int i=0; i<  ties_.size (); i++)
140         {
141           typeset_tie (ties_[i]);
142         }
143
144       ties_.clear();
145       last_event_ = 0;
146       if (tie_column_)
147         {
148           typeset_grob (tie_column_);
149           tie_column_ =0;
150         }
151     }
152   
153   if (event_)
154     {
155       heads_to_tie_ = now_heads_;
156       last_event_ = event_;
157     }
158   event_ = 0;
159   now_heads_.clear ();
160 }
161
162 void
163 Tie_engraver::typeset_tie (Grob *her)
164 {
165   if (! (Tie::head (her,LEFT) && Tie::head (her,RIGHT)))
166     warning (_ ("lonely tie"));
167
168   Direction d = LEFT;
169   Drul_array<Grob *> new_head_drul;
170   new_head_drul[LEFT] = Tie::head (her,LEFT);
171   new_head_drul[RIGHT] = Tie::head (her,RIGHT);  
172   do {
173     if (!Tie::head (her,d))
174       new_head_drul[d] = Tie::head (her, (Direction)-d);
175   } while (flip (&d) != LEFT);
176
177   index_set_cell (her->get_property ("head-pair"), LEFT, new_head_drul[LEFT]->self_scm ());
178   index_set_cell (her->get_property ("head-pair"), RIGHT, new_head_drul[RIGHT]->self_scm ());
179
180   typeset_grob (her);
181 }
182
183
184 ENTER_DESCRIPTION(Tie_engraver,
185 /* descr */       "Generate ties between noteheads of equal pitch.",
186 /* creats*/       "Tie TieColumn",
187 /* accepts */     "tie-event",
188 /* acks  */      "rhythmic-head-interface",
189 /* reads */       "tieMelismaBusy",
190 /* write */       "");