]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-signature-interface.cc
Run `make grand-replace'.
[lilypond.git] / lily / key-signature-interface.cc
1 /*
2   key-item.cc -- implement Key_signature_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8   keyplacement by Mats Bengtsson
9 */
10
11 #include "accidental-interface.hh"
12 #include "font-interface.hh"
13 #include "international.hh"
14 #include "item.hh"
15 #include "lookup.hh"
16 #include "output-def.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "rational.hh"
19
20 struct Key_signature_interface
21 {
22   DECLARE_SCHEME_CALLBACK (print, (SCM));
23   DECLARE_GROB_INTERFACE ();
24 };
25
26
27 /*
28   TODO
29   - space the `natural' signs wider
30 */
31 MAKE_SCHEME_CALLBACK (Key_signature_interface, print, 1);
32 SCM
33 Key_signature_interface::print (SCM smob)
34 {
35   Item *me = dynamic_cast<Item *> (unsmob_grob (smob));
36
37   Real inter = Staff_symbol_referencer::staff_space (me) / 2.0;
38
39   SCM scm_style = me->get_property ("style");
40   string style;
41   if (scm_is_symbol (scm_style))
42     style = ly_symbol2string (scm_style);
43   else
44     style = "";
45
46   Stencil mol;
47
48   SCM c0s = me->get_property ("c0-position");
49
50   bool is_cancellation = me->internal_has_interface
51     (ly_symbol2scm ("key-cancellation-interface"));
52
53   /*
54     SCM lists are stacks, so we work from right to left, ending with
55     the cancellation signature.
56   */
57
58   int last_pos = -1000;
59   SCM last_glyph_name = SCM_BOOL_F;
60   SCM padding_pairs = me->get_property ("padding-pairs");
61     
62   Font_metric *fm = Font_interface::get_default_font (me);
63   SCM alist = me->get_property ("glyph-name-alist");
64
65   for (SCM s = me->get_property ("alteration-alist"); scm_is_pair (s); s = scm_cdr (s))
66     {
67       SCM alt = is_cancellation
68         ? scm_from_int (0)
69         : scm_cdar (s);
70
71       SCM glyph_name_scm = ly_assoc_get (alt, alist, SCM_BOOL_F);
72       if (!scm_is_string (glyph_name_scm))
73         {
74           me->warning (_f ("No glyph found for alteration: %s",
75                            ly_scm2rational (alt).to_string ().c_str ()));
76           continue;
77         }
78
79       string glyph_name = ly_scm2string (glyph_name_scm);
80
81       Stencil acc (fm->find_by_name (glyph_name));
82
83       if (acc.is_empty ())
84         me->warning (_ ("alteration not found"));
85       else
86         {
87           SCM what = scm_caar (s);
88
89           SCM proc = ly_lily_module_constant ("key-signature-interface::alteration-position");
90
91           int pos = scm_to_int (scm_call_3 (proc, what, scm_cdar (s), c0s));
92           acc.translate_axis (pos * inter, Y_AXIS);
93
94           /*
95             The natural sign (unlike flat & sharp)
96             has vertical edges on both sides. A little padding is
97             needed to prevent collisions.
98           */
99           Real padding = robust_scm2double (me->get_property ("padding"),
100                                             0.0);
101           SCM handle = scm_assoc (scm_cons (glyph_name_scm, last_glyph_name),
102                                   padding_pairs);
103           if (scm_is_pair (handle))
104             padding = robust_scm2double (scm_cdr (handle), 0.0);
105           else if (glyph_name ==  "accidentals.natural"
106               && last_pos < pos + 2
107               && last_pos > pos - 6)
108             padding += 0.3;
109
110           mol.add_at_edge (X_AXIS, LEFT, acc, padding);
111           
112           last_pos = pos;
113           last_glyph_name = glyph_name_scm;
114         }
115     }
116
117   mol.align_to (X_AXIS, LEFT);
118
119   return mol.smobbed_copy ();
120 }
121
122 ADD_INTERFACE (Key_signature_interface,
123                "A group of accidentals, to be printed as signature sign.",
124
125                /* properties */
126                "alteration-alist "
127                "c0-position "
128                "glyph-name-alist "
129                "padding "
130                "padding-pairs "
131                "style "
132                );