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