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