release commit
[lilypond.git] / lily / text-item.cc
blob098ef17402e5cad7cba87197374826aee0c61181
1 /*
2 text-item.cc -- implement Text_item
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9 #include <math.h>
11 #include "warn.hh"
12 #include "text-item.hh"
13 #include "paper-def.hh"
14 #include "font-interface.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "main.hh"
18 #include "all-font-metrics.hh"
19 #include "afm.hh"
20 #include "lookup.hh"
21 #include "virtual-font-metric.hh"
25 TEXT: STRING
26 | (MARKUP? TEXT+)
29 HEAD: MARKUP-ITEM | (MARKUP-ITEM+)
31 MARKUP-ITEM: PROPERTY | ABBREV | FONT-STYLE
32 PROPERTY: (key . value)
33 ABBREV: rows lines roman music bold italic named super sub text
37 Molecule
38 Text_item::text2molecule (Grob *me, SCM text, SCM alist_chain)
40 if (gh_string_p (text))
41 return string2molecule (me, text, alist_chain);
42 else if (gh_pair_p (text))
44 /* urg, why not just do this in markup_text2molecule ? */
45 if (gh_string_p (ly_car (text)))
46 return markup_text2molecule (me,
47 gh_append2 (scm_list_n (SCM_EOL,
48 SCM_UNDEFINED),
49 text),
50 alist_chain);
52 Allow (faulty) texts that are in an extra list:
53 #'(("foo"))
55 else if (scm_ilength (text) <= 1)
56 return text2molecule (me, ly_car (text), alist_chain);
57 else
58 return markup_text2molecule (me, text, alist_chain);
60 return Molecule ();
64 MAKE_SCHEME_CALLBACK(Text_item,text_to_molecule,3);
65 SCM
66 Text_item::text_to_molecule (SCM grob, SCM props, SCM markup)
68 Grob *me = unsmob_grob (grob);
70 return Text_item::text2molecule (me, markup, props).smobbed_copy();
74 Molecule
75 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
77 SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
78 alist_chain);
79 if (gh_pair_p (style) && gh_symbol_p (ly_cdr (style)))
80 alist_chain = Font_interface::add_style (me, ly_cdr (style), alist_chain);
82 Font_metric *fm = Font_interface::get_font (me, alist_chain);
84 SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
86 Molecule mol;
87 if (gh_pair_p (lookup) && ly_cdr (lookup) ==ly_symbol2scm ("name"))
88 mol = lookup_character (me, fm, text);
89 else
90 mol = lookup_text (me, fm, text);
92 return mol;
95 Molecule
96 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
98 return fm->find_by_name (ly_scm2string (char_name));
103 Molecule
104 Text_item::lookup_text (Grob *, Font_metric*fm, SCM text)
106 SCM list = scm_list_n (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
108 if (dynamic_cast<Virtual_font_metric*> (fm))
111 ARGH.
113 programming_error ("Can't use virtual font for text.");
115 else
116 list = fontify_atom (fm, list);
118 return Molecule (fm->text_dimension (ly_scm2string (text)), list);
123 TODO:
125 DOCME.
128 MARKUP_TEXT must be compound (may not be simple string.)
131 Molecule
132 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
133 SCM alist_chain)
135 SCM f = me->get_paper ()->lookup_variable (ly_symbol2scm ("markup-to-properties"));
137 SCM markup = ly_car (markup_text);
138 SCM text = ly_cdr (markup_text);
140 /* ARGRGRRGRARGRA
143 SCM abbrev = me->get_paper ()->lookup_variable (ly_symbol2scm ("abbreviation-alist"));
144 SCM style = me->get_paper ()->lookup_variable (ly_symbol2scm ("style-alist"));
146 SCM p = gh_cons (scm_call_3 (f, abbrev, style, markup), alist_chain);
148 Real staff_space = Staff_symbol_referencer::staff_space (me);
151 Line mode is default.
153 Axis axis = X_AXIS;
155 SCM a = ly_assoc_chain (ly_symbol2scm ("axis"), p);
156 if (gh_pair_p (a) && ly_axis_p (ly_cdr (a)))
157 axis = (Axis)gh_scm2int (ly_cdr (a));
159 Real baseline_skip = 0;
160 SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
161 if (gh_pair_p (b) && gh_number_p (ly_cdr (b)))
162 baseline_skip = gh_scm2double (ly_cdr (b)) * staff_space;
164 Real kern[2] = {0,0};
166 SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
167 if (gh_pair_p (k) && gh_number_p (ly_cdr (k)))
168 kern[axis] = gh_scm2double (ly_cdr (k)) * staff_space;
170 Real raise = 0;
171 SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
172 if (gh_pair_p (r) && gh_number_p (ly_cdr (r)))
173 raise = gh_scm2double (ly_cdr (r)) * staff_space;
176 Interval extent;
177 bool extent_b = false;
178 SCM e = ly_assoc_chain (ly_symbol2scm ("extent"), p);
179 if (gh_pair_p (e) && ly_number_pair_p (ly_cdr (e)))
181 extent = Interval (gh_scm2double (ly_cadr (e)) * staff_space,
182 gh_scm2double (ly_cddr (e)) * staff_space);
183 extent_b = true;
186 Offset o (kern[X_AXIS], raise - kern[Y_AXIS]);
188 Molecule mol = Lookup::filledbox (Box (Interval (0,0), Interval (0,0)));
190 SCM cp = ly_deep_copy (p);
191 if (raise)
193 SCM cr = ly_assoc_chain (ly_symbol2scm ("raise"), cp);
194 scm_set_cdr_x (cr, gh_int2scm (0));
197 while (gh_pair_p (text))
199 Molecule m = text2molecule (me, ly_car (text), cp);
201 if (!m.empty_b ())
203 m.translate_axis (mol.extent (axis)[axis == X_AXIS ? RIGHT : DOWN]
204 - (axis == Y_AXIS ? baseline_skip : 0),
205 axis);
206 mol.add_molecule (m);
208 text = ly_cdr (text);
212 /* Set extend to markup evented value. */
213 if (extent_b)
215 Box b = mol.extent_box ();
216 SCM expr = mol.get_expr ();
218 b[axis] = extent;
219 mol = Molecule (b, expr);
222 mol.translate (o);
224 return mol;
227 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
228 SCM
229 Text_item::brew_molecule (SCM smob)
231 Grob *me = unsmob_grob (smob);
233 SCM text = me->get_grob_property ("text");
235 SCM properties = Font_interface::font_alist_chain (me);
236 Molecule mol = Text_item::text2molecule (me, text, properties);
238 SCM space = me->get_grob_property ("word-space");
239 if (gh_number_p (space))
241 Molecule m;
242 m.set_empty (false);
243 mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
244 * Staff_symbol_referencer::staff_space (me), 0);
246 return mol.smobbed_copy ();
251 ADD_INTERFACE (Text_item,"text-interface",
252 "A scheme markup text, see @ref{Markup functions}.",
253 "text baseline-skip word-space");
257 Ugh. Duplicated from Scheme.
259 bool
260 new_markup_p (SCM x)
262 return
263 gh_string_p (x) ||
264 (gh_pair_p (x)
265 && SCM_BOOL_F != scm_object_property (gh_car (x), ly_symbol2scm ("markup-signature")));
269 new_markup_brewer ()
271 static SCM proc ;
273 if (!proc)
274 proc = scm_c_eval_string ("brew-new-markup-molecule");
276 return proc;