2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
21 #include "text-interface.hh"
24 #include "font-interface.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"
36 replace_special_characters (string *str, SCM props)
39 SCM replacement_alist = ly_chain_assoc_get (ly_symbol2scm ("replacement-alist"),
44 for (SCM s = replacement_alist; scm_is_pair (s); s = scm_cdr (s))
46 max_length = max (max_length, scm_to_int
47 (scm_string_length (scm_caar (s))));
50 while (i <= str->size ())
52 for (vsize j = max_length + 1; j--;)
54 string dummy = str->substr (i, j);
55 string ligature = robust_scm2string
56 (ly_assoc_get (ly_string2scm (dummy),
57 replacement_alist, SCM_BOOL_F), "");
59 str->replace (i, j, ligature);
61 i += utf8_char_len ((*str)[i]);
65 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
67 Text_interface::interpret_string (SCM layout_smob,
71 LY_ASSERT_SMOB (Output_def, layout_smob, 1);
72 LY_ASSERT_TYPE (scm_is_string, markup, 3);
74 string str = ly_scm2string (markup);
75 Output_def *layout = unsmob_output_def (layout_smob);
76 Font_metric *fm = select_encoded_font (layout, props);
78 replace_special_characters (&str, props);
81 We want to filter strings with a music font that pass through
82 the text interface. Here the font encoding is checked to see
83 if it matches one of the music font encodings. --pmccarty
85 SCM encoding = ly_chain_assoc_get (ly_symbol2scm ("font-encoding"),
88 SCM music_encodings = ly_lily_module_constant ("all-music-font-encodings");
90 bool is_music = (scm_memq (encoding, music_encodings) != SCM_BOOL_F);
91 return fm->text_stencil (layout, str, is_music).smobbed_copy ();
94 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
95 "Convert a text markup into a stencil."
96 " Takes three arguments, @var{layout}, @var{props}, and @var{markup}.\n"
98 "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with"
99 " @code{ly:grob-layout}. @var{props} is an alist chain, i.e. a list of"
100 " alists. This is typically obtained with"
101 " @code{(ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))}."
102 " @var{markup} is the markup text to be processed.");
104 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
106 if (scm_is_string (markup))
107 return interpret_string (layout_smob, props, markup);
108 else if (scm_is_pair (markup))
110 SCM func = scm_car (markup);
111 SCM args = scm_cdr (markup);
112 if (!is_markup (markup))
113 programming_error ("markup head has no markup signature");
115 /* Use a hare/tortoise algorithm to detect whether we are in a cycle,
116 * i.e. whether we have already encountered the same markup in the
117 * current branch of the markup tree structure. */
118 static vector<SCM> encountered_markups;
119 size_t depth = encountered_markups.size ();
122 int slow = depth / 2;
123 if (ly_is_equal (encountered_markups[slow], markup))
125 string name = ly_symbol2string (scm_procedure_name (func));
126 // TODO: Also print the arguments of the markup!
127 non_fatal_error (_f ("Cyclic markup detected: %s", name));
128 return Stencil ().smobbed_copy ();
132 /* Check for non-terminating markups, e.g. recursive calls with
133 * changing arguments */
134 SCM opt_depth = ly_get_option (ly_symbol2scm ("max-markup-depth"));
135 size_t max_depth = robust_scm2int (opt_depth, 1024);
136 if (depth > max_depth)
138 string name = ly_symbol2string (scm_procedure_name (func));
139 // TODO: Also print the arguments of the markup!
140 non_fatal_error (_f ("Markup depth exceeds maximal value of %d; "
141 "Markup: %s", max_depth, name.c_str ()));
142 return Stencil ().smobbed_copy ();
145 encountered_markups.push_back (markup);
146 SCM retval = scm_apply_2 (func, layout_smob, props, args);
147 encountered_markups.pop_back ();
152 programming_error ("Object is not a markup. ");
153 scm_puts ("This object should be a markup: ", scm_current_error_port ());
154 scm_display (markup, scm_current_error_port ());
155 scm_puts ("\n", scm_current_error_port ());
157 return Stencil ().smobbed_copy ();
161 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
163 Text_interface::print (SCM grob)
165 Grob *me = unsmob_grob (grob);
167 SCM t = me->get_property ("text");
168 SCM chain = Font_interface::text_font_alist_chain (me);
169 return interpret_markup (me->layout ()->self_scm (), chain, t);
172 /* Ugh. Duplicated from Scheme. */
174 Text_interface::is_markup (SCM x)
176 return (scm_is_string (x)
179 != scm_object_property (scm_car (x),
180 ly_symbol2scm ("markup-signature"))));
184 Text_interface::is_markup_list (SCM x)
186 SCM music_list_p = ly_lily_module_constant ("markup-list?");
187 return scm_is_true (scm_call_1 (music_list_p, x));
190 ADD_INTERFACE (Text_interface,
191 "A Scheme markup text, see @ruser{Formatting text} and"
192 " @rextend{New markup command definition}.\n"
194 "There are two important commands:"
195 " @code{ly:text-interface::print}, which is a"
196 " grob callback, and"
197 " @code{ly:text-interface::interpret-markup}.",