]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
remove Note_head::extent.
[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 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_stencil,1);
89 SCM
90 Note_head::brew_ez_stencil (SCM smob)
91 {
92   Grob *me = unsmob_grob (smob);
93   int l = Note_head::get_balltype (me);
94
95   int b = (l >= 2);
96
97   SCM cause = me->get_property ("cause");
98   SCM spitch = unsmob_music (cause)->get_property ("pitch");
99   Pitch* pit =  unsmob_pitch (spitch);
100
101   SCM idx = scm_int2num (pit->get_notename ());
102   SCM names = me->get_property ("note-names");
103   SCM charstr = SCM_EOL;
104   if (ly_c_vector_p (names))
105     charstr = scm_vector_ref (names, idx);
106   else
107     {
108       char s[2] = "a";
109       s[0] = (pit->get_notename () + 2)%7 + 'a';
110       s[0] = toupper (s[0]);
111       charstr = scm_makfrom0str (s);
112     }
113   
114   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
115                        charstr,
116                        scm_int2num (b),
117                        scm_int2num (1-b),
118                        SCM_UNDEFINED);
119   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
120   Stencil m (bx, at);
121
122   return m.smobbed_copy ();
123 }
124
125
126 Real
127 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
128 {
129   SCM brewer = me->get_property ("print-function");
130   Font_metric * fm  = Font_interface::get_default_font (me);
131   
132   if (brewer == Note_head::print_proc)
133     {
134       SCM style  = me->get_property ("style");
135       if (!scm_is_symbol (style))
136         {
137           return 0.0;
138         }
139       
140       SCM log = scm_int2num (Note_head::get_balltype (me));
141       SCM proc = me->get_property ("glyph-name-procedure");
142       SCM scm_font_char = scm_call_2 (proc, log, style);
143       String font_char = "noteheads-" + ly_scm2string (scm_font_char);
144
145       int k = fm->name_to_index (font_char) ;
146
147       if (k >= 0)
148         {
149           Box b = fm->get_indexed_char (k);
150           Offset wxwy = fm->get_indexed_wxwy (k);
151           Interval v = b[a];
152           if (!v.is_empty ())
153             return 2 * (wxwy[a] - v.center ()) / v.length ();
154         }
155     }
156   
157   /*
158     Fallback
159    */
160   SCM v = me->get_property ("stem-attachment-function");
161   if (!ly_c_procedure_p (v))
162     return 0.0;
163   
164   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
165   if (!ly_c_pair_p (result))
166     return 0.0;
167
168   result = (a == X_AXIS) ? ly_car (result) : ly_cdr (result);
169   
170   return robust_scm2double (result,0);
171 }
172
173 int
174 Note_head::get_balltype (Grob*me) 
175 {
176   SCM s = me->get_property ("duration-log");
177   return scm_is_number (s) ? scm_to_int (s) <? 2 : 0;
178 }
179
180 ADD_INTERFACE (Note_head,"note-head-interface",
181   "Note head",
182   "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
183