*** empty log message ***
[lilypond.git] / lily / key-signature-interface.cc
blob762e703fae52d06365da8779854f544df3eb7481
1 /*
2 key-item.cc -- implement Key_signature_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 keyplacement by Mats Bengtsson
9 */
11 #include "item.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
15 #include "font-interface.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "lookup.hh"
18 #include "lily-guile.hh"
19 #include "lily-proto.hh"
20 #include "accidental-interface.hh"
22 struct Key_signature_interface
24 DECLARE_SCHEME_CALLBACK (brew_molecule, (SCM ));
26 static bool has_interface (Grob*);
31 FIXME: too much hardcoding here.
33 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
34 const int SHARP_TOP_PITCH=4; /* ais and bis typeset in lower octave */
37 TODO: look this up. I'm not sure where the naturals ought to go.
39 const int NATURAL_TOP_PITCH = 4;
45 FIXME: key-item should just get a list of (position, acc), and leave
46 the thinking to other parties.
48 - TODO: put this in Scheme
50 - lots of values trivially shared (key doesn't change very
51 often). Compute those once, and use that as cache for the rest.
54 int
55 alteration_pos (SCM what, int alter, int c0p)
57 if (gh_pair_p (what))
58 return gh_scm2int (ly_car (what)) * 7 + gh_scm2int (ly_cdr (what)) + c0p;
60 int p = gh_scm2int (what);
62 // Find the c in the range -4 through 2
63 int from_bottom_pos = c0p + 4;
64 from_bottom_pos = from_bottom_pos%7;
65 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
66 int c0 = from_bottom_pos - 4;
69 if ((alter <0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
70 || (alter >0 && ((p > SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2))
71 || (alter == 0 && ((p > NATURAL_TOP_PITCH) || (p + c0>5)) && (p + c0>2)))
73 p -= 7; /* Typeset below c_position */
75 /* Provide for the four cases in which there's a glitch
76 it's a hack, but probably not worth
77 the effort of finding a nicer solution.
78 --dl. */
79 if (c0==2 && alter >0 && p==3)
80 p -= 7;
81 if (c0==-3 && alter>0 && p==-1)
82 p += 7;
83 if (c0==-4 && alter<0 && p==-1)
84 p += 7;
85 if (c0==-2 && alter<0 && p==-3)
86 p += 7;
88 return p + c0;
92 TODO
93 - space the `natural' signs wider
95 MAKE_SCHEME_CALLBACK (Key_signature_interface,brew_molecule,1);
96 SCM
97 Key_signature_interface::brew_molecule (SCM smob)
99 Grob*me =unsmob_grob (smob);
101 Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
103 SCM scm_style = me->get_grob_property ("style");
104 String style;
105 if (gh_symbol_p (scm_style))
107 style = ly_symbol2string (scm_style);
109 else
111 style = "";
114 SCM newas = me->get_grob_property ("new-accidentals");
115 Molecule mol;
117 SCM c0s = me->get_grob_property ("c0-position");
118 int c0p = 0;
119 if (gh_number_p (c0s))
120 c0p = gh_scm2int (c0s);
123 SCM lists are stacks, so we work from right to left, ending with
124 the cancellation signature.
127 Font_metric *fm = Font_interface::get_default_font (me);
128 for (SCM s = newas; gh_pair_p (s); s = ly_cdr (s))
130 int alteration = gh_scm2int (ly_cdar (s));
131 String font_char =
132 Accidental_interface::get_fontcharname (style, alteration);
133 Molecule acc (fm->find_by_name ("accidentals-" + font_char));
135 if (acc.empty_b())
137 me->warning (_f ("accidental `%s' not found", font_char));
139 else
141 SCM what = ly_caar (s);
142 int pos = alteration_pos (what, alteration, c0p);
143 acc.translate_axis (pos * inter, Y_AXIS);
144 mol.add_at_edge (X_AXIS, LEFT, acc, 0, 0);
148 Item *it = dynamic_cast<Item*> (me) ;
149 if (it->break_status_dir () != RIGHT)
151 SCM old = me->get_grob_property ("old-accidentals");
154 Add half a space between cancellation and key sig.
156 As suggested by [Ross], p.148.
158 Interval x (0, inter);
159 Interval y (0,0);
161 mol.add_at_edge (X_AXIS, LEFT, Lookup::blank (Box (x,y)), 0, 0);
163 Molecule natural;
164 if (gh_pair_p (old))
165 natural=Font_interface::get_default_font (me)->
166 find_by_name (String ("accidentals-") + style + String ("0"));
168 for (; gh_pair_p (old); old = ly_cdr (old))
170 SCM found = scm_assoc (ly_caar (old), newas);
171 if (found == SCM_BOOL_F
172 || ly_cdr (found) != ly_cdar (old))
174 SCM what = ly_caar (old);
175 int alteration = 0;
176 int pos = alteration_pos (what, alteration, c0p);
178 Molecule m = natural;
179 m.translate_axis (pos* inter, Y_AXIS);
182 The natural sign (unlike flat & sharp)
183 has vertical edges on both sides. A little padding is
184 needed to prevent collisions.
186 Real padding = 0.1 ;
187 mol.add_at_edge (X_AXIS, LEFT, m, padding, 0);
192 mol.align_to (X_AXIS, LEFT);
194 return mol.smobbed_copy ();
197 ADD_INTERFACE (Key_signature_interface, "key-signature-interface",
198 "A group of accidentals, to be printed as signature sign.",
199 "c0-position old-accidentals new-accidentals");