]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric.cc
* lily/performance.cc (output): remap modulo 16.
[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 <math.h>
12 #include <ctype.h>
13
14 #include "scaled-font-metric.hh"
15 #include "virtual-methods.hh"
16 #include "warn.hh"
17 #include "stencil.hh"
18 #include "ly-smobs.icc"
19 #include "font-metric.hh"
20 #include "string.hh"
21
22 Real
23 Font_metric::design_size () const
24 {
25   return 1.0;
26 }
27
28 String
29 Font_metric::coding_scheme () const
30 {
31   return "FontSpecific";
32 }
33
34 Stencil
35 Font_metric::find_by_name (String s) const
36 {
37   int idx = name_to_index (s);
38   Box b;
39   
40   SCM expr = SCM_EOL;
41   if (idx >= 0)
42     {
43       expr = scm_list_3 (ly_symbol2scm ("char"),
44                          self_scm (),
45                          gh_int2scm (index_to_ascii (idx)));
46       b = get_indexed_char (idx);
47     }
48   
49   Stencil q (b, expr);
50   return q;
51 }
52
53 Font_metric::Font_metric ()
54 {
55   description_ = SCM_EOL;
56   self_scm_ = SCM_EOL;
57   smobify_self ();
58 }
59
60 Font_metric::Font_metric (Font_metric const &)
61 {
62 }
63
64
65 Font_metric::~Font_metric ()
66 {
67 }
68
69 int
70 Font_metric::count () const
71 {
72   return 0;
73 }
74
75 Box 
76 Font_metric::get_ascii_char (int) const
77 {
78   return Box (Interval (0, 0), Interval (0, 0));
79 }
80
81 Box 
82 Font_metric::get_indexed_char (int k) const
83 {
84   return get_ascii_char (k);
85 }
86
87 int
88 Font_metric::name_to_index (String) const
89 {
90   return -1;
91 }
92
93 Offset
94 Font_metric::get_indexed_wxwy (int )const
95 {
96   return Offset (0, 0);
97 }
98
99 void
100 Font_metric::derived_mark ()const
101 {
102 }
103
104 SCM
105 Font_metric::mark_smob (SCM s)
106 {
107   Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
108   m->derived_mark ();
109   return m->description_;
110 }
111
112 int
113 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
114 {
115   Font_metric *m = unsmob_metrics (s);
116   scm_puts ("#<", port);
117   scm_puts (classname (m), port);
118   scm_puts (" ", port);
119   scm_write (m->description_, port);
120   scm_puts (">", port);
121   return 1;
122 }
123
124
125
126 IMPLEMENT_SMOBS (Font_metric);
127 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
128 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
129
130
131 LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name",
132            2, 0, 0,
133           (SCM font, SCM name),
134           "This function retrieves a Stencil for the glyph named @var{name} "
135            "in "
136            "@var{font}.  "
137            "The font must be available as an AFM file. If the glyph "
138            "is not found, @code{#f} is returned. ")
139 {
140   Font_metric *fm = unsmob_metrics (font);
141   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
142   SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG2, __FUNCTION__, "string");
143
144   Stencil m = fm->find_by_name (ly_scm2string (name));
145
146   /* TODO: make optional argument for default if not found.  */
147   return m.smobbed_copy ();
148 }
149
150 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
151            2, 0, 0,
152           (SCM font, SCM index),
153            "Retrieve a Stencil for the glyph numbered @var{index} "
154            "in @var{font}.")
155 {
156   Font_metric *fm = unsmob_metrics (font);
157   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
158   SCM_ASSERT_TYPE (gh_number_p (index), index, SCM_ARG2, __FUNCTION__, "number");
159
160   return fm->get_ascii_char_stencil (gh_scm2int (index)).smobbed_copy ();
161 }
162
163 LY_DEFINE (ly_text_dimension,"ly:text-dimension",
164            2, 0, 0,
165           (SCM font, SCM text),
166           "Given the font metric in @var{font} and the string @var{text}, "
167            "compute the extents of that text in that font.  "
168            "The return value is a pair of number-pairs.")
169 {
170   Box b;
171   Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
172     (unsmob_metrics (font));
173   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
174   SCM_ASSERT_TYPE (gh_string_p (text), text, SCM_ARG2, __FUNCTION__, "string");
175   
176   b = fm->text_dimension (ly_scm2string (text));
177   
178   return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
179 }
180
181 LY_DEFINE (ly_font_name,"ly:font-name",
182            1, 0, 0,
183            (SCM font),
184            "Given the font metric @var{font}, "
185            "return the corresponding file name.")
186 {
187   Font_metric *fm = unsmob_metrics (font);
188   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
189   return gh_car (fm->description_);
190 }
191
192 LY_DEFINE (ly_font_magnification,"ly:font-magnification", 1 , 0, 0,
193           (SCM font),
194            "Given the font metric @var{font}, return the "
195            "magnification, relative to the current outputscale.")
196 {
197   Font_metric *fm = unsmob_metrics (font);
198   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
199   return gh_cdr (fm->description_);
200 }
201
202 LY_DEFINE (ly_font_design_size,"ly:font-design-size", 1 , 0, 0,
203           (SCM font),
204            "Given the font metric @var{font}, return the "
205            "design size, relative to the current outputscale.")
206 {
207   Font_metric *fm = unsmob_metrics (font);
208   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
209   return gh_double2scm (fm->design_size ());
210 }
211
212
213
214 int
215 Font_metric::index_to_ascii (int i) const
216 {
217   return i;
218 }
219
220 Stencil
221 Font_metric::get_ascii_char_stencil (int code) const
222 {
223   SCM at = scm_list_2 (ly_symbol2scm ("char"), gh_int2scm (code));
224   at = fontify_atom (this, at);
225   Box b = get_ascii_char (code);
226   return Stencil (b, at);
227 }
228
229 Stencil
230 Font_metric::get_indexed_char_stencil (int code) const
231 {
232   SCM at = scm_list_2 (ly_symbol2scm ("char"), gh_int2scm (code));
233   at = fontify_atom (this, at);
234   Box b = get_indexed_char (code);
235   return Stencil (b, at);
236 }
237
238 int
239 /*Font_metric::*/
240 get_encoded_index (Font_metric *m, String input_coding, int code)
241 {
242   String font_coding = m->coding_scheme ();
243   if (font_coding == input_coding)
244     return code;
245   SCM s = scm_call_3 (ly_scheme_function ("encoded-index"),
246                       scm_makfrom0str (input_coding.to_str0 ()),
247                       scm_makfrom0str (font_coding.to_str0 ()),
248                       scm_int2num (code));
249   return gh_scm2int (s);
250 }