]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
Run grand-replace for 2012
[lilypond.git] / lily / accidental.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--2012 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 "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"
28
29 Stencil
30 parenthesize (Grob *me, Stencil m)
31 {
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");
38
39   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0);
40   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0);
41
42   return m;
43 }
44
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)
49 {
50   Stencil *s = unsmob_stencil (Accidental_interface::get_stencil (me));
51
52   if (s)
53     return ly_interval2scm (s->extent (a));
54   return ly_interval2scm (Interval ());
55 }
56
57 MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
58 SCM
59 Accidental_interface::height (SCM smob)
60 {
61   return get_extent (unsmob_grob (smob), Y_AXIS);
62 }
63
64 MAKE_SCHEME_CALLBACK (Accidental_interface, width, 1);
65 SCM
66 Accidental_interface::width (SCM smob)
67 {
68   return get_extent (unsmob_grob (smob), X_AXIS);
69 }
70
71 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
72 SCM
73 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
74 {
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 ();
78
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"))))
83     {
84       Stencil *s = unsmob_stencil (get_stencil (me));
85       if (s)
86         return ly_interval2scm (s->extent (Y_AXIS));
87     }
88
89   return ly_interval2scm (Interval ());
90 }
91
92 vector<Box>
93 Accidental_interface::accurate_boxes (Grob *me, Grob **common)
94 {
95   Box b;
96   b[X_AXIS] = me->extent (me, X_AXIS);
97   b[Y_AXIS] = me->extent (me, Y_AXIS);
98
99   vector<Box> boxes;
100
101   bool parens = to_boolean (me->get_property ("parenthesized"));
102   if (!me->is_live ())
103     return boxes;
104
105   if (!to_boolean (me->get_property ("restore-first"))
106       && !parens)
107     {
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                                              "");
112
113       if (glyph_name == "accidentals.flat"
114           || glyph_name == "accidentals.mirroredflat")
115         {
116           Box stem = b;
117           Box bulb = b;
118
119           /*
120             we could make the stem thinner, but that places the flats
121             really close.
122           */
123           Direction bulb_dir
124             = glyph_name == "accidentals.mirroredflat" ? LEFT : RIGHT;
125           stem[X_AXIS][bulb_dir] = stem[X_AXIS].center ();
126
127           /*
128             To prevent vertical alignment for 6ths
129           */
130           stem[Y_AXIS] *= 1.1;
131           bulb[Y_AXIS][UP] *= .35;
132
133           boxes.push_back (bulb);
134           boxes.push_back (stem);
135         }
136       else if (glyph_name == "accidentals.natural")
137         {
138           Box lstem = b;
139           Box rstem = b;
140           Box belly = b;
141
142           lstem[Y_AXIS] *= 1.1;
143           rstem[Y_AXIS] *= 1.1;
144
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);
153         }
154       /*
155         TODO: add support for, double flat.
156       */
157     }
158
159   if (!boxes.size ())
160     boxes.push_back (b);
161
162   Offset o (me->relative_coordinate (common[X_AXIS], X_AXIS),
163             me->relative_coordinate (common[Y_AXIS], Y_AXIS));
164
165   for (vsize i = boxes.size (); i--;)
166     boxes[i].translate (o);
167
168   return boxes;
169 }
170
171 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
172 SCM
173 Accidental_interface::print (SCM smob)
174 {
175   Grob *me = unsmob_grob (smob);
176   Grob *tie = unsmob_grob (me->get_object ("tie"));
177
178   if (tie
179       && (to_boolean (me->get_property ("hide-tied-accidental-after-break"))
180           || (!tie->original () && !to_boolean (me->get_property ("forced")))))
181     {
182       me->suicide ();
183       return SCM_EOL;
184     }
185
186   return get_stencil (me);
187 }
188
189 SCM
190 Accidental_interface::get_stencil (Grob *me)
191 {
192   Font_metric *fm = Font_interface::get_default_font (me);
193
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);
197
198   if (!scm_is_string (glyph_name))
199     {
200       me->warning (_f ("Could not find glyph-name for alteration %s",
201                        ly_scm_write_string (alt).c_str ()));
202       return SCM_EOL;
203     }
204
205   Stencil mol (fm->find_by_name (ly_scm2string (glyph_name)));
206   if (to_boolean (me->get_property ("restore-first")))
207     {
208       /*
209         this isn't correct for ancient accidentals, but they don't
210         use double flats/sharps anyway.
211         */
212       Stencil acc (fm->find_by_name ("accidentals.natural"));
213
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);
218     }
219
220   if (to_boolean (me->get_property ("parenthesized")))
221     mol = parenthesize (me, mol);
222
223   return mol.smobbed_copy ();
224 }
225
226 ADD_INTERFACE (Accidental_interface,
227                "A single accidental.",
228
229                /* properties */
230                "alteration "
231                "avoid-slur "
232                "forced "
233                "glyph-name-alist "
234                "hide-tied-accidental-after-break "
235                "parenthesized "
236                "restore-first "
237                "tie "
238               );