]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-signature-interface.cc
Update.
[lilypond.git] / lily / key-signature-interface.cc
1 /*
2   key-item.cc -- implement Key_signature_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   keyplacement by Mats Bengtsson
9 */
10
11 #include "item.hh"
12 #include "output-def.hh"
13 #include "font-interface.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "lookup.hh"
16 #include "accidental-interface.hh"
17
18 struct Key_signature_interface
19 {
20   DECLARE_SCHEME_CALLBACK (print, (SCM));
21
22   static bool has_interface (Grob *);
23 };
24
25 /*
26   FIXME: too much hardcoding here.
27 */
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 */
30
31 /*
32   TODO: look this up. I'm not sure where the naturals ought to go.
33 */
34 const int NATURAL_TOP_PITCH = 4;
35
36 /*
37   FIXME: key-item should just get a list of (position, acc), and leave
38   the thinking to other parties.
39
40   - TODO: put this in Scheme
41
42   TODO: can  we do without c0pos? it's partly musical.
43 */
44 int
45 alteration_pos (SCM what, int alter, int c0p)
46 {
47   if (scm_is_pair (what))
48     return scm_to_int (scm_car (what)) * 7 + scm_to_int (scm_cdr (what)) + c0p;
49
50   int p = scm_to_int (what);
51
52   // Find the c in the range -4 through 2
53   int from_bottom_pos = c0p + 4;
54   from_bottom_pos = from_bottom_pos%7;
55   from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
56   int c0 = from_bottom_pos - 4;
57
58   if ((alter < 0 && ((p > FLAT_TOP_PITCH) || (p + c0 > 4)) && (p + c0 > 1))
59       || (alter > 0 && ((p > SHARP_TOP_PITCH) || (p + c0 > 5)) && (p + c0 > 2))
60       || (alter == 0 && ((p > NATURAL_TOP_PITCH) || (p + c0 > 5)) && (p + c0 > 2)))
61     {
62       p -= 7; /* Typeset below c_position */
63     }
64
65   /* Provide for the four cases in which there's a glitch
66      it's a hack, but probably not worth
67      the effort of finding a nicer solution.
68      --dl. */
69   if (c0 == 2 && alter > 0 && p == 3)
70     p -= 7;
71   if (c0==-3 && alter > 0 && p ==-1)
72     p += 7;
73   if (c0==-4 && alter < 0 && p ==-1)
74     p += 7;
75   if (c0==-2 && alter < 0 && p ==-3)
76     p += 7;
77
78   return p + c0;
79 }
80
81 /*
82   TODO
83   - space the `natural' signs wider
84 */
85 MAKE_SCHEME_CALLBACK (Key_signature_interface, print, 1);
86 SCM
87 Key_signature_interface::print (SCM smob)
88 {
89   Grob *me = unsmob_grob (smob);
90
91   Real inter = Staff_symbol_referencer::staff_space (me) / 2.0;
92
93   SCM scm_style = me->get_property ("style");
94   String style;
95   if (scm_is_symbol (scm_style))
96     {
97       style = ly_symbol2string (scm_style);
98     }
99   else
100     {
101       style = "";
102     }
103
104   SCM newas = me->get_property ("new-accidentals");
105   Stencil mol;
106
107   SCM c0s = me->get_property ("c0-position");
108   int c0p = 0;
109   if (scm_is_number (c0s))
110     c0p = scm_to_int (c0s);
111
112   /*
113     SCM lists are stacks, so we work from right to left, ending with
114     the cancellation signature.
115   */
116
117   Font_metric *fm = Font_interface::get_default_font (me);
118   for (SCM s = newas; scm_is_pair (s); s = scm_cdr (s))
119     {
120       int alteration = scm_to_int (scm_cdar (s));
121       String font_char
122         = Accidental_interface::get_fontcharname (style, alteration);
123       Stencil acc (fm->find_by_name ("accidentals." + font_char));
124
125       if (acc.is_empty ())
126         {
127           me->warning (_f ("accidental `%s' not found", font_char));
128         }
129       else
130         {
131           SCM what = scm_caar (s);
132           int pos = alteration_pos (what, alteration, c0p);
133           acc.translate_axis (pos * inter, Y_AXIS);
134           mol.add_at_edge (X_AXIS, LEFT, acc, 0, 0);
135         }
136     }
137
138   Item *it = dynamic_cast<Item *> (me);
139   if (it->break_status_dir () != RIGHT)
140     {
141       SCM old = me->get_property ("old-accidentals");
142
143       Stencil natural;
144       if (scm_is_pair (old))
145         natural = Font_interface::get_default_font (me)->
146           find_by_name (String ("accidentals.") + style + String ("0"));
147
148       int last_pos = -100;
149       for (; scm_is_pair (old); old = scm_cdr (old))
150         {
151           SCM found = scm_assoc (scm_caar (old), newas);
152           if (found == SCM_BOOL_F
153               || scm_cdr (found) != scm_cdar (old))
154             {
155               SCM what = scm_caar (old);
156               int alteration = 0;
157               int pos = alteration_pos (what, alteration, c0p);
158
159               Stencil m = natural;
160               m.translate_axis (pos * inter, Y_AXIS);
161
162               /*
163                 The natural sign (unlike flat & sharp)
164                 has vertical edges on both sides. A little padding is
165                 needed to prevent collisions.
166               */
167               Real padding = 0.0;
168               if (last_pos < pos + 2
169                   && last_pos > pos - 6)
170                 padding = 0.3;
171
172               mol.add_at_edge (X_AXIS, LEFT, m, padding, 0);
173               last_pos = pos;
174             }
175         }
176     }
177
178   mol.align_to (X_AXIS, LEFT);
179
180   return mol.smobbed_copy ();
181 }
182
183 ADD_INTERFACE (Key_signature_interface, "key-signature-interface",
184                "A group of accidentals, to be printed as signature sign.",
185                "style c0-position old-accidentals new-accidentals");