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