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>
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 MAKE_SCHEME_CALLBACK(Text_item
,text_to_molecule
,3);
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();
75 Text_item::string2molecule (Grob
*me
, SCM text
, SCM alist_chain
)
77 SCM style
= ly_assoc_chain (ly_symbol2scm ("font-style"),
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
);
87 if (gh_pair_p (lookup
) && ly_cdr (lookup
) ==ly_symbol2scm ("name"))
88 mol
= lookup_character (me
, fm
, text
);
90 mol
= lookup_text (me
, fm
, text
);
96 Text_item::lookup_character (Grob
*, Font_metric
*fm
, SCM char_name
)
98 return fm
->find_by_name (ly_scm2string (char_name
));
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
))
113 programming_error ("Can't use virtual font for text.");
116 list
= fontify_atom (fm
, list
);
118 return Molecule (fm
->text_dimension (ly_scm2string (text
)), list
);
128 MARKUP_TEXT must be compound (may not be simple string.)
132 Text_item::markup_text2molecule (Grob
*me
, SCM markup_text
,
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
);
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.
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
;
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
;
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
);
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
);
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
);
203 m
.translate_axis (mol
.extent (axis
)[axis
== X_AXIS
? RIGHT
: DOWN
]
204 - (axis
== Y_AXIS
? baseline_skip
: 0),
206 mol
.add_molecule (m
);
208 text
= ly_cdr (text
);
212 /* Set extend to markup evented value. */
215 Box b
= mol
.extent_box ();
216 SCM expr
= mol
.get_expr ();
219 mol
= Molecule (b
, expr
);
227 MAKE_SCHEME_CALLBACK (Text_item
, brew_molecule
, 1);
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
))
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",
253 "text axis baseline-skip extent lookup raise kern word-space");
257 Ugh. Duplicated from Scheme.
265 && SCM_BOOL_F
!= scm_object_property (gh_car (x
), ly_symbol2scm ("markup-signature")));
274 proc
= scm_c_eval_string ("brew-new-markup-molecule");