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