]> git.donarmstrong.com Git - lilypond.git/blob - lily/tfm.cc
(get_indexed_char): scale metrics by
[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 Tex_font_char_metric dummy_static_char_metric;
19
20 Tex_font_char_metric::Tex_font_char_metric ()
21 {
22   exists_ = false;
23   code_ = 0;;
24   width_ = 0;
25   height_ = 0;
26   depth_ = 0;
27   italic_correction_ = 0;
28   width_fix_ = 0;
29   height_fix_ = 0;
30   depth_fix_ = 0;
31   italic_correction_fix_ = 0;
32 }
33
34 Box
35 Tex_font_char_metric::dimensions () const
36 {
37   if (!exists_)
38     {
39       Box b;
40       b.set_empty ();
41       return b;
42     }
43   
44   Real d = -depth_;
45
46   Real point_constant = 1 PT;
47   
48   return Box (Interval (0, width_*point_constant ),
49               Interval ((d <? height_)*point_constant,
50                         (d >? height_)*point_constant));
51 }
52
53 Tex_font_metric::Tex_font_metric ()
54 {
55   encoding_table_ = SCM_EOL;
56 }
57
58 void
59 Tex_font_metric::derived_mark () const
60 {
61   scm_gc_mark (encoding_table_);
62 }
63
64 Tex_font_char_metric const *
65 Tex_font_metric::find_ascii (int ascii, bool warn) const
66 {
67   if (ascii >= 0 && ascii < ascii_to_metric_idx_.size ()
68       && ascii_to_metric_idx_[ascii] >= 0)
69     return & char_metrics_[ascii_to_metric_idx_ [ascii]];
70   else if (warn)
71     warning (_f ("can't find ascii character: %d", ascii));
72   return &dummy_static_char_metric;  
73 }
74
75 /* UGH: glyphs need not be consecutive in TFM. */
76 int
77 Tex_font_metric::count () const
78 {
79   for (int i = ascii_to_metric_idx_.size (); i--;)
80     if (ascii_to_metric_idx_[i] != -1)
81       return i + 1;
82   return 0;
83 }
84
85 Box
86 Tex_font_metric::get_ascii_char (int a) const
87 {
88   Box b = find_ascii (a)->dimensions ();
89   return b;
90 }
91
92 SCM
93 Tex_font_metric::make_tfm (String file_name)
94 {
95   Tex_font_metric *tfm = new Tex_font_metric;
96   Tex_font_metric_reader reader (file_name);
97
98   tfm->info_ = reader.info_;
99   tfm->header_ = reader.header_;
100   tfm->char_metrics_ = reader.char_metrics_;
101   tfm->ascii_to_metric_idx_ = reader.ascii_to_metric_idx_;
102
103   tfm->encoding_table_
104     = scm_call_1 (ly_scheme_function ("get-coding-table"),
105                   scm_makfrom0str (tfm->coding_scheme ().to_str0 ()));
106
107   return tfm->self_scm ();
108 }
109
110 Real
111 Tex_font_metric::design_size () const
112 {
113   return info_.design_size;
114 }
115
116 String
117 Tex_font_metric::coding_scheme () const
118 {
119   String scm = info_.coding_scheme;
120
121   for(int i = 0; i < scm.length (); i++)
122     if (scm[i] == ' ')
123       scm[i] = '-';
124
125   return scm;
126 }
127
128 int
129 Tex_font_metric::name_to_index (String s) const
130 {
131   SCM sym = ly_symbol2scm (s.to_str0 ());
132
133   SCM idx = scm_hash_ref (encoding_table_, sym, SCM_BOOL_F);
134   if (ly_c_char_p (idx))
135     {
136       return (unsigned char) ly_scm2char (idx);
137     }
138   else
139     return -1;  
140 }