]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def-scheme.cc
Add newlines at EOF where appropriate.
[lilypond.git] / lily / output-def-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2011 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   LY_ASSERT_SMOB (Output_def, def, 1);
35   Output_def *op = unsmob_output_def (def);
36   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
37
38   SCM answer = op->lookup_variable (sym);
39   if (answer == SCM_UNDEFINED)
40     {
41       if (val == SCM_UNDEFINED)
42         val = SCM_EOL;
43
44       answer = val;
45     }
46
47   return answer;
48 }
49
50 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
51            1, 0, 0, (SCM def),
52            "Return the variable scope inside @var{def}.")
53 {
54   LY_ASSERT_SMOB (Output_def, def, 1);
55   Output_def *op = unsmob_output_def (def);
56   return op->scope_;
57 }
58
59 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
60            1, 0, 0, (SCM def),
61            "Return the parent output definition of @var{def}.")
62 {
63   LY_ASSERT_SMOB (Output_def, def, 1);
64   Output_def *op = unsmob_output_def (def);
65   return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
66 }
67
68 LY_DEFINE (ly_output_def_set_variable_x, "ly:output-def-set-variable!",
69            3, 0, 0, (SCM def, SCM sym, SCM val),
70            "Set an output definition @var{def} variable @var{sym} to @var{val}.")
71 {
72   LY_ASSERT_SMOB (Output_def, def, 1);
73   Output_def *output_def = unsmob_output_def (def);
74   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
75   output_def->set_variable (sym, val);
76   return SCM_UNSPECIFIED;
77 }
78
79 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
80            1, 0, 0, (SCM def),
81            "Clone output definition @var{def}.")
82 {
83   LY_ASSERT_SMOB (Output_def, def, 1);
84   Output_def *op = unsmob_output_def (def);
85
86   Output_def *clone = op->clone ();
87   return clone->unprotect ();
88 }
89
90 LY_DEFINE (ly_output_description, "ly:output-description",
91            1, 0, 0, (SCM output_def),
92            "Return the description of translators in @var{output-def}.")
93 {
94   Output_def *id = unsmob_output_def (output_def);
95
96   SCM al = ly_module_2_alist (id->scope_);
97   SCM ell = SCM_EOL;
98   for (SCM s = al; scm_is_pair (s); s = scm_cdr (s))
99     {
100       Context_def *td = unsmob_context_def (scm_cdar (s));
101       SCM key = scm_caar (s);
102       if (td && key == td->get_context_name ())
103         ell = scm_cons (scm_cons (key, td->to_alist ()), ell);
104     }
105   return ell;
106 }
107
108 LY_DEFINE (ly_output_def_p, "ly:output-def?",
109            1, 0, 0, (SCM def),
110            "Is @var{def} an output definition?")
111 {
112   return ly_bool2scm (unsmob_output_def (def));
113 }
114
115 LY_DEFINE (ly_paper_outputscale, "ly:paper-outputscale",
116            1, 0, 0, (SCM def),
117            "Return the output-scale for output definition @var{def}.")
118 {
119   LY_ASSERT_SMOB (Output_def, def, 1);
120   Output_def *b = unsmob_output_def (def);
121   return scm_from_double (output_scale (b));
122 }
123
124 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
125            0, 0, 0, (),
126            "Make an output definition.")
127 {
128   Output_def *bp = new Output_def;
129   return bp->unprotect ();
130 }
131
132 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font",
133            2, 0, 0, (SCM def, SCM chain),
134            "Find a font metric in output definition @var{def} satisfying"
135            " the font-qualifiers in alist chain @var{chain}, and return"
136            " it.  (An alist chain is a list of alists, containing grob"
137            " properties.)")
138 {
139   LY_ASSERT_SMOB (Output_def, def, 1);
140
141   Output_def *paper = unsmob_output_def (def);
142   Font_metric *fm = select_font (paper, chain);
143   return fm->self_scm ();
144 }
145
146 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number",
147            2, 0, 0, (SCM def, SCM sym),
148            "Return the value of variable @var{sym} in output definition"
149            " @var{def} as a double.")
150 {
151   LY_ASSERT_SMOB (Output_def, def, 1);
152   Output_def *layout = unsmob_output_def (def);
153   return scm_from_double (layout->get_dimension (sym));
154 }
155
156 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
157            1, 0, 0, (SCM def),
158            "Return a list containing the fonts from output definition"
159            " @var{def} (e.g., @code{\\paper}).")
160 {
161   LY_ASSERT_SMOB (Output_def, def, 1);
162   Output_def *b = unsmob_output_def (def);
163
164   SCM tab1 = b->lookup_variable (ly_symbol2scm ("scaled-fonts"));
165   SCM tab2 = b->lookup_variable (ly_symbol2scm ("pango-fonts"));
166
167   SCM alist1 = SCM_EOL;
168   if (scm_hash_table_p (tab1) == SCM_BOOL_T)
169     {
170       alist1 = scm_append (ly_alist_vals (ly_hash2alist (tab1)));
171
172       alist1 = ly_alist_vals (alist1);
173     }
174
175   SCM alist2 = SCM_EOL;
176   if (scm_hash_table_p (tab2) == SCM_BOOL_T)
177     {
178       // strip original-fonts/pango-font-descriptions
179       alist2 = scm_append (ly_alist_vals (ly_hash2alist (tab2)));
180
181       // strip size factors
182       alist2 = ly_alist_vals (alist2);
183     }
184
185   SCM alist = scm_append (scm_list_2 (alist1, alist2));
186   SCM font_list = SCM_EOL;
187   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
188     {
189       SCM entry = scm_car (s);
190
191       Font_metric *fm = unsmob_metrics (entry);
192
193       if (dynamic_cast<Modified_font_metric *> (fm)
194           || dynamic_cast<Pango_font *> (fm))
195         font_list = scm_cons (fm->self_scm (), font_list);
196     }
197
198   return font_list;
199 }