]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus.cc
* lily/ledger-line-engraver.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--2004 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "staff-symbol-referencer.hh"
10 #include "pitch.hh"
11 #include "ambitus.hh"
12 #include "stencil.hh"
13 #include "note-head.hh"
14 #include "item.hh"
15 #include "font-interface.hh"
16 #include "output-def.hh"
17 #include "lookup.hh"
18
19 /*
20   UGH UGH UGH
21
22   This does 3 things at one:
23
24   - acc positioning
25   - drawing accidentals
26   - drawing note heads
27
28   It confuses interpretation & formatting.
29
30   UGH.
31   --hwn.
32  */
33
34 /*
35  * TODO: note-head collision handling
36  *
37  * TODO: accidentals collision handling
38  *
39  * TODO: alternative representation: adding the ambitus as text script
40  * to the instrument name (e.g. "Soprano (c^1 - f^2)").
41  *
42  * FIXME: Accidentals are too close at the note heads (it seems that
43  * the extent of the ledger lines is ignored).
44  *
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.
48  *
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.
52  *
53  * TODO: make ignore_octave and force_accidental of function
54  * number_accidentals accessible via grob properties.
55  */
56
57 /**
58   Given a pitch and a key_signature, decide what accidentals to show.
59  
60   Possible return values:
61  
62   0: do not show any accidental
63   1: show pitch->alteration_ only
64   2: show pitch->get_alteration, preceded by a natural sign
65
66   UGH: code duplication! See accidental-engraver.
67  
68  */
69 static int
70 number_accidentals (SCM key_signature, Pitch *pitch,
71                     bool ignore_octave_b, bool force_accidental)
72 {
73   int notename = pitch->get_notename ();
74   int octave = pitch->get_octave ();
75   int alteration = pitch->get_alteration ();
76
77   if (force_accidental) // ignore key signature
78     return 1;
79
80   
81 #if DEBUG_AMBITUS
82   scm_display (key_signature, scm_current_output_port ());
83 #endif
84
85   SCM prev;
86   if (ignore_octave_b)
87     prev = ly_assoc_cdr (scm_int2num (notename), key_signature);
88   else
89     prev = scm_assoc (scm_cons (scm_int2num (octave), scm_int2num (notename)),
90                      key_signature);
91
92   /* should really be true unless prev == SCM_BOOL_F */
93   if (ly_c_pair_p (prev) && ly_c_pair_p (ly_cdr (prev)))
94     {
95       prev = scm_cons (ly_car (prev), ly_cadr (prev));
96     }
97
98   /* If an accidental was not found */
99   if (prev == SCM_BOOL_F)
100     prev = scm_assoc (scm_int2num (notename), key_signature);
101
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;
104
105   if (alteration == sig_alteration) // no accidental at all needed
106     return 0;
107
108   if ((alteration == 0) && (sig_alteration != 0)) // need ordinary natural
109     return 2;
110
111   if (sig_alteration == 0) // use pitch's alteration
112     return 1;
113
114   return 2;
115 }
116
117
118
119 void
120 add_accidentals (Item *me, Stencil *head, int num_acc,
121                  Pitch *pitch, String accidentals_style, Real yoffs)
122 {
123   if (!num_acc)
124     return;
125   if (pitch->get_alteration ())
126     {
127       Stencil accidental (Font_interface::get_default_font (me)->
128                            find_by_name (String ("accidentals-") +
129                                          accidentals_style +
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);
133     }
134   if (num_acc == 2)
135     {
136       Stencil natural (Font_interface::get_default_font (me)->
137                         find_by_name (String ("accidentals-") +
138                                       accidentals_style +
139                                       to_string ("0")));
140       natural.translate_axis (yoffs, Y_AXIS);
141       head->add_at_edge (X_AXIS,  LEFT, natural, 0.1, 0);
142     }
143 }
144
145 MAKE_SCHEME_CALLBACK (Ambitus,print,1);
146 SCM
147 Ambitus::print (SCM smob)
148 {
149   Item *me = (Item*) unsmob_grob (smob);
150   Stencil stencil;
151
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))
155     {
156       String note_head_style =
157         ly_symbol2string (scm_note_head_style);
158     }
159   else
160     {
161       note_head_style = String ("noteheads-2");
162     }
163   if (Font_interface::get_default_font (me)->find_by_name (note_head_style).is_empty ())
164     {
165       String message = "Ambitus: no such note head: `" + note_head_style + "'";
166       me->warning (_ (message.to_str0 ()));
167       return SCM_EOL;
168     }
169
170   /*
171     FIXME: Use positions. 
172    */
173   int p_min, p_max;
174   Slice posns = get_positions(me);
175   
176   p_min = posns[LEFT];
177   p_max = posns[RIGHT];
178
179   // create heads
180   Stencil head_min =
181     Font_interface::get_default_font (me)->find_by_name (note_head_style);
182   head_min.translate_axis (0.5*p_min, Y_AXIS);
183   Stencil head_max =
184     Font_interface::get_default_font (me)->find_by_name (note_head_style);
185   head_max.translate_axis (0.5*p_max, Y_AXIS);
186
187   // join heads
188   if (to_boolean (me->get_property ("join-heads")) &&
189       ((p_max - p_min) >= 3))
190     {
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);
199     }
200
201   
202   // add accidentals
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))
207     {
208       accidentals_style =
209         ly_symbol2string (scm_accidentals_style);
210     }
211   else
212     {
213       accidentals_style = String ("");
214     }
215   
216   int num_acc;
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);
225
226   // add heads
227   stencil.add_stencil (head_min);
228   stencil.add_stencil (head_max);
229
230   return stencil.smobbed_copy ();
231 }
232
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");