2 text-item.cc -- implement Text_item
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
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"
18 #include "all-font-metrics.hh"
21 #include "virtual-font-metric.hh"
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
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,
52 Allow (faulty) texts that are in an extra list:
55 else if (scm_ilength (text) <= 1)
56 return text2molecule (me, ly_car (text), alist_chain);
58 return markup_text2molecule (me, text, alist_chain);
64 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
66 SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
68 if (gh_pair_p (style) && gh_symbol_p (ly_cdr (style)))
69 alist_chain = Font_interface::add_style (me, ly_cdr (style), alist_chain);
71 Font_metric *fm = Font_interface::get_font (me, alist_chain);
73 SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
76 if (gh_pair_p (lookup) && ly_cdr (lookup) ==ly_symbol2scm ("name"))
77 mol = lookup_character (me, fm, text);
79 mol = lookup_text (me, fm, text);
85 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
87 return fm->find_by_name (ly_scm2string (char_name));
93 Text_item::lookup_text (Grob *, Font_metric*fm, SCM text)
95 SCM list = scm_list_n (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
97 if (dynamic_cast<Virtual_font_metric*> (fm))
102 programming_error ("Can't use virtual font for text.");
105 list = fontify_atom (fm, list);
107 return Molecule (fm->text_dimension (ly_scm2string (text)), list);
117 MARKUP_TEXT must be compound (may not be simple string.)
121 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
124 SCM f = me->get_paper ()->lookup_variable (ly_symbol2scm ("markup-to-properties"));
126 SCM markup = ly_car (markup_text);
127 SCM text = ly_cdr (markup_text);
132 SCM abbrev = me->get_paper ()->lookup_variable (ly_symbol2scm ("abbreviation-alist"));
133 SCM style = me->get_paper ()->lookup_variable (ly_symbol2scm ("style-alist"));
135 SCM p = gh_cons (scm_call_3 (f, abbrev, style, markup), alist_chain);
137 Real staff_space = Staff_symbol_referencer::staff_space (me);
140 Line mode is default.
144 SCM a = ly_assoc_chain (ly_symbol2scm ("axis"), p);
145 if (gh_pair_p (a) && ly_axis_p (ly_cdr (a)))
146 axis = (Axis)gh_scm2int (ly_cdr (a));
148 Real baseline_skip = 0;
149 SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
150 if (gh_pair_p (b) && gh_number_p (ly_cdr (b)))
151 baseline_skip = gh_scm2double (ly_cdr (b)) * staff_space;
153 Real kern[2] = {0,0};
155 SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
156 if (gh_pair_p (k) && gh_number_p (ly_cdr (k)))
157 kern[axis] = gh_scm2double (ly_cdr (k)) * staff_space;
160 SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
161 if (gh_pair_p (r) && gh_number_p (ly_cdr (r)))
162 raise = gh_scm2double (ly_cdr (r)) * staff_space;
166 bool extent_b = false;
167 SCM e = ly_assoc_chain (ly_symbol2scm ("extent"), p);
168 if (gh_pair_p (e) && ly_number_pair_p (ly_cdr (e)))
170 extent = Interval (gh_scm2double (ly_cadr (e)) * staff_space,
171 gh_scm2double (ly_cddr (e)) * staff_space);
175 Offset o (kern[X_AXIS], raise - kern[Y_AXIS]);
177 Molecule mol = Lookup::filledbox (Box (Interval (0,0), Interval (0,0)));
179 SCM cp = ly_deep_copy (p);
182 SCM cr = ly_assoc_chain (ly_symbol2scm ("raise"), cp);
183 scm_set_cdr_x (cr, gh_int2scm (0));
186 while (gh_pair_p (text))
188 Molecule m = text2molecule (me, ly_car (text), cp);
192 m.translate_axis (mol.extent (axis)[axis == X_AXIS ? RIGHT : DOWN]
193 - (axis == Y_AXIS ? baseline_skip : 0),
195 mol.add_molecule (m);
197 text = ly_cdr (text);
201 /* Set extend to markup evented value. */
204 Box b = mol.extent_box ();
205 SCM expr = mol.get_expr ();
208 mol = Molecule (b, expr);
216 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
218 Text_item::brew_molecule (SCM smob)
220 Grob *me = unsmob_grob (smob);
222 SCM text = me->get_grob_property ("text");
224 SCM properties = Font_interface::font_alist_chain (me);
225 Molecule mol = Text_item::text2molecule (me, text, properties);
227 SCM space = me->get_grob_property ("word-space");
228 if (gh_number_p (space))
232 mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
233 * Staff_symbol_referencer::staff_space (me));
235 return mol.smobbed_copy ();
240 ADD_INTERFACE (Text_item,"text-interface",
241 "A scheme markup text",
242 "text align baseline-skip lookup raise kern word-space magnify");