]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-spacing-engraver.cc
Release: bump VERSION_DEVEL.
[lilypond.git] / lily / note-spacing-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2014 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   DECLARE_ACKNOWLEDGER (rhythmic_grob);
44   DECLARE_ACKNOWLEDGER (note_column);
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 ()
59 {
60   spacing_ = 0;
61   last_spacing_ = 0;
62 }
63
64 void
65 Note_spacing_engraver::add_spacing_item (Grob *g)
66 {
67   if (!spacing_)
68     {
69       spacing_ = make_item ("NoteSpacing", g->self_scm ());
70     }
71
72   if (spacing_)
73     {
74       Pointer_group_interface::add_grob (spacing_,
75                                          ly_symbol2scm ("left-items"),
76                                          g);
77
78       if (last_spacing_)
79         Pointer_group_interface::add_grob (last_spacing_,
80                                            ly_symbol2scm ("right-items"),
81                                            g);
82     }
83 }
84
85 void
86 Note_spacing_engraver::acknowledge_note_column (Grob_info gi)
87 {
88   add_spacing_item (gi.grob ());
89 }
90
91 void
92 Note_spacing_engraver::acknowledge_rhythmic_grob (Grob_info gi)
93 {
94   add_spacing_item (gi.grob ());
95 }
96
97 void
98 Note_spacing_engraver::finalize ()
99 {
100   Context *parent = context ()->get_parent_context ();
101   Grob *last_spacing = last_spacings_[parent];
102
103   if (last_spacing
104       && !Grob_array::unsmob (last_spacing->get_object ("right-items")))
105     {
106       Grob *col = Grob::unsmob (get_property ("currentCommandColumn"));
107
108       Pointer_group_interface::add_grob (last_spacing,
109                                          ly_symbol2scm ("right-items"),
110                                          col);
111     }
112 }
113
114 void
115 Note_spacing_engraver::stop_translation_timestep ()
116 {
117   Context *parent = context ()->get_parent_context ();
118   Grob *last_spacing = last_spacings_[parent];
119
120   if (last_spacing
121       && to_boolean (get_property ("hasStaffSpacing")))
122     {
123       Grob *col = Grob::unsmob (get_property ("currentCommandColumn"));
124       Pointer_group_interface::add_grob (last_spacing,
125                                          ly_symbol2scm ("right-items"),
126                                          col);
127     }
128
129   if (spacing_)
130     {
131       last_spacings_[parent] = spacing_;
132       last_spacing_ = spacing_;
133       spacing_ = 0;
134     }
135
136 }
137
138 ADD_ACKNOWLEDGER (Note_spacing_engraver, note_column);
139 ADD_ACKNOWLEDGER (Note_spacing_engraver, rhythmic_grob);
140
141 ADD_TRANSLATOR (Note_spacing_engraver,
142                 /* doc */
143                 "Generate @code{NoteSpacing}, an object linking horizontal"
144                 " lines for use in spacing.",
145
146                 /* create */
147                 "NoteSpacing ",
148
149                 /* read */
150                 "",
151
152                 /* write */
153                 ""
154                );