]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/afm.cc
* make/lilypond.fedora.spec.in (Group): add lilypond-internals to
[lilypond.git] / lily / afm.cc
index 71a1e25ea98c9d7e8f26e44bdb5bf83c1dd3c5b7..95f138090abc29bace0eda1fa066ff1ab8f0ae75 100644 (file)
-/*   
-  afm.cc --  implement Adobe_font_metric
-  
+/*
+  afm.cc -- implement Adobe_font_metric
+
   source file of the GNU LilyPond music typesetter
-  
-  (c) 1998--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-  
- */
+
+  (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+*/
 
 #include "afm.hh"
-#include "box.hh"
-#include "direction.hh"
-#include "debug.hh"
 
-Box &
-Adobe_font_char_metric::bbox ()
+#include <cstring>
+using namespace std;
+
+#include "warn.hh"
+#include "libc-extension.hh"
+#include "dimensions.hh"
+
+Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
 {
-  return B_;
+  checksum_ = 0;
+  font_info_ = fi;
+  design_size_ = 1.0;
+
+  for (int i = max (256, fi->numOfChars); i--;)
+    ascii_to_metric_idx_.push (-1);
+
+  for (int i = 0; i < fi->numOfChars; i++)
+    {
+      AFM_CharMetricInfo *c = fi->cmi + 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;
+    }
 }
 
-String &
-Adobe_font_char_metric::name ()
+Adobe_font_metric::~Adobe_font_metric ()
 {
-  return N_;
+  AFM_free (font_info_);
 }
 
-int &
-Adobe_font_char_metric::code ()
+SCM
+Adobe_font_metric::make_afm (AFM_Font_info *fi,
+                            unsigned int checksum,
+                            Real design_size)
 {
-  return C_;
+  Adobe_font_metric *fm = new Adobe_font_metric (fi);
+  fm->checksum_ = checksum;
+  fm->design_size_ = design_size;
+  return fm->self_scm ();
 }
 
-Real &
-Adobe_font_char_metric::width ()
+AFM_CharMetricInfo const *
+Adobe_font_metric::find_ascii_metric (int a) const
 {
-  return WX_;
+  if (ascii_to_metric_idx_[a] >= 0)
+    {
+      int code = ascii_to_metric_idx_[a];
+      if (code >= 0)
+       return font_info_->cmi + code;
+    }
+  return 0;
 }
 
-Adobe_font_char_metric::Adobe_font_char_metric ()
+AFM_CharMetricInfo const *
+Adobe_font_metric::find_char_metric (String nm) const
 {
-  B_ = Box( Interval(0,0), Interval (0,0));
-  WX_ = 0.0;
-  C_ = 0;
-  C_ = -1;
+  int idx = name_to_index (nm);
+  if (idx >= 0)
+    return font_info_->cmi + idx;
+  return 0;
 }
 
-Adobe_font_metric::Adobe_font_metric ()
+int
+Adobe_font_metric::name_to_index (String name) const
 {
-  ItalicAngle_ = 0.0;
-  IsFixedPitch_ = false;
-  UnderlinePosition_ =0.;
-  UnderlineThickness_=0.;
+  map<String, int>::const_iterator ai = name_to_metric_dict_.find (name);
+  if (ai == name_to_metric_dict_.end ())
+    return -1;
+  return (*ai).second;
 }
 
+int
+Adobe_font_metric::count () const
+{
+  return font_info_->numOfChars;
+}
 
 Box
-Adobe_font_char_metric::dimensions () const
+Adobe_font_metric::get_ascii_char (int code) const
 {
-  Box b= B_;
-  
-  b[X_AXIS] *= size_ / 1000.0;
-  b[Y_AXIS] *= size_ / 1000.0;
-
+  AFM_CharMetricInfo const *c = find_ascii_metric (code);
+  Box b (Interval (0, 0), Interval (0, 0));
+  if (c)
+    b = afm_bbox_to_box (c->charBBox);
   return b;
 }
 
-
-
-#define APPEND_CHAR_METRIC_ELT(k)  outstr += to_str (#k) + " "  + to_str (k ## _)  + "; "
-
-String
-box_str (Box b)
+int
+Adobe_font_metric::index_to_ascii (int code) const
 {
-  return to_str (b[X_AXIS][SMALLER]) + " " +
-    to_str(b[Y_AXIS][SMALLER]) + " " +
-    to_str (b[X_AXIS][BIGGER]) + " "+
-    to_str (b[Y_AXIS][BIGGER]);
+  return font_info_->cmi[code].code;
 }
 
-#define APPEND_BOX(k)  outstr += to_str (#k) + " "  + box_str (k ## _)  + ";"
-
-String
-Adobe_font_char_metric::str () const
+Box
+Adobe_font_metric::get_indexed_char (int code) const
 {
-  String outstr ;
-
-  APPEND_CHAR_METRIC_ELT (C);
-  APPEND_CHAR_METRIC_ELT(N);
-  APPEND_CHAR_METRIC_ELT(WX);
-  
-  APPEND_BOX(B);
-  return outstr + "\n";
+  if (code >= 0)
+    return afm_bbox_to_box (font_info_->cmi[code].charBBox);
+  else
+    return Box (Interval (0, 0), Interval (0, 0));
 }
 
-#define WRITESTRING(k)  outstr += String (#k) + " "  + to_str (k ## _)  + "\n"
-
-String
-Adobe_font_metric::str () const
+SCM
+read_afm_file (String nm)
 {
-  String outstr;
-  WRITESTRING(FontName);
-  WRITESTRING(FullName);
-  WRITESTRING(FamilyName);
-  WRITESTRING(Weight);
-  WRITESTRING(Version);
-  WRITESTRING(Notice);
-  WRITESTRING(EncodingScheme);
-  WRITESTRING(ItalicAngle);
-  WRITESTRING(UnderlineThickness);
-  WRITESTRING(UnderlinePosition);
-  outstr += "FontBBox " +  box_str (FontBBox_) +  "\n";
-
-  for (int i=0; i < char_metrics_.size (); i++)
-    outstr += char_metrics_[i].str ();
-  
-  return outstr;
-}
+  FILE *f = fopen (nm.to_str0 (), "r");
+  char s[2048] = "";
+  char *check_key = "Comment TfmCheckSum";
+  char *size_key = "Comment DesignSize";
 
-Adobe_font_char_metric dummy_static_char_metric;
+  unsigned int cs = 0;
+  Real ds = 1.0;
 
-Adobe_font_char_metric const &
-Adobe_font_metric::find_char (String nm, bool warn) const
-{
-  if (!name_to_metric_dict_.elem_b (nm))
+  /* Assume check_key in first 10 lines */
+  for (int i = 0; i < 10; i++)
     {
-      if (warn)
-       {
-         warning (_f ("Can't find character called: `%s'", nm.ch_C()));
-       }
-      return dummy_static_char_metric;
+      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);
     }
-  
-  return char_metrics_[name_to_metric_dict_ [nm]];
+
+  rewind (f);
+
+  AFM_Font_info *fi;
+  int e = AFM_parseFile (f, &fi, ~0);
+
+  if (e)
+    error (_f ("parsing AFM file: `%s'", nm.to_str0 ()));
+  fclose (f);
+
+  return Adobe_font_metric::make_afm (fi, cs, ds);
+}
+
+Box
+afm_bbox_to_box (AFM_BBox bb)
+{
+#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
+}
 
-Character_metric const *
-Adobe_font_metric::get_char (int code, bool warn) const
+Real
+Adobe_font_metric::design_size () const
 {
-  return &find_ascii (code,warn);
+  return design_size_;
 }
 
-Adobe_font_char_metric const &
-Adobe_font_metric::find_ascii (int a , bool warn) const
+String
+Adobe_font_metric::font_name () const
 {
-  int  code = ascii_to_metric_idx_[a];
-  if (code>=0)
-    {
-      return char_metrics_[code];
-    }
-  else if (warn )
-    {
-      warning (_f ("Can't find character number: %d", a));
-    }
-  return dummy_static_char_metric;
+  return font_info_->gfi->fontName;
 }