]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-engraver.cc
new file.
[lilypond.git] / lily / tuplet-engraver.cc
1 /*
2   tuplet-engraver.cc -- implement Tuplet_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "tuplet-bracket.hh"
10 #include "note-column.hh"
11 #include "beam.hh"
12 #include "engraver.hh"
13 #include "spanner.hh"
14
15 #include "translator.icc"
16
17 struct Tuplet_description
18 {
19   Music *music_;
20   Spanner *bracket_;
21   Spanner *number_;
22   Tuplet_description ()
23   {
24     music_ = 0;
25     bracket_ = 0;
26     number_ = 0;
27   }
28 };
29
30 class Tuplet_engraver : public Engraver
31 {
32 public:
33   TRANSLATOR_DECLARATIONS (Tuplet_engraver);
34
35 protected:
36   vector<Tuplet_description> tuplets_;
37   vector<Tuplet_description> stopped_tuplets_;
38   vector<Spanner*> last_tuplets_;
39   DECLARE_ACKNOWLEDGER (note_column);
40   virtual bool try_music (Music *r);
41   virtual void finalize ();
42   void start_translation_timestep ();
43   void process_music ();
44 };
45
46 bool
47 Tuplet_engraver::try_music (Music *music)
48 {
49   if (music->is_mus_type ("tuplet-spanner-event"))
50     {
51       Direction dir = to_dir (music->get_property ("span-direction"));
52       if (dir == START)
53         {
54           Tuplet_description d;
55           d.music_ = music;
56           tuplets_.push_back (d);
57         }
58       if (dir == STOP)
59         {
60           stopped_tuplets_.push_back (tuplets_.back ());
61           tuplets_.pop_back ();
62         }
63       return true;
64     }
65   return false;
66 }
67
68 void
69 Tuplet_engraver::process_music ()
70 {
71   for (vsize i = 0; i < stopped_tuplets_.size (); i++)
72     {
73       bool full_length = to_boolean (get_property ("tupletFullLength"));
74       if (stopped_tuplets_[i].bracket_)
75         {
76           if (full_length)
77             {
78               Item *col = unsmob_item (get_property ("currentMusicalColumn"));
79               
80               stopped_tuplets_[i].bracket_->set_bound (RIGHT, col);
81               stopped_tuplets_[i].number_->set_bound (RIGHT, col);
82             }
83           else if (!stopped_tuplets_[i].bracket_->get_bound (RIGHT))
84             {
85               stopped_tuplets_[i].bracket_->set_bound (RIGHT,
86                                                        stopped_tuplets_[i].bracket_->get_bound (LEFT));
87               stopped_tuplets_[i].number_->set_bound (RIGHT,
88                                                       stopped_tuplets_[i].bracket_->get_bound (LEFT));
89             }
90           // todo: scrap last_tuplets_, use stopped_tuplets_ only.
91           // clear stopped_tuplets_ at start_translation_timestep
92           last_tuplets_.push_back (tuplets_[i].bracket_);
93           last_tuplets_.push_back (tuplets_[i].number_);
94         }
95     }
96   stopped_tuplets_.clear ();
97
98   for (vsize j = tuplets_.size (); j > 0; j--)
99     {
100       /* i goes from size-1 downto 0, inclusively */
101       vsize i = j - 1;
102
103       if (tuplets_[i].bracket_)
104         continue;
105
106       tuplets_[i].bracket_ = make_spanner ("TupletBracket",
107                                            tuplets_[i].music_->self_scm ());
108       tuplets_[i].number_ = make_spanner ("TupletNumber",
109                                           tuplets_[i].music_->self_scm ());
110       tuplets_[i].number_->set_object ("bracket", tuplets_[i].bracket_->self_scm ());
111       tuplets_[i].bracket_->set_object ("tuplet-number", tuplets_[i].number_->self_scm ());
112       
113       if (i < tuplets_.size () - 1 && tuplets_[i + 1].bracket_)
114         Tuplet_bracket::add_tuplet_bracket (tuplets_[i].bracket_, tuplets_[i + 1].bracket_);
115       
116       if (i > 0 && tuplets_[i - 1].bracket_)
117         Tuplet_bracket::add_tuplet_bracket (tuplets_[i - 1].bracket_, tuplets_[i].bracket_);
118
119     }
120 }
121
122 void
123 Tuplet_engraver::acknowledge_note_column (Grob_info inf)
124 {
125   for (vsize j = 0; j < tuplets_.size (); j++)
126     if (tuplets_[j].bracket_)
127       {
128         Item *i = dynamic_cast<Item *> (inf.grob ());
129         Tuplet_bracket::add_column (tuplets_[j].bracket_, i);
130         add_bound_item (tuplets_[j].number_, i);
131       }
132 }
133
134 void
135 Tuplet_engraver::start_translation_timestep ()
136 {
137   last_tuplets_.clear ();
138 }
139
140 void
141 Tuplet_engraver::finalize ()
142 {
143   if (to_boolean (get_property ("tupletFullLength")))
144     {
145       for (vsize i = 0; i < last_tuplets_.size (); i++)
146         {
147           Item *col = unsmob_item (get_property ("currentCommandColumn"));
148           last_tuplets_[i]->set_bound (RIGHT, col);
149         }
150     }
151 }
152
153 Tuplet_engraver::Tuplet_engraver ()
154 {
155 }
156
157 ADD_ACKNOWLEDGER (Tuplet_engraver, note_column);
158 ADD_TRANSLATOR (Tuplet_engraver,
159                 /* doc */ "Catch TupletSpannerEvent and generate appropriate bracket  ",
160                 /* create */ "TupletBracket TupletNumber ",
161                 /* accept */ "tuplet-spanner-event",
162                 /* read */ "tupletFullLength ",
163                 /* write */ "");