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