]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-signature-interface.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / key-signature-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   keyplacement by Mats Bengtsson
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "accidental-interface.hh"
23 #include "font-interface.hh"
24 #include "international.hh"
25 #include "item.hh"
26 #include "lookup.hh"
27 #include "output-def.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "rational.hh"
30 #include "lily-imports.hh"
31
32 using std::string;
33
34 struct Key_signature_interface
35 {
36   DECLARE_SCHEME_CALLBACK (print, (SCM));
37 };
38
39 /*
40   TODO
41   - space the `natural' signs wider
42 */
43 MAKE_SCHEME_CALLBACK (Key_signature_interface, print, 1);
44 SCM
45 Key_signature_interface::print (SCM smob)
46 {
47   Item *me = unsmob<Item> (smob);
48
49   Real inter = Staff_symbol_referencer::staff_space (me) / 2.0;
50
51   Stencil mol;
52
53   SCM c0s = me->get_property ("c0-position");
54
55   bool is_cancellation = me->internal_has_interface
56                          (ly_symbol2scm ("key-cancellation-interface"));
57
58   /*
59     SCM lists are stacks, so we work from right to left, ending with
60     the cancellation signature.
61   */
62
63   Slice pos, overlapping_pos;
64   SCM last_glyph_name = SCM_BOOL_F;
65   SCM padding_pairs = me->get_property ("padding-pairs");
66
67   Font_metric *fm = Font_interface::get_default_font (me);
68   SCM alist = me->get_property ("glyph-name-alist");
69
70   for (SCM s = me->get_property ("alteration-alist"); scm_is_pair (s); s = scm_cdr (s))
71     {
72       SCM alt = is_cancellation
73                 ? scm_from_int (0)
74                 : scm_cdar (s);
75
76       SCM glyph_name_scm = ly_assoc_get (alt, alist, SCM_BOOL_F);
77       if (!scm_is_string (glyph_name_scm))
78         {
79           me->warning (_f ("No glyph found for alteration: %s",
80                            ly_scm2rational (alt).to_string ().c_str ()));
81           continue;
82         }
83
84       string glyph_name = ly_scm2string (glyph_name_scm);
85
86       Stencil acc (fm->find_by_name (glyph_name));
87
88       if (acc.is_empty ())
89         me->warning (_ ("alteration not found"));
90       else
91         {
92           pos.set_empty ();
93           Stencil column;
94           for (SCM pos_list = Lily::key_signature_interface_alteration_positions
95                  (scm_car (s), c0s, smob);
96                scm_is_pair (pos_list); pos_list = scm_cdr (pos_list))
97             {
98               int p = scm_to_int (scm_car (pos_list));
99               pos.add_point (p);
100               column.add_stencil (acc.translated (Offset (0, p * inter)));
101             }
102           /*
103             The natural sign (unlike flat & sharp)
104             has vertical edges on both sides. A little padding is
105             needed to prevent collisions.
106           */
107           Real padding = robust_scm2double (me->get_property ("padding"),
108                                             0.0);
109           SCM handle = scm_assoc (scm_cons (glyph_name_scm, last_glyph_name),
110                                   padding_pairs);
111           if (scm_is_pair (handle))
112             padding = robust_scm2double (scm_cdr (handle), 0.0);
113           else if (glyph_name == "accidentals.natural"
114                    && !intersection (overlapping_pos, pos).is_empty ())
115             padding += 0.3;
116
117           mol.add_at_edge (X_AXIS, LEFT, column, padding);
118
119           pos.widen (4);
120           overlapping_pos = pos + 2;
121           last_glyph_name = glyph_name_scm;
122         }
123     }
124
125   mol.align_to (X_AXIS, LEFT);
126
127   return mol.smobbed_copy ();
128 }
129
130 ADD_INTERFACE (Key_signature_interface,
131                "A group of accidentals, to be printed as signature sign.",
132
133                /* properties */
134                "alteration-alist "
135                "c0-position "
136                "glyph-name-alist "
137                "flat-positions "
138                "sharp-positions "
139                "padding "
140                "padding-pairs "
141               );