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