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