]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-interface.cc
New alist to replace special characters.
[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 "warn.hh"
32
33 static void
34 replace_special_characters (string *str, SCM props)
35 {
36   vsize i = 0;
37   SCM replacement_alist = ly_chain_assoc_get (ly_symbol2scm ("replacement-alist"),
38                                               props,
39                                               SCM_BOOL_F);
40
41   if (!to_boolean (scm_list_p (replacement_alist))
42         || to_boolean (scm_null_p (replacement_alist)))
43     return;
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   while (i <= str->size ())
53     {
54       for (int j = max_length; j > 0; j--)
55         {
56           string dummy = str->substr (i, j);
57           string ligature = robust_scm2string
58                               (ly_assoc_get (ly_string2scm (dummy),
59                               replacement_alist, SCM_BOOL_F), "");
60           if (ligature != "")
61             str->replace (i, j, ligature);
62         }
63       i += utf8_char_len ((*str)[i]);
64     }
65 }
66
67 MAKE_SCHEME_CALLBACK (Text_interface, interpret_string, 3);
68 SCM
69 Text_interface::interpret_string (SCM layout_smob,
70                                   SCM props,
71                                   SCM markup)
72 {
73   LY_ASSERT_SMOB (Output_def, layout_smob, 1);
74   LY_ASSERT_TYPE (scm_is_string, markup, 3);
75
76   string str = ly_scm2string (markup);
77   Output_def *layout = unsmob_output_def (layout_smob);
78   Font_metric *fm = select_encoded_font (layout, props);
79
80   replace_special_characters (&str, props);
81
82   /*
83     We want to filter strings with a music font that pass through
84     the text interface.  Here the font encoding is checked to see
85     if it matches one of the music font encodings.  --pmccarty
86   */
87   SCM encoding = ly_chain_assoc_get (ly_symbol2scm ("font-encoding"),
88                                      props,
89                                      SCM_BOOL_F);
90   SCM music_encodings = ly_lily_module_constant ("all-music-font-encodings");
91
92   bool is_music = (scm_memq (encoding, music_encodings) != SCM_BOOL_F);
93   return fm->text_stencil (layout, str, is_music).smobbed_copy ();
94 }
95
96 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Text_interface, interpret_markup, 3, 0,
97                                    "Convert a text markup into a stencil."
98                                    "  Takes three arguments, @var{layout}, @var{props}, and @var{markup}.\n"
99                                    "\n"
100                                    "@var{layout} is a @code{\\layout} block; it may be obtained from a grob with"
101                                    " @code{ly:grob-layout}.  @var{props} is an alist chain, i.e. a list of"
102                                    "  alists.  This is typically obtained with"
103                                    " @code{(ly:grob-alist-chain grob (ly:output-def-lookup layout 'text-font-defaults))}."
104                                    "  @var{markup} is the markup text to be processed.");
105 SCM
106 Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
107 {
108   if (scm_is_string (markup))
109     return interpret_string (layout_smob, props, markup);
110   else if (scm_is_pair (markup))
111     {
112       SCM func = scm_car (markup);
113       SCM args = scm_cdr (markup);
114       if (!is_markup (markup))
115         programming_error ("markup head has no markup signature");
116
117       return scm_apply_2 (func, layout_smob, props, args);
118     }
119   else
120     {
121       programming_error ("Object is not a markup. ");
122       scm_puts ("This object should be a markup: ", scm_current_error_port ());
123       scm_display (markup, scm_current_error_port ());
124       scm_puts ("\n", scm_current_error_port ());
125
126       Box b;
127       b[X_AXIS].set_empty ();
128       b[Y_AXIS].set_empty ();
129
130       Stencil s (b, SCM_EOL);
131       return s.smobbed_copy ();
132     }
133 }
134
135 MAKE_SCHEME_CALLBACK (Text_interface, print, 1);
136 SCM
137 Text_interface::print (SCM grob)
138 {
139   Grob *me = unsmob_grob (grob);
140
141   SCM t = me->get_property ("text");
142   SCM chain = Font_interface::text_font_alist_chain (me);
143   return interpret_markup (me->layout ()->self_scm (), chain, t);
144 }
145
146 /* Ugh. Duplicated from Scheme.  */
147 bool
148 Text_interface::is_markup (SCM x)
149 {
150   return (scm_is_string (x)
151           || (scm_is_pair (x)
152               && SCM_BOOL_F
153               != scm_object_property (scm_car (x),
154                                       ly_symbol2scm ("markup-signature"))));
155 }
156
157 bool
158 Text_interface::is_markup_list (SCM x)
159 {
160   SCM music_list_p = ly_lily_module_constant ("markup-list?");
161   return scm_is_true (scm_call_1 (music_list_p, x));
162 }
163
164 ADD_INTERFACE (Text_interface,
165                "A Scheme markup text, see @ruser{Formatting text} and"
166                " @rextend{New markup command definition}.\n"
167                "\n"
168                "There are two important commands:"
169                " @code{ly:text-interface::print}, which is a"
170                " grob callback, and"
171                " @code{ly:text-interface::interpret-markup}.",
172
173                /* properties */
174                "baseline-skip "
175                "replacement-alist "
176                "text "
177                "word-space "
178                "text-direction "
179               );
180