]> git.donarmstrong.com Git - lilypond.git/blob - lily/tfm.cc
9d385184e5be8e5593c2b3bfa1cd45f9cb5459a5
[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 #define APPEND_CHAR_METRIC_ELT(k) \
54     outstr += ::to_string (#k) + " "  + ::to_string (k ## _)  + "; "
55
56 String
57 Tex_font_char_metric::to_string () const
58 {
59   String outstr;
60
61   APPEND_CHAR_METRIC_ELT (exists);
62   APPEND_CHAR_METRIC_ELT (code);
63   APPEND_CHAR_METRIC_ELT (width);
64   APPEND_CHAR_METRIC_ELT (height);
65   APPEND_CHAR_METRIC_ELT (depth);
66   APPEND_CHAR_METRIC_ELT (italic_correction);
67   
68   return outstr + "\n";
69 }
70
71 Tex_font_metric::Tex_font_metric ()
72 {
73 }
74
75 Tex_font_char_metric const *
76 Tex_font_metric::find_ascii (int ascii, bool warn) const
77 {
78   if (ascii >= 0 && ascii < ascii_to_metric_idx_.size ()
79       && ascii_to_metric_idx_[ascii] >= 0)
80     return & char_metrics_[ascii_to_metric_idx_ [ascii]];
81   else if (warn)
82     warning (_f ("can't find ascii character: %d", ascii));
83   return &dummy_static_char_metric;  
84 }
85
86 /* UGH: glyphs need not be consecutive in TFM. */
87 int
88 Tex_font_metric::count () const
89 {
90   for (int i = ascii_to_metric_idx_.size (); i--;)
91     if (ascii_to_metric_idx_[i] != -1)
92       return i + 1;
93   return 0;
94 }
95
96 Box
97 Tex_font_metric::get_ascii_char (int a) const
98 {
99   Box b = find_ascii (a)->dimensions ();
100   return b;
101 }
102
103 String
104 Tex_font_metric::to_string () const
105 {
106   String outstr;
107   for (int i=0; i < char_metrics_.size (); i++)
108     outstr += char_metrics_[i].to_string ();
109   return outstr;
110 }
111
112 SCM
113 Tex_font_metric::make_tfm (String filename)
114 {
115   Tex_font_metric *tfm = new Tex_font_metric;
116   Tex_font_metric_reader reader (filename);
117
118   tfm->info_ = reader.info_;
119   tfm->header_ = reader.header_;
120   tfm->char_metrics_ = reader.char_metrics_;
121   tfm->ascii_to_metric_idx_ = reader.ascii_to_metric_idx_;
122
123   return tfm->self_scm ();
124 }
125
126 Real
127 Tex_font_metric::design_size () const
128 {
129   return info_.design_size;
130 }
131
132 String
133 Tex_font_metric::coding_scheme () const
134 {
135   return info_.coding_scheme;
136 }