]> git.donarmstrong.com Git - lilypond.git/blob - lily/tfm.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[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 #include "tfm-reader.hh"
13 #include "string-convert.hh"
14 #include "warn.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
59
60 void
61 Tex_font_metric::derived_mark () const
62 {
63   scm_gc_mark (encoding_table_);
64 }
65
66 Tex_font_char_metric const *
67 Tex_font_metric::find_ascii (int ascii, bool warn) const
68 {
69   if (ascii >= 0 && ascii < ascii_to_metric_idx_.size ()
70       && ascii_to_metric_idx_[ascii] >= 0)
71     return & char_metrics_[ascii_to_metric_idx_ [ascii]];
72   else if (warn)
73     warning (_f ("can't find ascii character: %d", ascii));
74   return &dummy_static_char_metric;  
75 }
76
77 /* UGH: glyphs need not be consecutive in TFM. */
78 int
79 Tex_font_metric::count () const
80 {
81   for (int i = ascii_to_metric_idx_.size (); i--;)
82     if (ascii_to_metric_idx_[i] != -1)
83       return i + 1;
84   return 0;
85 }
86
87 Box
88 Tex_font_metric::get_ascii_char (int a) const
89 {
90   Box b = find_ascii (a)->dimensions ();
91   return b;
92 }
93
94 SCM
95 Tex_font_metric::make_tfm (String filename)
96 {
97   Tex_font_metric *tfm = new Tex_font_metric;
98   Tex_font_metric_reader reader (filename);
99
100   tfm->info_ = reader.info_;
101   tfm->header_ = reader.header_;
102   tfm->char_metrics_ = reader.char_metrics_;
103   tfm->ascii_to_metric_idx_ = reader.ascii_to_metric_idx_;
104
105   
106   tfm->encoding_table_ =
107     scm_call_1 (ly_scheme_function ("get-coding-table"),
108                 scm_makfrom0str (tfm->info_.coding_scheme.to_str0 ()));
109
110   return tfm->self_scm ();
111 }
112
113 Real
114 Tex_font_metric::design_size () const
115 {
116   return info_.design_size;
117 }
118
119 String
120 Tex_font_metric::coding_scheme () const
121 {
122   return info_.coding_scheme;
123 }
124
125 int
126 Tex_font_metric::name_to_index (String s) const
127 {
128   SCM sym = ly_symbol2scm (s.to_str0 ());
129
130   SCM idx = scm_hash_ref (encoding_table_, sym, SCM_BOOL_F);
131   if (ly_char_p (idx))
132     {
133       return (unsigned char) ly_scm2char (idx);
134     }
135   else
136     return -1;  
137 }