2 text-item.cc -- implement Text_item
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 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"
23 TEXT : STRING | (MARKUP SENTENCE)
24 MARKUP: PROPERTY | ABBREV
25 SENTENCE: TEXT | SENTENCE TEXT
26 PROPERTY: (key . value)
27 ABBREV: rows lines roman music bold italic named super sub text, or any font-style
33 rewrite routines and syntax to be like
36 | (head-expression* TEXT*)
39 head-expression is a list, containing a tag and a variable number of
40 arguments. If necessary, the number of arguments can be stored in a alist,
52 (tag . (argcount function-to-handle-the-tag ))
57 Text_item::text2molecule (Score_element *me, SCM text, SCM alist_chain)
59 if (gh_string_p (text))
60 return string2molecule (me, text, alist_chain);
61 else if (gh_list_p (text))
63 if (!gh_pair_p (gh_car (text)) && gh_string_p (gh_car (text)))
64 return string2molecule (me, gh_car (text), alist_chain);
66 return markup_sentence2molecule (me, text, alist_chain);
72 ly_assoc_chain (SCM key, SCM achain)
74 if (gh_pair_p (achain))
76 SCM handle = scm_assoc (key, gh_car (achain));
77 if (gh_pair_p (handle))
80 return ly_assoc_chain (key, gh_cdr (achain));
87 Text_item::string2molecule (Score_element *me, SCM text, SCM alist_chain)
89 SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
91 if (gh_pair_p (style) && gh_symbol_p (gh_cdr (style)))
92 alist_chain = Font_interface::add_style (me, gh_cdr(style), alist_chain);
94 Font_metric *fm = Font_interface::get_font (me, alist_chain);
96 SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
99 if (gh_pair_p (lookup) && gh_cdr (lookup) ==ly_symbol2scm ("name"))
100 mol = lookup_character (me, fm, text);
102 mol = lookup_text (me, fm, text);
108 Text_item::lookup_character (Score_element *, Font_metric*fm, SCM char_name)
110 return fm->find_by_name (ly_scm2string (char_name));
115 Text_item::lookup_text (Score_element *me, Font_metric*fm, SCM text)
119 Fixme; should be done differently, move to font-interface?
122 SCM magnification = me->get_elt_property ("font-magnification");
124 Font_metric* metric = 0;
125 if (gh_number_p (magnification))
128 Real realmag = pow (1.2, gh_scm2int (magnification));
129 metric = all_fonts_global_p->find_scaled (ly_scm2string (font_name), realmag);
136 SCM list = gh_list (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
137 list = fontify_atom (fm, list);
139 return Molecule (fm->text_dimension (ly_scm2string (text)), list);
143 Text_item::markup_sentence2molecule (Score_element *me, SCM markup_sentence,
151 // return Molecule ();
153 SCM sheet = me->paper_l ()->style_sheet_;
154 SCM f = gh_cdr (scm_assoc (ly_symbol2scm ("markup-to-properties"), sheet));
156 SCM markup = gh_car (markup_sentence);
157 SCM sentence = gh_cdr (markup_sentence);
159 SCM p = gh_cons (gh_call2 (f, sheet, markup), alist_chain);
162 SCM a = ly_assoc_chain (ly_symbol2scm ("align"), p);
163 if (gh_pair_p (a) && gh_number_p (gh_cdr (a)))
164 align = (Axis)gh_scm2int (gh_cdr (a));
166 Real staff_space = Staff_symbol_referencer::staff_space (me);
168 SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
169 if (gh_pair_p (k) && gh_number_p (gh_cdr (k)))
170 kern = gh_scm2double (gh_cdr (k)) * staff_space;
173 SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
174 if (gh_pair_p (r) && gh_number_p (gh_cdr (r)))
175 raise = gh_scm2double (gh_cdr (r)) * staff_space;
177 Offset o (align == X_AXIS ? kern : 0,
178 (align == Y_AXIS ? - kern : 0) + raise);
181 while (gh_pair_p (sentence))
183 Molecule m = text2molecule (me, gh_car (sentence), p);
187 mol.add_at_edge (align, align == X_AXIS ? RIGHT : DOWN, m, 0);
189 sentence = gh_cdr (sentence);
194 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
196 Text_item::brew_molecule (SCM smob)
198 Score_element *me = unsmob_element (smob);
200 SCM text = me->get_elt_property ("text");
202 SCM properties = gh_list (me->immutable_property_alist_,
203 me->mutable_property_alist_, SCM_UNDEFINED);
205 Molecule mol = Text_item::text2molecule (me, text, properties);
207 SCM space = me->get_elt_property ("word-space");
208 if (gh_number_p (space))
212 mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
213 * Staff_symbol_referencer::staff_space (me));
215 return mol.create_scheme ();