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