2 ambitus.cc -- implement Ambitus
4 source file of the GNU LilyPond music typesetter
6 (C) 2002 Juergen Reuter <reuter@ipd.uka.de>
9 #include "staff-symbol-referencer.hh"
12 #include "molecule.hh"
13 #include "note-head.hh"
15 #include "font-interface.hh"
16 #include "paper-def.hh"
20 * TODO: note-head collision handling
22 * TODO: accidentals collision handling
24 * TODO: alternative representation: adding the ambitus as text script
25 * to the instrument name (e.g. "Soprano (c^1 - f^2)").
27 * FIXME: Accidentals are too close at the note heads (it seems that
28 * the extent of the ledger lines is ignored).
30 * TODO: If (depending on breakAlignOrder) ambitus is put behind
31 * key-signature, then do not repeat accidentals that already appear
32 * in the key signature.
34 * FIXME: A staff containing more than a single context will result in
35 * multiple ambitus grobs per staff. This is basically ok, but there is
36 * currently no proper collision handling for this case.
38 * TODO: make ignore_octave and force_accidental of function
39 * number_accidentals accessible via grob properties.
43 * Given a pitch and a key_signature, decide what accidentals to show.
45 * Possible return values:
47 * 0: do not show any accidental
48 * 1: show pitch->alteration_ only
49 * 2: show pitch->alteration_, preceded by a natural sign
52 number_accidentals (SCM key_signature, Pitch *pitch,
53 bool ignore_octave_b, bool force_accidental)
55 int notename = pitch->notename_;
56 int octave = pitch->octave_;
57 int alteration = pitch->alteration_;
59 if (force_accidental) // ignore key signature
63 scm_display (key_signature, scm_current_output_port ());
68 prev = ly_assoc_cdr (gh_int2scm (notename), key_signature);
70 prev = gh_assoc (gh_cons (gh_int2scm (octave), gh_int2scm (notename)),
73 /* should really be true unless prev == SCM_BOOL_F */
74 if (gh_pair_p (prev) && gh_pair_p (ly_cdr (prev)))
76 prev = gh_cons (ly_car (prev), ly_cadr (prev));
79 /* If an accidental was not found */
80 if (prev == SCM_BOOL_F)
81 prev = gh_assoc (gh_int2scm (notename), key_signature);
83 SCM prev_acc = (prev == SCM_BOOL_F) ? gh_int2scm (0) : ly_cdr (prev);
84 int sig_alteration = gh_number_p (prev_acc) ? gh_scm2int (prev_acc) : 0;
86 if (alteration == sig_alteration) // no accidental at all needed
89 if ((alteration == 0) && (sig_alteration != 0)) // need ordinary natural
92 if (sig_alteration == 0) // use pitch's alteration
99 add_accidentals (Item *me, Molecule *head, int num_acc,
100 Pitch *pitch, String accidentals_style, Real yoffs)
104 if (pitch->alteration_)
106 Molecule accidental (Font_interface::get_default_font (me)->
107 find_by_name (String ("accidentals-") +
109 to_string (pitch->alteration_)));
110 accidental.translate_axis (yoffs, Y_AXIS);
111 head->add_at_edge (X_AXIS, LEFT, accidental, 0.1);
115 Molecule natural (Font_interface::get_default_font (me)->
116 find_by_name (String ("accidentals-") +
119 natural.translate_axis (yoffs, Y_AXIS);
120 head->add_at_edge (X_AXIS, LEFT, natural, 0.1);
124 MAKE_SCHEME_CALLBACK (Ambitus,brew_molecule,1);
126 Ambitus::brew_molecule (SCM smob)
128 Item *me = (Item *)unsmob_grob (smob);
129 Molecule molecule = Molecule ();
131 SCM scm_note_head_style = me->get_grob_property ("note-head-style");
132 String note_head_style;
133 if (gh_symbol_p (scm_note_head_style))
135 String note_head_style =
136 ly_scm2string (scm_symbol_to_string (scm_note_head_style));
140 note_head_style = String ("noteheads-2");
142 if (Font_interface::get_default_font (me)->find_by_name (note_head_style).empty_b ())
144 String message = "Ambitus: no such note head: `" + note_head_style + "'";
145 me->warning (_ (message.to_str0 ()));
150 Pitch *pitch_min = unsmob_pitch (me->get_grob_property ("pitch-min"));
153 me->programming_error("Ambitus: pitch_min undefined; assuming 0");
158 p_min = pitch_min->steps ();
160 Pitch *pitch_max = unsmob_pitch (me->get_grob_property ("pitch-max"));
163 me->programming_error("Ambitus: pitch_max undefined; assuming 0");
168 p_max = pitch_max->steps ();
172 me->programming_error ("Ambitus: reverse range");
175 SCM c0 = me->get_grob_property ("centralCPosition");
176 if (gh_number_p (c0))
178 p_min += gh_scm2int (c0);
179 p_max += gh_scm2int (c0);
184 Font_interface::get_default_font (me)->find_by_name (note_head_style);
185 head_min.translate_axis (0.5*p_min, Y_AXIS);
187 Font_interface::get_default_font (me)->find_by_name (note_head_style);
188 head_max.translate_axis (0.5*p_max, Y_AXIS);
191 if (to_boolean (me->get_grob_property ("join-heads")) &&
192 ((p_max - p_min) >= 3))
194 Real linethickness = me->get_paper ()->get_var ("linethickness");
195 Real blotdiameter = me->get_paper ()->get_var ("blotdiameter");
196 Interval x_extent = 0.5 * Interval (-linethickness, +linethickness);
197 Interval y_extent = 0.5 * Interval (p_min + 1.35, p_max - 1.35);
198 Box line_box (x_extent, y_extent);
199 Molecule line = Lookup::roundfilledbox (line_box, blotdiameter);
200 line.translate_axis (0.5 * head_min.extent (X_AXIS).length (), X_AXIS);
201 molecule.add_molecule (line);
205 Interval hd = head_min.extent (X_AXIS);
206 Real left_ledger_protusion = hd.length () / 4;
207 Real right_ledger_protusion = left_ledger_protusion;
208 Interval l_extents = Interval (hd[LEFT] - left_ledger_protusion,
209 hd[RIGHT] + right_ledger_protusion);
210 Molecule ledger_lines;
211 int interspaces = Staff_symbol_referencer::line_count (me) - 1;
213 Note_head::brew_ledger_lines (me, p_min, interspaces, l_extents, true);
214 ledger_lines.translate_axis (0.5 * p_min, Y_AXIS);
215 molecule.add_molecule (ledger_lines);
217 Note_head::brew_ledger_lines (me, p_max, interspaces, l_extents, true);
218 ledger_lines.translate_axis (0.5 * p_max, Y_AXIS);
219 molecule.add_molecule (ledger_lines);
222 SCM key_signature = me->get_grob_property ("keySignature");
223 SCM scm_accidentals_style = me->get_grob_property ("accidentals-style");
224 String accidentals_style;
225 if (gh_symbol_p (scm_accidentals_style))
228 ly_scm2string (scm_symbol_to_string (scm_accidentals_style));
232 accidentals_style = String ("");
235 num_acc = number_accidentals (key_signature, pitch_min, true, false);
236 add_accidentals (me, &head_min, num_acc, pitch_min,
237 accidentals_style, 0.5 * p_min);
238 num_acc = number_accidentals (key_signature, pitch_max, true, false);
239 add_accidentals (me, &head_max, num_acc, pitch_max,
240 accidentals_style, 0.5 * p_max);
243 molecule.add_molecule (head_min);
244 molecule.add_molecule (head_max);
246 return molecule.smobbed_copy ();
249 ADD_INTERFACE (Ambitus, "ambitus-interface",
250 "An ambitus represents the pitch range of a voice.",
251 "note-head-style join-heads");