]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
b88ad73280b9027fce9213031dc22b8788568e5b
[lilypond.git] / lily / text-item.cc
1 /*   
2   text-item.cc -- implement Text_item
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 <math.h>
11
12 #include "warn.hh"
13 #include "grob.hh"
14 #include "text-item.hh"
15 #include "font-interface.hh"
16 #include "virtual-font-metric.hh"
17 #include "output-def.hh"
18 #include "modified-font-metric.hh"
19 #include "ly-module.hh"
20
21 MAKE_SCHEME_CALLBACK (Text_item, interpret_string, 4)
22 SCM
23 Text_item::interpret_string (SCM paper_smob,
24                              SCM props, SCM input_encoding, SCM markup)
25 {
26   Output_def *paper = unsmob_output_def (paper_smob);
27   
28   SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
29                    __FUNCTION__, "Paper definition");
30   SCM_ASSERT_TYPE (ly_c_string_p (markup), markup, SCM_ARG3,
31                    __FUNCTION__, "string");
32   SCM_ASSERT_TYPE (input_encoding == SCM_EOL || ly_c_symbol_p (input_encoding),
33                    input_encoding, SCM_ARG2, __FUNCTION__, "symbol");
34   
35   String str = ly_scm2string (markup);
36   if (!ly_c_symbol_p (input_encoding))
37     {
38       /* FIXME: input_encoding is set in *book*_paper
39
40          Options include:
41          
42          1. produce a bookpaper from a paper.
43          2. add BOOKPAPER parameter to all callers, ie, all MARKUPs.  */
44       
45       programming_error ("FIXME: looking up input_encoding in paper");
46       programming_error ("as a workaround, set inputencoding in your \\paper block");
47       SCM var = ly_module_lookup (paper->scope_,
48                                   ly_symbol2scm ("inputencoding"));
49       if (var != SCM_BOOL_F) 
50         input_encoding = scm_variable_ref (var);
51       
52     }
53   
54   Font_metric *fm = select_encoded_font (paper, props, input_encoding);
55
56   SCM lst = SCM_EOL;      
57   Box b;
58   if (Modified_font_metric* mf = dynamic_cast<Modified_font_metric*> (fm))
59     {
60       lst = scm_list_3 (ly_symbol2scm ("text"),
61                         mf->self_scm (),
62                         markup);
63         
64       b = mf->text_dimension (str);
65     }
66   else
67     {
68       /* ARGH. */
69       programming_error ("Must have Modified_font_metric for text.");
70       scm_display (fm->description_, scm_current_error_port ());
71     }
72       
73   return Stencil (b, lst).smobbed_copy ();
74 }
75
76
77 MAKE_SCHEME_CALLBACK (Text_item, interpret_markup, 3)
78 SCM
79 Text_item::interpret_markup (SCM paper_smob, SCM props, SCM markup)
80 {
81   if (ly_c_string_p (markup))
82     return interpret_string (paper_smob, props, SCM_EOL, markup);
83   else if (ly_c_pair_p (markup))
84     {
85       SCM func = ly_car (markup);
86       SCM args = ly_cdr (markup);
87       if (!markup_p (markup))
88         programming_error ("Markup head has no markup signature.");
89       
90       return scm_apply_2 (func, paper_smob, props, args);
91     }
92   return SCM_EOL;
93 }
94
95 MAKE_SCHEME_CALLBACK (Text_item,print,1);
96 SCM
97 Text_item::print (SCM grob)
98 {
99   Grob *me = unsmob_grob (grob);
100   
101   SCM t = me->get_property ("text");
102   SCM chain = Font_interface::text_font_alist_chain (me);
103   return interpret_markup (me->get_paper ()->self_scm (), chain, t);
104 }
105
106 /* Ugh. Duplicated from Scheme.  */
107 bool
108 Text_item::markup_p (SCM x)
109 {
110   return (ly_c_string_p (x)
111           || (ly_c_pair_p (x)
112               && SCM_BOOL_F
113               != scm_object_property (ly_car (x),
114                                       ly_symbol2scm ("markup-signature"))));
115 }
116
117 ADD_INTERFACE (Text_item,"text-interface",
118                "A scheme markup text, see @usermanref{Text-markup}.",
119                "text baseline-skip word-space");
120
121
122
123