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