]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
use classnames for interface naming; remove inclusion of
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "note-head.hh"
10
11 #include <cmath>
12 #include <cctype>
13 #include <algorithm>            //  min, max
14
15 using namespace std;
16
17 #include "directional-element-interface.hh"
18 #include "font-interface.hh"
19 #include "international.hh"
20 #include "warn.hh"
21
22 static Stencil
23 internal_print (Grob *me, string *font_char)
24 {
25   SCM style = me->get_property ("style");
26   if (!scm_is_symbol (style))
27     style = ly_symbol2scm ("default");
28
29   string suffix = to_string (min (robust_scm2int (me->get_property ("duration-log"), 2), 2));
30   if (style != ly_symbol2scm ("default"))
31     {
32       SCM gn = me->get_property ("glyph-name");
33       if (scm_is_string (gn))
34         suffix = ly_scm2string (gn);
35     }
36
37   Font_metric *fm = Font_interface::get_default_font (me);
38
39   string idx_symmetric;
40   string idx_directed;
41   string idx_either;
42   idx_symmetric = idx_either = "noteheads.s" + suffix;
43   Stencil out = fm->find_by_name (idx_symmetric);
44   if (out.is_empty ())
45     {
46       string prefix = "noteheads.";
47
48       Grob *stem = unsmob_grob (me->get_object ("stem"));
49       Direction stem_dir = stem ? get_grob_direction (stem) : CENTER;
50       
51       if (stem_dir == CENTER)
52         programming_error ("must have stem dir for note head");
53       
54       idx_directed = idx_either =
55         prefix + ((stem_dir == UP) ? "u" : "d") + suffix;
56       out = fm->find_by_name (idx_directed);
57     }
58
59
60   if (out.is_empty ())
61     {
62       me->warning (_f ("none of note heads `%s' or `%s' found",
63                        idx_symmetric.c_str (), idx_directed.c_str ()));
64       out = Stencil (Box (Interval (0, 0), Interval (0, 0)), SCM_EOL);
65     }
66   else
67     *font_char = idx_either;
68
69   return out;
70 }
71
72 /*
73   TODO: make stem X-parent of notehead. 
74  */
75 MAKE_SCHEME_CALLBACK (Note_head, stem_x_shift, 1);
76 SCM
77 Note_head::stem_x_shift (SCM smob)
78 {
79   Grob *me = unsmob_grob (smob);
80   Grob *stem = unsmob_grob (me->get_object ("stem"));
81   if (stem)
82     (void) stem->get_property ("positioning-done");
83
84   return scm_from_int (0);
85 }
86
87 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
88 SCM
89 Note_head::print (SCM smob)
90 {
91   Grob *me = unsmob_grob (smob);
92
93   string idx;
94   return internal_print (me, &idx).smobbed_copy ();
95 }
96
97 Real
98 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
99 {
100   Offset off = robust_scm2offset (me->get_property ("stem-attachment"),
101                                   Offset (0,0));
102   
103   return off [a];
104 }
105
106 Offset
107 Note_head::get_stem_attachment (Font_metric *fm, string key)
108 {
109   Offset att;
110   
111   int k = fm->name_to_index (key);
112   if (k >= 0)
113     {
114       Box b = fm->get_indexed_char (k);
115       Offset wxwy = fm->attachment_point (key);
116       for (int i = X_AXIS ; i < NO_AXES; i++)
117         {
118           Axis a = Axis (i);
119           
120           Interval v = b[a];
121           if (!v.is_empty ())
122             {
123               att[a] = (2 * (wxwy[a] - v.center ()) / v.length ());
124             }
125         }
126     }
127
128   return att;
129 }
130
131 MAKE_SCHEME_CALLBACK(Note_head, calc_stem_attachment, 1);
132 SCM
133 Note_head::calc_stem_attachment (SCM smob)
134 {
135   Grob *me  = unsmob_grob (smob);
136   Font_metric *fm = Font_interface::get_default_font (me);
137   string key;
138   internal_print (me, &key);
139
140   return ly_offset2scm (get_stem_attachment (fm, key));
141 }
142
143
144 ADD_INTERFACE (Note_head,
145                "Note head",
146
147                /* properties */
148                "note-names "
149                "accidental-grob "
150                "glyph-name "
151                "stem-attachment "
152                "style "
153                );
154