2 key-item.cc -- implement Key_signature_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 keyplacement by Mats Bengtsson
12 #include "output-def.hh"
13 #include "font-interface.hh"
14 #include "staff-symbol-referencer.hh"
16 #include "accidental-interface.hh"
18 struct Key_signature_interface
20 DECLARE_SCHEME_CALLBACK (print, (SCM));
22 static bool has_interface (Grob *);
26 FIXME: too much hardcoding here.
28 const int FLAT_TOP_PITCH =2; /* fes, ges, as and bes typeset in lower octave */
29 const int SHARP_TOP_PITCH =4; /* ais and bis typeset in lower octave */
32 TODO: look this up. I'm not sure where the naturals ought to go.
34 const int NATURAL_TOP_PITCH = 4;
38 FIXME: key-item should just get a list of (position, acc), and leave
39 the thinking to other parties.
41 - TODO: put this in Scheme
43 TODO: can we do without c0pos? it's partly musical.
46 alteration_pos (SCM what, int alter, int c0p)
48 if (scm_is_pair (what))
49 return scm_to_int (scm_car (what)) * 7 + scm_to_int (scm_cdr (what)) + c0p;
51 int p = scm_to_int (what);
53 // Find the c in the range -4 through 2
54 int from_bottom_pos = c0p + 4;
55 from_bottom_pos = from_bottom_pos%7;
56 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
57 int c0 = from_bottom_pos - 4;
59 if ((alter <0 && ((p > FLAT_TOP_PITCH) || (p + c0 > 4)) && (p + c0 > 1))
60 || (alter >0 && ((p > SHARP_TOP_PITCH) || (p + c0 > 5)) && (p + c0 > 2))
61 || (alter == 0 && ((p > NATURAL_TOP_PITCH) || (p + c0 > 5)) && (p + c0 > 2)))
63 p -= 7; /* Typeset below c_position */
66 /* Provide for the four cases in which there's a glitch
67 it's a hack, but probably not worth
68 the effort of finding a nicer solution.
70 if (c0 == 2 && alter >0 && p ==3)
72 if (c0==-3 && alter > 0 && p ==-1)
74 if (c0==-4 && alter < 0 && p ==-1)
76 if (c0==-2 && alter < 0 && p ==-3)
84 - space the `natural' signs wider
86 MAKE_SCHEME_CALLBACK (Key_signature_interface, print, 1);
88 Key_signature_interface::print (SCM smob)
90 Grob *me = unsmob_grob (smob);
92 Real inter = Staff_symbol_referencer::staff_space (me) / 2.0;
94 SCM scm_style = me->get_property ("style");
96 if (scm_is_symbol (scm_style))
98 style = ly_symbol2string (scm_style);
105 SCM newas = me->get_property ("new-accidentals");
108 SCM c0s = me->get_property ("c0-position");
110 if (scm_is_number (c0s))
111 c0p = scm_to_int (c0s);
114 SCM lists are stacks, so we work from right to left, ending with
115 the cancellation signature.
118 Font_metric *fm = Font_interface::get_default_font (me);
119 for (SCM s = newas; scm_is_pair (s); s = scm_cdr (s))
121 int alteration = scm_to_int (scm_cdar (s));
123 = Accidental_interface::get_fontcharname (style, alteration);
124 Stencil acc (fm->find_by_name ("accidentals." + font_char));
128 me->warning (_f ("accidental `%s' not found", font_char));
132 SCM what = scm_caar (s);
133 int pos = alteration_pos (what, alteration, c0p);
134 acc.translate_axis (pos * inter, Y_AXIS);
135 mol.add_at_edge (X_AXIS, LEFT, acc, 0, 0);
139 Item *it = dynamic_cast<Item *> (me);
140 if (it->break_status_dir () != RIGHT)
142 SCM old = me->get_property ("old-accidentals");
145 if (scm_is_pair (old))
146 natural = Font_interface::get_default_font (me)->
147 find_by_name (String ("accidentals.") + style + String ("0"));
150 for (; scm_is_pair (old); old = scm_cdr (old))
152 SCM found = scm_assoc (scm_caar (old), newas);
153 if (found == SCM_BOOL_F
154 || scm_cdr (found) != scm_cdar (old))
156 SCM what = scm_caar (old);
158 int pos = alteration_pos (what, alteration, c0p);
161 m.translate_axis (pos* inter, Y_AXIS);
164 The natural sign (unlike flat & sharp)
165 has vertical edges on both sides. A little padding is
166 needed to prevent collisions.
169 if (last_pos < pos + 2
170 && last_pos> pos - 6)
173 mol.add_at_edge (X_AXIS, LEFT, m, padding, 0);
179 mol.align_to (X_AXIS, LEFT);
181 return mol.smobbed_copy ();
184 ADD_INTERFACE (Key_signature_interface, "key-signature-interface",
185 "A group of accidentals, to be printed as signature sign.",
186 "style c0-position old-accidentals new-accidentals");