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