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