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
26 static void set_interface (Grob*);
27 static bool has_interface (Grob*);
28 DECLARE_SCHEME_CALLBACK (brew_molecule, (SCM ));
33 FIXME: too much hardcoding here.
35 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
36 const int SHARP_TOP_PITCH=4; /* ais and bis typeset in lower octave */
40 FIXME: key-item should just get a list of (position, acc), and leave
41 the thinking to other parties.
43 - TODO: put this in Scheme
45 - lots of values trivially shared (key doesn't change very
46 often). Compute those once, and use that as cache for the rest.
50 alteration_pos (SCM what, int alter, int c0p)
53 return gh_scm2int (ly_car (what)) * 7 + gh_scm2int (ly_cdr (what)) + c0p;
55 int p = gh_scm2int (what);
57 // Find the c in the range -4 through 2
58 int from_bottom_pos = c0p + 4;
59 from_bottom_pos = from_bottom_pos%7;
60 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
61 int c0 = from_bottom_pos - 4;
64 if ((alter <0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
66 (alter >0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2)))
68 p -= 7; /* Typeset below c_position */
70 /* Provide for the four cases in which there's a glitch
71 it's a hack, but probably not worth
72 the effort of finding a nicer solution.
74 if (c0==2 && alter >0 && p==3)
76 if (c0==-3 && alter>0 && p==-1)
78 if (c0==-4 && alter<0 && p==-1)
80 if (c0==-2 && alter<0 && p==-3)
88 - space the `natural' signs wider
90 MAKE_SCHEME_CALLBACK (Key_signature_interface,brew_molecule,1);
92 Key_signature_interface::brew_molecule (SCM smob)
94 Grob*me =unsmob_grob (smob);
96 Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
98 SCM scm_style = me->get_grob_property ("style");
100 if (gh_symbol_p (scm_style))
102 style = ly_scm2string (scm_symbol_to_string (scm_style));
109 SCM newas = me->get_grob_property ("new-accidentals");
112 SCM c0s = me->get_grob_property ("c0-position");
114 if (gh_number_p (c0s))
115 c0p = gh_scm2int (c0s);
118 SCM lists are stacks, so we work from right to left, ending with
119 the cancellation signature.
122 for (SCM s = newas; gh_pair_p (s); s = ly_cdr (s))
124 SCM what = ly_caar (s);
125 int alter = gh_scm2int (ly_cdar (s));
126 int pos = alteration_pos (what, alter, c0p);
128 Molecule m = Font_interface::get_default_font (me)->
129 find_by_name (String ("accidentals-") + style + to_str (alter));
130 m.translate_axis (pos * inter, Y_AXIS);
131 mol.add_at_edge (X_AXIS, LEFT, m, 0);
134 Item *it = dynamic_cast<Item*> (me) ;
135 if (it->break_status_dir () != RIGHT)
137 SCM old = me->get_grob_property ("old-accidentals");
140 Add half a space between cancellation and key sig.
142 As suggested by [Ross], p.148.
144 Interval x (0, inter);
147 mol.add_at_edge (X_AXIS, LEFT, Lookup::blank (Box (x,y)),0);
151 natural=Font_interface::get_default_font (me)->
152 find_by_name (String ("accidentals-") + style + String ("0"));
154 for (; gh_pair_p (old); old = ly_cdr (old))
156 SCM found = scm_assoc (ly_caar (old), newas);
157 if (found == SCM_BOOL_F
158 || ly_cdr (found) != ly_cdar (old))
160 SCM what = ly_caar (old);
162 int pos = alteration_pos (what, alter, c0p);
164 Molecule m = natural;
165 m.translate_axis (pos* inter, Y_AXIS);
167 mol.add_at_edge (X_AXIS, LEFT, m, 0);
172 mol.align_to (X_AXIS, LEFT);
174 return mol.smobbed_copy ();
178 Key_signature_interface::has_interface (Grob*m)
180 return m && m->has_interface (ly_symbol2scm ("key-signature-interface"));