* stepmake/stepmake/metafont-rules.make: backport 1.7 fixes.
[lilypond.git] / lily / text-item.cc
blob389129867042648d28140ea7b982a2b07831a9a2
1 /*
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>
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"
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 ();
63 Molecule
64 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
66 SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
67 alist_chain);
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);
75 Molecule mol;
76 if (gh_pair_p (lookup) && ly_cdr (lookup) ==ly_symbol2scm ("name"))
77 mol = lookup_character (me, fm, text);
78 else
79 mol = lookup_text (me, fm, text);
81 return mol;
84 Molecule
85 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
87 return fm->find_by_name (ly_scm2string (char_name));
91 Molecule
92 Text_item::lookup_text (Grob *, Font_metric*fm, SCM text)
94 SCM list = scm_list_n (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
95 list = fontify_atom (fm, list);
97 return Molecule (fm->text_dimension (ly_scm2string (text)), list);
102 TODO:
104 DOCME.
107 MARKUP_TEXT must be compound (may not be simple string.)
110 Molecule
111 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
112 SCM alist_chain)
114 SCM sheet = me->get_paper ()->style_sheet_;
115 SCM f = ly_cdr (scm_assoc (ly_symbol2scm ("markup-to-properties"), sheet));
117 SCM markup = ly_car (markup_text);
118 SCM text = ly_cdr (markup_text);
120 SCM p = gh_cons (gh_call2 (f, sheet, markup), alist_chain);
122 Real staff_space = Staff_symbol_referencer::staff_space (me);
125 Line mode is default.
127 Axis axis = X_AXIS;
129 SCM a = ly_assoc_chain (ly_symbol2scm ("axis"), p);
130 if (gh_pair_p (a) && ly_axis_p (ly_cdr (a)))
131 axis = (Axis)gh_scm2int (ly_cdr (a));
133 Real baseline_skip = 0;
134 SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
135 if (gh_pair_p (b) && gh_number_p (ly_cdr (b)))
136 baseline_skip = gh_scm2double (ly_cdr (b)) * staff_space;
138 Real kern[2] = {0,0};
140 SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
141 if (gh_pair_p (k) && gh_number_p (ly_cdr (k)))
142 kern[axis] = gh_scm2double (ly_cdr (k)) * staff_space;
144 Real raise = 0;
145 SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
146 if (gh_pair_p (r) && gh_number_p (ly_cdr (r)))
147 raise = gh_scm2double (ly_cdr (r)) * staff_space;
150 Interval extent;
151 bool extent_b = false;
152 SCM e = ly_assoc_chain (ly_symbol2scm ("extent"), p);
153 if (gh_pair_p (e) && ly_number_pair_p (ly_cdr (e)))
155 extent = Interval (gh_scm2double (ly_cadr (e)) * staff_space,
156 gh_scm2double (ly_cddr (e)) * staff_space);
157 extent_b = true;
160 Offset o (kern[X_AXIS], raise - kern[Y_AXIS]);
162 Molecule mol = Lookup::filledbox (Box (Interval (0,0), Interval (0,0)));
164 SCM cp = ly_deep_copy (p);
165 if (raise)
167 SCM cr = ly_assoc_chain (ly_symbol2scm ("raise"), cp);
168 scm_set_cdr_x (cr, gh_int2scm (0));
171 while (gh_pair_p (text))
173 Molecule m = text2molecule (me, ly_car (text), cp);
175 if (!m.empty_b ())
177 m.translate_axis (mol.extent (axis)[axis == X_AXIS ? RIGHT : DOWN]
178 - (axis == Y_AXIS ? baseline_skip : 0),
179 axis);
180 mol.add_molecule (m);
182 text = ly_cdr (text);
186 /* Set extend to markup requested value. */
187 if (extent_b)
189 Box b = mol.extent_box ();
190 SCM expr = mol.get_expr ();
192 b[axis] = extent;
193 mol = Molecule (b, expr);
196 mol.translate (o);
198 return mol;
201 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
202 SCM
203 Text_item::brew_molecule (SCM smob)
205 Grob *me = unsmob_grob (smob);
207 SCM text = me->get_grob_property ("text");
209 SCM properties = Font_interface::font_alist_chain (me);
210 Molecule mol = Text_item::text2molecule (me, text, properties);
212 SCM space = me->get_grob_property ("word-space");
213 if (gh_number_p (space))
215 Molecule m;
216 m.set_empty (false);
217 mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
218 * Staff_symbol_referencer::staff_space (me));
220 return mol.smobbed_copy ();
225 ADD_INTERFACE (Text_item,"text-interface",
226 "A scheme markup text",
227 "text align baseline-skip lookup raise kern word-space magnify");