]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-signature-interface.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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 #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 /*
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 const int NATURAL_TOP_PITCH = 4;  
36
37
38
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 */
49 int
50 alteration_pos  (SCM what, int alter, int c0p)
51 {
52   if (scm_is_pair (what))
53     return scm_to_int (scm_car (what)) * 7 + scm_to_int (scm_cdr (what)) + c0p;
54
55   int p = scm_to_int (what);
56
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;
62
63     
64   if ((alter <0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
65       || (alter >0 && ((p > SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2))
66       || (alter == 0 && ((p > NATURAL_TOP_PITCH) || (p + c0>5)) && (p + c0>2)))
67     {
68       p -= 7; /* Typeset below c_position */
69     }
70   
71   /* Provide for the four cases in which there's a glitch 
72        it's a hack, but probably not worth  
73        the effort of finding a nicer solution.
74        --dl. */
75   if (c0==2 && alter >0 && p==3)
76     p -= 7;
77   if (c0==-3 && alter>0 && p==-1)
78     p += 7;
79   if (c0==-4 && alter<0 && p==-1)
80     p += 7;
81   if (c0==-2 && alter<0 && p==-3)
82     p += 7;
83     
84   return p + c0;
85 }
86
87 /*
88   TODO
89   - space the `natural' signs wider
90  */
91 MAKE_SCHEME_CALLBACK (Key_signature_interface,print,1);
92 SCM
93 Key_signature_interface::print (SCM smob)
94 {
95   Grob*me =unsmob_grob (smob);
96
97   Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
98
99   SCM scm_style = me->get_property ("style");
100   String style;
101   if (scm_is_symbol (scm_style))
102     {
103       style = ly_symbol2string (scm_style);
104     }
105   else
106     {
107       style = "";
108     }
109
110   SCM newas = me->get_property ("new-accidentals");  
111   Stencil mol;
112
113   SCM c0s = me->get_property ("c0-position");
114   int c0p = 0;
115   if (scm_is_number (c0s))
116     c0p = scm_to_int (c0s);
117
118   /*
119     SCM lists are stacks, so we work from right to left, ending with
120     the cancellation signature.
121   */
122
123   Font_metric *fm = Font_interface::get_default_font (me);
124   for (SCM s = newas; scm_is_pair (s); s = scm_cdr (s))
125     {
126       int alteration = scm_to_int (scm_cdar (s));
127       String font_char =
128         Accidental_interface::get_fontcharname (style, alteration);
129       Stencil acc (fm->find_by_name ("accidentals-" + font_char));
130
131       if (acc.is_empty ())
132         {
133           me->warning (_f ("accidental `%s' not found", font_char));
134         }
135       else
136         {
137           SCM what = scm_caar (s);
138           int pos = alteration_pos (what, alteration, c0p);
139           acc.translate_axis (pos * inter, Y_AXIS);
140           mol.add_at_edge (X_AXIS, LEFT, acc, 0, 0);
141         }
142     }
143
144   Item *it = dynamic_cast<Item*> (me) ;
145   if (it->break_status_dir () != RIGHT)
146     {
147       SCM old = me->get_property ("old-accidentals");
148       
149       Stencil natural;
150       if (scm_is_pair (old))
151         natural=Font_interface::get_default_font (me)->
152             find_by_name (String ("accidentals-") + style + String ("0"));
153       
154
155       int last_pos = -100;
156       for (; scm_is_pair (old); old = scm_cdr (old))
157         {
158           SCM found = scm_assoc (scm_caar (old), newas);
159           if (found == SCM_BOOL_F
160               || scm_cdr (found) != scm_cdar (old))
161             {
162               SCM what = scm_caar (old);
163               int alteration = 0;
164               int pos = alteration_pos (what, alteration, c0p);
165
166               Stencil m = natural;
167               m.translate_axis (pos* inter, Y_AXIS);
168
169               /*
170                 The natural sign (unlike flat & sharp)
171                 has vertical edges on both sides. A little padding is
172                 needed to prevent collisions.
173                */
174               Real padding = 0.0;
175               if (last_pos < pos + 2
176                   &&  last_pos> pos - 6)
177                 padding = 0.3;
178               
179               mol.add_at_edge (X_AXIS, LEFT, m, padding, 0);
180               last_pos = pos;
181             }
182         }
183     }
184
185   mol.align_to (X_AXIS, LEFT);
186   
187   return mol.smobbed_copy ();
188 }
189
190 ADD_INTERFACE (Key_signature_interface, "key-signature-interface",
191   "A group of accidentals, to be printed as signature sign.",
192   "style c0-position old-accidentals new-accidentals");