]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
* lily/slur-quanting.cc (score_extra_encompass): Bigger penalty
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "font-interface.hh"
9 #include "item.hh"
10 #include "stencil.hh"
11 #include "accidental-interface.hh"
12 #include "output-def.hh"
13 #include "pitch.hh"
14
15 /*
16   TODO: insert support for smaller cautionaries, tie-break-reminders.
17   Either here or in new-accidental-engraver.
18
19   'accidentals should go, for a single 'accidental property -- see
20   accidental-placement.cc
21
22 */
23 Stencil
24 parenthesize (Grob*me, Stencil m)
25 {
26   Stencil open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-leftparen"));
27   Stencil close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-rightparen"));
28   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0,0);
29   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0,0);
30
31   return m;
32 }
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 (!ly_c_symbol_p (scm_style)
68       && !parens
69       && scm_ilength (accs) == 1)
70     {
71       if (ly_scm2int (ly_car (accs)) == 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       
92       /*
93         TODO: add support for natural, double flat.
94        */
95     }
96
97   if (!boxes.size ())
98     boxes.push (b);
99
100   Offset o (a->relative_coordinate (common[X_AXIS],  X_AXIS),
101             a->relative_coordinate (common[Y_AXIS],  Y_AXIS));
102   for (int i = boxes.size (); i--;)
103     {
104       boxes[i].translate (o);
105     }
106   
107   return boxes;
108 }
109
110 /*
111  * Some styles do not provide all flavours of accidentals, e.g. there
112  * is currently no sharp accidental in vaticana style.  In these cases
113  * this function falls back to one of the other styles.
114  */
115
116 /*
117   todo: this sort of stuff in Scheme. --hwn.
118  */
119 String
120 Accidental_interface::get_fontcharname (String style, int alteration)
121 {
122   if (alteration == DOUBLE_FLAT
123       || alteration == DOUBLE_SHARP)
124     {
125       return to_string (alteration);
126     }
127   
128   if (style == "hufnagel")
129     switch (alteration)
130       {
131       case FLAT: return "hufnagel-1";
132       case 0: return "vaticana0";
133       case SHARP: return "mensural1";
134       }
135   if (style == "medicaea")
136     switch (alteration)
137       {
138       case FLAT: return "medicaea-1";
139       case 0: return "vaticana0";
140       case SHARP: return "mensural1";
141       }
142   if (style == "vaticana")
143     switch (alteration)
144       {
145       case FLAT: return "vaticana-1";
146       case 0: return "vaticana0";
147       case SHARP: return "mensural1";
148       }
149   if (style == "mensural")
150     switch (alteration)
151       {
152       case FLAT: return "mensural-1";
153       case 0: return "vaticana0";
154       case SHARP: return "mensural1";
155       }
156   
157   if (style == "neo_mensural")
158     style = ""; // currently same as default
159   if (style == "default")
160     style = "";
161   return style + to_string (alteration);
162 }
163
164 MAKE_SCHEME_CALLBACK (Accidental_interface,print,1);
165 SCM
166 Accidental_interface::print (SCM smob)
167 {
168   Grob *me = unsmob_grob (smob);
169   bool smaller = false;
170   bool parens = false;
171
172   bool caut  = to_boolean (me->get_property ("cautionary"));
173   if (caut)
174     {
175       SCM cstyle = me->get_property ("cautionary-style");
176       parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
177       smaller = ly_c_equal_p (cstyle, ly_symbol2scm ("smaller"));
178     }
179
180   SCM scm_style = me->get_property ("style");
181   String style;
182   if (ly_c_symbol_p (scm_style))
183     {
184       style = ly_symbol2string (scm_style);
185     }
186   else
187     {
188       /*
189         preferably no name for the default style.
190       */
191       style = "";
192     }
193
194   Font_metric *fm = 0;
195   if (smaller)
196     {
197       SCM ac = Font_interface::text_font_alist_chain (me);
198       ac = scm_cons (scm_cons (scm_cons
199                              (ly_symbol2scm ("font-size"),
200                               scm_int2num (-2)), SCM_EOL),
201                     ac);
202       fm = select_font (me->get_paper (), ac);
203     }
204   else
205     fm = Font_interface::get_default_font (me);
206
207   Stencil mol;
208   for (SCM s = me->get_property ("accidentals");
209        ly_c_pair_p (s); s = ly_cdr (s))
210     {
211       int alteration = ly_scm2int (ly_car (s));
212       String font_char = get_fontcharname (style, alteration);
213       Stencil acc (fm->find_by_name ("accidentals-" + font_char));
214
215       if (acc.is_empty ())
216         {
217           me->warning (_f ("accidental `%s' not found", font_char));
218         }
219       else
220         {
221           mol.add_at_edge (X_AXIS,  RIGHT, acc, 0.1,0);
222         }
223     }
224
225   if (parens)
226     mol = parenthesize (me, mol); 
227
228   return mol.smobbed_copy ();
229 }
230
231
232
233 ADD_INTERFACE (Accidental_interface, "accidental-interface",
234               "a single accidental",
235                "cautionary cautionary-style style tie accidentals");