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