Docs: Snippets: Rename new snippets so titles match file names.
[lilypond/mpolesky.git] / lily / accidental.cc
blob1bbf7e8a058ba8092ffb13925aefbb71608a0348
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2001--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "accidental-interface.hh"
21 #include "font-interface.hh"
22 #include "international.hh"
23 #include "item.hh"
24 #include "output-def.hh"
25 #include "paper-column.hh"
26 #include "pitch.hh"
27 #include "stencil.hh"
29 Stencil
30 parenthesize (Grob *me, Stencil m)
32 Font_metric * font
33 = Font_interface::get_default_font (me);
34 Stencil open
35 = font->find_by_name ("accidentals.leftparen");
36 Stencil close
37 = font->find_by_name ("accidentals.rightparen");
39 m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0);
40 m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0);
42 return m;
45 /* If this gets called before line breaking, we will return a non-trivial
46 extent even if we belong to a tie and won't actually get printed. */
47 static SCM
48 get_extent (Grob *me, Axis a)
50 Stencil *s = unsmob_stencil (Accidental_interface::get_stencil (me));
52 if (s)
53 return ly_interval2scm (s->extent (a));
54 return ly_interval2scm (Interval ());
57 MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
58 SCM
59 Accidental_interface::height (SCM smob)
61 return get_extent (unsmob_grob (smob), Y_AXIS);
64 MAKE_SCHEME_CALLBACK (Accidental_interface, width, 1);
65 SCM
66 Accidental_interface::width (SCM smob)
68 return get_extent (unsmob_grob (smob), X_AXIS);
71 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
72 SCM
73 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
75 Item *me = dynamic_cast<Item*> (unsmob_grob (smob));
76 int start = scm_to_int (start_scm);
77 int rank = me->get_column ()->get_rank ();
79 if (to_boolean (me->get_property ("forced"))
80 || !unsmob_grob (me->get_object ("tie"))
81 || (rank == start + 1 && /* we are at the start of a line */
82 !to_boolean (me->get_property ("hide-tied-accidental-after-break"))))
84 Stencil *s = unsmob_stencil (get_stencil (me));
85 if (s)
86 return ly_interval2scm (s->extent (Y_AXIS));
89 return ly_interval2scm (Interval ());
92 vector<Box>
93 Accidental_interface::accurate_boxes (Grob *me, Grob **common)
95 Box b;
96 b[X_AXIS] = me->extent (me, X_AXIS);
97 b[Y_AXIS] = me->extent (me, Y_AXIS);
99 vector<Box> boxes;
101 bool parens = to_boolean (me->get_property ("parenthesized"));
102 if (!me->is_live ())
103 return boxes;
105 if (!to_boolean (me->get_property ("restore-first"))
106 && !parens)
108 SCM alist = me->get_property ("glyph-name-alist");
109 SCM alt = me->get_property ("alteration");
110 string glyph_name = robust_scm2string (ly_assoc_get (alt, alist, SCM_BOOL_F),
111 "");
113 if (glyph_name == "accidentals.flat"
114 || glyph_name == "accidentals.mirroredflat")
116 Box stem = b;
117 Box bulb = b;
120 we could make the stem thinner, but that places the flats
121 really close.
123 Direction bulb_dir =
124 glyph_name == "accidentals.mirroredflat" ? LEFT : RIGHT;
125 stem[X_AXIS][bulb_dir] = stem[X_AXIS].center ();
128 To prevent vertical alignment for 6ths
130 stem[Y_AXIS] *= 1.1;
131 bulb[Y_AXIS][UP] *= .35;
133 boxes.push_back (bulb);
134 boxes.push_back (stem);
136 else if (glyph_name == "accidentals.natural")
138 Box lstem = b;
139 Box rstem = b;
140 Box belly = b;
142 lstem[Y_AXIS] *= 1.1;
143 rstem[Y_AXIS] *= 1.1;
145 belly[Y_AXIS] *= 0.75;
146 lstem[X_AXIS][RIGHT] *= .33;
147 rstem[X_AXIS][LEFT] = rstem[X_AXIS].linear_combination (1.0 / 3.0);
148 lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
149 rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
150 boxes.push_back (belly);
151 boxes.push_back (lstem);
152 boxes.push_back (rstem);
155 TODO: add support for, double flat.
159 if (!boxes.size ())
160 boxes.push_back (b);
162 Offset o (me->relative_coordinate (common[X_AXIS], X_AXIS),
163 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
165 for (vsize i = boxes.size (); i--;)
166 boxes[i].translate (o);
168 return boxes;
171 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
173 Accidental_interface::print (SCM smob)
175 Grob *me = unsmob_grob (smob);
176 Grob *tie = unsmob_grob (me->get_object ("tie"));
178 if (tie &&
179 (to_boolean (me->get_property ("hide-tied-accidental-after-break"))
180 || (!tie->original () && !to_boolean (me->get_property ("forced")))))
182 me->suicide ();
183 return SCM_EOL;
186 return get_stencil (me);
190 Accidental_interface::get_stencil (Grob *me)
192 Font_metric *fm = Font_interface::get_default_font (me);
194 SCM alist = me->get_property ("glyph-name-alist");
195 SCM alt = me->get_property ("alteration");
196 SCM glyph_name = ly_assoc_get (alt, alist, SCM_BOOL_F);
198 if (!scm_is_string (glyph_name))
200 me->warning (_f ("Could not find glyph-name for alteration %s",
201 ly_scm_write_string (alt).c_str ()));
202 return SCM_EOL;
205 Stencil mol (fm->find_by_name (ly_scm2string (glyph_name)));
206 if (to_boolean (me->get_property ("restore-first")))
209 this isn't correct for ancient accidentals, but they don't
210 use double flats/sharps anyway.
212 Stencil acc (fm->find_by_name ("accidentals.natural"));
214 if (acc.is_empty ())
215 me->warning (_ ("natural alteration glyph not found"));
216 else
217 mol.add_at_edge (X_AXIS, LEFT, acc, 0.1);
220 if (to_boolean (me->get_property ("parenthesized")))
221 mol = parenthesize (me, mol);
223 return mol.smobbed_copy ();
227 ADD_INTERFACE (Accidental_interface,
228 "A single accidental.",
230 /* properties */
231 "alteration "
232 "avoid-slur "
233 "forced "
234 "glyph-name-alist "
235 "hide-tied-accidental-after-break "
236 "parenthesized "
237 "restore-first "
238 "tie "