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