]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric.cc
* scm/framework-ps.scm (output-variables): separately scale the
[lilypond.git] / lily / font-metric.cc
1 /*   
2   font-metric.cc -- implement Font_metric
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8     Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
9 */
10
11 #include "font-metric.hh"
12
13 #include <math.h>
14 #include <cctype>
15
16 #include "dimensions.hh"
17 #include "modified-font-metric.hh"
18 #include "open-type-font.hh"
19 #include "stencil.hh"
20 #include "virtual-methods.hh"
21 #include "warn.hh"
22
23 #include "ly-smobs.icc"
24
25 Real
26 Font_metric::design_size () const
27 {
28   return 1.0 * point_constant;
29 }
30
31 String
32 Font_metric::coding_scheme () const
33 {
34   return "FontSpecific";
35 }
36
37 Stencil
38 Font_metric::find_by_name (String s) const
39 {
40   s.substitute_char ('-', "M");
41   int idx = name_to_index (s);
42   Box b;
43   
44   SCM expr = SCM_EOL;
45   if (idx >= 0)
46     {
47       expr = scm_list_3 (ly_symbol2scm ("named-glyph"),
48                          self_scm (),
49                          scm_makfrom0str (s.to_str0 ())
50                          );
51       b = get_indexed_char (idx);
52     }
53   
54   Stencil q (b, expr);
55   return q;
56 }
57
58 Font_metric::Font_metric ()
59 {
60   description_ = SCM_EOL;
61   self_scm_ = SCM_EOL;
62   smobify_self ();
63 }
64
65 Font_metric::Font_metric (Font_metric const &)
66 {
67 }
68
69
70 Font_metric::~Font_metric ()
71 {
72 }
73
74 int
75 Font_metric::count () const
76 {
77   return 0;
78 }
79
80 Box 
81 Font_metric::get_ascii_char (int) const
82 {
83   return Box (Interval (0, 0), Interval (0, 0));
84 }
85
86 Box 
87 Font_metric::get_indexed_char (int k) const
88 {
89   return get_ascii_char (k);
90 }
91
92 int
93 Font_metric::name_to_index (String) const
94 {
95   return -1;
96 }
97
98 Offset
99 Font_metric::get_indexed_wxwy (int) const
100 {
101   return Offset (0, 0);
102 }
103
104 void
105 Font_metric::derived_mark () const
106 {
107 }
108
109 SCM
110 Font_metric::mark_smob (SCM s)
111 {
112   Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
113   m->derived_mark ();
114   return m->description_;
115 }
116
117 int
118 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
119 {
120   Font_metric *m = unsmob_metrics (s);
121   scm_puts ("#<", port);
122   scm_puts (classname (m), port);
123   scm_puts (" ", port);
124   scm_write (m->description_, port);
125   scm_puts (">", port);
126   return 1;
127 }
128
129
130
131 IMPLEMENT_SMOBS (Font_metric);
132 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
133 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
134
135
136 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
137            2, 0, 0,
138           (SCM font, SCM name),
139            "Return a Stencil from @var{font} for the glyph named @var{name}.  "
140            "@var{font} must be available as an AFM file.  If the glyph "
141            "is not available, return @code{#f}.")
142 {
143   Font_metric *fm = unsmob_metrics (font);
144   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
145   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
146
147   Stencil m = fm->find_by_name (ly_scm2string (name));
148
149   /* TODO: make optional argument for default if not found.  */
150   return m.smobbed_copy ();
151 }
152
153 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
154            2, 0, 0,
155           (SCM font, SCM index),
156            "Retrieve a Stencil for the glyph numbered @var{index} "
157            "in @var{font}.")
158 {
159   Font_metric *fm = unsmob_metrics (font);
160   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
161   SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
162
163   return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
164 }
165
166 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
167            2, 0, 0,
168           (SCM font, SCM name),
169            "Return the index for @var{name} in @var{font}.")
170 {
171   Font_metric *fm = unsmob_metrics (font);
172   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
173   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
174
175   return scm_from_int (fm->name_to_index (ly_scm2string (name)));
176 }
177
178 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
179            2, 0, 0,
180           (SCM font, SCM index),
181            "Return the character code for @var{index} @var{font}.")
182 {
183   Font_metric *fm = unsmob_metrics (font);
184   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
185   SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
186
187   return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
188 }
189
190 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
191            2, 0, 0,
192           (SCM font, SCM name),
193            "Return the character code for glyph @var{name} in @var{font}.")
194 {
195   Font_metric *fm = unsmob_metrics (font);
196   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
197   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
198 #if 1
199   return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
200 #else
201   return scm_from_unsigned_integer (fm->glyph_name_to_charcode (ly_scm2string (name)));
202 #endif
203 }
204
205 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
206            2, 0, 0,
207           (SCM font, SCM text),
208           "Given the font metric in @var{font} and the string @var{text}, "
209            "compute the extents of that text in that font.  "
210            "The return value is a pair of number-pairs.")
211 {
212   Box b;
213   Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
214     (unsmob_metrics (font));
215   
216   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
217   SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
218   Stencil stc (fm->text_stencil (ly_scm2string (text)));
219   return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
220                    ly_interval2scm (stc.extent (Y_AXIS)));
221 }
222
223 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
224            1, 0, 0,
225            (SCM font),
226            "Given the font metric @var{font}, "
227            "return the corresponding file name.")
228 {
229   Font_metric *fm = unsmob_metrics (font);
230   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
231   return fm->font_file_name();
232 }
233
234 SCM 
235 Font_metric::font_file_name () const
236 {
237   return scm_car (description_);
238 }
239
240 String
241 Font_metric::font_name () const
242 {
243   String s ("unknown");
244   return s;
245 }
246
247 #include "afm.hh"
248
249 LY_DEFINE (ly_font_name, "ly:font-name",
250            1, 0, 0,
251            (SCM font),
252            "Given the font metric @var{font}, "
253            "return the corresponding name.")
254 {
255   Font_metric *fm = unsmob_metrics (font);
256       
257   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
258   return scm_makfrom0str (fm->font_name().to_str0 ());
259 }
260
261 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
262           (SCM font),
263            "Given the font metric @var{font}, return the "
264            "magnification, relative to the current outputscale.")
265 {
266   Font_metric *fm = unsmob_metrics (font);
267   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
268   return scm_cdr (fm->description_);
269 }
270
271 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
272           (SCM font),
273            "Given the font metric @var{font}, return the "
274            "design size, relative to the current outputscale.")
275 {
276   Font_metric *fm = unsmob_metrics (font);
277   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
278   return scm_make_real (fm->design_size ());
279 }
280
281
282
283 int
284 Font_metric::index_to_ascii (int i) const
285 {
286   return i;
287 }
288
289 unsigned
290 Font_metric::index_to_charcode (int i) const
291 {
292   return (unsigned) index_to_ascii (i);
293 }
294
295 Stencil
296 Font_metric::get_ascii_char_stencil (int code) const
297 {
298   SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
299                        scm_int2num (code));
300   Box b = get_ascii_char (code);
301   return Stencil (b, at);
302 }
303
304 Stencil
305 Font_metric::get_indexed_char_stencil (int code) const
306 {
307   int idx = index_to_ascii (code);
308   SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
309   Box b = get_indexed_char (code);
310   return Stencil (b, at);
311 }
312
313 int
314 /*Font_metric::*/
315 get_encoded_index (Font_metric *m, String input_coding, int code)
316 {
317   String font_coding = m->coding_scheme ();
318   if (font_coding == input_coding)
319     return code;
320   SCM s = scm_call_3 (ly_lily_module_constant ("encoded-index"),
321                       scm_makfrom0str (input_coding.to_str0 ()),
322                       scm_makfrom0str (font_coding.to_str0 ()),
323                       scm_int2num (code));
324   return scm_to_int (s);
325 }
326
327 Offset
328 Font_metric::attachment_point (String) const
329 {
330   return Offset (0, 0);
331 }
332
333 SCM
334 Font_metric::sub_fonts () const
335 {
336   return SCM_EOL;
337 }
338
339 Stencil
340 Font_metric::text_stencil (String str) const
341 {
342   SCM lst = scm_list_3 (ly_symbol2scm ("text"),
343                         this->self_scm (),
344                         scm_makfrom0str (str.to_str0 ()));
345   
346   Box b = text_dimension (str);
347   return Stencil (b, lst);
348 }
349
350 Box
351 Font_metric::text_dimension (String) const
352 {
353   return Box (Interval (0,0), Interval (0,0));
354 }