]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
*** empty log message ***
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "note-head.hh"
10
11 #include <math.h>
12 #include <cctype>
13
14 #include "directional-element-interface.hh"
15 #include "staff-symbol.hh"
16 #include "misc.hh"
17 #include "dots.hh"
18 #include "warn.hh"
19 #include "font-interface.hh"
20 #include "event.hh"
21 #include "rhythmic-head.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "lookup.hh"
24 #include "output-def.hh"
25
26 /*
27   clean up the mess left by ledger line handling.
28 */
29 static Stencil
30 internal_print (Grob *me, String * font_char)
31 {
32   SCM style  = me->get_property ("style");
33   if (!scm_is_symbol (style))
34     {
35       return Stencil ();
36     }
37
38   SCM log = scm_int2num (Note_head::get_balltype (me));
39   SCM proc = me->get_property ("glyph-name-procedure");
40   SCM scm_font_char = scm_call_2 (proc, log, style);
41
42   Font_metric * fm = Font_interface::get_default_font (me);
43
44   Direction stem_dir = CENTER;
45   if (Grob *stem = unsmob_grob (me->get_property ("stem")))
46     {
47       stem_dir = get_grob_direction (stem);
48       if (stem_dir == CENTER)
49         {
50           programming_error ("Must have stem dir for note head");
51         }
52     }
53   
54   Stencil out;
55
56   String prefix = "noteheads.";
57   String idx =
58     prefix + ((stem_dir == UP) ? "u" : "d")  + ly_scm2string (scm_font_char);
59   out = fm->find_by_name (idx);
60   if (out.is_empty ())
61     {
62       idx = prefix + "s" + ly_scm2string (scm_font_char);
63       out = fm->find_by_name (idx);
64     }
65   
66   if (out.is_empty())
67     {
68       me->warning (_f ("note head `%s' not found", idx.to_str0 ()));
69     }
70   else
71     {
72       *font_char = idx;
73     }
74
75   return out;
76 }
77
78
79 MAKE_SCHEME_CALLBACK (Note_head,print,1);
80 SCM
81 Note_head::print (SCM smob)  
82 {
83   Grob *me = unsmob_grob (smob);
84
85   String idx;
86   return internal_print (me, &idx).smobbed_copy ();
87 }
88
89
90 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_stencil,1);
91 SCM
92 Note_head::brew_ez_stencil (SCM smob)
93 {
94   Grob *me = unsmob_grob (smob);
95   int l = Note_head::get_balltype (me);
96
97   int b = (l >= 2);
98
99   SCM cause = me->get_property ("cause");
100   SCM spitch = unsmob_music (cause)->get_property ("pitch");
101   Pitch* pit =  unsmob_pitch (spitch);
102
103   SCM idx = scm_int2num (pit->get_notename ());
104   SCM names = me->get_property ("note-names");
105   SCM charstr = SCM_EOL;
106   if (scm_is_vector (names))
107     charstr = scm_vector_ref (names, idx);
108   else 
109     {
110       char s[2] = "a";
111       s[0] = (pit->get_notename () + 2)%7 + 'a';
112       s[0] = toupper (s[0]);
113       charstr = scm_makfrom0str (s);
114     }
115   
116   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
117                        charstr,
118                        scm_int2num (b),
119                        scm_int2num (1-b),
120                        SCM_UNDEFINED);
121   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
122   Stencil m (bx, at);
123
124   return m.smobbed_copy ();
125 }
126
127
128 Real
129 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
130 {
131   SCM brewer = me->get_property ("print-function");
132   Font_metric * fm  = Font_interface::get_default_font (me);
133   
134   if (brewer == Note_head::print_proc)
135     {
136       String key;
137       internal_print (me, &key);
138
139       int k = fm->name_to_index (key) ;
140       if (k >= 0)
141         {
142           Box b = fm->get_indexed_char (k);
143           Offset wxwy = fm->attachment_point (key);
144           Interval v = b[a];
145           if (!v.is_empty ())
146             return 2 * (wxwy[a] - v.center ()) / v.length ();
147         }
148     }
149   
150   /*
151     Fallback
152    */
153   SCM v = me->get_property ("stem-attachment-function");
154   if (!ly_c_procedure_p (v))
155     return 0.0;
156   
157   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
158   if (!scm_is_pair (result))
159     return 0.0;
160
161   result = (a == X_AXIS) ? scm_car (result) : scm_cdr (result);
162   
163   return robust_scm2double (result,0);
164 }
165
166 int
167 Note_head::get_balltype (Grob*me) 
168 {
169   SCM s = me->get_property ("duration-log");
170   return scm_is_number (s) ? scm_to_int (s) <? 2 : 0;
171 }
172
173 ADD_INTERFACE (Note_head,"note-head-interface",
174   "Note head",
175   "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
176