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