release commit
[lilypond.git] / lily / accidental.cc
blobcbbbec7f2cade6daac4ffec37380a1d977f81d0a
1 #include "font-interface.hh"
2 #include "item.hh"
3 #include "molecule.hh"
4 #include "accidental-interface.hh"
6 /*
7 TODO: insert support for smaller cautionaries, tie-break-reminders.
8 Either here or in new-accidental-engraver.
10 'accidentals should go, for a single 'accidental property -- see
11 accidental-placement.cc
16 Molecule
17 parenthesize (Grob*me, Molecule m)
19 Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-leftparen"));
20 Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-rightparen"));
21 m.add_at_edge (X_AXIS, LEFT, Molecule (open), 0,0);
22 m.add_at_edge (X_AXIS, RIGHT, Molecule (close), 0,0);
24 return m;
28 MAKE_SCHEME_CALLBACK (Accidental_interface,after_line_breaking,1);
29 SCM
30 Accidental_interface::after_line_breaking (SCM smob)
32 Grob *me = unsmob_grob (smob);
33 Grob *tie = unsmob_grob (me->get_grob_property ("tie"));
35 if (tie && !tie->original_)
37 me->suicide ();
39 return SCM_UNSPECIFIED;
42 Array<Box>
43 Accidental_interface::accurate_boxes (Grob *a,Grob**common)
45 Box b;
46 b[X_AXIS] = a->extent (a, X_AXIS);
47 b[Y_AXIS] = a->extent (a, Y_AXIS);
49 Array<Box> boxes;
51 bool parens = false;
52 if (to_boolean (a->get_grob_property ("cautionary")))
54 SCM cstyle = a->get_grob_property ("cautionary-style");
55 parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
59 SCM accs = a->get_grob_property ("accidentals");
60 SCM scm_style = a->get_grob_property ("style");
61 if (!gh_symbol_p (scm_style)
62 && !parens
63 && scm_ilength (accs) == 1)
65 if (gh_scm2int (gh_car (accs)) == -1)
67 Box stem = b;
68 Box bulb = b;
71 we could make the stem thinner, but that places the flats
72 really close.
74 stem[X_AXIS][RIGHT] *= .5;
75 bulb[Y_AXIS][UP] *= .35;
77 boxes.push (bulb);
78 boxes.push (stem);
81 TODO: add support for natural, double flat.
85 if (!boxes.size())
86 boxes.push (b);
88 Offset o (a->relative_coordinate (common[X_AXIS], X_AXIS),
89 a->relative_coordinate (common[Y_AXIS], Y_AXIS));
90 for(int i = boxes.size(); i--;)
92 boxes[i].translate(o);
95 return boxes;
99 * Some styles do not provide all flavours of accidentals, e.g. there
100 * is currently no sharp accidental in vaticana style. In these cases
101 * this function falls back to one of the other styles.
103 String
104 Accidental_interface::get_fontcharname(String style, int alteration)
106 if (style == "hufnagel")
107 switch (alteration)
109 case -2: return "-2";
110 case -1: return "hufnagel-1";
111 case 0: return "vaticana0";
112 case 1: return "mensural1";
113 case 2: return "2";
115 if (style == "medicaea")
116 switch (alteration)
118 case -2: return "-2";
119 case -1: return "medicaea-1";
120 case 0: return "vaticana0";
121 case 1: return "mensural1";
122 case 2: return "2";
124 if (style == "vaticana")
125 switch (alteration)
127 case -2: return "-2";
128 case -1: return "vaticana-1";
129 case 0: return "vaticana0";
130 case 1: return "mensural1";
131 case 2: return "2";
133 if (style == "mensural")
134 switch (alteration)
136 case -2: return "-2";
137 case -1: return "mensural-1";
138 case 0: return "vaticana0";
139 case 1: return "mensural1";
140 case 2: return "2";
142 if (style == "neo_mensural")
143 style = ""; // currently same as default
144 if (style == "default")
145 style = "";
146 return style + to_string (alteration);
149 MAKE_SCHEME_CALLBACK (Accidental_interface,brew_molecule,1);
151 Accidental_interface::brew_molecule (SCM smob)
153 Grob *me = unsmob_grob (smob);
154 bool smaller = false;
155 bool parens = false;
157 bool caut = to_boolean (me->get_grob_property ("cautionary"));
158 if (caut)
160 SCM cstyle = me->get_grob_property ("cautionary-style");
161 parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
162 smaller = gh_equal_p (cstyle, ly_symbol2scm ("smaller"));
165 SCM scm_style = me->get_grob_property ("style");
166 String style;
167 if (gh_symbol_p (scm_style))
169 style = ly_symbol2string (scm_style);
171 else
174 preferably no name for the default style.
176 style = "";
179 Font_metric *fm = 0;
180 if (smaller)
182 SCM ac = Font_interface::font_alist_chain (me);
183 ac = gh_cons (gh_cons (gh_cons
184 (ly_symbol2scm ("font-relative-size"),
185 scm_int2num (-1)), SCM_EOL),
186 ac);
187 fm = Font_interface::get_font (me, ac);
189 else
190 fm = Font_interface::get_default_font (me);
192 Molecule mol;
193 for (SCM s = me->get_grob_property ("accidentals");
194 gh_pair_p (s); s = gh_cdr (s))
196 int alteration = gh_scm2int (gh_car (s));
197 String font_char = get_fontcharname (style, alteration);
198 Molecule acc (fm->find_by_name ("accidentals-" + font_char));
200 if (acc.empty_b())
202 me->warning (_f ("accidental `%s' not found", font_char));
204 else
206 mol.add_at_edge (X_AXIS, RIGHT, acc, 0.1,0);
210 if (parens)
211 mol = parenthesize (me, mol);
213 return mol.smobbed_copy();
218 ADD_INTERFACE (Accidental_interface, "accidental-interface",
219 "a single accidental",
220 "cautionary cautionary-style style tie accidentals");