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