]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-interface.cc
Merge branch 'master' of git+ssh://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / text-interface.cc
1 /*
2   text-interface.cc -- implement Text_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "text-interface.hh"
11
12
13 #include "main.hh"
14 #include "config.hh"
15 #include "pango-font.hh"
16 #include "warn.hh"
17 #include "grob.hh"
18 #include "font-interface.hh"
19 #include "output-def.hh"
20 #include "modified-font-metric.hh"
21
22 static void
23 replace_whitespace (string *str)
24 {
25   for (vsize i = 0; i < str->size (); i++)
26     if (isspace ((*str)[i]))
27       (*str)[i] = ' ';
28 }
29
30 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
31 SCM
32 Text_interface::interpret_string (SCM layout_smob,
33                                   SCM props,
34                                   SCM markup)
35 {
36   LY_ASSERT_SMOB (Output_def, layout_smob, 1);
37   LY_ASSERT_TYPE (scm_is_string, markup, 3);
38
39   string str = ly_scm2string (markup);
40   Output_def *layout = unsmob_output_def (layout_smob);
41   Font_metric *fm = select_encoded_font (layout, props);
42
43   replace_whitespace (&str);
44   return fm->word_stencil (str).smobbed_copy ();
45 }
46
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"
50 "\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.");
56 SCM
57 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
58 {
59   if (scm_is_string (markup))
60     return interpret_string (layout_smob, props, markup);
61   else if (scm_is_pair (markup))
62     {
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");
67
68       return scm_apply_2 (func, layout_smob, props, args);
69     }
70   else
71     {
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 ());
76
77       Box b;
78       b[X_AXIS].set_empty ();
79       b[Y_AXIS].set_empty ();
80
81       Stencil s (b, SCM_EOL);
82       return s.smobbed_copy ();
83     }
84 }
85
86 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
87 SCM
88 Text_interface::print (SCM grob)
89 {
90   Grob *me = unsmob_grob (grob);
91
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);
95 }
96
97 /* Ugh. Duplicated from Scheme.  */
98 bool
99 Text_interface::is_markup (SCM x)
100 {
101   return (scm_is_string (x)
102           || (scm_is_pair (x)
103               && SCM_BOOL_F
104               != scm_object_property (scm_car (x),
105                                       ly_symbol2scm ("markup-signature"))));
106 }
107
108 bool
109 Text_interface::is_markup_list (SCM x)
110 {
111   SCM music_list_p = ly_lily_module_constant ("markup-list?");
112   return scm_is_true (scm_call_1 (music_list_p, x));
113 }
114
115
116 ADD_INTERFACE (Text_interface,
117                "A scheme markup text, see @ruser{Text markup} and "
118                "@ruser{New markup command definition}. "
119                "\n\n"
120                "There are two important commands: ly:text-interface::print, which is a "
121                "grob callback, and ly:text-interface::interpret-markup ",
122
123                /* props */
124                "baseline-skip "
125                "text "
126                "word-space "
127                "text-direction "
128                );
129