X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fafm.cc;h=f601e22e138530536d139723fa53239dacd21bbb;hb=34e6d3d79f3b5fc6188a20f9a2a5c0d34b2c81c8;hp=9f1f8045f5d4e7bb6567d38e59b89efa464d14b3;hpb=fdfd147f1233eef6ea8b176290221b5a4ec38868;p=lilypond.git diff --git a/lily/afm.cc b/lily/afm.cc index 9f1f8045f5..f601e22e13 100644 --- a/lily/afm.cc +++ b/lily/afm.cc @@ -1,108 +1,184 @@ -/* - afm.cc -- implement Adobe_font_metric - - source file of the Flower Library - - (c) 2000 Han-Wen Nienhuys - - */ +/* + afm.cc -- implement Adobe_font_metric + + source file of the GNU LilyPond music typesetter + + (c) 2000--2005 Han-Wen Nienhuys +*/ + #include "afm.hh" + +#include + #include "warn.hh" +#include "libc-extension.hh" +#include "dimensions.hh" -Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi) +Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi) { - font_inf_ = fi; + checksum_ = 0; + font_info_ = fi; + design_size_ = 1.0; - for (int i= 256; i--;) + for (int i = 256 >? fi->numOfChars; i--;) ascii_to_metric_idx_.push (-1); - - for (int i=0; i < fi->numOfChars; i++) + + for (int i = 0; i < fi->numOfChars; i++) { - AFM_CharMetricInfo * c = fi->cmi + i; + AFM_CharMetricInfo *c = fi->cmi + i; - ascii_to_metric_idx_[c->code] = i; + /* Some TeX afm files contain code = -1. We don't know why, + let's ignore it. */ + if (c->code >= 0) + ascii_to_metric_idx_[c->code] = i; name_to_metric_dict_[c->name] = i; } } - + +Adobe_font_metric::~Adobe_font_metric () +{ + AFM_free (font_info_); +} + +SCM +Adobe_font_metric::make_afm (AFM_Font_info *fi, + unsigned int checksum, + Real design_size) +{ + Adobe_font_metric *fm = new Adobe_font_metric (fi); + fm->checksum_ = checksum; + fm->design_size_ = design_size; + return fm->self_scm (); +} AFM_CharMetricInfo const * -Adobe_font_metric::find_ascii_metric (int a , bool warn) const +Adobe_font_metric::find_ascii_metric (int a) const { - if (ascii_to_metric_idx_[a] >=0) + if (ascii_to_metric_idx_[a] >= 0) { int code = ascii_to_metric_idx_[a]; - if (code>=0) - { - return font_inf_->cmi + code; - } - } - else if (warn ) - { - warning (_f ("can't find character number: %d", a)); + if (code >= 0) + return font_info_->cmi + code; } - return 0; } AFM_CharMetricInfo const * -Adobe_font_metric::find_char_metric (String nm, bool warn) const +Adobe_font_metric::find_char_metric (String nm) const { - map::const_iterator ai = name_to_metric_dict_.find (nm); - + int idx = name_to_index (nm); + if (idx >= 0) + return font_info_->cmi + idx; + return 0; +} + +int +Adobe_font_metric::name_to_index (String name) const +{ + std::map::const_iterator ai = name_to_metric_dict_.find (name); if (ai == name_to_metric_dict_.end ()) - { - if (warn) - { - warning (_f ("can't find character called: `%s'", nm.ch_C())); - } - return 0; - } - else - return font_inf_->cmi + (*ai).second; + return -1; + return (*ai).second; } +int +Adobe_font_metric::count () const +{ + return font_info_->numOfChars; +} Box -Adobe_font_metric::get_char (int code, bool warn) const +Adobe_font_metric::get_ascii_char (int code) const { - AFM_CharMetricInfo const - * c = find_ascii_metric (code,warn); + AFM_CharMetricInfo const *c = find_ascii_metric (code); + Box b (Interval (0, 0), Interval (0, 0)); if (c) - return afm_bbox_to_box (c->charBBox); + b = afm_bbox_to_box (c->charBBox); + return b; +} + +int +Adobe_font_metric::index_to_ascii (int code) const +{ + return font_info_->cmi[code].code; +} + +Box +Adobe_font_metric::get_indexed_char (int code) const +{ + if (code >= 0) + return afm_bbox_to_box (font_info_->cmi[code].charBBox); else - return Box (Interval (0,0),Interval(0,0)); + return Box (Interval (0, 0), Interval (0, 0)); } -Adobe_font_metric* +SCM read_afm_file (String nm) { - FILE *f = fopen (nm.ch_C() , "r"); + FILE *f = fopen (nm.to_str0 (), "r"); + char s[2048] = ""; + char *check_key = "Comment TfmCheckSum"; + char *size_key = "Comment DesignSize"; - AFM_Font_info * fi; - int ok = AFM_parseFile (f, &fi, ~1); + unsigned int cs = 0; + Real ds = 1.0; + + /* Assume check_key in first 10 lines */ + for (int i = 0; i < 10; i++) + { + fgets (s, sizeof (s), f); + if (strncmp (s, check_key, strlen (check_key)) == 0) + sscanf (s + strlen (check_key), "%ud", &cs); + else if (strncmp (s, size_key, strlen (size_key)) == 0) + sscanf (s + strlen (size_key), "%lf", &ds); + } + + rewind (f); + + AFM_Font_info *fi; + int ok = AFM_parseFile (f, &fi, ~0); if (ok) { - error (_("Error parsing AFM file")); + error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ())); exit (2); } fclose (f); - return new Adobe_font_metric (fi); + return Adobe_font_metric::make_afm (fi, cs, ds); } - Box afm_bbox_to_box (AFM_BBox bb) { - return Box (Interval (bb.llx, bb.urx)* (1/1000.0), - Interval (bb.lly, bb.ury)* (1/1000.0)); +#if 1 /* Fine for feta; ec-fonts-mftraced do not have AFM. */ + return Box (Interval (bb.llx, bb.urx) * 0.001 PT, + Interval (bb.lly, bb.ury) * 0.001 PT); +#else /* FIXME: about right for lmodern. */ + return Box (Interval (bb.llx, bb.urx) * 1.14 * 0.001 PT, + Interval (bb.lly, bb.ury) * 1.14 * 0.001 PT); +#endif +} + +Offset +Adobe_font_metric::get_indexed_wxwy (int k) const +{ + AFM_CharMetricInfo const *mi = font_info_->cmi + k; +#if 1 /* Fine for feta; ec-fonts-mftraced do not have AFM. */ + return Offset (mi->wx, mi->wy) * 0.001 PT; +#else /* FIXME: about right for lmodern. */ + return Offset (mi->wx, mi->wy) * 1.14 * 0.001 PT; +#endif +} +Real +Adobe_font_metric::design_size () const +{ + return design_size_; } - -Adobe_font_metric::~Adobe_font_metric () +String +Adobe_font_metric::font_name () const { - AFM_free (font_inf_); + return font_info_->gfi->fontName; }