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