]> git.donarmstrong.com Git - lilypond.git/blob - lily/cue-clef-engraver.cc
Clef support for cue notes
[lilypond.git] / lily / cue-clef-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5                            Mats Bengtsson <matsb@s3.kth.se>
6   Copyright (C) 2010 Reinhold Kainhofer <reinhold@kainhofer.com>
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <cctype>
23 using namespace std;
24
25 #include "item.hh"
26 #include "context.hh"
27 #include "bar-line.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "engraver.hh"
30 #include "direction.hh"
31 #include "side-position-interface.hh"
32 #include "warn.hh"
33 #include "international.hh"
34
35 #include "translator.icc"
36
37 class Cue_clef_engraver : public Engraver
38 {
39 public:
40   TRANSLATOR_DECLARATIONS (Cue_clef_engraver);
41
42 protected:
43   void stop_translation_timestep ();
44   void process_music ();
45   DECLARE_ACKNOWLEDGER (bar_line);
46
47   virtual void derived_mark () const;
48 private:
49   Item *clef_;
50   Item *octavate_;
51
52   SCM prev_glyph_;
53   SCM prev_cpos_;
54   SCM prev_octavation_;
55   void create_clef ();
56   void create_end_clef ();
57   void set_glyph ();
58   void inspect_clef_properties ();
59   void create_octavate_eight (SCM oct);
60 };
61
62 void
63 Cue_clef_engraver::derived_mark () const
64 {
65   scm_gc_mark (prev_octavation_);
66   scm_gc_mark (prev_cpos_);
67   scm_gc_mark (prev_glyph_);
68 }
69
70 Cue_clef_engraver::Cue_clef_engraver ()
71 {
72   clef_ = 0;
73   octavate_ = 0;
74
75   prev_octavation_ = prev_cpos_ = prev_glyph_ = SCM_EOL;
76 }
77
78 void
79 Cue_clef_engraver::set_glyph ()
80 {
81   SCM glyph_sym = ly_symbol2scm ("glyph");
82   SCM basic = ly_symbol2scm ("CueClef");
83   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
84   execute_pushpop_property (context (), basic, glyph_sym, get_property ("cueClefGlyph"));
85
86   basic = ly_symbol2scm ("CueEndClef");
87   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
88   execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
89 }
90
91 /**
92    Generate a clef at the start of a measure. (when you see a Bar,
93    ie. a breakpoint)
94 */
95 void
96 Cue_clef_engraver::acknowledge_bar_line (Grob_info info)
97 {
98   Item *item = info.item ();
99   if (item && scm_is_string (get_property ("cueClefGlyph")))
100     create_clef ();
101 }
102
103 void
104 Cue_clef_engraver::create_octavate_eight (SCM oct)
105 {
106   if (scm_is_number (oct) && scm_to_int (oct))
107     {
108       Item *g = make_item ("OctavateEight", SCM_EOL);
109
110       int abs_oct = scm_to_int (oct);
111       int dir = sign (abs_oct);
112       abs_oct = abs (abs_oct) + 1;
113
114       SCM txt = scm_number_to_string (scm_from_int (abs_oct),
115                                       scm_from_int (10));
116
117       g->set_property ("text",
118                        scm_list_n (ly_lily_module_constant ("vcenter-markup"),
119                                    txt, SCM_UNDEFINED));
120       Side_position_interface::add_support (g, clef_);
121
122       g->set_parent (clef_, Y_AXIS);
123       g->set_parent (clef_, X_AXIS);
124       g->set_property ("direction", scm_from_int (dir));
125
126       // Inherit the break-visibility from the clef!
127       SCM vis = clef_->get_property ("break-visibility");
128       if (vis && g)
129         g->set_property ("break-visibility", vis);
130
131       octavate_ = g;
132     }
133 }
134
135 void
136 Cue_clef_engraver::create_clef ()
137 {
138   if (!clef_)
139     {
140       Item *c = make_item ("CueClef", SCM_EOL);
141
142       clef_ = c;
143       SCM cpos = get_property ("cueClefPosition");
144       if (scm_is_number (cpos))
145         clef_->set_property ("staff-position", cpos);
146
147       create_octavate_eight (get_property ("cueClefOctavation"));
148     }
149 }
150
151 void
152 Cue_clef_engraver::create_end_clef ()
153 {
154   if (!clef_)
155     {
156       clef_ = make_item ("CueEndClef", SCM_EOL);
157       SCM cpos = get_property ("clefPosition");
158       if (scm_is_number (cpos))
159         clef_->set_property ("staff-position", cpos);
160
161       create_octavate_eight (get_property ("clefOctavation"));
162     }
163 }
164
165 void
166 Cue_clef_engraver::process_music ()
167 {
168   inspect_clef_properties ();
169 }
170
171 void
172 Cue_clef_engraver::inspect_clef_properties ()
173 {
174   SCM glyph = get_property ("cueClefGlyph");
175   SCM clefpos = get_property ("cueClefPosition");
176   SCM octavation = get_property ("cueClefOctavation");
177
178   if (scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
179       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
180       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
181       || to_boolean (force_clef))
182     {
183       set_glyph ();
184       if (scm_is_string (glyph))
185         {
186           create_clef ();
187           if (clef_)
188             clef_->set_property ("non-default", SCM_BOOL_T);
189         }
190       else
191         create_end_clef ();
192
193       prev_cpos_ = clefpos;
194       prev_glyph_ = glyph;
195       prev_octavation_ = octavation;
196     }
197
198 }
199
200 void
201 Cue_clef_engraver::stop_translation_timestep ()
202 {
203   if (clef_)
204     {
205       SCM vis = 0;
206       if (to_boolean (clef_->get_property ("non-default")))
207         vis = get_property ("explicitCueClefVisibility");
208
209       if (vis)
210         {
211           clef_->set_property ("break-visibility", vis);
212           if (octavate_)
213             octavate_->set_property ("break-visibility", vis);
214         }
215
216       clef_ = 0;
217       octavate_ = 0;
218     }
219 }
220
221 ADD_ACKNOWLEDGER (Cue_clef_engraver, bar_line);
222 ADD_TRANSLATOR (Cue_clef_engraver,
223                 /* doc */
224                 "Determine and set reference point for pitches in cued voices.",
225
226                 /* create */
227                 "CueClef "
228                 "CueEndClef "
229                 "OctavateEight ",
230
231                 /* read */
232                 "cueClefGlyph "
233                 "cueClefOctavation "
234                 "cueClefPosition "
235                 "explicitCueClefVisibility "
236                 "middleCCuePosition "
237                 "clefOctavation ",
238
239                 /* write */
240                 ""
241                 );