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