]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
* lily/my-lily-parser.cc: remove paper_description function
[lilypond.git] / lily / paper-def.cc
1 /*
2   paper-def.cc -- implement Paper_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <math.h>
11
12 #include "virtual-font-metric.hh"
13 #include "all-font-metrics.hh"
14 #include "string.hh"
15 #include "misc.hh"
16 #include "paper-def.hh"
17 #include "warn.hh"
18 #include "scaled-font-metric.hh"
19 #include "main.hh"
20 #include "scm-hash.hh"
21 #include "input-file-results.hh" // urg? header_global
22 #include "paper-outputter.hh"
23 #include "ly-module.hh"
24
25 /*
26   This is an almost empty thing. The only substantial thing this class
27   handles is scaling up and down to real-world dimensions (internally
28   dimensions are against global staff-space.)
29  */
30
31 Paper_def::Paper_def ()
32 {
33   /* Do not remove this statement, scm_make_hash_table may trigger GC.  */
34   scaled_fonts_ = SCM_EOL;
35   scaled_fonts_ = scm_c_make_hash_table (11);
36 }
37
38 Paper_def::Paper_def (Paper_def const&src)
39   : Music_output_def (src)
40 {
41   /* Do not remove this statement, scm_make_hash_table may trigger GC.  */
42   scaled_fonts_ = SCM_EOL;
43   scaled_fonts_ = scm_c_make_hash_table (11);
44 }
45
46 Paper_def::~Paper_def ()
47 {
48 }
49
50 void
51 Paper_def::derived_mark ()
52 {
53   scm_gc_mark (scaled_fonts_);
54 }
55
56 Real
57 Paper_def::get_dimension (SCM s) const
58 {
59   SCM val = lookup_variable (s);
60   SCM scale = lookup_variable (ly_symbol2scm ("outputscale"));
61   
62   Real sc = ly_scm2double (scale);
63   return ly_scm2double (val) / sc;
64 }
65
66 /* FIXME.  This is broken until we have a generic way of
67    putting lists inside the \paper block.  */
68 Interval
69 Paper_def::line_dimensions_int (int n) const
70 {
71   Real lw =  get_dimension (ly_symbol2scm ("linewidth"));
72   Real ind = n? 0.0:get_dimension (ly_symbol2scm ("indent"));
73
74   return Interval (ind, lw);
75 }
76
77 Paper_outputter*
78 Paper_def::get_paper_outputter (String outname) const
79 {
80   progress_indication (_f ("paper output to `%s'...",
81                            outname == "-" ? String ("<stdout>") : outname));
82
83   // fixme: dependencies
84   //  add_target_file (outname);
85   
86   Paper_outputter * po = new Paper_outputter (outname);
87   Path p = split_path (outname);
88   p.ext = "";
89   po->basename_ = p.to_string ();
90   return po;
91 }
92
93 Font_metric*
94 Paper_def::find_scaled_font (Font_metric *f, Real m, SCM input_enc_name)
95 {
96   SCM sizes = scm_hashq_ref (scaled_fonts_, f->self_scm (), SCM_BOOL_F);
97   if (sizes != SCM_BOOL_F)
98     {
99       SCM met = scm_assoc (scm_make_real (m), sizes);
100       if (is_pair (met))
101         return unsmob_metrics (ly_cdr (met));
102     }
103   else
104     sizes = SCM_EOL;
105   
106   /* Hmm. We're chaining font - metrics.  Should consider whether to
107      merge virtual-font and scaled_font.  */
108   SCM val = SCM_EOL;
109   if (Virtual_font_metric * vf = dynamic_cast<Virtual_font_metric*> (f))
110     {
111       /* For fontify_atom (), the magnification and name must be known
112          at the same time. That's impossible for
113
114           Scaled (Virtual_font (Font1,Font2))
115
116         so we replace by
117
118           Virtual_font (Scaled (Font1), Scaled (Font2))  */
119       SCM lst = SCM_EOL;
120       SCM *t = &lst;
121       for (SCM s = vf->get_font_list (); is_pair (s); s = ly_cdr (s))
122         {
123           Font_metric *scaled = find_scaled_font (unsmob_metrics (ly_car (s)),
124                                                   m, input_enc_name);
125           *t = scm_cons (scaled->self_scm (), SCM_EOL);
126           t = SCM_CDRLOC(*t);
127         }
128
129       vf = new Virtual_font_metric (lst);
130       val = vf->self_scm ();
131     }
132   else
133     {
134       SCM scale_var = ly_module_lookup (scope_, ly_symbol2scm ("outputscale"));
135
136       if (!is_symbol (input_enc_name))
137         {
138           SCM var = ly_module_lookup (scope_, ly_symbol2scm ("inputencoding"));
139           input_enc_name = scm_variable_ref (var);
140         }
141       m /= ly_scm2double (scm_variable_ref (scale_var));
142       val = Modified_font_metric::make_scaled_font_metric (input_enc_name,
143                                                            f, m);
144     }
145
146   sizes = scm_acons (scm_make_real (m), val, sizes);
147   scm_gc_unprotect_object (val);
148   scm_hashq_set_x (scaled_fonts_, f->self_scm (), sizes);
149   return unsmob_metrics (val);
150 }
151
152
153 /* Return alist to translate internally used fonts back to real-world
154    coordinates.  */
155 SCM
156 Paper_def::font_descriptions () const
157 {
158   SCM func = ly_scheme_function ("hash-table->alist");
159
160   SCM l = SCM_EOL;
161   for (SCM s = scm_call_1 (func, scaled_fonts_); is_pair (s); s = ly_cdr (s))
162     {
163       SCM entry = ly_car (s);
164       for (SCM t = ly_cdr (entry); is_pair (t); t  = ly_cdr (t))
165         {
166           Font_metric *fm= unsmob_metrics (ly_cdar (t));
167
168           if (dynamic_cast<Modified_font_metric*> (fm))
169             l = scm_cons (fm->self_scm (), l);
170         }
171     }
172   return l;
173 }
174
175 Paper_def* 
176 unsmob_paper (SCM x)
177 {
178   return dynamic_cast<Paper_def*> (unsmob_music_output_def (x));
179 }
180   
181
182 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
183            1, 0, 0, (SCM def),
184            "Is @var{def} a paper definition?")
185 {
186   Paper_def *op = dynamic_cast<Paper_def*> (unsmob_music_output_def (def));
187
188   bool pap = op;
189   return ly_bool2scm (pap);
190 }