]> git.donarmstrong.com Git - lilypond.git/blob - lily/separating-line-group-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / separating-line-group-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 "separation-item.hh"
23 #include "paper-column.hh"
24 #include "output-def.hh"
25 #include "axis-group-interface.hh"
26 #include "note-spacing.hh"
27 #include "accidental-placement.hh"
28 #include "context.hh"
29 #include "spanner.hh"
30 #include "grob-array.hh"
31 #include "pointer-group-interface.hh"
32
33 #include "translator.icc"
34
35 struct Spacings
36 {
37   Item *staff_spacing_;
38   vector<Item *> note_spacings_;
39
40   Spacings ()
41   {
42     staff_spacing_ = 0;
43   }
44
45   bool is_empty () const
46   {
47     return !staff_spacing_ && !note_spacings_.size ();
48   }
49   void clear ()
50   {
51     staff_spacing_ = 0;
52     note_spacings_.clear ();
53   }
54 };
55
56 class Separating_line_group_engraver : public Engraver
57 {
58 protected:
59   Spacings current_spacings_;
60   Spacings last_spacings_;
61
62   void acknowledge_item (Grob_info);
63   void acknowledge_break_aligned (Grob_info);
64   void stop_translation_timestep ();
65   void start_translation_timestep ();
66
67   vector<Grob *> break_aligned_;
68 public:
69   TRANSLATOR_DECLARATIONS (Separating_line_group_engraver);
70 };
71
72 Separating_line_group_engraver::Separating_line_group_engraver (Context *c)
73   : Engraver (c)
74 {
75 }
76
77 void
78 Separating_line_group_engraver::acknowledge_item (Grob_info i)
79 {
80   Item *it = i.item ();
81
82   if (has_interface<Note_spacing> (it))
83     {
84       current_spacings_.note_spacings_.push_back (it);
85       return;
86     }
87
88   if (Item::is_non_musical (it)
89       && !current_spacings_.staff_spacing_
90       && to_boolean (get_property ("createSpacing")))
91     {
92       Grob *col = unsmob<Grob> (get_property ("currentCommandColumn"));
93
94       current_spacings_.staff_spacing_ = make_item ("StaffSpacing", SCM_EOL);
95       context ()->set_property ("hasStaffSpacing", SCM_BOOL_T);
96
97       Pointer_group_interface::add_grob (current_spacings_.staff_spacing_,
98                                          ly_symbol2scm ("left-items"),
99                                          col);
100
101       if (!last_spacings_.note_spacings_.size ()
102           && last_spacings_.staff_spacing_)
103         {
104           SCM ri = last_spacings_.staff_spacing_->get_object ("right-items");
105           Grob_array *ga = unsmob<Grob_array> (ri);
106           if (!ga)
107             {
108               SCM ga_scm = Grob_array::make_array ();
109               last_spacings_.staff_spacing_->set_object ("right-items", ga_scm);
110               ga = unsmob<Grob_array> (ga_scm);
111             }
112
113           ga->clear ();
114           ga->add (col);
115         }
116     }
117 }
118
119 void
120 Separating_line_group_engraver::acknowledge_break_aligned (Grob_info gi)
121 {
122   break_aligned_.push_back (gi.grob ());
123 }
124
125 void
126 Separating_line_group_engraver::start_translation_timestep ()
127 {
128   context ()->unset_property (ly_symbol2scm ("hasStaffSpacing"));
129 }
130
131 void
132 Separating_line_group_engraver::stop_translation_timestep ()
133 {
134   for (vsize i = 0; i < break_aligned_.size (); i++)
135     {
136       SCM smob = break_aligned_[i]->self_scm ();
137
138       if (Item *sp = current_spacings_.staff_spacing_)
139         Pointer_group_interface::add_grob (sp, ly_symbol2scm ("left-break-aligned"), smob);
140
141       for (vsize j = 0; j < last_spacings_.note_spacings_.size (); j++)
142         Pointer_group_interface::add_grob (last_spacings_.note_spacings_[j],
143                                            ly_symbol2scm ("right-break-aligned"), smob);
144     }
145
146   if (!current_spacings_.is_empty ())
147     last_spacings_ = current_spacings_;
148
149   if (Item *sp = current_spacings_.staff_spacing_)
150     if (Grob *col = unsmob<Grob> (get_property ("currentMusicalColumn")))
151       Pointer_group_interface::add_grob (sp, ly_symbol2scm ("right-items"), col);
152
153   current_spacings_.clear ();
154   break_aligned_.clear ();
155 }
156
157
158 void
159 Separating_line_group_engraver::boot ()
160 {
161   ADD_ACKNOWLEDGER (Separating_line_group_engraver, item);
162   ADD_ACKNOWLEDGER (Separating_line_group_engraver, break_aligned);
163 }
164
165 ADD_TRANSLATOR (Separating_line_group_engraver,
166                 /* doc */
167                 "Generate objects for computing spacing parameters.",
168
169                 /* create */
170                 "StaffSpacing ",
171
172                 /* read */
173                 "createSpacing ",
174
175                 /* write */
176                 "hasStaffSpacing "
177                );