]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def-scheme.cc
Merge with master
[lilypond.git] / lily / output-def-scheme.cc
1 /*
2   output-def-scheme.cc -- implement Output_def bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "output-def.hh"
10
11 #include "pango-font.hh"
12 #include "modified-font-metric.hh"
13 #include "ly-module.hh"
14 #include "context-def.hh"
15 #include "lily-parser.hh"
16
17 LY_DEFINE (ly_output_def_lookup, "ly:output-def-lookup",
18            2, 1, 0, (SCM pap, SCM sym, SCM def),
19            "Lookup @var{sym} in the Output_def @var{pap}. "
20            "Return the value or @var{def} (which defaults to  @code{'()}) if undefined.")
21 {
22   LY_ASSERT_SMOB (Output_def, pap, 1);
23   Output_def *op = unsmob_output_def (pap);
24   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
25
26   SCM answer = op->lookup_variable (sym);
27   if (answer == SCM_UNDEFINED)
28     {
29       if (def == SCM_UNDEFINED)
30         def = SCM_EOL;
31
32       answer = def;
33     }
34
35   return answer;
36 }
37
38 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
39            1, 0, 0, (SCM def),
40            "Get the variable scope inside @var{def}.")
41 {
42   LY_ASSERT_SMOB (Output_def, def, 1);
43   Output_def *op = unsmob_output_def (def);
44   return op->scope_;
45 }
46
47 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
48            1, 0, 0, (SCM def),
49            "Get the parent output-def of @var{def}.")
50 {
51   LY_ASSERT_SMOB (Output_def, def, 1);
52   Output_def *op = unsmob_output_def (def);
53   return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
54 }
55
56 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
57            1, 0, 0, (SCM def),
58            "Clone @var{def}.")
59 {
60   LY_ASSERT_SMOB (Output_def, def, 1);
61   Output_def *op = unsmob_output_def (def);
62
63   Output_def *clone = op->clone ();
64   return clone->unprotect ();
65 }
66
67 LY_DEFINE (ly_output_description, "ly:output-description",
68            1, 0, 0, (SCM output_def),
69            "Return the description of translators in @var{output-def}.")
70 {
71   Output_def *id = unsmob_output_def (output_def);
72
73   SCM al = ly_module_2_alist (id->scope_);
74   SCM ell = SCM_EOL;
75   for (SCM s = al; scm_is_pair (s); s = scm_cdr (s))
76     {
77       Context_def *td = unsmob_context_def (scm_cdar (s));
78       SCM key = scm_caar (s);
79       if (td && key == td->get_context_name ())
80         ell = scm_cons (scm_cons (key, td->to_alist ()), ell);
81     }
82   return ell;
83 }
84
85 LY_DEFINE (ly_output_def_p, "ly:output-def?",
86            1, 0, 0, (SCM def),
87            "Is @var{def} a layout definition?")
88 {
89   return ly_bool2scm (unsmob_output_def (def));
90 }
91
92 LY_DEFINE (ly_paper_outputscale, "ly:paper-outputscale",
93            1, 0, 0, (SCM bp),
94            "Get output-scale for BP.")
95 {
96   LY_ASSERT_SMOB (Output_def, bp, 1);
97   Output_def *b = unsmob_output_def (bp);
98   return scm_from_double (output_scale (b));
99 }
100
101 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
102            0, 0, 0, (),
103            "Make a output def.")
104 {
105   Output_def *bp = new Output_def;
106   return bp->unprotect ();
107 }
108
109 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font", 2, 0, 0,
110            (SCM paper_smob, SCM chain),
111
112            "Return a font metric satisfying the font-qualifiers "
113            "in the alist chain @var{chain}.\n"
114            "(An alist chain is a list of alists, "
115            "containing grob properties).\n")
116 {
117   LY_ASSERT_SMOB (Output_def, paper_smob, 1);
118
119   Output_def *paper = unsmob_output_def (paper_smob);
120   Font_metric *fm = select_font (paper, chain);
121   return fm->self_scm ();
122 }
123
124 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
125            (SCM layout_smob, SCM name),
126            "Return the layout variable @var{name}.")
127 {
128  
129   LY_ASSERT_SMOB (Output_def, layout_smob, 1);
130   Output_def *layout = unsmob_output_def (layout_smob);
131   return scm_from_double (layout->get_dimension (name));
132 }
133
134 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
135            1, 0, 0,
136            (SCM bp),
137            "Return fonts from the @code{\\paper} block @var{bp}.")
138 {
139   LY_ASSERT_SMOB (Output_def, bp, 1);
140   Output_def *b = unsmob_output_def (bp);
141
142   SCM tab1 = b->lookup_variable (ly_symbol2scm ("scaled-fonts"));
143   SCM tab2 = b->lookup_variable (ly_symbol2scm ("pango-fonts"));
144
145   SCM alist1 = SCM_EOL;
146   if (scm_hash_table_p (tab1) == SCM_BOOL_T)
147     {
148       alist1 = scm_append (ly_alist_vals (ly_hash2alist (tab1)));
149
150       alist1 = ly_alist_vals (alist1);
151     }
152
153   SCM alist2 = SCM_EOL;
154   if (scm_hash_table_p (tab2) == SCM_BOOL_T)
155     {
156       // strip original-fonts/pango-font-descriptions
157       alist2 = scm_append (ly_alist_vals (ly_hash2alist (tab2)));
158
159       // strip size factors
160       alist2 = ly_alist_vals (alist2);
161     }
162
163   SCM alist = scm_append (scm_list_2 (alist1, alist2));
164   SCM font_list = SCM_EOL;
165   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
166     {
167       SCM entry = scm_car (s);
168
169       Font_metric *fm = unsmob_metrics (entry);
170
171       if (dynamic_cast<Modified_font_metric *> (fm)
172           || dynamic_cast<Pango_font *> (fm))
173         font_list = scm_cons (fm->self_scm (), font_list);
174     }
175
176   return font_list;
177 }