]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / system-start-delimiter-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--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 #include "output-def.hh"
22 #include "paper-column.hh"
23 #include "pointer-group-interface.hh"
24 #include "side-position-interface.hh"
25 #include "spanner.hh"
26 #include "staff-symbol.hh"
27 #include "system-start-delimiter.hh"
28
29 #include "translator.icc"
30
31 struct Bracket_nesting_node
32 {
33 public:
34   virtual ~Bracket_nesting_node () {}
35   virtual bool add_staff (Grob *) { return false; }
36   virtual void add_support (Grob *) {}
37   virtual void set_bound (Direction, Grob *) {}
38   virtual void set_nesting_support (Grob *) {}
39   virtual void create_grobs (Engraver *, SCM) {}
40 };
41
42 struct Bracket_nesting_group : public Bracket_nesting_node
43 {
44   Spanner *delimiter_;
45   vector<Bracket_nesting_node *> children_;
46   SCM symbol_;
47
48   void from_list (SCM);
49   virtual void add_support (Grob *grob);
50   virtual bool add_staff (Grob *grob);
51   virtual void set_nesting_support (Grob *);
52   virtual void set_bound (Direction, Grob *grob);
53   virtual void create_grobs (Engraver *, SCM);
54   ~Bracket_nesting_group ();
55   Bracket_nesting_group ();
56 };
57
58 struct Bracket_nesting_staff : public Bracket_nesting_node
59 {
60   Grob *staff_;
61
62   Bracket_nesting_staff (Grob *s) { staff_ = s; }
63   virtual bool add_staff (Grob *);
64 };
65
66 Bracket_nesting_group::Bracket_nesting_group ()
67 {
68   symbol_ = SCM_EOL;
69   delimiter_ = 0;
70 }
71
72 bool
73 Bracket_nesting_staff::add_staff (Grob *g)
74 {
75   if (!staff_)
76     {
77       staff_ = g;
78       return true;
79     }
80   return false;
81 }
82
83 void
84 Bracket_nesting_group::create_grobs (Engraver *engraver, SCM default_type)
85 {
86   SCM type = scm_is_symbol (symbol_) ? symbol_ : default_type;
87   delimiter_ = engraver->make_spanner (ly_symbol2string (type).c_str (),
88                                        SCM_EOL);
89
90   for (vsize i = 0; i < children_.size (); i++)
91     children_[i]->create_grobs (engraver, default_type);
92 }
93
94 void
95 Bracket_nesting_group::add_support (Grob *g)
96 {
97   Side_position_interface::add_support (g, delimiter_);
98   for (vsize i = 0; i < children_.size (); i++)
99     children_[i]->add_support (g);
100 }
101
102 Bracket_nesting_group::~Bracket_nesting_group ()
103 {
104   junk_pointers (children_);
105 }
106
107 void
108 Bracket_nesting_group::set_bound (Direction d, Grob *g)
109 {
110   delimiter_->set_bound (d, g);
111   for (vsize i = 0; i < children_.size (); i++)
112     children_[i]->set_bound (d, g);
113 }
114
115 void
116 Bracket_nesting_group::set_nesting_support (Grob *parent)
117 {
118   if (parent)
119     Side_position_interface::add_support (delimiter_, parent);
120
121   for (vsize i = 0; i < children_.size (); i++)
122     children_[i]->set_nesting_support (delimiter_);
123 }
124
125 void
126 Bracket_nesting_group::from_list (SCM x)
127 {
128   for (SCM s = x; scm_is_pair (s); s = scm_cdr (s))
129     {
130       SCM entry = scm_car (s);
131       if (scm_is_pair (entry))
132         {
133           Bracket_nesting_group *node = new Bracket_nesting_group;
134           node->from_list (entry);
135           children_.push_back (node);
136         }
137       else if (scm_is_eq (entry, ly_symbol2scm ("SystemStartBrace"))
138                || scm_is_eq (entry, ly_symbol2scm ("SystemStartBracket"))
139                || scm_is_eq (entry, ly_symbol2scm ("SystemStartBar"))
140                || scm_is_eq (entry, ly_symbol2scm ("SystemStartSquare")))
141         symbol_ = entry;
142       else
143         children_.push_back (new Bracket_nesting_staff (0));
144     }
145 }
146
147 bool
148 Bracket_nesting_group::add_staff (Grob *grob)
149 {
150   for (vsize i = 0; i < children_.size (); i++)
151     {
152       if (children_[i]->add_staff (grob))
153         {
154           Pointer_group_interface::add_grob (delimiter_,
155                                              ly_symbol2scm ("elements"), grob);
156           return true;
157         }
158     }
159   return false;
160 }
161
162 /****************/
163
164 class System_start_delimiter_engraver : public Engraver
165 {
166 public:
167   TRANSLATOR_DECLARATIONS (System_start_delimiter_engraver);
168
169 protected:
170   Bracket_nesting_group *nesting_;
171
172   void acknowledge_system_start_delimiter (Grob_info);
173   void acknowledge_staff_symbol (Grob_info);
174
175   void process_music ();
176   virtual void finalize ();
177 };
178
179 System_start_delimiter_engraver::System_start_delimiter_engraver (Context *c)
180   : Engraver (c)
181 {
182   nesting_ = 0;
183 }
184
185 void
186 System_start_delimiter_engraver::process_music ()
187 {
188   if (!nesting_)
189     {
190       nesting_ = new Bracket_nesting_group ();
191       SCM hierarchy = get_property ("systemStartDelimiterHierarchy");
192       SCM delimiter_name = get_property ("systemStartDelimiter");
193
194       nesting_->from_list (hierarchy);
195       nesting_->create_grobs (this, delimiter_name);
196       nesting_->set_bound (LEFT,
197                            unsmob<Grob> (get_property ("currentCommandColumn")));
198     }
199 }
200
201 void
202 System_start_delimiter_engraver::finalize ()
203 {
204   if (nesting_)
205     {
206       nesting_->set_bound (RIGHT,
207                            unsmob<Grob> (get_property ("currentCommandColumn")));
208       nesting_->set_nesting_support (0);
209
210       delete nesting_;
211     }
212 }
213
214 void
215 System_start_delimiter_engraver::acknowledge_staff_symbol (Grob_info inf)
216 {
217   Grob *staff = inf.grob ();
218   bool succ = nesting_->add_staff (staff);
219
220   if (!succ)
221     {
222       nesting_->children_.push_back (new Bracket_nesting_staff (0));
223       nesting_->add_staff (staff);
224     }
225 }
226
227 void
228 System_start_delimiter_engraver::acknowledge_system_start_delimiter (Grob_info inf)
229 {
230   nesting_->add_support (inf.grob ());
231 }
232
233
234 void
235 System_start_delimiter_engraver::boot ()
236 {
237   ADD_ACKNOWLEDGER (System_start_delimiter_engraver, staff_symbol);
238   ADD_ACKNOWLEDGER (System_start_delimiter_engraver, system_start_delimiter);
239 }
240
241 ADD_TRANSLATOR (System_start_delimiter_engraver,
242                 /* doc */
243                 "Create a system start delimiter (i.e., a"
244                 " @code{SystemStartBar}, @code{SystemStartBrace},"
245                 " @code{SystemStartBracket} or @code{SystemStartSquare}"
246                 " spanner).",
247
248                 /* create */
249                 "SystemStartSquare "
250                 "SystemStartBrace "
251                 "SystemStartBracket "
252                 "SystemStartBar ",
253
254                 /* read */
255                 "systemStartDelimiter "
256                 "systemStartDelimiterHierarchy "
257                 "currentCommandColumn ",
258
259                 /* write */
260                 ""
261                );