]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-interface.cc
document Text_interface::interpret_markup.
[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 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
23 SCM
24 Text_interface::interpret_string (SCM layout_smob,
25                                   SCM props,
26                                   SCM markup)
27 {
28   Output_def *layout = unsmob_output_def (layout_smob);
29
30   SCM_ASSERT_TYPE (layout, layout_smob, SCM_ARG1,
31                    __FUNCTION__, "Layout definition");
32   SCM_ASSERT_TYPE (scm_is_string (markup), markup, SCM_ARG3,
33                    __FUNCTION__, "string");
34
35   string str = ly_scm2string (markup);
36
37   Font_metric *fm = select_encoded_font (layout, props);
38   return fm->word_stencil (str).smobbed_copy ();
39 }
40
41 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
42                                    "Convert a text markup into a stencil. "
43 "Takes 3 arguments, @var{layout}, @var{props} and @var{markup}. "
44 "\n\n"
45 "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with "
46 "@code{ly:grob-layout}.  @var{props} is a alist chain, ie. a list of alists. "
47 "This is typically obtained with "
48 "@code{(ly:grob-alist-chain (ly:layout-lookup layout 'text-font-defaults))}. "
49 "@var{markup} is the markup text to be processed. "
50                                    );
51 SCM
52 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
53 {
54   if (scm_is_string (markup))
55     return interpret_string (layout_smob, props, markup);
56   else if (scm_is_pair (markup))
57     {
58       SCM func = scm_car (markup);
59       SCM args = scm_cdr (markup);
60       if (!is_markup (markup))
61         programming_error ("markup head has no markup signature");
62
63       return scm_apply_2 (func, layout_smob, props, args);
64     }
65   else
66     {
67       programming_error ("Object is not a markup. ");
68       scm_puts ("This object should be a markup: ", scm_current_error_port ());
69       scm_display (markup, scm_current_error_port ());
70       scm_puts ("\n", scm_current_error_port ());
71
72       Box b;
73       b[X_AXIS].set_empty ();
74       b[Y_AXIS].set_empty ();
75
76       Stencil s (b, SCM_EOL);
77       return s.smobbed_copy ();
78     }
79 }
80
81 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
82 SCM
83 Text_interface::print (SCM grob)
84 {
85   Grob *me = unsmob_grob (grob);
86
87   SCM t = me->get_property ("text");
88   SCM chain = Font_interface::text_font_alist_chain (me);
89   return interpret_markup (me->layout ()->self_scm (), chain, t);
90 }
91
92 /* Ugh. Duplicated from Scheme.  */
93 bool
94 Text_interface::is_markup (SCM x)
95 {
96   return (scm_is_string (x)
97           || (scm_is_pair (x)
98               && SCM_BOOL_F
99               != scm_object_property (scm_car (x),
100                                       ly_symbol2scm ("markup-signature"))));
101 }
102
103 ADD_INTERFACE (Text_interface,
104                "A scheme markup text, see @usermanref{Text markup} and "
105                "@usermanref{New markup command definition}. "
106                "\n\n"
107                "There are two important commands: ly:text-interface::print, which is a "
108                "grob callback, and ly:text-interface::interpret-markup ",
109
110                /* props */
111                "baseline-skip "
112                "text "
113                "word-space "
114                "text-direction "
115                );
116