]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
* scm/stencil.scm (stack-lines): return empty-stencil if argument
[lilypond.git] / lily / text-item.cc
1 /*   
2   text-item.cc -- implement Text_interface
3
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7                  Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "text-item.hh"
11
12 #include <cmath>
13
14 #include "warn.hh"
15 #include "grob.hh"
16 #include "font-interface.hh"
17 #include "virtual-font-metric.hh"
18 #include "output-def.hh"
19 #include "modified-font-metric.hh"
20 #include "ly-module.hh"
21
22 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 4)
23 SCM
24 Text_interface::interpret_string (SCM layout_smob,
25                                   SCM props, SCM input_encoding, SCM markup)
26 {
27   Output_def *layout = unsmob_output_def (layout_smob);
28   
29   SCM_ASSERT_TYPE (layout, layout_smob, SCM_ARG1,
30                    __FUNCTION__, "Layout definition");
31   SCM_ASSERT_TYPE (scm_is_string (markup), markup, SCM_ARG3,
32                    __FUNCTION__, "string");
33   SCM_ASSERT_TYPE (input_encoding == SCM_EOL || scm_is_symbol (input_encoding),
34                    input_encoding, SCM_ARG2, __FUNCTION__, "symbol");
35   
36   String str = ly_scm2string (markup);
37   if (!scm_is_symbol (input_encoding))
38     {
39       SCM enc = layout->lookup_variable (ly_symbol2scm ("inputencoding"));
40       if (scm_is_string (enc))
41         input_encoding = scm_string_to_symbol (enc);
42       else if (scm_is_symbol (enc))
43         input_encoding = enc;
44     }
45   
46   Font_metric *fm = select_encoded_font (layout, props, input_encoding);
47
48   SCM lst = SCM_EOL;      
49   Box b;
50   if (Modified_font_metric* mf = dynamic_cast<Modified_font_metric*> (fm))
51     {
52       lst = scm_list_3 (ly_symbol2scm ("text"),
53                         mf->self_scm (),
54                         markup);
55         
56       b = mf->text_dimension (str);
57     }
58   else
59     {
60       /* ARGH. */
61       programming_error ("Must have Modified_font_metric for text.");
62       scm_display (fm->description_, scm_current_error_port ());
63     }
64       
65   return Stencil (b, lst).smobbed_copy ();
66 }
67
68
69 MAKE_SCHEME_CALLBACK (Text_interface, interpret_markup, 3)
70 SCM
71 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
72 {
73   if (scm_is_string (markup))
74     return interpret_string (layout_smob, props, SCM_EOL, markup);
75   else if (scm_is_pair (markup))
76     {
77       SCM func = scm_car (markup);
78       SCM args = scm_cdr (markup);
79       if (!markup_p (markup))
80         programming_error ("Markup head has no markup signature.");
81       
82       return scm_apply_2 (func, layout_smob, props, args);
83     }
84   else
85     {
86       programming_error ("Is not a markup: ");
87       scm_display (markup, scm_current_error_port());
88       assert (false);
89       Box b;
90       b[X_AXIS].set_empty();
91       b[Y_AXIS].set_empty();
92       
93       Stencil s(b, SCM_EOL);
94       return s.smobbed_copy();
95     }
96 }
97
98 MAKE_SCHEME_CALLBACK (Text_interface,print,1);
99 SCM
100 Text_interface::print (SCM grob)
101 {
102   Grob *me = unsmob_grob (grob);
103   
104   SCM t = me->get_property ("text");
105   SCM chain = Font_interface::text_font_alist_chain (me);
106   return interpret_markup (me->get_layout ()->self_scm (), chain, t);
107 }
108
109 /* Ugh. Duplicated from Scheme.  */
110 bool
111 Text_interface::markup_p (SCM x)
112 {
113   return (scm_is_string (x)
114           || (scm_is_pair (x)
115               && SCM_BOOL_F
116               != scm_object_property (scm_car (x),
117                                       ly_symbol2scm ("markup-signature"))));
118 }
119
120 ADD_INTERFACE (Text_interface,"text-interface",
121                "A scheme markup text, see @usermanref{Text-markup}.",
122                "text baseline-skip word-space");
123
124
125
126