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