]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
better fix for 366
[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 /* This callback exists for the sole purpose of allowing us to override
36    its pure equivalent to accidental-interface::pure-height */
37 MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
38 SCM
39 Accidental_interface::height (SCM smob)
40 {
41   return Grob::stencil_height (smob);
42 }
43
44 /* If this gets called before line breaking, we will return a non-trivial
45    width even if we belong to a tie and won't actually get printed. */
46 MAKE_SCHEME_CALLBACK (Accidental_interface, width, 1);
47 SCM
48 Accidental_interface::width (SCM smob)
49 {
50   Grob *me = unsmob_grob (smob);
51   Stencil *s = unsmob_stencil (get_stencil (me));
52
53   if (s)
54     return ly_interval2scm (s->extent (X_AXIS));
55   return ly_interval2scm (Interval ());
56 }
57
58 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
59 SCM
60 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
61 {
62   Item *me = dynamic_cast<Item*> (unsmob_grob (smob));
63   int start = scm_to_int (start_scm);
64   int rank = me->get_column ()->get_rank ();
65
66   bool visible = to_boolean (me->get_property ("forced"))
67     || !unsmob_grob (me->get_object ("tie"))
68     || rank != start + 1; /* we are in the middle of a line */
69
70   return visible ? Grob::stencil_height (smob) : ly_interval2scm (Interval ());
71 }
72
73 vector<Box>
74 Accidental_interface::accurate_boxes (Grob *me, Grob **common)
75 {
76   Box b;
77   b[X_AXIS] = me->extent (me, X_AXIS);
78   b[Y_AXIS] = me->extent (me, Y_AXIS);
79
80   vector<Box> boxes;
81
82   bool parens = to_boolean (me->get_property ("parenthesized"));
83   if (!me->is_live ())
84     return boxes;
85
86   if (!to_boolean (me->get_property ("restore-first"))
87       && !parens)
88     {
89       SCM alist = me->get_property ("glyph-name-alist");
90       SCM alt = me->get_property ("alteration");
91       string glyph_name = robust_scm2string (ly_assoc_get (alt, alist, SCM_BOOL_F),
92                                              "");
93       
94       if (glyph_name == "accidentals.flat"
95           || glyph_name == "accidentals.mirroredflat")
96         {
97           Box stem = b;
98           Box bulb = b;
99
100           /*
101             we could make the stem thinner, but that places the flats
102             really close.
103           */
104           Direction bulb_dir =
105             glyph_name=="accidentals.mirroredflat" ? LEFT : RIGHT;
106           stem[X_AXIS][bulb_dir] = stem[X_AXIS].center ();
107
108           /*
109             To prevent vertical alignment for 6ths
110           */
111           stem[Y_AXIS] *= 1.1;
112           bulb[Y_AXIS][UP] *= .35;
113
114           boxes.push_back (bulb);
115           boxes.push_back (stem);
116         }
117       else if (glyph_name ==  "accidentals.natural")
118         {
119           Box lstem = b;
120           Box rstem = b;
121           Box belly = b;
122
123           lstem[Y_AXIS] *= 1.1;
124           rstem[Y_AXIS] *= 1.1;
125
126           belly[Y_AXIS] *= 0.75;
127           lstem[X_AXIS][RIGHT] *= .33;
128           rstem[X_AXIS][LEFT] = rstem[X_AXIS].linear_combination (1.0 / 3.0);
129           lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
130           rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
131           boxes.push_back (belly);
132           boxes.push_back (lstem);
133           boxes.push_back (rstem);
134         }
135       /*
136         TODO: add support for, double flat.
137       */
138     }
139
140   if (!boxes.size ())
141     boxes.push_back (b);
142
143   Offset o (me->relative_coordinate (common[X_AXIS], X_AXIS),
144             me->relative_coordinate (common[Y_AXIS], Y_AXIS));
145
146   for (vsize i = boxes.size (); i--;)
147     boxes[i].translate (o);
148
149   return boxes;
150 }
151
152 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
153 SCM
154 Accidental_interface::print (SCM smob)
155 {
156   Grob *me = unsmob_grob (smob);
157   Grob *tie = unsmob_grob (me->get_object ("tie"));
158
159   if (tie && !tie->original ()
160       && !to_boolean (me->get_property ("forced")))
161     {
162       me->suicide ();
163       return SCM_EOL;
164     }
165
166   return get_stencil (me);
167 }
168
169 SCM
170 Accidental_interface::get_stencil (Grob *me)
171 {
172   Font_metric *fm = Font_interface::get_default_font (me);
173
174   SCM alist = me->get_property ("glyph-name-alist");
175   SCM alt = me->get_property ("alteration");
176   SCM glyph_name = ly_assoc_get (alt, alist, SCM_BOOL_F);
177   
178   if (!scm_is_string (glyph_name))
179     {
180       me->warning (_f ("Could not find glyph-name for alteration %s",
181                        ly_scm_write_string (alt).c_str ()));
182       return SCM_EOL;
183     }
184   
185   Stencil mol (fm->find_by_name (scm_i_string_chars (glyph_name)));
186   if (to_boolean (me->get_property ("restore-first")))
187     {
188       /*
189         this isn't correct for ancient accidentals, but they don't
190         use double flats/sharps anyway.
191         */
192       Stencil acc (fm->find_by_name ("accidentals.natural"));
193
194       if (acc.is_empty ())
195         me->warning (_ ("natural alteration glyph not found"));
196       else
197         mol.add_at_edge (X_AXIS, LEFT, acc, 0.1);
198     }
199   
200   if (to_boolean (me->get_property ("parenthesized")))
201     mol = parenthesize (me, mol);
202
203   return mol.smobbed_copy ();
204 }
205
206   
207 ADD_INTERFACE (Accidental_interface,
208                "a single accidental",
209                
210                /* props */
211                "alteration "
212                "avoid-slur "
213                "forced "
214                "parenthesized "
215                "restore-first "
216                "glyph-name-alist "
217                "tie "
218                );