* scm/beam.scm (check-slope-callbacks): check sign of slope.
[lilypond.git] / lily / afm.cc
blobc92433bd1a079e968dd486be3bd4fbecdee70596
1 /*
2 afm.cc -- implement Adobe_font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include <string.h>
12 #include "warn.hh"
13 #include "libc-extension.hh"
14 #include "afm.hh"
15 #include "dimensions.hh"
17 Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
19 checksum_ = 0;
20 font_info_ = fi;
21 design_size_ = 1.0;
23 for (int i = 256 >? fi->numOfChars; i--;)
24 ascii_to_metric_idx_.push (-1);
26 for (int i=0; i < fi->numOfChars; i++)
28 AFM_CharMetricInfo * c = fi->cmi + i;
30 /* Some TeX afm files contain code = -1. We don't know why,
31 let's ignore it. */
32 if (c->code >= 0)
33 ascii_to_metric_idx_[c->code] = i;
34 name_to_metric_dict_[c->name] = i;
38 Adobe_font_metric::~Adobe_font_metric ()
40 AFM_free (font_info_);
43 SCM
44 Adobe_font_metric::make_afm (AFM_Font_info *fi,
45 unsigned int checksum,
46 Real design_size)
48 Adobe_font_metric *fm = new Adobe_font_metric (fi);
49 fm->checksum_ = checksum;
50 fm->design_size_ = design_size;
51 return fm->self_scm ();
54 AFM_CharMetricInfo const*
55 Adobe_font_metric::find_ascii_metric (int a) const
57 if (ascii_to_metric_idx_[a] >=0)
59 int code = ascii_to_metric_idx_[a];
60 if (code >= 0)
61 return font_info_->cmi + code;
63 return 0;
66 AFM_CharMetricInfo const*
67 Adobe_font_metric::find_char_metric (String nm) const
69 int idx = name_to_index (nm);
70 if (idx >= 0)
71 return font_info_->cmi + idx;
72 return 0;
75 int
76 Adobe_font_metric::name_to_index (String name) const
78 std::map<String,int>::const_iterator ai = name_to_metric_dict_.find (name);
79 if (ai == name_to_metric_dict_.end ())
80 return -1;
81 return (*ai).second;
84 int
85 Adobe_font_metric::count () const
87 return font_info_->numOfChars;
90 Box
91 Adobe_font_metric::get_ascii_char (int code) const
93 AFM_CharMetricInfo const *c = find_ascii_metric (code);
94 Box b (Interval (0, 0), Interval (0, 0));
95 if (c)
96 b = afm_bbox_to_box (c->charBBox);
97 return b;
101 Adobe_font_metric::index_to_ascii (int code) const
103 return font_info_->cmi[code].code;
107 Adobe_font_metric::get_indexed_char (int code) const
109 if (code >= 0)
110 return afm_bbox_to_box (font_info_->cmi[code].charBBox);
111 else
112 return Box (Interval (0, 0), Interval (0, 0));
116 read_afm_file (String nm)
118 FILE *f = fopen (nm.to_str0 () , "r");
119 char s[2048] = "";
120 char *check_key = "Comment TfmCheckSum";
121 char *size_key = "Comment DesignSize";
123 unsigned int cs = 0;
124 Real ds = 1.0;
126 /* Assume check_key in first 10 lines */
127 for (int i = 0; i < 10; i++)
129 fgets (s, sizeof (s), f);
130 if (strncmp (s, check_key, strlen (check_key)) == 0)
131 sscanf (s + strlen (check_key), "%ud", &cs);
132 else if (strncmp (s, size_key, strlen (size_key)) == 0)
133 sscanf (s + strlen (size_key), "%lf", &ds);
136 rewind (f);
138 AFM_Font_info *fi;
139 int ok = AFM_parseFile (f, &fi, ~0);
141 if (ok)
143 error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ()));
144 exit (2);
146 fclose (f);
148 return Adobe_font_metric::make_afm (fi, cs, ds);
152 /* Actually, AFMs will be printers point, usually, but our .py script dumps
153 real points. */
155 afm_bbox_to_box (AFM_BBox bb)
157 return Box (Interval (bb.llx, bb.urx)* (1/1000.0) PT,
158 Interval (bb.lly, bb.ury)* (1/1000.0) PT);
162 Offset
163 Adobe_font_metric::get_indexed_wxwy (int k) const
165 AFM_CharMetricInfo const *mi = font_info_->cmi+ k;
166 return 1/1000.0 PT * Offset (mi->wx, mi->wy);
169 Real
170 Adobe_font_metric::design_size () const
172 return design_size_;
175 String
176 Adobe_font_metric::coding_scheme () const
178 return font_info_->gfi->encodingScheme;