]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
*** empty log message ***
[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 "font-interface.hh"
10 #include "item.hh"
11 #include "stencil.hh"
12 #include "accidental-interface.hh"
13 #include "output-def.hh"
14 #include "pitch.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     {
44       me->suicide ();
45     }
46   return SCM_UNSPECIFIED;
47 }
48
49 Array<Box>
50 Accidental_interface::accurate_boxes (Grob *a, Grob **common)
51 {
52   Box b;
53   b[X_AXIS] = a->extent (a, X_AXIS);
54   b[Y_AXIS] = a->extent (a, Y_AXIS);
55
56   Array<Box> boxes;
57
58   bool parens = false;
59   if (to_boolean (a->get_property ("cautionary")))
60     {
61       SCM cstyle = a->get_property ("cautionary-style");
62       parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
63     }
64
65   SCM accs = a->get_property ("accidentals");
66   SCM scm_style = a->get_property ("style");
67   if (!scm_is_symbol (scm_style)
68       && !parens
69       && scm_ilength (accs) == 1)
70     {
71       switch (scm_to_int (scm_car (accs)))
72         {
73         case FLAT:
74           {
75             Box stem = b;
76             Box bulb = b;
77
78             /*
79               we could make the stem thinner, but that places the flats
80               really close.
81             */
82             stem[X_AXIS][RIGHT] *= .5;
83
84             /*
85               To prevent vertical alignment for 6ths
86             */
87             stem[Y_AXIS] *= 1.1;
88             bulb[Y_AXIS][UP] *= .35;
89
90             boxes.push (bulb);
91             boxes.push (stem);
92           }
93           break;
94         case NATURAL:
95           {
96             Box lstem = b;
97             Box rstem = b;
98             Box belly = b;
99
100             lstem[Y_AXIS] *= 1.1;
101             rstem[Y_AXIS] *= 1.1;
102
103             belly[Y_AXIS] *= 0.75;
104             lstem[X_AXIS][RIGHT] *= .5;
105             rstem[X_AXIS][LEFT] = lstem[X_AXIS][RIGHT];
106             lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
107             rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
108             boxes.push (belly);
109             boxes.push (lstem);
110             boxes.push (rstem);
111           }
112           break;
113           /*
114             TODO: add support for, double flat.
115           */
116         }
117     }
118       
119   if (!boxes.size ())
120     boxes.push (b);
121
122   Offset o (a->relative_coordinate (common[X_AXIS], X_AXIS),
123             a->relative_coordinate (common[Y_AXIS], Y_AXIS));
124   for (int i = boxes.size (); i--;)
125     {
126       boxes[i].translate (o);
127     }
128
129   return boxes;
130 }
131
132
133 /*
134        * Some styles do not provide all flavours of accidentals, e.g. there
135        * is currently no sharp accidental in vaticana style.  In these cases
136        * this function falls back to one of the other styles.
137        */
138
139       /*
140         todo: this sort of stuff in Scheme. --hwn.
141       */
142       String
143         Accidental_interface::get_fontcharname (String style, int alteration)
144       {
145         if (alteration == DOUBLE_FLAT
146             || alteration == DOUBLE_SHARP)
147           {
148             return to_string (alteration);
149           }
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_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
200             smaller = ly_c_equal_p (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           {
207             style = ly_symbol2string (scm_style);
208           }
209         else
210           {
211             /*
212               preferably no name for the default style.
213             */
214             style = "";
215           }
216
217         Font_metric *fm = 0;
218         if (smaller)
219           {
220             SCM ac = Font_interface::music_font_alist_chain (me);
221             /*
222               TODO: should calc font-size by adding -2 to current font-size
223             */
224             ac = scm_cons (scm_list_1 (scm_cons
225                                        (ly_symbol2scm ("font-size"),
226                                         scm_int2num (-2))),
227                            ac);
228             fm = select_font (me->get_layout (), ac);
229           }
230         else
231           fm = Font_interface::get_default_font (me);
232
233         Stencil mol;
234         for (SCM s = me->get_property ("accidentals");
235              scm_is_pair (s); s = scm_cdr (s))
236           {
237             int alteration = scm_to_int (scm_car (s));
238             String font_char = get_fontcharname (style, alteration);
239             Stencil acc (fm->find_by_name ("accidentals." + font_char));
240
241             if (acc.is_empty ())
242               {
243                 me->warning (_f ("accidental `%s' not found", font_char));
244               }
245             else
246               {
247                 mol.add_at_edge (X_AXIS, RIGHT, acc, 0.1, 0);
248               }
249           }
250
251         if (parens)
252           mol = parenthesize (me, mol);
253
254         return mol.smobbed_copy ();
255       }
256
257 /*
258   TODO: should move inside-slur into item?
259
260 */
261 ADD_INTERFACE (Accidental_interface, "accidental-interface",
262                "a single accidental",
263                "inside-slur cautionary cautionary-style style tie accidentals");