]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
* input/regression/key-signature-cancellation.ly (Module): new file.
[lilypond.git] / lily / note-head.cc
1 /*
2   notehead.cc -- implement Note_head
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>
9 #include <ctype.h>
10
11 #include "staff-symbol.hh"
12 #include "misc.hh"
13 #include "dots.hh"
14 #include "note-head.hh"
15 #include "warn.hh"
16 #include "font-interface.hh"
17 #include "stencil.hh"
18 #include "event.hh"
19 #include "rhythmic-head.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "lookup.hh"
22 #include "output-def.hh"
23
24 /*
25   clean up the mess left by ledger line handling.
26 */
27 static Stencil
28 internal_print (Grob *me)
29 {
30   SCM style  = me->get_property ("style");
31   if (!scm_is_symbol (style))
32     {
33       return Stencil ();
34     }
35
36   SCM log = scm_int2num (Note_head::get_balltype (me));
37   SCM proc = me->get_property ("glyph-name-procedure");
38   SCM scm_font_char = scm_call_2 (proc, log, style);
39   String font_char = "noteheads-" + ly_scm2string (scm_font_char);
40
41   Font_metric * fm = Font_interface::get_default_font (me);
42   Stencil out = fm->find_by_name (font_char);
43   if (out.is_empty ())
44     {
45       me->warning (_f ("note head `%s' not found", font_char.to_str0 ()));
46     }
47
48   return out;
49 }
50
51
52 MAKE_SCHEME_CALLBACK (Note_head,print,1);
53 SCM
54 Note_head::print (SCM smob)  
55 {
56   Grob *me = unsmob_grob (smob);
57
58   return internal_print (me).smobbed_copy ();
59 }
60
61 /*
62   Compute the width the head without ledgers.
63
64   -- there used to be some code from the time that ledgers
65   did take space. Nowadays, we can simply take the standard extent.
66  */
67 Interval
68 Note_head::head_extent (Grob *me, Axis a)
69 {
70   SCM brewer = me->get_property ("print-function");
71   if (brewer == Note_head::print_proc)
72     {
73       Stencil mol = internal_print (me);
74   
75       if (!mol.is_empty ())
76         return mol.extent (a);
77     }
78   else
79     {
80       Stencil * mol = me->get_stencil ();
81       if (mol)
82         return  mol->extent (a) ;
83     }
84   
85   return Interval (0,0);
86 }
87
88 /*
89   This is necessary to prevent a cyclic dependency: the appearance of
90   the ledgers depends on positioning, so the Grob::get_stencil () can
91   not be used for determining the note head extent.
92   
93  */ 
94 MAKE_SCHEME_CALLBACK (Note_head,extent,2);
95 SCM
96 Note_head::extent (SCM smob, SCM axis)  
97 {
98   Grob *me = unsmob_grob (smob);
99
100   return ly_interval2scm (head_extent (me, (Axis) scm_to_int (axis)));
101 }
102
103 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_stencil,1);
104 SCM
105 Note_head::brew_ez_stencil (SCM smob)
106 {
107   Grob *me = unsmob_grob (smob);
108   int l = Note_head::get_balltype (me);
109
110   int b = (l >= 2);
111
112   SCM cause = me->get_property ("cause");
113   SCM spitch = unsmob_music (cause)->get_property ("pitch");
114   Pitch* pit =  unsmob_pitch (spitch);
115
116   SCM idx = scm_int2num (pit->get_notename ());
117   SCM names = me->get_property ("note-names");
118   SCM charstr = SCM_EOL;
119   if (ly_c_vector_p (names))
120     charstr = scm_vector_ref (names, idx);
121   else
122     {
123       char s[2] = "a";
124       s[0] = (pit->get_notename () + 2)%7 + 'a';
125       s[0] = toupper (s[0]);
126       charstr = scm_makfrom0str (s);
127     }
128   
129   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
130                        charstr,
131                        scm_int2num (b),
132                        scm_int2num (1-b),
133                        SCM_UNDEFINED);
134   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
135   Stencil m (bx, at);
136
137   return m.smobbed_copy ();
138 }
139
140
141 Real
142 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
143 {
144   SCM brewer = me->get_property ("print-function");
145   Font_metric * fm  = Font_interface::get_default_font (me);
146   
147   if (brewer == Note_head::print_proc)
148     {
149       SCM style  = me->get_property ("style");
150       if (!scm_is_symbol (style))
151         {
152           return 0.0;
153         }
154       
155       SCM log = scm_int2num (Note_head::get_balltype (me));
156       SCM proc = me->get_property ("glyph-name-procedure");
157       SCM scm_font_char = scm_call_2 (proc, log, style);
158       String font_char = "noteheads-" + ly_scm2string (scm_font_char);
159
160       int k = fm->name_to_index (font_char) ;
161
162       if (k >= 0)
163         {
164           Box b = fm->get_indexed_char (k);
165           Offset wxwy = fm->get_indexed_wxwy (k);
166           Interval v = b[a];
167           if (!v.is_empty ())
168             return 2 * (wxwy[a] - v.center ()) / v.length ();
169         }
170     }
171   
172   /*
173     Fallback
174    */
175   SCM v = me->get_property ("stem-attachment-function");
176   if (!ly_c_procedure_p (v))
177     return 0.0;
178   
179   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
180   if (!ly_c_pair_p (result))
181     return 0.0;
182
183   result = (a == X_AXIS) ? ly_car (result) : ly_cdr (result);
184   
185   return robust_scm2double (result,0);
186 }
187
188 int
189 Note_head::get_balltype (Grob*me) 
190 {
191   SCM s = me->get_property ("duration-log");
192   return scm_is_number (s) ? scm_to_int (s) <? 2 : 0;
193 }
194
195 ADD_INTERFACE (Note_head,"note-head-interface",
196   "Note head",
197   "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
198