]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
Run grand-replace for 2010.
[lilypond.git] / lily / note-head.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "note-head.hh"
21
22 #include <cmath>
23 #include <cctype>
24 #include <algorithm>            //  min, max
25
26 using namespace std;
27
28 #include "directional-element-interface.hh"
29 #include "font-interface.hh"
30 #include "international.hh"
31 #include "warn.hh"
32 #include "grob.hh"
33
34 static Stencil
35 internal_print (Grob *me, string *font_char)
36 {
37   SCM style = me->get_property ("style");
38   if (!scm_is_symbol (style))
39     style = ly_symbol2scm ("default");
40
41   string suffix = to_string (min (robust_scm2int (me->get_property ("duration-log"), 2), 2));
42   if (style != ly_symbol2scm ("default"))
43     {
44       SCM gn = me->get_property ("glyph-name");
45       if (scm_is_string (gn))
46         suffix = ly_scm2string (gn);
47     }
48
49   Font_metric *fm = Font_interface::get_default_font (me);
50
51   string idx_symmetric;
52   string idx_directed;
53   string idx_either;
54   idx_symmetric = idx_either = "noteheads.s" + suffix;
55   Stencil out = fm->find_by_name (idx_symmetric);
56   if (out.is_empty ())
57     {
58       string prefix = "noteheads.";
59
60       Grob *stem = unsmob_grob (me->get_object ("stem"));
61       Direction stem_dir = stem ? get_grob_direction (stem) : CENTER;
62       
63       if (stem_dir == CENTER)
64         programming_error ("must have stem dir for note head");
65       
66       idx_directed = idx_either =
67         prefix + ((stem_dir == UP) ? "u" : "d") + suffix;
68       out = fm->find_by_name (idx_directed);
69     }
70
71
72   if (out.is_empty ())
73     {
74       me->warning (_f ("none of note heads `%s' or `%s' found",
75                        idx_symmetric.c_str (), idx_directed.c_str ()));
76       out = Stencil (Box (Interval (0, 0), Interval (0, 0)), SCM_EOL);
77     }
78   else
79     *font_char = idx_either;
80
81   return out;
82 }
83
84 /*
85   TODO: make stem X-parent of notehead. 
86  */
87 MAKE_SCHEME_CALLBACK (Note_head, stem_x_shift, 1);
88 SCM
89 Note_head::stem_x_shift (SCM smob)
90 {
91   Grob *me = unsmob_grob (smob);
92   Grob *stem = unsmob_grob (me->get_object ("stem"));
93   if (stem)
94     (void) stem->get_property ("positioning-done");
95
96   return scm_from_int (0);
97 }
98
99 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
100 SCM
101 Note_head::print (SCM smob)
102 {
103   Grob *me = unsmob_grob (smob);
104
105   string idx;
106   return internal_print (me, &idx).smobbed_copy ();
107 }
108
109 Real
110 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
111 {
112   Offset off = robust_scm2offset (me->get_property ("stem-attachment"),
113                                   Offset (0,0));
114   
115   return off [a];
116 }
117
118 Offset
119 Note_head::get_stem_attachment (Font_metric *fm, string key)
120 {
121   Offset att;
122   
123   int k = fm->name_to_index (key);
124   if (k >= 0)
125     {
126       Box b = fm->get_indexed_char (k);
127       Offset wxwy = fm->attachment_point (key);
128       for (int i = X_AXIS ; i < NO_AXES; i++)
129         {
130           Axis a = Axis (i);
131           
132           Interval v = b[a];
133           if (!v.is_empty ())
134             {
135               att[a] = (2 * (wxwy[a] - v.center ()) / v.length ());
136             }
137         }
138     }
139
140   return att;
141 }
142
143 MAKE_SCHEME_CALLBACK (Note_head, calc_stem_attachment, 1);
144 SCM
145 Note_head::calc_stem_attachment (SCM smob)
146 {
147   Grob *me  = unsmob_grob (smob);
148   Font_metric *fm = Font_interface::get_default_font (me);
149   string key;
150   internal_print (me, &key);
151
152   return ly_offset2scm (get_stem_attachment (fm, key));
153 }
154
155
156 ADD_INTERFACE (Note_head,
157                "A note head.  There are many possible values for"
158                " @code{style}.  For a complete list, see"
159                " @ruser{Note head styles}.",
160
161                /* properties */
162                "note-names "
163                "accidental-grob "
164                "glyph-name "
165                "stem-attachment "
166                "style "
167                );
168