]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
Revert "Do accidental suicide in print function."
[lilypond.git] / lily / accidental.cc
1 /*
2   accidental.cc -- implement Accidental_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "accidental-interface.hh"
10 #include "font-interface.hh"
11 #include "international.hh"
12 #include "item.hh"
13 #include "output-def.hh"
14 #include "paper-column.hh"
15 #include "pitch.hh"
16 #include "stencil.hh"
17
18 Stencil
19 parenthesize (Grob *me, Stencil m)
20 {
21   Font_metric * font
22     = Font_interface::get_default_font (me); 
23   Stencil open
24     = font->find_by_name ("accidentals.leftparen");
25   Stencil close
26     = font->find_by_name ("accidentals.rightparen");
27
28   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0);
29   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0);
30
31   return m;
32 }
33
34 /*
35   Hmm. Need separate callback, or perhaps #'live bool property.
36  */
37 MAKE_SCHEME_CALLBACK (Accidental_interface, after_line_breaking, 1);
38 SCM
39 Accidental_interface::after_line_breaking (SCM smob)
40 {
41   Grob *me = unsmob_grob (smob);
42   Grob *tie = unsmob_grob (me->get_object ("tie"));
43
44   if (tie && !tie->original ()
45       && !to_boolean (me->get_property ("forced")))
46     {
47       me->suicide ();
48     }
49  
50   return SCM_UNSPECIFIED;
51 }
52
53 /* This callback exists for the sole purpose of allowing us to override
54    its pure equivalent to accidental-interface::pure-height */
55 MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
56 SCM
57 Accidental_interface::height (SCM smob)
58 {
59   return Grob::stencil_height (smob);
60 }
61
62 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
63 SCM
64 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
65 {
66   Item *me = dynamic_cast<Item*> (unsmob_grob (smob));
67   int start = scm_to_int (start_scm);
68   int rank = me->get_column ()->get_rank ();
69
70   bool visible = to_boolean (me->get_property ("forced"))
71     || !unsmob_grob (me->get_object ("tie"))
72     || rank != start + 1; /* we are in the middle of a line */
73
74   return visible ? Grob::stencil_height (smob) : ly_interval2scm (Interval ());
75 }
76
77 vector<Box>
78 Accidental_interface::accurate_boxes (Grob *me, Grob **common)
79 {
80   Box b;
81   b[X_AXIS] = me->extent (me, X_AXIS);
82   b[Y_AXIS] = me->extent (me, Y_AXIS);
83
84   vector<Box> boxes;
85
86   bool parens = to_boolean (me->get_property ("parenthesized"));
87   if (!me->is_live ())
88     return boxes;
89
90   if (!to_boolean (me->get_property ("restore-first"))
91       && !parens)
92     {
93       SCM alist = me->get_property ("glyph-name-alist");
94       SCM alt = me->get_property ("alteration");
95       string glyph_name = robust_scm2string (ly_assoc_get (alt, alist, SCM_BOOL_F),
96                                              "");
97       
98       if (glyph_name == "accidentals.flat"
99           || glyph_name == "accidentals.mirroredflat")
100         {
101           Box stem = b;
102           Box bulb = b;
103
104           /*
105             we could make the stem thinner, but that places the flats
106             really close.
107           */
108           Direction bulb_dir =
109             glyph_name=="accidentals.mirroredflat" ? LEFT : RIGHT;
110           stem[X_AXIS][bulb_dir] = stem[X_AXIS].center ();
111
112           /*
113             To prevent vertical alignment for 6ths
114           */
115           stem[Y_AXIS] *= 1.1;
116           bulb[Y_AXIS][UP] *= .35;
117
118           boxes.push_back (bulb);
119           boxes.push_back (stem);
120         }
121       else if (glyph_name ==  "accidentals.natural")
122         {
123           Box lstem = b;
124           Box rstem = b;
125           Box belly = b;
126
127           lstem[Y_AXIS] *= 1.1;
128           rstem[Y_AXIS] *= 1.1;
129
130           belly[Y_AXIS] *= 0.75;
131           lstem[X_AXIS][RIGHT] *= .33;
132           rstem[X_AXIS][LEFT] = rstem[X_AXIS].linear_combination (1.0 / 3.0);
133           lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
134           rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
135           boxes.push_back (belly);
136           boxes.push_back (lstem);
137           boxes.push_back (rstem);
138         }
139       /*
140         TODO: add support for, double flat.
141       */
142     }
143
144   if (!boxes.size ())
145     boxes.push_back (b);
146
147   Offset o (me->relative_coordinate (common[X_AXIS], X_AXIS),
148             me->relative_coordinate (common[Y_AXIS], Y_AXIS));
149
150   for (vsize i = boxes.size (); i--;)
151     boxes[i].translate (o);
152
153   return boxes;
154 }
155
156 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
157 SCM
158 Accidental_interface::print (SCM smob)
159 {
160   Grob *me = unsmob_grob (smob);
161
162   Font_metric *fm = Font_interface::get_default_font (me);
163
164   SCM alist = me->get_property ("glyph-name-alist");
165   SCM alt = me->get_property ("alteration");
166   SCM glyph_name = ly_assoc_get (alt, alist, SCM_BOOL_F);
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       return SCM_EOL;
173     }
174   
175   Stencil mol (fm->find_by_name (scm_i_string_chars (glyph_name)));
176   if (to_boolean (me->get_property ("restore-first")))
177     {
178       /*
179         this isn't correct for ancient accidentals, but they don't
180         use double flats/sharps anyway.
181         */
182       Stencil acc (fm->find_by_name ("accidentals.natural"));
183
184       if (acc.is_empty ())
185         me->warning (_ ("natural alteration glyph not found"));
186       else
187         mol.add_at_edge (X_AXIS, LEFT, acc, 0.1);
188     }
189   
190   if (to_boolean (me->get_property ("parenthesized")))
191     mol = parenthesize (me, mol);
192
193   return mol.smobbed_copy ();
194 }
195
196   
197 ADD_INTERFACE (Accidental_interface,
198                "a single accidental",
199                
200                /* props */
201                "alteration "
202                "avoid-slur "
203                "forced "
204                "parenthesized "
205                "restore-first "
206                "glyph-name-alist "
207                "tie "
208                );