]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
patch::: 1.3.114.jcn1
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10
11 #include "all-font-metrics.hh"
12 #include "string.hh"
13 #include "misc.hh"
14 #include "paper-def.hh"
15 #include "debug.hh"
16 #include "scaled-font-metric.hh"
17 #include "main.hh"
18 #include "scope.hh"
19 #include "file-results.hh" // urg? header_global_p
20 #include "paper-stream.hh"
21
22 Paper_def::Paper_def ()
23 {
24   style_sheet_ = SCM_EOL;
25   scaled_fonts_ = SCM_EOL;
26 }
27
28 Paper_def::~Paper_def ()
29 {
30 }
31
32 Paper_def::Paper_def (Paper_def const&src)
33   : Music_output_def (src)
34 {
35   scaled_fonts_ = SCM_EOL;
36   style_sheet_ = src.style_sheet_;
37 }
38
39
40 Real
41 Paper_def::get_var (String s) const
42 {
43   return get_realvar (ly_symbol2scm (s.ch_C()));
44 }
45
46 SCM
47 Paper_def::get_scmvar (String s) const
48 {
49   return  scope_p_->scm_elem (ly_symbol2scm (s.ch_C()));
50 }
51
52 Real
53 Paper_def::get_realvar (SCM s) const
54 {
55   SCM val ;
56   if (!scope_p_->try_retrieve (s, &val))
57     {
58       programming_error ("unknown paper variable: " +  ly_symbol2string (s));
59       return 0.0;
60     }
61
62   Real sc = 1.0;
63   SCM ssc;
64   if (scope_p_->try_retrieve (ly_symbol2scm ("outputscale"), &ssc))
65     {
66       sc = gh_scm2double (ssc);
67     }
68   if (gh_number_p (val))
69     {
70       return gh_scm2double (val) / sc;
71     }
72   else
73     {
74       programming_error ("not a real variable");
75       return 0.0;
76     }
77 }
78
79 /*
80   FIXME. This is broken until we have a generic way of
81   putting lists inside the \paper block.
82  */
83 Interval
84 Paper_def::line_dimensions_int (int n) const
85 {
86   Real lw =  get_var ("linewidth");
87   Real ind = n? 0.0:get_var ("indent");
88
89   return Interval (ind, lw);
90 }
91
92
93
94 int Paper_def::default_count_i_ = 0;
95
96 int
97 Paper_def::get_next_default_count () const
98 {
99   return default_count_i_ ++;
100 }
101
102 void
103 Paper_def::reset_default_count()
104 {
105   default_count_i_ = 0;
106 }
107
108
109 Paper_stream*
110 Paper_def::paper_stream_p () const
111 {
112   String outname = base_output_str ();
113
114   if (outname != "-")
115     outname += String (".") + output_global_ch;
116   progress_indication (_f ("paper output to %s...",
117                            outname == "-" ? String ("<stdout>") : outname));
118
119   target_str_global_array.push (outname);
120   return new Paper_stream (outname);
121 }
122
123
124 /* URGURGUGUUGH
125
126    not const.
127
128    Wat een puinhoop is dit. */
129 String
130 Paper_def::base_output_str () const
131 {
132   String str = get_default_output ();
133
134   if (str.empty_b ())
135     {
136       str = default_outname_base_global;
137       int def = get_next_default_count ();
138       if (def)
139         str += "-" + to_str (def);
140     }
141
142   /* Must store value, as this function can be called only once */
143   Paper_def *urg = (Paper_def*)this;
144   urg->current_output_base_ = str;
145
146   return str;
147 }
148
149 /*
150   todo: use symbols and hashtable idx?
151 */
152 Font_metric *
153 Paper_def::find_font (SCM fn, Real m)
154 {
155   SCM key = gh_cons (fn, gh_double2scm (m));
156   SCM met = scm_assoc (key, scaled_fonts_);
157
158   if (gh_pair_p (met))
159     return unsmob_metrics (gh_cdr (met));
160
161   SCM ssc;
162   if (scope_p_->try_retrieve (ly_symbol2scm ("outputscale"), &ssc))
163     {
164       m /= gh_scm2double (ssc);
165     }
166   
167   Font_metric*  f = all_fonts_global_p->find_font (ly_scm2string (fn));
168   SCM val = Scaled_font_metric::make_scaled_font_metric (f, m);
169   scaled_fonts_ = scm_acons (key, val, scaled_fonts_ );
170
171   scm_unprotect_object (val);
172
173   return dynamic_cast<Scaled_font_metric*> (unsmob_metrics (val));
174 }
175
176
177 /*
178   Return alist to translate internally used fonts back to real-world
179   coordinates.  */
180 SCM
181 Paper_def::font_descriptions ()const
182 {
183
184   
185   SCM l = SCM_EOL;
186   for (SCM s = scaled_fonts_; gh_pair_p (s); s = gh_cdr(s))
187     {
188       SCM desc = gh_caar (s);
189       SCM mdesc = unsmob_metrics (gh_cdar (s))->description_;
190
191       l = gh_cons (gh_cons (mdesc, desc), l);
192     }
193   return l;
194 }