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