]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
dc3c873878f095709d4c7187bf37f30bcbbf0849
[lilypond.git] / lily / accidental.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--2014 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 #include "skyline-pair.hh"
29
30 Stencil
31 parenthesize (Grob *me, Stencil m)
32 {
33   Font_metric *font
34     = Font_interface::get_default_font (me);
35   Stencil open
36     = font->find_by_name ("accidentals.leftparen");
37   Stencil close
38     = font->find_by_name ("accidentals.rightparen");
39
40   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0);
41   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0);
42
43   return m;
44 }
45
46 /* If this gets called before line breaking, we will return a non-trivial
47    extent even if we belong to a tie and won't actually get printed. */
48 static SCM
49 get_extent (Grob *me, Axis a)
50 {
51   Stencil *s = unsmob_stencil (Accidental_interface::get_stencil (me));
52
53   if (s)
54     return ly_interval2scm (s->extent (a));
55   return ly_interval2scm (Interval ());
56 }
57
58 MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
59 SCM
60 Accidental_interface::height (SCM smob)
61 {
62   return get_extent (unsmob_grob (smob), Y_AXIS);
63 }
64
65 MAKE_SCHEME_CALLBACK (Accidental_interface, width, 1);
66 SCM
67 Accidental_interface::width (SCM smob)
68 {
69   return get_extent (unsmob_grob (smob), X_AXIS);
70 }
71
72 MAKE_SCHEME_CALLBACK (Accidental_interface, horizontal_skylines, 1);
73 SCM
74 Accidental_interface::horizontal_skylines (SCM smob)
75 {
76   Grob *me = unsmob_grob (smob);
77   if (!me->is_live ())
78     return Skyline_pair ().smobbed_copy ();
79
80   /*
81    * Using the print function may trigger a suicide
82    * before line breaking. It is therefore `unpure' (c).
83    * We use the more basic get_stencil.
84    */
85   Stencil *my_stencil = unsmob_stencil (get_stencil (me));
86   if (!my_stencil)
87     return Skyline_pair ().smobbed_copy ();
88
89   Skyline_pair *sky =
90     Skyline_pair::unsmob
91       (Stencil::skylines_from_stencil
92         (my_stencil->smobbed_copy (), 0.0, Y_AXIS));
93
94   SCM alist = me->get_property ("glyph-name-alist");
95   SCM alt = me->get_property ("alteration");
96   string glyph_name = robust_scm2string (ly_assoc_get (alt, alist, SCM_BOOL_F),
97                                                        "");
98   if (glyph_name == "accidentals.flat"
99       || glyph_name == "accidentals.flatflat")
100     {
101       // a bit more padding for the right of the stem
102       // we raise the stem horizontally to a bit less than the average
103       // horizontal "height" of the entire glyph. This will bring flats
104       // closer to doubleflats, which looks better (MS opinion).
105       // this should work for all fonts where the flat is not
106       // completely bizarre
107       Real left = my_stencil->extent (X_AXIS)[LEFT];
108       Real right = my_stencil->extent (X_AXIS)[RIGHT] * 0.375;
109       Real down = my_stencil->extent (Y_AXIS)[DOWN];
110       Real up = my_stencil->extent (Y_AXIS)[UP];
111       vector<Box> boxes;
112       boxes.push_back (Box (Interval (left, right), Interval (down, up)));
113       Skyline merge_with_me (boxes, Y_AXIS, RIGHT);
114       (*sky)[RIGHT].merge (merge_with_me);
115     }
116   return sky->smobbed_copy ();
117 }
118
119 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
120 SCM
121 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
122 {
123   Item *me = dynamic_cast<Item *> (unsmob_grob (smob));
124   int start = scm_to_int (start_scm);
125   int rank = me->get_column ()->get_rank ();
126
127   if (to_boolean (me->get_property ("forced"))
128       || !unsmob_grob (me->get_object ("tie"))
129       || (rank == start + 1 && /* we are at the start of a line */
130           !to_boolean (me->get_property ("hide-tied-accidental-after-break"))))
131     {
132       Stencil *s = unsmob_stencil (get_stencil (me));
133       if (s)
134         return ly_interval2scm (s->extent (Y_AXIS));
135     }
136
137   return ly_interval2scm (Interval ());
138 }
139
140 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
141 SCM
142 Accidental_interface::print (SCM smob)
143 {
144   Grob *me = unsmob_grob (smob);
145   Grob *tie = unsmob_grob (me->get_object ("tie"));
146
147   if (tie
148       && (to_boolean (me->get_property ("hide-tied-accidental-after-break"))
149           || (!tie->original () && !to_boolean (me->get_property ("forced")))))
150     {
151       me->suicide ();
152       return SCM_EOL;
153     }
154
155   return get_stencil (me);
156 }
157
158 SCM
159 Accidental_interface::get_stencil (Grob *me)
160 {
161   Font_metric *fm = Font_interface::get_default_font (me);
162
163   SCM alist = me->get_property ("glyph-name-alist");
164   SCM alt = me->get_property ("alteration");
165   SCM glyph_name = ly_assoc_get (alt, alist, SCM_BOOL_F);
166   Stencil mol;
167
168   if (!scm_is_string (glyph_name))
169     {
170       me->warning (_f ("Could not find glyph-name for alteration %s",
171                        ly_scm_write_string (alt).c_str ()));
172       mol = fm->find_by_name ("noteheads.s1cross");
173     }
174   else
175     mol = fm->find_by_name (ly_scm2string (glyph_name));
176
177   if (to_boolean (me->get_property ("restore-first")))
178     {
179       /*
180         this isn't correct for ancient accidentals, but they don't
181         use double flats/sharps anyway.
182         */
183       Stencil acc (fm->find_by_name ("accidentals.natural"));
184
185       if (acc.is_empty ())
186         me->warning (_ ("natural alteration glyph not found"));
187       else
188         mol.add_at_edge (X_AXIS, LEFT, acc, 0.1);
189     }
190
191   if (to_boolean (me->get_property ("parenthesized")))
192     mol = parenthesize (me, mol);
193
194   return mol.smobbed_copy ();
195 }
196
197 ADD_INTERFACE (Accidental_interface,
198                "A single accidental.",
199
200                /* properties */
201                "alteration "
202                "avoid-slur "
203                "forced "
204                "glyph-name-alist "
205                "glyph-name "
206                "hide-tied-accidental-after-break "
207                "parenthesized "
208                "restore-first "
209                "tie "
210               );