]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-spacing-engraver.cc
PO: fetch eo form FTP
[lilypond.git] / lily / note-spacing-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "grob-array.hh"
23 #include "context.hh"
24 #include "item.hh"
25 #include "pointer-group-interface.hh"
26
27 #include <map>
28
29 #include "translator.icc"
30
31 class Note_spacing_engraver : public Engraver
32 {
33   typedef map <Context *, Grob *> Last_spacing_map;
34   Last_spacing_map last_spacings_;
35   Grob *last_spacing_;
36
37   Grob *spacing_;
38
39   void add_spacing_item (Grob *);
40   TRANSLATOR_DECLARATIONS (Note_spacing_engraver);
41 protected:
42
43   void acknowledge_rhythmic_grob (Grob_info);
44   void acknowledge_note_column (Grob_info);
45   void stop_translation_timestep ();
46   virtual void finalize ();
47   virtual void derived_mark () const;
48 };
49
50 void
51 Note_spacing_engraver::derived_mark () const
52 {
53   for (Last_spacing_map::const_iterator i = last_spacings_.begin ();
54        i != last_spacings_.end (); i++)
55     scm_gc_mark (i->first->self_scm ());
56 }
57
58 Note_spacing_engraver::Note_spacing_engraver (Context *c)
59   : Engraver (c)
60 {
61   spacing_ = 0;
62   last_spacing_ = 0;
63 }
64
65 void
66 Note_spacing_engraver::add_spacing_item (Grob *g)
67 {
68   if (!spacing_)
69     {
70       spacing_ = make_item ("NoteSpacing", g->self_scm ());
71     }
72
73   if (spacing_)
74     {
75       Pointer_group_interface::add_grob (spacing_,
76                                          ly_symbol2scm ("left-items"),
77                                          g);
78
79       if (last_spacing_)
80         Pointer_group_interface::add_grob (last_spacing_,
81                                            ly_symbol2scm ("right-items"),
82                                            g);
83     }
84 }
85
86 void
87 Note_spacing_engraver::acknowledge_note_column (Grob_info gi)
88 {
89   add_spacing_item (gi.grob ());
90 }
91
92 void
93 Note_spacing_engraver::acknowledge_rhythmic_grob (Grob_info gi)
94 {
95   add_spacing_item (gi.grob ());
96 }
97
98 void
99 Note_spacing_engraver::finalize ()
100 {
101   Context *parent = context ()->get_parent_context ();
102   Grob *last_spacing = last_spacings_[parent];
103
104   if (last_spacing
105       && !unsmob<Grob_array> (last_spacing->get_object ("right-items")))
106     {
107       Grob *col = unsmob<Grob> (get_property ("currentCommandColumn"));
108
109       Pointer_group_interface::add_grob (last_spacing,
110                                          ly_symbol2scm ("right-items"),
111                                          col);
112     }
113 }
114
115 void
116 Note_spacing_engraver::stop_translation_timestep ()
117 {
118   Context *parent = context ()->get_parent_context ();
119   Grob *last_spacing = last_spacings_[parent];
120
121   if (last_spacing
122       && to_boolean (get_property ("hasStaffSpacing")))
123     {
124       Grob *col = unsmob<Grob> (get_property ("currentCommandColumn"));
125       Pointer_group_interface::add_grob (last_spacing,
126                                          ly_symbol2scm ("right-items"),
127                                          col);
128     }
129
130   if (spacing_)
131     {
132       last_spacings_[parent] = spacing_;
133       last_spacing_ = spacing_;
134       spacing_ = 0;
135     }
136
137 }
138
139
140 void
141 Note_spacing_engraver::boot ()
142 {
143   ADD_ACKNOWLEDGER (Note_spacing_engraver, note_column);
144   ADD_ACKNOWLEDGER (Note_spacing_engraver, rhythmic_grob);
145 }
146
147 ADD_TRANSLATOR (Note_spacing_engraver,
148                 /* doc */
149                 "Generate @code{NoteSpacing}, an object linking horizontal"
150                 " lines for use in spacing.",
151
152                 /* create */
153                 "NoteSpacing ",
154
155                 /* read */
156                 "",
157
158                 /* write */
159                 ""
160                );