]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus.cc
* lily/horizontal-bracket.cc: new file.
[lilypond.git] / lily / ambitus.cc
1 /*
2   ambitus.cc -- implement Ambitus
3
4   source file of the GNU LilyPond music typesetter
5
6   (C) 2002 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "staff-symbol-referencer.hh"
10 #include "pitch.hh"
11 #include "ambitus.hh"
12 #include "molecule.hh"
13 #include "note-head.hh"
14 #include "item.hh"
15 #include "font-interface.hh"
16 #include "paper-def.hh"
17 #include "lookup.hh"
18
19 /*
20  * TODO: note-head collision handling
21  *
22  * TODO: accidentals collision handling
23  *
24  * TODO: alternative representation: adding the ambitus as text script
25  * to the instrument name (e.g. "Soprano (c^1 - f^2)").
26  *
27  * FIXME: Accidentals are too close at the note heads (it seems that
28  * the extent of the ledger lines is ignored).
29  *
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.
33  *
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.
37  *
38  * TODO: make ignore_octave and force_accidental of function
39  * number_accidentals accessible via grob properties.
40  */
41
42 /**
43  * Given a pitch and a key_signature, decide what accidentals to show.
44  *
45  * Possible return values:
46  *
47  * 0: do not show any accidental
48  * 1: show pitch->alteration_ only
49  * 2: show pitch->alteration_, preceded by a natural sign
50  */
51 static int
52 number_accidentals (SCM key_signature, Pitch *pitch,
53                     bool ignore_octave_b, bool force_accidental)
54 {
55   int notename = pitch->notename_;
56   int octave = pitch->octave_;
57   int alteration = pitch->alteration_;
58
59   if (force_accidental) // ignore key signature
60     return 1;
61
62 #if DEBUG_AMBITUS
63   scm_display (key_signature, scm_current_output_port ());
64 #endif
65
66   SCM prev;
67   if (ignore_octave_b)
68     prev = ly_assoc_cdr (scm_int2num (notename), key_signature);
69   else
70     prev = gh_assoc (gh_cons (scm_int2num (octave), scm_int2num (notename)),
71                      key_signature);
72
73   /* should really be true unless prev == SCM_BOOL_F */
74   if (gh_pair_p (prev) && gh_pair_p (ly_cdr (prev)))
75     {
76       prev = gh_cons (ly_car (prev), ly_cadr (prev));
77     }
78
79   /* If an accidental was not found */
80   if (prev == SCM_BOOL_F)
81     prev = gh_assoc (scm_int2num (notename), key_signature);
82
83   SCM prev_acc = (prev == SCM_BOOL_F) ? scm_int2num (0) : ly_cdr (prev);
84   int sig_alteration = gh_number_p (prev_acc) ? gh_scm2int (prev_acc) : 0;
85
86   if (alteration == sig_alteration) // no accidental at all needed
87     return 0;
88
89   if ((alteration == 0) && (sig_alteration != 0)) // need ordinary natural
90     return 2;
91
92   if (sig_alteration == 0) // use pitch's alteration
93     return 1;
94
95   return 2;
96 }
97
98 void
99 add_accidentals (Item *me, Molecule *head, int num_acc,
100                  Pitch *pitch, String accidentals_style, Real yoffs)
101 {
102   if (!num_acc)
103     return;
104   if (pitch->alteration_)
105     {
106       Molecule accidental (Font_interface::get_default_font (me)->
107                            find_by_name (String ("accidentals-") +
108                                          accidentals_style +
109                                          to_string (pitch->alteration_)));
110       accidental.translate_axis (yoffs, Y_AXIS);
111       head->add_at_edge (X_AXIS,  LEFT, accidental, 0.1);
112     }
113   if (num_acc == 2)
114     {
115       Molecule natural (Font_interface::get_default_font (me)->
116                         find_by_name (String ("accidentals-") +
117                                       accidentals_style +
118                                       to_string ("0")));
119       natural.translate_axis (yoffs, Y_AXIS);
120       head->add_at_edge (X_AXIS,  LEFT, natural, 0.1);
121     }
122 }
123
124 MAKE_SCHEME_CALLBACK (Ambitus,brew_molecule,1);
125 SCM
126 Ambitus::brew_molecule (SCM smob)
127 {
128   Item *me = (Item *)unsmob_grob (smob);
129   Molecule molecule = Molecule ();
130
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))
134     {
135       String note_head_style =
136         ly_symbol2string (scm_note_head_style);
137     }
138   else
139     {
140       note_head_style = String ("noteheads-2");
141     }
142   if (Font_interface::get_default_font (me)->find_by_name (note_head_style).empty_b ())
143     {
144       String message = "Ambitus: no such note head: `" + note_head_style + "'";
145       me->warning (_ (message.to_str0 ()));
146       return SCM_EOL;
147     }
148
149   int p_min, p_max;
150   Pitch *pitch_min = unsmob_pitch (me->get_grob_property ("pitch-min"));
151   if (!pitch_min)
152     {
153       me->programming_error("Ambitus: pitch_min undefined; assuming 0");
154       p_min = 0;
155     }
156   else
157     {
158       p_min = pitch_min->steps ();
159     }
160   Pitch *pitch_max = unsmob_pitch (me->get_grob_property ("pitch-max"));
161   if (!pitch_max)
162     {
163       me->programming_error("Ambitus: pitch_max undefined; assuming 0");
164       p_max = 0;
165     }
166   else
167     {
168       p_max = pitch_max->steps ();
169     }
170   if (p_min > p_max)
171     {
172       me->programming_error ("Ambitus: reverse range");
173     }
174
175   SCM c0 = me->get_grob_property ("c0-position");
176   if (gh_number_p (c0))
177     {
178       p_min += gh_scm2int (c0);
179       p_max += gh_scm2int (c0);
180     }
181
182   // create heads
183   Molecule head_min =
184     Font_interface::get_default_font (me)->find_by_name (note_head_style);
185   head_min.translate_axis (0.5*p_min, Y_AXIS);
186   Molecule head_max =
187     Font_interface::get_default_font (me)->find_by_name (note_head_style);
188   head_max.translate_axis (0.5*p_max, Y_AXIS);
189
190   // join heads
191   if (to_boolean (me->get_grob_property ("join-heads")) &&
192       ((p_max - p_min) >= 3))
193     {
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);
202     }
203
204   // add ledger lines
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;
212   ledger_lines =
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);
216   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);
220
221   // add accidentals
222   SCM key_signature = me->get_grob_property ("key-signature");
223   SCM scm_accidentals_style = me->get_grob_property ("accidentals-style");
224   String accidentals_style;
225   if (gh_symbol_p (scm_accidentals_style))
226     {
227       accidentals_style =
228         ly_symbol2string (scm_accidentals_style);
229     }
230   else
231     {
232       accidentals_style = String ("");
233     }
234   int num_acc;
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);
241
242   // add heads
243   molecule.add_molecule (head_min);
244   molecule.add_molecule (head_max);
245
246   return molecule.smobbed_copy ();
247 }
248
249 ADD_INTERFACE (Ambitus, "ambitus-interface",
250   "An ambitus represents the pitch range of a voice.",
251   "c0-position pitch-min pitch-max accidentals note-head-style join-heads");