]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-interface.cc
Issue 1388: Initial work to support opentype font features.
[lilypond.git] / lily / text-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 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 #include "lily-imports.hh"
37
38 static void
39 replace_special_characters (string &str, SCM props)
40 {
41   SCM replacement_alist = ly_chain_assoc_get (ly_symbol2scm ("replacement-alist"),
42                                               props,
43                                               SCM_EOL);
44
45   int max_length = 0;
46   for (SCM s = replacement_alist; scm_is_pair (s); s = scm_cdr (s))
47     {
48       max_length = max (max_length, scm_to_int
49                         (scm_string_length (scm_caar (s))));
50     }
51
52   for (vsize i = 0; i < str.size (); i++)
53     {
54       /* Don't match in mid-UTF-8 */
55       if ((str[i] & 0xc0) == 0x80)
56         continue;
57       for (vsize j = max_length + 1; j--;)
58         {
59           if (j > str.size () - i)
60             continue;
61           string dummy = str.substr (i, j);
62           SCM ligature = ly_assoc_get (ly_string2scm (dummy),
63                                        replacement_alist, SCM_BOOL_F);
64           if (scm_is_true (ligature))
65             str.replace (i, j, robust_scm2string (ligature, ""));
66         }
67     }
68 }
69
70 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
71 SCM
72 Text_interface::interpret_string (SCM layout_smob,
73                                   SCM props,
74                                   SCM markup)
75 {
76   LY_ASSERT_SMOB (Output_def, layout_smob, 1);
77   LY_ASSERT_TYPE (scm_is_string, markup, 3);
78
79   string str = ly_scm2string (markup);
80   Output_def *layout = unsmob<Output_def> (layout_smob);
81   Font_metric *fm = select_encoded_font (layout, props);
82
83   replace_special_characters (str, props);
84
85   /*
86     We want to filter strings with a music font that pass through
87     the text interface.  Here the font encoding is checked to see
88     if it matches one of the music font encodings.  --pmccarty
89   */
90   SCM encoding = ly_chain_assoc_get (ly_symbol2scm ("font-encoding"),
91                                      props,
92                                      SCM_BOOL_F);
93   SCM music_encodings = Lily::all_music_font_encodings;
94
95   SCM features = ly_chain_assoc_get (ly_symbol2scm ("font-features"),
96                                      props,
97                                      SCM_BOOL_F);
98
99   // The font-features value is stored in a scheme list. This joins the entries
100   // with commas for processing with pango.
101   string features_str = string ();
102   if (scm_is_pair (features))
103     {
104       bool first = true;
105       for (SCM s = features; scm_is_pair (s); s = scm_cdr (s))
106         {
107           SCM feature = scm_car (s);
108           if (scm_is_string (feature))
109             {
110               if (first)
111                 {
112                   first = false;
113                 }
114               else
115                 {
116                   features_str += ",";
117                 }
118               features_str += ly_scm2string (feature);
119             }
120           else
121             {
122               scm_misc_error (__FUNCTION__, "Found non-string in font-features list", SCM_EOL);
123             }
124         }
125     }
126   else if (!scm_is_false (features))
127     {
128       scm_misc_error (__FUNCTION__, "Expecting a list for font-features value", SCM_EOL);
129     }
130
131   bool is_music = scm_is_true (scm_memq (encoding, music_encodings));
132   return fm->text_stencil (layout, str, is_music, features_str).smobbed_copy ();
133 }
134
135 static size_t markup_depth = 0;
136
137 void markup_up_depth (void *) { ++markup_depth; }
138 void markup_down_depth (void *) { --markup_depth; }
139
140 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
141                                    "Convert a text markup into a stencil."
142                                    "  Takes three arguments, @var{layout}, @var{props}, and @var{markup}.\n"
143                                    "\n"
144                                    "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with"
145                                    " @code{ly:grob-layout}.  @var{props} is an alist chain, i.e. a list of"
146                                    "  alists.  This is typically obtained with"
147                                    " @code{(ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))}."
148                                    "  @var{markup} is the markup text to be processed.");
149 SCM
150 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
151 {
152   if (scm_is_string (markup))
153     return interpret_string (layout_smob, props, markup);
154   else if (is_markup (markup))
155     {
156       SCM func = scm_car (markup);
157       SCM args = scm_cdr (markup);
158
159       /* Check for non-terminating markups, e.g. recursive calls with
160        * changing arguments */
161       SCM opt_depth = ly_get_option (ly_symbol2scm ("max-markup-depth"));
162       size_t max_depth = robust_scm2int (opt_depth, 1024);
163
164       // Don't use SCM_F_DYNWIND_REWINDABLE since it may be expensive
165       // without any obvious use for retaining continuations into
166       // markup expansion
167       scm_dynwind_begin ((scm_t_dynwind_flags)0);
168       // scm_dynwind_rewind_handler (markup_up_depth, 0, SCM_F_WIND_EXPLICITLY);
169       markup_up_depth (0);
170       scm_dynwind_unwind_handler (markup_down_depth, 0, SCM_F_WIND_EXPLICITLY);
171       if (markup_depth > max_depth)
172         {
173           scm_dynwind_end ();
174           string name = ly_symbol2string (scm_procedure_name (func));
175           // TODO: Also print the arguments of the markup!
176           non_fatal_error (_f ("Markup depth exceeds maximal value of %d; "
177                                "Markup: %s", max_depth, name.c_str ()));
178           return Stencil ().smobbed_copy ();
179         }
180
181       SCM retval = scm_apply_2 (func, layout_smob, props, args);
182       scm_dynwind_end ();
183       return retval;
184     }
185   else
186     {
187       programming_error ("Object is not a markup.");
188       scm_puts ("This object should be a markup: ", scm_current_error_port ());
189       scm_display (markup, scm_current_error_port ());
190       scm_puts ("\n", scm_current_error_port ());
191
192       return Stencil ().smobbed_copy ();
193     }
194 }
195
196 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
197 SCM
198 Text_interface::print (SCM grob)
199 {
200   Grob *me = unsmob<Grob> (grob);
201
202   SCM t = me->get_property ("text");
203   SCM chain = Font_interface::text_font_alist_chain (me);
204   return interpret_markup (me->layout ()->self_scm (), chain, t);
205 }
206
207 /* Ugh. Duplicated from Scheme.  */
208 bool
209 Text_interface::is_markup (SCM x)
210 {
211   return scm_is_string (x)
212     || (scm_is_pair (x)
213         && scm_is_true
214         (scm_object_property (scm_car (x),
215                               ly_symbol2scm ("markup-signature")))
216         && scm_is_false
217         (scm_object_property (scm_car (x),
218                               ly_symbol2scm ("markup-list-command"))));
219 }
220
221 bool
222 Text_interface::is_markup_list (SCM x)
223 {
224   return scm_is_true (Lily::markup_list_p (x));
225 }
226
227 ADD_INTERFACE (Text_interface,
228                "A Scheme markup text, see @ruser{Formatting text} and"
229                " @rextend{New markup command definition}.\n"
230                "\n"
231                "There are two important commands:"
232                " @code{ly:text-interface::print}, which is a"
233                " grob callback, and"
234                " @code{ly:text-interface::interpret-markup}.",
235
236                /* properties */
237                "baseline-skip "
238                "replacement-alist "
239                "text "
240                "word-space "
241                "text-direction "
242                "flag-style "
243               );
244