]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / note-head.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 "grob.hh"
31 #include "international.hh"
32 #include "staff-symbol.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "warn.hh"
35
36 static Stencil
37 internal_print (Grob *me, string *font_char)
38 {
39   SCM style = me->get_property ("style");
40   if (!scm_is_symbol (style))
41     style = ly_symbol2scm ("default");
42
43   string suffix = to_string (min (robust_scm2int (me->get_property ("duration-log"), 2), 2));
44   if (style != ly_symbol2scm ("default"))
45     {
46       SCM gn = me->get_property ("glyph-name");
47       if (scm_is_string (gn))
48         suffix = ly_scm2string (gn);
49     }
50
51   Font_metric *fm = Font_interface::get_default_font (me);
52
53   string idx_symmetric;
54   string idx_directed;
55   string idx_either;
56   idx_symmetric = idx_either = "noteheads.s" + suffix;
57   Stencil out = fm->find_by_name (idx_symmetric);
58   if (out.is_empty ())
59     {
60       string prefix = "noteheads.";
61
62       Grob *stem = unsmob_grob (me->get_object ("stem"));
63       Direction stem_dir = stem ? get_grob_direction (stem) : CENTER;
64
65       if (stem_dir == CENTER)
66         programming_error ("must have stem dir for note head");
67
68       idx_directed = idx_either
69                      = prefix + ((stem_dir == UP) ? "u" : "d") + suffix;
70       out = fm->find_by_name (idx_directed);
71     }
72
73   if (out.is_empty ())
74     {
75       me->warning (_f ("none of note heads `%s' or `%s' found",
76                        idx_symmetric.c_str (), idx_directed.c_str ()));
77       out = Stencil (Box (Interval (0, 0), Interval (0, 0)), SCM_EOL);
78     }
79   else
80     *font_char = idx_either;
81
82   return out;
83 }
84
85 /*
86   TODO: make stem X-parent of notehead.
87  */
88 MAKE_SCHEME_CALLBACK (Note_head, stem_x_shift, 1);
89 SCM
90 Note_head::stem_x_shift (SCM smob)
91 {
92   Grob *me = unsmob_grob (smob);
93   Grob *stem = unsmob_grob (me->get_object ("stem"));
94   if (stem)
95     (void) stem->get_property ("positioning-done");
96
97   return scm_from_int (0);
98 }
99
100 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
101 SCM
102 Note_head::print (SCM smob)
103 {
104   Grob *me = unsmob_grob (smob);
105
106   string idx;
107   return internal_print (me, &idx).smobbed_copy ();
108 }
109
110 MAKE_SCHEME_CALLBACK (Note_head, include_ledger_line_height, 1);
111 SCM
112 Note_head::include_ledger_line_height (SCM smob)
113 {
114   Grob *me = unsmob_grob (smob);
115   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
116
117   if (staff)
118     {
119       Real ss = Staff_symbol::staff_space (staff);
120       Interval lines = Staff_symbol::line_span (staff) * (ss / 2.0);
121       Real my_pos = Staff_symbol_referencer::get_position (me) * ss / 2.0;
122       Interval my_ext = me->extent (me, Y_AXIS) + my_pos;
123
124       // The +1 and -1 come from the fact that we only want to add
125       // the interval between the note and the first ledger line, not
126       // the whole interval between the note and the staff.
127       Interval iv (min (0.0, lines[UP] - my_ext[DOWN] + 1),
128                    max (0.0, lines[DOWN] - my_ext[UP] - 1));
129       return ly_interval2scm (iv);
130     }
131
132   return ly_interval2scm (Interval (0, 0));
133 }
134
135 Real
136 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
137 {
138   Offset off = robust_scm2offset (me->get_property ("stem-attachment"),
139                                   Offset (0, 0));
140
141   return off [a];
142 }
143
144 Offset
145 Note_head::get_stem_attachment (Font_metric *fm, string key)
146 {
147   Offset att;
148
149   int k = fm->name_to_index (key);
150   if (k >= 0)
151     {
152       Box b = fm->get_indexed_char_dimensions (k);
153       Offset wxwy = fm->attachment_point (key);
154       for (int i = X_AXIS; i < NO_AXES; i++)
155         {
156           Axis a = Axis (i);
157
158           Interval v = b[a];
159           if (!v.is_empty ())
160             {
161               att[a] = (2 * (wxwy[a] - v.center ()) / v.length ());
162             }
163         }
164     }
165
166   return att;
167 }
168
169 MAKE_SCHEME_CALLBACK (Note_head, calc_stem_attachment, 1);
170 SCM
171 Note_head::calc_stem_attachment (SCM smob)
172 {
173   Grob *me = unsmob_grob (smob);
174   Font_metric *fm = Font_interface::get_default_font (me);
175   string key;
176   internal_print (me, &key);
177
178   return ly_offset2scm (get_stem_attachment (fm, key));
179 }
180
181 ADD_INTERFACE (Note_head,
182                "A note head.  There are many possible values for"
183                " @code{style}.  For a complete list, see"
184                " @ruser{Note head styles}.",
185
186                /* properties */
187                "note-names "
188                "accidental-grob "
189                "glyph-name "
190                "stem-attachment "
191                "style "
192               );
193