]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-interface.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / text-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "text-interface.hh"
22
23 #include "config.hh"
24 #include "font-interface.hh"
25 #include "grob.hh"
26 #include "main.hh"
27 #include "misc.hh"
28 #include "modified-font-metric.hh"
29 #include "output-def.hh"
30 #include "pango-font.hh"
31 #include "program-option.hh"
32 #include "international.hh"
33 #include "warn.hh"
34
35 static void
36 replace_whitespace (string *str)
37 {
38   vsize i = 0;
39   vsize n = str->size ();
40
41   while (i < n)
42     {
43       char cur = (*str)[i];
44
45       // avoid the locale-dependent isspace
46       if (cur == '\n' || cur == '\t' || cur == '\v')
47         (*str)[i] = ' ';
48
49       vsize char_len = utf8_char_len (cur);
50
51       i += char_len;
52     }
53 }
54
55 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
56 SCM
57 Text_interface::interpret_string (SCM layout_smob,
58                                   SCM props,
59                                   SCM markup)
60 {
61   LY_ASSERT_SMOB (Output_def, layout_smob, 1);
62   LY_ASSERT_TYPE (scm_is_string, markup, 3);
63
64   string str = ly_scm2string (markup);
65   Output_def *layout = unsmob_output_def (layout_smob);
66   Font_metric *fm = select_encoded_font (layout, props);
67
68   replace_whitespace (&str);
69
70   /*
71     We want to filter strings with a music font that pass through
72     the text interface.  Here the font encoding is checked to see
73     if it matches one of the music font encodings.  --pmccarty
74   */
75   SCM encoding = ly_chain_assoc_get (ly_symbol2scm ("font-encoding"),
76                                      props,
77                                      SCM_BOOL_F);
78   SCM music_encodings = ly_lily_module_constant ("all-music-font-encodings");
79
80   bool is_music = (scm_memq (encoding, music_encodings) != SCM_BOOL_F);
81   return fm->text_stencil (layout, str, is_music).smobbed_copy ();
82 }
83
84 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
85                                    "Convert a text markup into a stencil."
86                                    "  Takes three arguments, @var{layout}, @var{props}, and @var{markup}.\n"
87                                    "\n"
88                                    "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with"
89                                    " @code{ly:grob-layout}.  @var{props} is an alist chain, i.e. a list of"
90                                    "  alists.  This is typically obtained with"
91                                    " @code{(ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))}."
92                                    "  @var{markup} is the markup text to be processed.");
93 SCM
94 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
95 {
96   if (scm_is_string (markup))
97     return interpret_string (layout_smob, props, markup);
98   else if (scm_is_pair (markup))
99     {
100       SCM func = scm_car (markup);
101       SCM args = scm_cdr (markup);
102       if (!is_markup (markup))
103         programming_error ("markup head has no markup signature");
104
105       /* Use a hare/tortoise algorithm to detect whether we are in a cycle,
106        * i.e. whether we have already encountered the same markup in the
107        * current branch of the markup tree structure. */
108       static vector<SCM> encountered_markups;
109       size_t depth = encountered_markups.size ();
110       if (depth > 0)
111         {
112           int slow = depth / 2;
113           if (ly_is_equal (encountered_markups[slow], markup))
114             {
115               string name = ly_symbol2string (scm_procedure_name (func));
116               // TODO: Also print the arguments of the markup!
117               non_fatal_error (_f("Cyclic markup detected: %s", name));
118               return Stencil().smobbed_copy ();
119             }
120         }
121
122       /* Check for non-terminating markups, e.g. recursive calls with
123        * changing arguments */
124       SCM opt_depth = ly_get_option (ly_symbol2scm ("max-markup-depth"));
125       size_t max_depth = robust_scm2int(opt_depth, 1024);
126       if (depth > max_depth)
127         {
128           string name = ly_symbol2string (scm_procedure_name (func));
129           // TODO: Also print the arguments of the markup!
130           non_fatal_error (_f("Markup depth exceeds maximal value of %d; "
131                               "Markup: %s", max_depth, name.c_str ()));
132           return Stencil().smobbed_copy ();
133         }
134
135       encountered_markups.push_back (markup);
136       SCM retval = scm_apply_2 (func, layout_smob, props, args);
137       encountered_markups.pop_back ();
138       return retval;
139     }
140   else
141     {
142       programming_error ("Object is not a markup. ");
143       scm_puts ("This object should be a markup: ", scm_current_error_port ());
144       scm_display (markup, scm_current_error_port ());
145       scm_puts ("\n", scm_current_error_port ());
146
147       return Stencil ().smobbed_copy ();
148     }
149 }
150
151 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
152 SCM
153 Text_interface::print (SCM grob)
154 {
155   Grob *me = unsmob_grob (grob);
156
157   SCM t = me->get_property ("text");
158   SCM chain = Font_interface::text_font_alist_chain (me);
159   return interpret_markup (me->layout ()->self_scm (), chain, t);
160 }
161
162 /* Ugh. Duplicated from Scheme.  */
163 bool
164 Text_interface::is_markup (SCM x)
165 {
166   return (scm_is_string (x)
167           || (scm_is_pair (x)
168               && SCM_BOOL_F
169               != scm_object_property (scm_car (x),
170                                       ly_symbol2scm ("markup-signature"))));
171 }
172
173 bool
174 Text_interface::is_markup_list (SCM x)
175 {
176   SCM music_list_p = ly_lily_module_constant ("markup-list?");
177   return scm_is_true (scm_call_1 (music_list_p, x));
178 }
179
180 ADD_INTERFACE (Text_interface,
181                "A Scheme markup text, see @ruser{Formatting text} and"
182                " @rextend{New markup command definition}.\n"
183                "\n"
184                "There are two important commands:"
185                " @code{ly:text-interface::print}, which is a"
186                " grob callback, and"
187                " @code{ly:text-interface::interpret-markup}.",
188
189                /* properties */
190                "baseline-skip "
191                "text "
192                "word-space "
193                "text-direction "
194               );
195