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