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