]> git.donarmstrong.com Git - lilypond.git/blob - lily/instrument-name-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / instrument-name-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--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 "align-interface.hh"
21 #include "axis-group-interface.hh"
22 #include "engraver.hh"
23 #include "page-layout-problem.hh"
24 #include "pointer-group-interface.hh"
25 #include "side-position-interface.hh"
26 #include "system.hh"
27 #include "text-interface.hh"
28
29 #include "translator.icc"
30
31 class Instrument_name_engraver : public Engraver
32 {
33 public:
34   TRANSLATOR_DECLARATIONS (Instrument_name_engraver);
35
36 protected:
37   Spanner *text_spanner_;
38
39   SCM long_text_;
40   SCM short_text_;
41
42   vector<Grob *> axis_groups_;
43   vector<Grob *> backup_axis_groups_;
44
45   virtual void finalize ();
46   void acknowledge_axis_group (Grob_info);
47   void process_music ();
48   void start_spanner ();
49   void consider_start_spanner ();
50   void stop_spanner ();
51
52   virtual void derived_mark () const;
53 };
54
55 void
56 Instrument_name_engraver::derived_mark () const
57 {
58   scm_gc_mark (long_text_);
59   scm_gc_mark (short_text_);
60 }
61
62 Instrument_name_engraver::Instrument_name_engraver (Context *c)
63   : Engraver (c)
64 {
65   text_spanner_ = 0;
66
67   long_text_ = SCM_EOL;
68   short_text_ = SCM_EOL;
69 }
70
71 void
72 Instrument_name_engraver::process_music ()
73 {
74   consider_start_spanner ();
75 }
76
77 void
78 Instrument_name_engraver::consider_start_spanner ()
79 {
80   SCM long_text = get_property ("instrumentName");
81   SCM short_text = get_property ("shortInstrumentName");
82
83   if (!(Text_interface::is_markup (long_text)
84         || Text_interface::is_markup (short_text)))
85     {
86       long_text = get_property ("vocalName");
87       short_text = get_property ("shortVocalName");
88     }
89
90   if ((Text_interface::is_markup (long_text)
91        || Text_interface::is_markup (short_text))
92       && (!text_spanner_
93           || !scm_is_eq (short_text_, short_text)
94           || !scm_is_eq (long_text_, long_text)))
95     {
96       if (text_spanner_)
97         stop_spanner ();
98
99       short_text_ = short_text;
100       long_text_ = long_text;
101
102       start_spanner ();
103     }
104 }
105
106 void
107 Instrument_name_engraver::start_spanner ()
108 {
109   text_spanner_ = make_spanner ("InstrumentName", SCM_EOL);
110
111   Grob *col = unsmob<Grob> (get_property ("currentCommandColumn"));
112   text_spanner_->set_bound (LEFT, col);
113   text_spanner_->set_property ("text", short_text_);
114   text_spanner_->set_property ("long-text", long_text_);
115
116   /*
117     UGH, should handle this in Score_engraver.
118   */
119   Grob *system = unsmob<Grob> (get_property ("rootSystem"));
120   if (system)
121     Axis_group_interface::add_element (system, text_spanner_);
122   else
123     text_spanner_->programming_error ("cannot find root system");
124 }
125
126 void
127 Instrument_name_engraver::acknowledge_axis_group (Grob_info info)
128 {
129   if (dynamic_cast<Spanner *> (info.grob ())
130       && Axis_group_interface::has_axis (info.grob (), Y_AXIS)
131
132       /* ugh. */
133
134       && !info.grob ()->internal_has_interface (ly_symbol2scm ("dynamic-interface"))
135       && !info.grob ()->internal_has_interface (ly_symbol2scm ("piano-pedal-interface"))
136       && !info.grob ()->internal_has_interface (ly_symbol2scm ("volta-interface"))
137       && (!has_interface<Align_interface> (info.grob ())))
138     {
139       if (Page_layout_problem::is_spaceable (info.grob ()))
140         axis_groups_.push_back (info.grob ());
141       else
142         // By default, don't include non-spaceable staves in the
143         // support of an instrument name.  However, if the only staves
144         // are non-spaceable, we'll fall back to using them.
145         backup_axis_groups_.push_back (info.grob ());
146     }
147 }
148
149 void
150 Instrument_name_engraver::finalize ()
151 {
152   if (text_spanner_)
153     stop_spanner ();
154 }
155
156 void
157 Instrument_name_engraver::stop_spanner ()
158 {
159   if (axis_groups_.empty ())
160     axis_groups_ = backup_axis_groups_;
161
162   for (vsize i = 0; i < axis_groups_.size (); i++)
163     Pointer_group_interface::add_grob (text_spanner_,
164                                        ly_symbol2scm ("elements"),
165                                        axis_groups_[i]);
166
167   text_spanner_->set_bound (RIGHT,
168                             unsmob<Grob> (get_property ("currentCommandColumn")));
169
170   Pointer_group_interface::set_ordered (text_spanner_,
171                                         ly_symbol2scm ("elements"),
172                                         false);
173
174   text_spanner_ = 0;
175 }
176
177
178 void
179 Instrument_name_engraver::boot ()
180 {
181   ADD_ACKNOWLEDGER (Instrument_name_engraver, axis_group);
182 }
183
184 ADD_TRANSLATOR (Instrument_name_engraver,
185                 /* doc */
186                 "Create a system start text for instrument or vocal names.",
187
188                 /* create */
189                 "InstrumentName ",
190
191                 /* read */
192                 "currentCommandColumn "
193                 "instrumentName "
194                 "shortInstrumentName "
195                 "shortVocalName "
196                 "vocalName ",
197
198                 /* write */
199                 ""
200                );