]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
* scm/chord-name.scm: remove new-chord-name-brew-molecule ; use
[lilypond.git] / lily / accidental.cc
1 /*
2   accidental.cc -- implement Accidental_interface
3
4   (c) 2001--2003 Han-Wen Nienhuys
5   
6  */
7 #include "font-interface.hh"
8 #include "item.hh"
9 #include "molecule.hh"
10 #include "accidental-interface.hh"
11 #include "paper-def.hh"
12
13 /*
14   TODO: insert support for smaller cautionaries, tie-break-reminders.
15   Either here or in new-accidental-engraver.
16
17   'accidentals should go, for a single 'accidental property -- see
18   accidental-placement.cc
19
20 */
21
22
23 Molecule
24 parenthesize (Grob*me, Molecule m)
25 {
26   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-leftparen"));
27   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-rightparen"));
28   m.add_at_edge (X_AXIS, LEFT, Molecule (open), 0,0);
29   m.add_at_edge (X_AXIS, RIGHT, Molecule (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_grob_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_grob_property ("cautionary")))
60     {
61       SCM cstyle = a->get_grob_property ("cautionary-style");
62       parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
63
64     }
65
66   SCM accs = a->get_grob_property ("accidentals");
67   SCM scm_style = a->get_grob_property ("style");
68   if (!gh_symbol_p (scm_style)
69       && !parens
70       && scm_ilength (accs) == 1)
71     {
72       if (gh_scm2int (gh_car (accs)) == -1)
73         {
74           Box stem = b;
75           Box bulb = b;
76
77           /*
78             we could make the stem thinner, but that places the flats
79             really close.
80           */
81           stem[X_AXIS][RIGHT] *= .5;
82           bulb[Y_AXIS][UP] *= .35;
83
84           boxes.push (bulb);
85           boxes.push (stem);
86         }
87       /*
88         TODO: add support for natural, double flat.
89        */
90     }
91
92   if (!boxes.size())
93     boxes.push (b);
94
95   Offset o (a->relative_coordinate (common[X_AXIS],  X_AXIS),
96             a->relative_coordinate (common[Y_AXIS],  Y_AXIS));
97   for(int i = boxes.size(); i--;)
98     {
99       boxes[i].translate(o);
100     }
101   
102   return boxes;
103 }
104
105 /*
106  * Some styles do not provide all flavours of accidentals, e.g. there
107  * is currently no sharp accidental in vaticana style.  In these cases
108  * this function falls back to one of the other styles.
109  */
110 String
111 Accidental_interface::get_fontcharname(String style, int alteration)
112 {
113   if (style == "hufnagel")
114     switch (alteration)
115       {
116       case -2: return "-2";
117       case -1: return "hufnagel-1";
118       case 0: return "vaticana0";
119       case 1: return "mensural1";
120       case 2: return "2";
121       }
122   if (style == "medicaea")
123     switch (alteration)
124       {
125       case -2: return "-2";
126       case -1: return "medicaea-1";
127       case 0: return "vaticana0";
128       case 1: return "mensural1";
129       case 2: return "2";
130       }
131   if (style == "vaticana")
132     switch (alteration)
133       {
134       case -2: return "-2";
135       case -1: return "vaticana-1";
136       case 0: return "vaticana0";
137       case 1: return "mensural1";
138       case 2: return "2";
139       }
140   if (style == "mensural")
141     switch (alteration)
142       {
143       case -2: return "-2";
144       case -1: return "mensural-1";
145       case 0: return "vaticana0";
146       case 1: return "mensural1";
147       case 2: return "2";
148       }
149   if (style == "neo_mensural")
150     style = ""; // currently same as default
151   if (style == "default")
152     style = "";
153   return style + to_string (alteration);
154 }
155
156 MAKE_SCHEME_CALLBACK (Accidental_interface,brew_molecule,1);
157 SCM
158 Accidental_interface::brew_molecule (SCM smob)
159 {
160   Grob *me = unsmob_grob (smob);
161   bool smaller = false;
162   bool parens = false;
163
164   bool caut  = to_boolean (me->get_grob_property ("cautionary"));
165   if (caut)
166     {
167       SCM cstyle = me->get_grob_property ("cautionary-style");
168       parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
169       smaller = gh_equal_p (cstyle, ly_symbol2scm ("smaller"));
170     }
171
172   SCM scm_style = me->get_grob_property ("style");
173   String style;
174   if (gh_symbol_p (scm_style))
175     {
176       style = ly_symbol2string (scm_style);
177     }
178   else
179     {
180       /*
181         preferably no name for the default style.
182       */
183       style = "";
184     }
185
186   Font_metric *fm = 0;
187   if (smaller)
188     {
189       SCM ac = Font_interface::font_alist_chain (me);
190       ac = gh_cons (gh_cons (gh_cons
191                              (ly_symbol2scm ("font-relative-size"),
192                               scm_int2num (-1)), SCM_EOL),
193                     ac);
194       fm = select_font (me->get_paper (), ac);
195     }
196   else
197     fm = Font_interface::get_default_font (me);
198
199   Molecule mol;
200   for (SCM s = me->get_grob_property ("accidentals");
201        gh_pair_p (s); s = gh_cdr (s))
202     {
203       int alteration = gh_scm2int (gh_car (s));
204       String font_char = get_fontcharname (style, alteration);
205       Molecule acc (fm->find_by_name ("accidentals-" + font_char));
206
207       if (acc.empty_b())
208         {
209           me->warning (_f ("accidental `%s' not found", font_char));
210         }
211       else
212         {
213           mol.add_at_edge (X_AXIS,  RIGHT, acc, 0.1,0);
214         }
215     }
216
217   if (parens)
218     mol = parenthesize (me, mol); 
219
220   return mol.smobbed_copy();
221 }
222
223
224
225 ADD_INTERFACE (Accidental_interface, "accidental-interface",
226               "a single accidental",
227                "cautionary cautionary-style style tie accidentals");