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