]> 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 #include <algorithm>            //  min, max
14
15 using namespace std; 
16
17 #include "directional-element-interface.hh"
18 #include "staff-symbol.hh"
19 #include "misc.hh"
20 #include "dots.hh"
21 #include "warn.hh"
22 #include "font-interface.hh"
23 #include "event.hh"
24 #include "rhythmic-head.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "lookup.hh"
27 #include "output-def.hh"
28
29 /*
30   clean up the mess left by ledger line handling.
31 */
32 static Stencil
33 internal_print (Grob *me, String *font_char)
34 {
35   SCM style = me->get_property ("style");
36   if (!scm_is_symbol (style))
37     {
38       return Stencil ();
39     }
40
41   SCM log = scm_int2num (Note_head::get_balltype (me));
42   SCM proc = me->get_property ("glyph-name-procedure");
43   SCM scm_font_char = scm_call_2 (proc, log, style);
44
45   Font_metric *fm = Font_interface::get_default_font (me);
46
47   Direction stem_dir = CENTER;
48   if (Grob *stem = unsmob_grob (me->get_property ("stem")))
49     {
50       stem_dir = get_grob_direction (stem);
51       if (stem_dir == CENTER)
52         programming_error ("must have stem dir for note head");
53     }
54
55   Stencil out;
56
57   String prefix = "noteheads.";
58   String idx
59     = prefix + ((stem_dir == UP) ? "u" : "d") + ly_scm2string (scm_font_char);
60   out = fm->find_by_name (idx);
61   if (out.is_empty ())
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 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 MAKE_SCHEME_CALLBACK (Note_head, brew_ez_stencil, 1);
90 SCM
91 Note_head::brew_ez_stencil (SCM smob)
92 {
93   Grob *me = unsmob_grob (smob);
94   int l = Note_head::get_balltype (me);
95
96   int b = (l >= 2);
97
98   SCM cause = me->get_property ("cause");
99   SCM spitch = unsmob_music (cause)->get_property ("pitch");
100   Pitch *pit = unsmob_pitch (spitch);
101
102   SCM idx = scm_int2num (pit->get_notename ());
103   SCM names = me->get_property ("note-names");
104   SCM charstr = SCM_EOL;
105   if (scm_is_vector (names))
106     charstr = scm_vector_ref (names, idx);
107   else
108     {
109       char s[2] = "a";
110       s[0] = (pit->get_notename () + 2)%7 + 'a';
111       s[0] = toupper (s[0]);
112       charstr = scm_makfrom0str (s);
113     }
114   
115   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
116                        charstr,
117                        scm_int2num (b),
118                        scm_int2num (1 - b),
119                        SCM_UNDEFINED);
120   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
121   Stencil m (bx, at);
122
123   return m.smobbed_copy ();
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       String key;
135       internal_print (me, &key);
136
137       int k = fm->name_to_index (key);
138       if (k >= 0)
139         {
140           Box b = fm->get_indexed_char (k);
141           Offset wxwy = fm->attachment_point (key);
142           Interval v = b[a];
143           if (!v.is_empty ())
144             return 2 * (wxwy[a] - v.center ()) / v.length ();
145         }
146     }
147
148   /*
149     Fallback
150   */
151   SCM v = me->get_property ("stem-attachment-function");
152   if (!ly_c_procedure_p (v))
153     return 0.0;
154
155   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
156   if (!scm_is_pair (result))
157     return 0.0;
158
159   result = (a == X_AXIS) ? scm_car (result) : scm_cdr (result);
160
161   return robust_scm2double (result, 0);
162 }
163
164 int
165 Note_head::get_balltype (Grob *me)
166 {
167   SCM s = me->get_property ("duration-log");
168   return scm_is_number (s) ? min ((int) scm_to_int (s), (int) 2) : 0;
169 }
170
171 ADD_INTERFACE (Note_head, "note-head-interface",
172                "Note head",
173                "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
174