]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/afm.cc
release: 1.3.73
[lilypond.git] / lily / afm.cc
index ca162249f14cbb2bb3f3c79fc7607ef242cd55c6..9f1f8045f5d4e7bb6567d38e59b89efa464d14b3 100644 (file)
 /*   
   afm.cc --  implement Adobe_font_metric
   
-  source file of the GNU LilyPond music typesetter
+  source file of the Flower Library
   
-  (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   
  */
-
 #include "afm.hh"
-#include "box.hh"
-#include "direction.hh"
-#include "debug.hh"
+#include "warn.hh"
 
-Box &
-Adobe_font_char_metric::bbox ()
+Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
 {
-  return B_;
-}
+  font_inf_ = fi;
 
-String &
-Adobe_font_char_metric::name ()
-{
-  return N_;
-}
+  for (int i= 256; i--;)
+    ascii_to_metric_idx_.push (-1);
+  
+  for (int i=0; i < fi->numOfChars; i++)
+    {
+      AFM_CharMetricInfo * c = fi->cmi + i;
 
-int &
-Adobe_font_char_metric::code ()
-{
-  return C_;
+      ascii_to_metric_idx_[c->code] = i;
+      name_to_metric_dict_[c->name] = i;
+    }
 }
+  
 
-Real &
-Adobe_font_char_metric::width ()
+AFM_CharMetricInfo const *
+Adobe_font_metric::find_ascii_metric (int a , bool warn) const
 {
-  return WX_;
-}
+  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));
+    }
 
-Adobe_font_char_metric::Adobe_font_char_metric ()
-{
-  B_ = Box( Interval(0,0), Interval (0,0));
-  WX_ = 0.0;
-  C_ = 0;
-  C_ = -1;
+  return 0;
 }
 
-Adobe_font_metric::Adobe_font_metric ()
+AFM_CharMetricInfo const *
+Adobe_font_metric::find_char_metric (String nm, bool warn) const
 {
-  ItalicAngle_ = 0.0;
-  IsFixedPitch_ = false;
-  UnderlinePosition_ =0.;
-  UnderlineThickness_=0.;
+  map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
+  
+  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;
 }
 
 
 Box
-Adobe_font_char_metric::dimensions () const
+Adobe_font_metric::get_char (int code, bool warn) const
 {
-  Box b= B_;
-  
-  b[X_AXIS] *= size_ / 1000.0;
-  b[Y_AXIS] *= size_ / 1000.0;
-
-  return b;
+  AFM_CharMetricInfo const
+    * c =  find_ascii_metric (code,warn);
+  if (c)
+    return afm_bbox_to_box (c->charBBox);                      
+  else
+    return Box (Interval (0,0),Interval(0,0));
 }
 
-
-
-#define APPEND_CHAR_METRIC_ELT(k)  outstr += to_str (#k) + " "  + to_str (k ## _)  + "; "
-
-String
-box_str (Box b)
+Adobe_font_metric*
+read_afm_file (String nm)
 {
-  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]);
-}
+  FILE *f = fopen (nm.ch_C() , "r");
 
-#define APPEND_BOX(k)  outstr += to_str (#k) + " "  + box_str (k ## _)  + ";"
+  AFM_Font_info * fi;
+  int ok = AFM_parseFile (f, &fi, ~1);
 
-String
-Adobe_font_char_metric::str () const
-{
-  String outstr ;
+  if (ok)
+    {
+      error (_("Error parsing AFM file"));
+      exit (2);
+    }
+  fclose (f);
 
-  APPEND_CHAR_METRIC_ELT (C);
-  APPEND_CHAR_METRIC_ELT(N);
-  APPEND_CHAR_METRIC_ELT(WX);
-  
-  APPEND_BOX(B);
-  return outstr + "\n";
+  return new Adobe_font_metric (fi);
 }
 
-#define WRITESTRING(k)  outstr += String (#k) + " "  + to_str (k ## _)  + "\n"
-
-String
-Adobe_font_metric::str () const
-{
-  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;
-}
-
-Adobe_font_char_metric dummy_static_char_metric;
-
-Adobe_font_char_metric const &
-Adobe_font_metric::find_char (String nm, bool warn) const
+Box
+afm_bbox_to_box (AFM_BBox bb)
 {
-  if (!name_to_metric_dict_.elem_b (nm))
-    {
-      if (warn)
-       {
-         warning (_f ("Can't find character called: `%s'", nm.ch_C()));
-       }
-      return dummy_static_char_metric;
-    }
-  
-  return char_metrics_[name_to_metric_dict_ [nm]];
-}
-
+  return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
+             Interval (bb.lly, bb.ury)* (1/1000.0));
 
-Character_metric const *
-Adobe_font_metric::get_char (int code, bool warn) const
-{
-  return &find_ascii (code,warn);
 }
+  
 
-Adobe_font_char_metric const &
-Adobe_font_metric::find_ascii (int a , bool warn) const
+Adobe_font_metric::~Adobe_font_metric ()
 {
-  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;
+  AFM_free (font_inf_);
 }