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