2 text-interface.cc -- implement Text_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "text-interface.hh"
15 #include "pango-font.hh"
18 #include "font-interface.hh"
19 #include "output-def.hh"
20 #include "modified-font-metric.hh"
23 replace_whitespace (string *str)
25 for (vsize i = 0; i < str->size (); i++)
26 if (isspace ((*str)[i]))
30 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
32 Text_interface::interpret_string (SCM layout_smob,
36 LY_ASSERT_SMOB (Output_def, layout_smob, 1);
37 LY_ASSERT_TYPE (scm_is_string, markup, 3);
39 string str = ly_scm2string (markup);
40 Output_def *layout = unsmob_output_def (layout_smob);
41 Font_metric *fm = select_encoded_font (layout, props);
43 replace_whitespace (&str);
44 return fm->word_stencil (str).smobbed_copy ();
47 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
48 "Convert a text markup into a stencil."
49 " Takes three arguments, @var{layout}, @var{props}, and @var{markup}.\n"
51 "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with"
52 " @code{ly:grob-layout}. @var{props} is a alist chain, ie. a list of alists."
53 " This is typically obtained with"
54 " @code{(ly:grob-alist-chain (ly:layout-lookup layout 'text-font-defaults))}."
55 " @var{markup} is the markup text to be processed.");
57 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
59 if (scm_is_string (markup))
60 return interpret_string (layout_smob, props, markup);
61 else if (scm_is_pair (markup))
63 SCM func = scm_car (markup);
64 SCM args = scm_cdr (markup);
65 if (!is_markup (markup))
66 programming_error ("markup head has no markup signature");
68 return scm_apply_2 (func, layout_smob, props, args);
72 programming_error ("Object is not a markup. ");
73 scm_puts ("This object should be a markup: ", scm_current_error_port ());
74 scm_display (markup, scm_current_error_port ());
75 scm_puts ("\n", scm_current_error_port ());
78 b[X_AXIS].set_empty ();
79 b[Y_AXIS].set_empty ();
81 Stencil s (b, SCM_EOL);
82 return s.smobbed_copy ();
86 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
88 Text_interface::print (SCM grob)
90 Grob *me = unsmob_grob (grob);
92 SCM t = me->get_property ("text");
93 SCM chain = Font_interface::text_font_alist_chain (me);
94 return interpret_markup (me->layout ()->self_scm (), chain, t);
97 /* Ugh. Duplicated from Scheme. */
99 Text_interface::is_markup (SCM x)
101 return (scm_is_string (x)
104 != scm_object_property (scm_car (x),
105 ly_symbol2scm ("markup-signature"))));
109 Text_interface::is_markup_list (SCM x)
111 SCM music_list_p = ly_lily_module_constant ("markup-list?");
112 return scm_is_true (scm_call_1 (music_list_p, x));
116 ADD_INTERFACE (Text_interface,
117 "A scheme markup text, see @ruser{Text markup} and "
118 "@ruser{New markup command definition}. "
120 "There are two important commands: ly:text-interface::print, which is a "
121 "grob callback, and ly:text-interface::interpret-markup ",