]> 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   int idx = name_to_index (s);
39   Box b;
40   
41   SCM expr = SCM_EOL;
42   if (idx >= 0)
43     {
44       expr = scm_list_3 (ly_symbol2scm ("char"),
45                          self_scm (),
46                          scm_int2num (index_to_ascii (idx)));
47       b = get_indexed_char (idx);
48     }
49   
50   Stencil q (b, expr);
51   return q;
52 }
53
54 Font_metric::Font_metric ()
55 {
56   description_ = SCM_EOL;
57   self_scm_ = SCM_EOL;
58   smobify_self ();
59 }
60
61 Font_metric::Font_metric (Font_metric const &)
62 {
63 }
64
65
66 Font_metric::~Font_metric ()
67 {
68 }
69
70 int
71 Font_metric::count () const
72 {
73   return 0;
74 }
75
76 Box 
77 Font_metric::get_ascii_char (int) const
78 {
79   return Box (Interval (0, 0), Interval (0, 0));
80 }
81
82 Box 
83 Font_metric::get_indexed_char (int k) const
84 {
85   return get_ascii_char (k);
86 }
87
88 int
89 Font_metric::name_to_index (String) const
90 {
91   return -1;
92 }
93
94 Offset
95 Font_metric::get_indexed_wxwy (int) const
96 {
97   return Offset (0, 0);
98 }
99
100 void
101 Font_metric::derived_mark () const
102 {
103 }
104
105 SCM
106 Font_metric::mark_smob (SCM s)
107 {
108   Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
109   m->derived_mark ();
110   return m->description_;
111 }
112
113 int
114 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
115 {
116   Font_metric *m = unsmob_metrics (s);
117   scm_puts ("#<", port);
118   scm_puts (classname (m), port);
119   scm_puts (" ", port);
120   scm_write (m->description_, port);
121   scm_puts (">", port);
122   return 1;
123 }
124
125
126
127 IMPLEMENT_SMOBS (Font_metric);
128 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
129 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
130
131
132 LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name",
133            2, 0, 0,
134           (SCM font, SCM name),
135           "This function retrieves a Stencil for the glyph named @var{name} "
136            "in "
137            "@var{font}.  "
138            "The font must be available as an AFM file. If the glyph "
139            "is not found, @code{#f} is returned. ")
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_text_dimension,"ly:text-dimension",
165            2, 0, 0,
166           (SCM font, SCM text),
167           "Given the font metric in @var{font} and the string @var{text}, "
168            "compute the extents of that text in that font.  "
169            "The return value is a pair of number-pairs.")
170 {
171   Box b;
172   Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
173     (unsmob_metrics (font));
174   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
175   SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
176   
177   b = fm->text_dimension (ly_scm2string (text));
178   
179   return scm_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
180 }
181
182 LY_DEFINE (ly_font_file_name,"ly:font-file-name",
183            1, 0, 0,
184            (SCM font),
185            "Given the font metric @var{font}, "
186            "return the corresponding file name.")
187 {
188   Font_metric *fm = unsmob_metrics (font);
189   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
190   return scm_car (fm->description_);
191 }
192
193
194 #include "afm.hh"
195
196 LY_DEFINE (ly_font_name,"ly:font-name",
197            1, 0, 0,
198            (SCM font),
199            "Given the font metric @var{font}, "
200            "return the corresponding name.")
201 {
202   Font_metric *fm = unsmob_metrics (font);
203       
204   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
205
206
207   if (Modified_font_metric* mfm = dynamic_cast<Modified_font_metric*> (fm))
208     return ly_font_name (mfm->original_font ()->self_scm ());
209   else if (Adobe_font_metric* afm = dynamic_cast<Adobe_font_metric*> (fm))
210     {
211       return scm_makfrom0str (afm->font_info_->gfi->fontName);
212     }
213   else
214     return SCM_BOOL_F;
215 }
216
217
218
219 LY_DEFINE (ly_font_magnification,"ly:font-magnification", 1 , 0, 0,
220           (SCM font),
221            "Given the font metric @var{font}, return the "
222            "magnification, relative to the current outputscale.")
223 {
224   Font_metric *fm = unsmob_metrics (font);
225   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
226   return scm_cdr (fm->description_);
227 }
228
229 LY_DEFINE (ly_font_design_size,"ly:font-design-size", 1 , 0, 0,
230           (SCM font),
231            "Given the font metric @var{font}, return the "
232            "design size, relative to the current outputscale.")
233 {
234   Font_metric *fm = unsmob_metrics (font);
235   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
236   return scm_make_real (fm->design_size ());
237 }
238
239
240
241 int
242 Font_metric::index_to_ascii (int i) const
243 {
244   return i;
245 }
246
247 Stencil
248 Font_metric::get_ascii_char_stencil (int code) const
249 {
250   SCM at = scm_list_3 (ly_symbol2scm ("char"),
251                        this->self_scm (),
252                        scm_int2num (code));
253   Box b = get_ascii_char (code);
254   return Stencil (b, at);
255 }
256
257 Stencil
258 Font_metric::get_indexed_char_stencil (int code) const
259 {
260   int idx = index_to_ascii (code);
261   SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
262   Box b = get_indexed_char (code);
263   return Stencil (b, at);
264 }
265
266 int
267 /*Font_metric::*/
268 get_encoded_index (Font_metric *m, String input_coding, int code)
269 {
270   String font_coding = m->coding_scheme ();
271   if (font_coding == input_coding)
272     return code;
273   SCM s = scm_call_3 (ly_scheme_function ("encoded-index"),
274                       scm_makfrom0str (input_coding.to_str0 ()),
275                       scm_makfrom0str (font_coding.to_str0 ()),
276                       scm_int2num (code));
277   return scm_to_int (s);
278 }