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