]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006 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 "pitch.hh"
15 #include "stencil.hh"
16
17 /*
18   TODO: insert support for smaller cautionaries, tie-break-reminders.
19   Either here or in new-accidental-engraver.
20
21   'accidentals should go, for a single 'accidental property -- see
22   accidental-placement.cc
23 */
24 Stencil
25 parenthesize (Grob *me, Stencil m)
26 {
27   Font_metric * font
28     = Font_interface::get_default_font (me); 
29   Stencil open
30     = font->find_by_name ("accidentals.leftparen");
31   Stencil close
32     = font->find_by_name ("accidentals.rightparen");
33
34   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0, 0);
35   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0, 0);
36
37   return m;
38 }
39
40 /*
41   Hmm. Need separate callback, or perhaps #'live bool property.
42  */
43 MAKE_SCHEME_CALLBACK (Accidental_interface, after_line_breaking, 1);
44 SCM
45 Accidental_interface::after_line_breaking (SCM smob)
46 {
47   Grob *me = unsmob_grob (smob);
48   Grob *tie = unsmob_grob (me->get_object ("tie"));
49
50   if (tie && !tie->original ())
51     me->suicide ();
52   return SCM_UNSPECIFIED;
53 }
54
55 vector<Box>
56 Accidental_interface::accurate_boxes (Grob *a, Grob **common)
57 {
58   Box b;
59   b[X_AXIS] = a->extent (a, X_AXIS);
60   b[Y_AXIS] = a->extent (a, Y_AXIS);
61
62   vector<Box> boxes;
63
64   bool parens = false;
65   if (to_boolean (a->get_property ("cautionary")))
66     {
67       SCM cstyle = a->get_property ("cautionary-style");
68       parens = ly_is_equal (cstyle, ly_symbol2scm ("parentheses"));
69     }
70
71   SCM accs = a->get_property ("accidentals");
72   SCM scm_style = a->get_property ("style");
73   if (!scm_is_symbol (scm_style)
74       && !parens
75       && scm_ilength (accs) == 1)
76     {
77       switch (scm_to_int (scm_car (accs)))
78         {
79         case FLAT:
80           {
81             Box stem = b;
82             Box bulb = b;
83
84             /*
85               we could make the stem thinner, but that places the flats
86               really close.
87             */
88             stem[X_AXIS][RIGHT] *= .5;
89
90             /*
91               To prevent vertical alignment for 6ths
92             */
93             stem[Y_AXIS] *= 1.1;
94             bulb[Y_AXIS][UP] *= .35;
95
96             boxes.push_back (bulb);
97             boxes.push_back (stem);
98           }
99           break;
100         case NATURAL:
101           {
102             Box lstem = b;
103             Box rstem = b;
104             Box belly = b;
105
106             lstem[Y_AXIS] *= 1.1;
107             rstem[Y_AXIS] *= 1.1;
108
109             belly[Y_AXIS] *= 0.75;
110             lstem[X_AXIS][RIGHT] *= .33;
111             rstem[X_AXIS][LEFT] = rstem[X_AXIS].linear_combination (1.0 / 3.0);
112             lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
113             rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
114             boxes.push_back (belly);
115             boxes.push_back (lstem);
116             boxes.push_back (rstem);
117           }
118           break;
119           /*
120             TODO: add support for, double flat.
121           */
122         }
123     }
124
125   if (!boxes.size ())
126     boxes.push_back (b);
127
128   Offset o (a->relative_coordinate (common[X_AXIS], X_AXIS),
129             a->relative_coordinate (common[Y_AXIS], Y_AXIS));
130   for (vsize i = boxes.size (); i--;)
131     boxes[i].translate (o);
132
133   return boxes;
134 }
135
136 /*
137  * Some styles do not provide all flavours of accidentals, e.g. there
138  * is currently no sharp accidental in vaticana style.  In these cases
139  * this function falls back to one of the other styles.
140  */
141
142 /*
143   todo: this sort of stuff in Scheme. --hwn.
144 */
145 string
146 Accidental_interface::get_fontcharname (string style, int alteration)
147 {
148   if (alteration == DOUBLE_FLAT
149       || alteration == DOUBLE_SHARP)
150     return to_string (alteration);
151
152   if (style == "hufnagel")
153     switch (alteration)
154       {
155       case FLAT: return "hufnagel-1";
156       case 0: return "vaticana0";
157       case SHARP: return "mensural1";
158       }
159   if (style == "medicaea")
160     switch (alteration)
161       {
162       case FLAT: return "medicaea-1";
163       case 0: return "vaticana0";
164       case SHARP: return "mensural1";
165       }
166   if (style == "vaticana")
167     switch (alteration)
168       {
169       case FLAT: return "vaticana-1";
170       case 0: return "vaticana0";
171       case SHARP: return "mensural1";
172       }
173   if (style == "mensural")
174     switch (alteration)
175       {
176       case FLAT: return "mensural-1";
177       case 0: return "vaticana0";
178       case SHARP: return "mensural1";
179       }
180
181   if (style == "neomensural")
182     style = ""; // currently same as default
183   if (style == "default")
184     style = "";
185   return style + to_string (alteration);
186 }
187
188 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
189 SCM
190 Accidental_interface::print (SCM smob)
191 {
192   Grob *me = unsmob_grob (smob);
193   bool smaller = false;
194   bool parens = false;
195
196   bool caut = to_boolean (me->get_property ("cautionary"));
197   if (caut)
198     {
199       SCM cstyle = me->get_property ("cautionary-style");
200       parens = ly_is_equal (cstyle, ly_symbol2scm ("parentheses"));
201       smaller = ly_is_equal (cstyle, ly_symbol2scm ("smaller"));
202     }
203
204   SCM scm_style = me->get_property ("style");
205   string style;
206   if (scm_is_symbol (scm_style))
207     style = ly_symbol2string (scm_style);
208   else
209     /*
210       preferably no name for the default style.
211     */
212     style = "";
213
214   Font_metric *fm = 0;
215   if (smaller)
216     {
217       SCM ac = Font_interface::music_font_alist_chain (me);
218       /*
219         TODO: should calc font-size by adding -2 to current font-size
220       */
221       ac = scm_cons (scm_list_1 (scm_cons
222                                  (ly_symbol2scm ("font-size"),
223                                   scm_from_int (-2))),
224                      ac);
225       fm = select_font (me->layout (), ac);
226     }
227   else
228     fm = Font_interface::get_default_font (me);
229
230   Stencil mol;
231   for (SCM s = me->get_property ("accidentals");
232        scm_is_pair (s); s = scm_cdr (s))
233     {
234       int alteration = scm_to_int (scm_car (s));
235       string font_char = get_fontcharname (style, alteration);
236       Stencil acc (fm->find_by_name ("accidentals." + font_char));
237
238       if (acc.is_empty ())
239         me->warning (_f ("accidental `%s' not found", font_char));
240       else
241         mol.add_at_edge (X_AXIS, RIGHT, acc, 0.1, 0);
242     }
243
244   if (parens)
245     mol = parenthesize (me, mol);
246
247   return mol.smobbed_copy ();
248 }
249
250 /*
251   TODO: should move avoid-slur into item?
252 */
253 ADD_INTERFACE (Accidental_interface, "accidental-interface",
254                "a single accidental",
255                "accidentals "
256                "avoid-slur "
257                "cautionary "
258                "cautionary-style "
259                "style "
260                "tie "
261                );