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