2 ambitus.cc -- implement Ambitus
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2004 Juergen Reuter <reuter@ipd.uka.de>
9 #include "staff-symbol-referencer.hh"
13 #include "note-head.hh"
15 #include "font-interface.hh"
16 #include "output-def.hh"
22 This does 3 things at one:
28 It confuses interpretation & formatting.
35 * TODO: note-head collision handling
37 * TODO: accidentals collision handling
39 * TODO: alternative representation: adding the ambitus as text script
40 * to the instrument name (e.g. "Soprano (c^1 - f^2)").
42 * FIXME: Accidentals are too close at the note heads (it seems that
43 * the extent of the ledger lines is ignored).
45 * TODO: If (depending on breakAlignOrder) ambitus is put behind
46 * key-signature, then do not repeat accidentals that already appear
47 * in the key signature.
49 * FIXME: A staff containing more than a single context will result in
50 * multiple ambitus grobs per staff. This is basically ok, but there is
51 * currently no proper collision handling for this case.
53 * TODO: make ignore_octave and force_accidental of function
54 * number_accidentals accessible via grob properties.
58 Given a pitch and a key_signature, decide what accidentals to show.
60 Possible return values:
62 0: do not show any accidental
63 1: show pitch->alteration_ only
64 2: show pitch->get_alteration, preceded by a natural sign
66 UGH: code duplication! See accidental-engraver.
70 number_accidentals (SCM key_signature, Pitch *pitch,
71 bool ignore_octave_b, bool force_accidental)
73 int notename = pitch->get_notename ();
74 int octave = pitch->get_octave ();
75 int alteration = pitch->get_alteration ();
77 if (force_accidental) // ignore key signature
82 scm_display (key_signature, scm_current_output_port ());
87 prev = ly_assoc_cdr (scm_int2num (notename), key_signature);
89 prev = scm_assoc (scm_cons (scm_int2num (octave), scm_int2num (notename)),
92 /* should really be true unless prev == SCM_BOOL_F */
93 if (ly_c_pair_p (prev) && ly_c_pair_p (ly_cdr (prev)))
95 prev = scm_cons (ly_car (prev), ly_cadr (prev));
98 /* If an accidental was not found */
99 if (prev == SCM_BOOL_F)
100 prev = scm_assoc (scm_int2num (notename), key_signature);
102 SCM prev_acc = (prev == SCM_BOOL_F) ? scm_int2num (0) : ly_cdr (prev);
103 int sig_alteration = ly_c_number_p (prev_acc) ? ly_scm2int (prev_acc) : 0;
105 if (alteration == sig_alteration) // no accidental at all needed
108 if ((alteration == 0) && (sig_alteration != 0)) // need ordinary natural
111 if (sig_alteration == 0) // use pitch's alteration
120 add_accidentals (Item *me, Stencil *head, int num_acc,
121 Pitch *pitch, String accidentals_style, Real yoffs)
125 if (pitch->get_alteration ())
127 Stencil accidental (Font_interface::get_default_font (me)->
128 find_by_name (String ("accidentals-") +
130 to_string (pitch->get_alteration ())));
131 accidental.translate_axis (yoffs, Y_AXIS);
132 head->add_at_edge (X_AXIS, LEFT, accidental, 0.1, 0);
136 Stencil natural (Font_interface::get_default_font (me)->
137 find_by_name (String ("accidentals-") +
140 natural.translate_axis (yoffs, Y_AXIS);
141 head->add_at_edge (X_AXIS, LEFT, natural, 0.1, 0);
145 MAKE_SCHEME_CALLBACK (Ambitus,print,1);
147 Ambitus::print (SCM smob)
149 Item *me = (Item*) unsmob_grob (smob);
152 SCM scm_note_head_style = me->get_property ("note-head-style");
153 String note_head_style;
154 if (ly_c_symbol_p (scm_note_head_style))
156 String note_head_style =
157 ly_symbol2string (scm_note_head_style);
161 note_head_style = String ("noteheads-2");
163 if (Font_interface::get_default_font (me)->find_by_name (note_head_style).is_empty ())
165 String message = "Ambitus: no such note head: `" + note_head_style + "'";
166 me->warning (_ (message.to_str0 ()));
171 FIXME: Use positions.
174 Slice posns = get_positions(me);
177 p_max = posns[RIGHT];
181 Font_interface::get_default_font (me)->find_by_name (note_head_style);
182 head_min.translate_axis (0.5*p_min, Y_AXIS);
184 Font_interface::get_default_font (me)->find_by_name (note_head_style);
185 head_max.translate_axis (0.5*p_max, Y_AXIS);
188 if (to_boolean (me->get_property ("join-heads")) &&
189 ((p_max - p_min) >= 3))
191 Real linethickness = me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
192 Real blotdiameter = me->get_paper ()->get_dimension (ly_symbol2scm ("blotdiameter"));
193 Interval x_extent = 0.5 * Interval (-linethickness, +linethickness);
194 Interval y_extent = 0.5 * Interval (p_min + 1.35, p_max - 1.35);
195 Box line_box (x_extent, y_extent);
196 Stencil line = Lookup::round_filled_box (line_box, blotdiameter);
197 line.translate_axis (0.5 * head_min.extent (X_AXIS).length (), X_AXIS);
198 stencil.add_stencil (line);
203 SCM key_signature = me->get_property ("key-signature");
204 SCM scm_accidentals_style = me->get_property ("accidentals-style");
205 String accidentals_style;
206 if (ly_c_symbol_p (scm_accidentals_style))
209 ly_symbol2string (scm_accidentals_style);
213 accidentals_style = String ("");
217 Pitch *pitch_min = unsmob_pitch (me->get_property ("pitch-min"));
218 Pitch *pitch_max = unsmob_pitch (me->get_property ("pitch-max"));
219 num_acc = number_accidentals (key_signature, pitch_min, true, false);
220 add_accidentals (me, &head_min, num_acc, pitch_min,
221 accidentals_style, 0.5 * p_min);
222 num_acc = number_accidentals (key_signature, pitch_max, true, false);
223 add_accidentals (me, &head_max, num_acc, pitch_max,
224 accidentals_style, 0.5 * p_max);
227 stencil.add_stencil (head_min);
228 stencil.add_stencil (head_max);
230 return stencil.smobbed_copy ();
233 ADD_INTERFACE (Ambitus, "ambitus-interface",
234 "An object that represents the pitch range of a voice.",
235 "c0-position pitch-min pitch-max accidentals note-head-style accidentals-style join-heads");