]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-interface.cc
936a7488e2810a001e42c8b87896e35c1a12f202
[lilypond.git] / lily / text-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 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 #include "skyline-pair.hh"
23
24 #include "lookup.hh"
25 #include "config.hh"
26 #include "font-interface.hh"
27 #include "grob.hh"
28 #include "main.hh"
29 #include "misc.hh"
30 #include "modified-font-metric.hh"
31 #include "output-def.hh"
32 #include "pango-font.hh"
33 #include "program-option.hh"
34 #include "international.hh"
35 #include "warn.hh"
36
37 static void
38 replace_special_characters (string &str, SCM props)
39 {
40   SCM replacement_alist = ly_chain_assoc_get (ly_symbol2scm ("replacement-alist"),
41                                               props,
42                                               SCM_EOL);
43
44   int max_length = 0;
45   for (SCM s = replacement_alist; scm_is_pair (s); s = scm_cdr (s))
46     {
47       max_length = max (max_length, scm_to_int
48                         (scm_string_length (scm_caar (s))));
49     }
50
51   for (vsize i = 0; i < str.size (); i++)
52     {
53       /* Don't match in mid-UTF-8 */
54       if ((str[i] & 0xc0) == 0x80)
55         continue;
56       for (vsize j = max_length + 1; j--;)
57         {
58           if (j > str.size () - i)
59             continue;
60           string dummy = str.substr (i, j);
61           SCM ligature = ly_assoc_get (ly_string2scm (dummy),
62                                        replacement_alist, SCM_BOOL_F);
63           if (scm_is_true (ligature))
64             str.replace (i, j, robust_scm2string (ligature, ""));
65         }
66     }
67 }
68
69 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
70 SCM
71 Text_interface::interpret_string (SCM layout_smob,
72                                   SCM props,
73                                   SCM markup)
74 {
75   LY_ASSERT_SMOB (Output_def, layout_smob, 1);
76   LY_ASSERT_TYPE (scm_is_string, markup, 3);
77
78   string str = ly_scm2string (markup);
79   Output_def *layout = unsmob_output_def (layout_smob);
80   Font_metric *fm = select_encoded_font (layout, props);
81
82   replace_special_characters (str, props);
83
84   /*
85     We want to filter strings with a music font that pass through
86     the text interface.  Here the font encoding is checked to see
87     if it matches one of the music font encodings.  --pmccarty
88   */
89   SCM encoding = ly_chain_assoc_get (ly_symbol2scm ("font-encoding"),
90                                      props,
91                                      SCM_BOOL_F);
92   SCM music_encodings = ly_lily_module_constant ("all-music-font-encodings");
93
94   bool is_music = (scm_memq (encoding, music_encodings) != SCM_BOOL_F);
95   return fm->text_stencil (layout, str, is_music).smobbed_copy ();
96 }
97
98 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
99                                    "Convert a text markup into a stencil."
100                                    "  Takes three arguments, @var{layout}, @var{props}, and @var{markup}.\n"
101                                    "\n"
102                                    "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with"
103                                    " @code{ly:grob-layout}.  @var{props} is an alist chain, i.e. a list of"
104                                    "  alists.  This is typically obtained with"
105                                    " @code{(ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))}."
106                                    "  @var{markup} is the markup text to be processed.");
107 SCM
108 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
109 {
110   if (scm_is_string (markup))
111     return interpret_string (layout_smob, props, markup);
112   else if (is_markup (markup))
113     {
114       SCM func = scm_car (markup);
115       SCM args = scm_cdr (markup);
116
117       /* Use a hare/tortoise algorithm to detect whether we are in a cycle,
118        * i.e. whether we have already encountered the same markup in the
119        * current branch of the markup tree structure. */
120       static vector<SCM> encountered_markups;
121       size_t depth = encountered_markups.size ();
122       if (depth > 0)
123         {
124           int slow = depth / 2;
125           if (ly_is_equal (encountered_markups[slow], markup))
126             {
127               string name = ly_symbol2string (scm_procedure_name (func));
128               // TODO: Also print the arguments of the markup!
129               non_fatal_error (_f ("Cyclic markup detected: %s", name));
130               return Stencil ().smobbed_copy ();
131             }
132         }
133
134       /* Check for non-terminating markups, e.g. recursive calls with
135        * changing arguments */
136       SCM opt_depth = ly_get_option (ly_symbol2scm ("max-markup-depth"));
137       size_t max_depth = robust_scm2int (opt_depth, 1024);
138       if (depth > max_depth)
139         {
140           string name = ly_symbol2string (scm_procedure_name (func));
141           // TODO: Also print the arguments of the markup!
142           non_fatal_error (_f ("Markup depth exceeds maximal value of %d; "
143                                "Markup: %s", max_depth, name.c_str ()));
144           return Stencil ().smobbed_copy ();
145         }
146
147       encountered_markups.push_back (markup);
148       SCM retval = scm_apply_2 (func, layout_smob, props, args);
149       encountered_markups.pop_back ();
150       return retval;
151     }
152   else
153     {
154       programming_error ("Object is not a markup.");
155       scm_puts ("This object should be a markup: ", scm_current_error_port ());
156       scm_display (markup, scm_current_error_port ());
157       scm_puts ("\n", scm_current_error_port ());
158
159       return Stencil ().smobbed_copy ();
160     }
161 }
162
163 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
164 SCM
165 Text_interface::print (SCM grob)
166 {
167   Grob *me = unsmob_grob (grob);
168
169   SCM t = me->get_property ("text");
170   SCM chain = Font_interface::text_font_alist_chain (me);
171   return interpret_markup (me->layout ()->self_scm (), chain, t);
172 }
173
174 /* Ugh. Duplicated from Scheme.  */
175 bool
176 Text_interface::is_markup (SCM x)
177 {
178   return scm_is_string (x)
179     || (scm_is_pair (x)
180         && scm_is_true
181         (scm_object_property (scm_car (x),
182                               ly_symbol2scm ("markup-signature")))
183         && scm_is_false
184         (scm_object_property (scm_car (x),
185                               ly_symbol2scm ("markup-list-command"))));
186 }
187
188 bool
189 Text_interface::is_markup_list (SCM x)
190 {
191   SCM music_list_p = ly_lily_module_constant ("markup-list?");
192   return scm_is_true (scm_call_1 (music_list_p, x));
193 }
194
195 ADD_INTERFACE (Text_interface,
196                "A Scheme markup text, see @ruser{Formatting text} and"
197                " @rextend{New markup command definition}.\n"
198                "\n"
199                "There are two important commands:"
200                " @code{ly:text-interface::print}, which is a"
201                " grob callback, and"
202                " @code{ly:text-interface::interpret-markup}.",
203
204                /* properties */
205                "baseline-skip "
206                "replacement-alist "
207                "text "
208                "word-space "
209                "text-direction "
210                "flag-style "
211               );
212