3 key-item.cc -- implement Key_signature_interface
5 source file of the GNU LilyPond music typesetter
7 (c) 1996--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 keyplacement by Mats Bengtsson
14 #include "molecule.hh"
15 #include "paper-def.hh"
16 #include "font-interface.hh"
17 #include "staff-symbol-referencer.hh"
19 #include "lily-guile.hh"
20 #include "lily-proto.hh"
23 struct Key_signature_interface
25 DECLARE_SCHEME_CALLBACK (brew_molecule, (SCM ));
30 FIXME: too much hardcoding here.
32 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
33 const int SHARP_TOP_PITCH=4; /* ais and bis typeset in lower octave */
37 FIXME: key-item should just get a list of (position, acc), and leave
38 the thinking to other parties.
40 - TODO: put this in Scheme
42 - lots of values trivially shared (key doesn't change very
43 often). Compute those once, and use that as cache for the rest.
47 alteration_pos (SCM what, int alter, int c0p)
50 return gh_scm2int (ly_car (what)) * 7 + gh_scm2int (ly_cdr (what)) + c0p;
52 int p = gh_scm2int (what);
54 // Find the c in the range -4 through 2
55 int from_bottom_pos = c0p + 4;
56 from_bottom_pos = from_bottom_pos%7;
57 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
58 int c0 = from_bottom_pos - 4;
61 if ((alter <0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
63 (alter >0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2)))
65 p -= 7; /* Typeset below c_position */
67 /* Provide for the four cases in which there's a glitch
68 it's a hack, but probably not worth
69 the effort of finding a nicer solution.
71 if (c0==2 && alter >0 && p==3)
73 if (c0==-3 && alter>0 && p==-1)
75 if (c0==-4 && alter<0 && p==-1)
77 if (c0==-2 && alter<0 && p==-3)
85 - space the `natural' signs wider
87 MAKE_SCHEME_CALLBACK (Key_signature_interface,brew_molecule,1);
89 Key_signature_interface::brew_molecule (SCM smob)
91 Grob*me =unsmob_grob (smob);
93 Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
95 SCM scm_style = me->get_grob_property ("style");
97 if (gh_symbol_p (scm_style))
99 style = ly_scm2string (scm_symbol_to_string (scm_style));
106 SCM newas = me->get_grob_property ("new-accidentals");
109 SCM c0s = me->get_grob_property ("c0-position");
111 if (gh_number_p (c0s))
112 c0p = gh_scm2int (c0s);
115 SCM lists are stacks, so we work from right to left, ending with
116 the cancellation signature.
119 for (SCM s = newas; gh_pair_p (s); s = ly_cdr (s))
121 SCM what = ly_caar (s);
122 int alter = gh_scm2int (ly_cdar (s));
123 int pos = alteration_pos (what, alter, c0p);
125 Molecule m = Font_interface::get_default_font (me)->
126 find_by_name (String ("accidentals-") + style + to_str (alter));
127 m.translate_axis (pos * inter, Y_AXIS);
128 mol.add_at_edge (X_AXIS, LEFT, m, 0);
131 Item *it = dynamic_cast<Item*> (me) ;
132 if (it->break_status_dir () != RIGHT)
134 SCM old = me->get_grob_property ("old-accidentals");
137 Add half a space between cancellation and key sig.
139 As suggested by [Ross], p.148.
141 Interval x (0, inter);
144 mol.add_at_edge (X_AXIS, LEFT, Lookup::blank (Box (x,y)),0);
148 natural=Font_interface::get_default_font (me)->
149 find_by_name (String ("accidentals-") + style + String ("0"));
151 for (; gh_pair_p (old); old = ly_cdr (old))
153 SCM found = scm_assoc (ly_caar (old), newas);
154 if (found == SCM_BOOL_F
155 || ly_cdr (found) != ly_cdar (old))
157 SCM what = ly_caar (old);
159 int pos = alteration_pos (what, alter, c0p);
161 Molecule m = natural;
162 m.translate_axis (pos* inter, Y_AXIS);
164 mol.add_at_edge (X_AXIS, LEFT, m, 0);
169 mol.align_to (X_AXIS, LEFT);
171 return mol.smobbed_copy ();
174 ADD_INTERFACE (Key_signature_interface, "key-signature-interface",
175 "A group of accidentals.",
176 "c0-position old-accidentals new-accidentals");