]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
* mf/feta-bolletjes.mf (overdone_heads): cleanup triangle
[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
9 #include "note-head.hh"
10
11 #include <cmath>
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       && stem_dir)
62     {
63       idx = prefix + "s" + ly_scm2string (scm_font_char);
64       out = fm->find_by_name (idx);
65     }
66   
67   if (out.is_empty())
68     {
69       me->warning (_f ("note head `%s' not found", idx.to_str0 ()));
70     }
71   else
72     {
73       *font_char = idx;
74     }
75
76   return out;
77 }
78
79
80 MAKE_SCHEME_CALLBACK (Note_head,print,1);
81 SCM
82 Note_head::print (SCM smob)  
83 {
84   Grob *me = unsmob_grob (smob);
85
86   String idx;
87   return internal_print (me, &idx).smobbed_copy ();
88 }
89
90
91 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_stencil,1);
92 SCM
93 Note_head::brew_ez_stencil (SCM smob)
94 {
95   Grob *me = unsmob_grob (smob);
96   int l = Note_head::get_balltype (me);
97
98   int b = (l >= 2);
99
100   SCM cause = me->get_property ("cause");
101   SCM spitch = unsmob_music (cause)->get_property ("pitch");
102   Pitch* pit =  unsmob_pitch (spitch);
103
104   SCM idx = scm_int2num (pit->get_notename ());
105   SCM names = me->get_property ("note-names");
106   SCM charstr = SCM_EOL;
107   if (ly_c_vector_p (names))
108     charstr = scm_vector_ref (names, idx);
109   else
110     {
111       char s[2] = "a";
112       s[0] = (pit->get_notename () + 2)%7 + 'a';
113       s[0] = toupper (s[0]);
114       charstr = scm_makfrom0str (s);
115     }
116   
117   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
118                        charstr,
119                        scm_int2num (b),
120                        scm_int2num (1-b),
121                        SCM_UNDEFINED);
122   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
123   Stencil m (bx, at);
124
125   return m.smobbed_copy ();
126 }
127
128
129 Real
130 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
131 {
132   SCM brewer = me->get_property ("print-function");
133   Font_metric * fm  = Font_interface::get_default_font (me);
134   
135   if (brewer == Note_head::print_proc)
136     {
137       String key;
138       internal_print (me, &key);
139
140       int k = fm->name_to_index (key) ;
141       if (k >= 0)
142         {
143           Box b = fm->get_indexed_char (k);
144           Offset wxwy = fm->get_indexed_wxwy (k);
145           Interval v = b[a];
146           if (!v.is_empty ())
147             return 2 * (wxwy[a] - v.center ()) / v.length ();
148         }
149     }
150   
151   /*
152     Fallback
153    */
154   SCM v = me->get_property ("stem-attachment-function");
155   if (!ly_c_procedure_p (v))
156     return 0.0;
157   
158   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
159   if (!scm_is_pair (result))
160     return 0.0;
161
162   result = (a == X_AXIS) ? scm_car (result) : scm_cdr (result);
163   
164   return robust_scm2double (result,0);
165 }
166
167 int
168 Note_head::get_balltype (Grob*me) 
169 {
170   SCM s = me->get_property ("duration-log");
171   return scm_is_number (s) ? scm_to_int (s) <? 2 : 0;
172 }
173
174 ADD_INTERFACE (Note_head,"note-head-interface",
175   "Note head",
176   "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
177