]> git.donarmstrong.com Git - lilypond.git/blob - lily/tfm.cc
* lily/modified-font-metric.cc (text_dimension): try
[lilypond.git] / lily / tfm.cc
1 /*   
2   tfm.cc -- implement Tex_font_metric
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7
8   some code shamelessly copied from GNU fontutils-0.6/tfm/tfm_input.c
9  */
10
11 #include "tfm.hh"
12
13 #include "tfm-reader.hh"
14 #include "string-convert.hh"
15 #include "warn.hh"
16 #include "dimensions.hh"
17
18 static SCM tex_dimension_hash_tab;
19 static Tex_font_char_metric dummy_static_char_metric;
20
21
22           
23 Box
24 lookup_tex_text_dimension (Font_metric *font,
25                            SCM text)
26 {
27   Box b;
28
29   SCM limit = ly_scheme_function ("TEX_STRING_HASHLIMIT");
30   String key_str = font->font_name ();
31   int hash_code = scm_to_int (scm_hash (text, limit));
32   key_str += to_string (hash_code);
33   
34   SCM val = scm_hash_ref (tex_dimension_hash_tab,
35                           scm_makfrom0str (key_str.to_str0 ()),
36                           SCM_BOOL_F);
37
38   if (scm_is_pair (val))
39     {
40       b[X_AXIS][LEFT] = 0.0;
41       b[X_AXIS][RIGHT] = scm_to_double (scm_car (val));
42       val = scm_cdr (val);
43       b[Y_AXIS][UP] = scm_to_double (scm_car (val));
44       val = scm_cdr (val);
45       b[Y_AXIS][RIGHT] = scm_to_double (scm_car (val)); 
46     }
47   
48   return b; 
49 }
50
51
52 Tex_font_char_metric::Tex_font_char_metric ()
53 {
54   exists_ = false;
55   code_ = 0;;
56   width_ = 0;
57   height_ = 0;
58   depth_ = 0;
59   italic_correction_ = 0;
60   width_fix_ = 0;
61   height_fix_ = 0;
62   depth_fix_ = 0;
63   italic_correction_fix_ = 0;
64 }
65
66 Box
67 Tex_font_char_metric::dimensions () const
68 {
69   if (!exists_)
70     {
71       Box b;
72       b.set_empty ();
73       return b;
74     }
75   
76   Real d = -depth_;
77
78   Real point_constant = 1 PT;
79   
80   return Box (Interval (0, width_*point_constant ),
81               Interval ((d <? height_)*point_constant,
82                         (d >? height_)*point_constant));
83 }
84
85 Tex_font_metric::Tex_font_metric ()
86 {
87   encoding_table_ = SCM_EOL;
88 }
89
90 void
91 Tex_font_metric::derived_mark () const
92 {
93   scm_gc_mark (encoding_table_);
94 }
95
96 Tex_font_char_metric const *
97 Tex_font_metric::find_ascii (int ascii, bool warn) const
98 {
99   if (ascii >= 0 && ascii < ascii_to_metric_idx_.size ()
100       && ascii_to_metric_idx_[ascii] >= 0)
101     return & char_metrics_[ascii_to_metric_idx_ [ascii]];
102   else if (warn)
103     warning (_f ("can't find ascii character: %d", ascii));
104   return &dummy_static_char_metric;  
105 }
106
107 /* UGH: glyphs need not be consecutive in TFM. */
108 int
109 Tex_font_metric::count () const
110 {
111   for (int i = ascii_to_metric_idx_.size (); i--;)
112     if (ascii_to_metric_idx_[i] != -1)
113       return i + 1;
114   return 0;
115 }
116
117 Box
118 Tex_font_metric::get_ascii_char (int a) const
119 {
120   Box b = find_ascii (a)->dimensions ();
121   return b;
122 }
123
124 SCM
125 Tex_font_metric::make_tfm (String file_name)
126 {
127   Tex_font_metric *tfm = new Tex_font_metric;
128   Tex_font_metric_reader reader (file_name);
129
130   tfm->info_ = reader.info_;
131   tfm->header_ = reader.header_;
132   tfm->char_metrics_ = reader.char_metrics_;
133   tfm->ascii_to_metric_idx_ = reader.ascii_to_metric_idx_;
134   tfm->encoding_table_
135     = scm_call_1 (ly_scheme_function ("get-coding-table"),
136                   scm_makfrom0str (tfm->coding_scheme ().to_str0 ()));
137
138   return tfm->self_scm ();
139 }
140
141 Real
142 Tex_font_metric::design_size () const
143 {
144   return info_.design_size;
145 }
146
147 String
148 Tex_font_metric::coding_scheme () const
149 {
150   String scm = info_.coding_scheme;
151
152   for(int i = 0; i < scm.length (); i++)
153     if (scm[i] == ' ')
154       scm[i] = '-';
155
156   return scm;
157 }
158
159 int
160 Tex_font_metric::name_to_index (String s) const
161 {
162   SCM sym = ly_symbol2scm (s.to_str0 ());
163
164   SCM idx = scm_hash_ref (encoding_table_, sym, SCM_BOOL_F);
165   if (ly_c_char_p (idx))
166     {
167       return (unsigned char) ly_scm2char (idx);
168     }
169   else
170     return -1;  
171 }
172
173
174 LY_DEFINE(ly_load_text_dimensions, "ly:load-text-dimensions",
175           1, 0, 0,
176           (SCM dimension_alist),
177           "Load dimensions from TeX in a (KEY . (W H D)) format alist")
178 {
179   if (!tex_dimension_hash_tab)
180     {
181       tex_dimension_hash_tab =
182         scm_gc_protect_object (scm_make_hash_table (scm_from_int (113)));
183     }
184
185   for (SCM s = dimension_alist;
186        scm_is_pair (s);
187        s = scm_cdr (s))
188     {
189       SCM key = scm_caar (s);
190       SCM val = scm_cdar (s);
191       
192       if (scm_hash_ref (tex_dimension_hash_tab, key, SCM_BOOL_F)
193           == SCM_BOOL_F)
194         {
195           scm_hash_set_x (tex_dimension_hash_tab, key, val);
196         }
197     }
198
199   return SCM_UNSPECIFIED;
200 }