1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2005--2021 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "output-def.hh"
21 
22 #include "pango-font.hh"
23 #include "modified-font-metric.hh"
24 #include "ly-module.hh"
25 #include "context-def.hh"
26 #include "lily-parser.hh"
27 
28 LY_DEFINE (ly_output_def_lookup, "ly:output-def-lookup",
29            2, 1, 0, (SCM def, SCM sym, SCM val),
30            "Return the value of @var{sym} in output definition @var{def}"
31            " (e.g., @code{\\paper}).  If no value is found, return"
32            " @var{val} or @code{'()} if @var{val} is undefined.")
33 {
34   auto *const op = LY_ASSERT_SMOB (Output_def, def, 1);
35   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
36 
37   SCM answer = op->lookup_variable (sym);
38   if (SCM_UNBNDP (answer))
39     {
40       if (SCM_UNBNDP (val))
41         val = SCM_EOL;
42 
43       answer = val;
44     }
45 
46   return answer;
47 }
48 
49 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
50            1, 0, 0, (SCM def),
51            "Return the variable scope inside @var{def}.")
52 {
53   auto *const op = LY_ASSERT_SMOB (Output_def, def, 1);
54   return op->scope_;
55 }
56 
57 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
58            1, 0, 0, (SCM def),
59            "Return the parent output definition of @var{def}.")
60 {
61   auto *const op = LY_ASSERT_SMOB (Output_def, def, 1);
62   return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
63 }
64 
65 LY_DEFINE (ly_output_def_set_variable_x, "ly:output-def-set-variable!",
66            3, 0, 0, (SCM def, SCM sym, SCM val),
67            "Set an output definition @var{def} variable @var{sym} to @var{val}.")
68 {
69   auto *const output_def = LY_ASSERT_SMOB (Output_def, def, 1);
70   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
71   output_def->set_variable (sym, val);
72   return SCM_UNSPECIFIED;
73 }
74 
75 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
76            1, 0, 0, (SCM def),
77            "Clone output definition @var{def}.")
78 {
79   auto *const op = LY_ASSERT_SMOB (Output_def, def, 1);
80 
81   Output_def *clone = op->clone ();
82   return clone->unprotect ();
83 }
84 
85 LY_DEFINE (ly_output_description, "ly:output-description",
86            1, 0, 0, (SCM output_def),
87            "Return the description of translators in @var{output-def}.")
88 {
89   auto *const id = LY_ASSERT_SMOB (Output_def, output_def, 1);
90 
91   SCM al = ly_module_2_alist (id->scope_);
92   SCM ell = SCM_EOL;
93   for (SCM s = al; scm_is_pair (s); s = scm_cdr (s))
94     {
95       Context_def *td = unsmob<Context_def> (scm_cdar (s));
96       SCM key = scm_caar (s);
97       if (td && scm_is_eq (key, td->get_context_name ()))
98         ell = scm_cons (scm_cons (key, td->to_alist ()), ell);
99     }
100   return ell;
101 }
102 
103 LY_DEFINE (ly_output_find_context_def, "ly:output-find-context-def",
104            1, 1, 0, (SCM output_def, SCM context_name),
105            "Return an alist of all context defs (matching @var{context-name}"
106            " if given) in @var{output-def}.")
107 {
108   auto *const id = LY_ASSERT_SMOB (Output_def, output_def, 1);
109   if (!SCM_UNBNDP (context_name))
110     LY_ASSERT_TYPE (ly_is_symbol, context_name, 2);
111 
112   SCM al = ly_module_2_alist (id->scope_);
113   SCM ell = SCM_EOL;
114   for (SCM s = al; scm_is_pair (s); s = scm_cdr (s))
115     {
116       SCM p = scm_car (s);
117       Context_def *td = unsmob<Context_def> (scm_cdr (p));
118       if (td && scm_is_eq (scm_car (p), td->get_context_name ())
119           && (SCM_UNBNDP (context_name) || td->is_alias (context_name)))
120         ell = scm_cons (p, ell);
121     }
122   return ell;
123 }
124 
125 const char
126 *const Output_def::type_p_name_ = "ly:output-def?";
127 
128 LY_DEFINE (ly_paper_outputscale, "ly:paper-outputscale",
129            1, 0, 0, (SCM def),
130            "Return the @code{output-scale} for output definition @var{def}.")
131 {
132   auto *const b = LY_ASSERT_SMOB (Output_def, def, 1);
133   return to_scm (output_scale (b));
134 }
135 
136 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
137            0, 0, 0, (),
138            "Make an output definition.")
139 {
140   Output_def *bp = new Output_def;
141   return bp->unprotect ();
142 }
143 
144 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font",
145            2, 0, 0, (SCM def, SCM chain),
146            "Find a font metric in output definition @var{def} satisfying"
147            " the font qualifiers in alist chain @var{chain}, and return"
148            " it.  (An alist chain is a list of alists, containing grob"
149            " properties.)")
150 {
151   auto *const paper = LY_ASSERT_SMOB (Output_def, def, 1);
152 
153   Font_metric *fm = select_font (paper, chain);
154   return fm->self_scm ();
155 }
156 
157 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number",
158            2, 0, 0, (SCM def, SCM sym),
159            "Return the value of variable @var{sym} in output definition"
160            " @var{def} as a double.")
161 {
162   auto *const layout = LY_ASSERT_SMOB (Output_def, def, 1);
163   return to_scm (layout->get_dimension (sym));
164 }
165 
166 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
167            1, 0, 0, (SCM def),
168            "Return a list containing the fonts from output definition"
169            " @var{def} (e.g., @code{\\paper}).")
170 {
171   auto *const b = LY_ASSERT_SMOB (Output_def, def, 1);
172 
173   SCM tab1 = b->lookup_variable (ly_symbol2scm ("scaled-fonts"));
174   SCM tab2 = b->lookup_variable (ly_symbol2scm ("pango-fonts"));
175 
176   SCM alist1 = SCM_EOL;
177   if (from_scm<bool> (scm_hash_table_p (tab1)))
178     {
179       alist1 = scm_append (ly_alist_vals (ly_hash2alist (tab1)));
180 
181       alist1 = ly_alist_vals (alist1);
182     }
183 
184   SCM alist2 = SCM_EOL;
185   if (scm_is_true (scm_hash_table_p (tab2)))
186     {
187       // strip original-fonts/pango-font-descriptions
188       alist2 = scm_append (ly_alist_vals (ly_hash2alist (tab2)));
189 
190       // strip size factors
191       alist2 = ly_alist_vals (alist2);
192     }
193 
194   SCM alist = scm_append (scm_list_2 (alist1, alist2));
195   SCM font_list = SCM_EOL;
196   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
197     {
198       SCM entry = scm_car (s);
199 
200       Font_metric *fm = unsmob<Font_metric> (entry);
201 
202       if (dynamic_cast<Modified_font_metric *> (fm)
203           || dynamic_cast<Pango_font *> (fm))
204         font_list = scm_cons (fm->self_scm (), font_list);
205     }
206 
207   return font_list;
208 }
209