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