]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
* flower
[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 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
79 SCM
80 Note_head::print (SCM smob)
81 {
82   Grob *me = unsmob_grob (smob);
83
84   String idx;
85   return internal_print (me, &idx).smobbed_copy ();
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 (scm_is_vector (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 Real
126 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
127 {
128   SCM brewer = me->get_property ("print-function");
129   Font_metric *fm = Font_interface::get_default_font (me);
130
131   if (brewer == Note_head::print_proc)
132     {
133       String key;
134       internal_print (me, &key);
135
136       int k = fm->name_to_index (key);
137       if (k >= 0)
138         {
139           Box b = fm->get_indexed_char (k);
140           Offset wxwy = fm->attachment_point (key);
141           Interval v = b[a];
142           if (!v.is_empty ())
143             return 2 * (wxwy[a] - v.center ()) / v.length ();
144         }
145     }
146
147   /*
148     Fallback
149   */
150   SCM v = me->get_property ("stem-attachment-function");
151   if (!ly_c_procedure_p (v))
152     return 0.0;
153
154   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
155   if (!scm_is_pair (result))
156     return 0.0;
157
158   result = (a == X_AXIS) ? scm_car (result) : scm_cdr (result);
159
160   return robust_scm2double (result, 0);
161 }
162
163 int
164 Note_head::get_balltype (Grob *me)
165 {
166   SCM s = me->get_property ("duration-log");
167   return scm_is_number (s) ? scm_to_int (s) <? 2 : 0;
168 }
169
170 ADD_INTERFACE (Note_head, "note-head-interface",
171                "Note head",
172                "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
173