]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
* flower/include/std-string.hh:
[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 /*
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     style = ly_symbol2scm ("default");
38
39   string suffix = to_string (min (robust_scm2int (me->get_property ("duration-log"), 2), 2));
40   if (style != ly_symbol2scm ("default"))
41     {
42       SCM gn = me->get_property ("glyph-name");
43       if (scm_is_string (gn))
44         suffix = ly_scm2string (gn);
45     }
46
47   Font_metric *fm = Font_interface::get_default_font (me);
48
49   string idx = "noteheads.s" + suffix;
50
51   Stencil out = fm->find_by_name (idx);
52   if (out.is_empty ())
53     {
54       string prefix = "noteheads.";
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 = prefix + ((stem_dir == UP) ? "u" : "d") + suffix;
62       out = fm->find_by_name (idx);
63     }
64
65   if (out.is_empty ())
66     {
67       me->warning (_f ("note head `%s' not found", idx.c_str ()));
68       out = Stencil (Box (Interval (0, 0), Interval (0, 0)), SCM_EOL);
69     }
70   else
71     *font_char = idx;
72
73   return out;
74 }
75
76 /*
77   TODO: make stem X-parent of notehead. 
78  */
79 MAKE_SCHEME_CALLBACK (Note_head, stem_x_shift, 1);
80 SCM
81 Note_head::stem_x_shift (SCM smob)
82 {
83   Grob *me = unsmob_grob (smob);
84   Grob *stem = unsmob_grob (me->get_object ("stem"));
85   if (stem)
86     (void) stem->get_property ("positioning-done");
87
88   return scm_from_int (0);
89 }
90
91 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
92 SCM
93 Note_head::print (SCM smob)
94 {
95   Grob *me = unsmob_grob (smob);
96
97   string idx;
98   return internal_print (me, &idx).smobbed_copy ();
99 }
100
101 Real
102 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
103 {
104   Offset off = robust_scm2offset (me->get_property ("stem-attachment"),
105                                   Offset (0,0));
106   
107   return off [a];
108 }
109
110 MAKE_SCHEME_CALLBACK(Note_head, calc_stem_attachment, 1);
111 SCM
112 Note_head::calc_stem_attachment (SCM smob)
113 {
114   Grob *me  = unsmob_grob (smob);
115   Font_metric *fm = Font_interface::get_default_font (me);
116   string key;
117   internal_print (me, &key);
118
119   Offset att;
120   
121   int k = fm->name_to_index (key);
122   if (k >= 0)
123     {
124       Box b = fm->get_indexed_char (k);
125       Offset wxwy = fm->attachment_point (key);
126       for (int i = X_AXIS ; i < NO_AXES; i++)
127         {
128           Axis a = Axis (i);
129           
130           Interval v = b[a];
131           if (!v.is_empty ())
132             {
133               att[a] = (2 * (wxwy[a] - v.center ()) / v.length ());
134             }
135         }
136     }
137
138   return ly_offset2scm (att);
139 }
140
141
142 int
143 Note_head::get_balltype (Grob *me)
144 {
145   SCM s = me->get_property ("duration-log");
146   return scm_is_number (s) ? min (scm_to_int (s), 2) : 0;
147 }
148
149 ADD_INTERFACE (Note_head, "note-head-interface",
150                "Note head",
151
152                /* properties */
153                "note-names "
154                "accidental-grob "
155                "glyph-name "
156                "stem-attachment "
157                "style "
158                );
159