filters

FontFile.h

00001 //========================================================================
00002 //
00003 // FontFile.h
00004 //
00005 // Copyright 1999-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef FONTFILE_H
00010 #define FONTFILE_H
00011 
00012 #include <aconf.h>
00013 
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017 
00018 #include <stdio.h>
00019 #include "gtypes.h"
00020 #include "GString.h"
00021 #include "CharTypes.h"
00022 
00023 class CharCodeToUnicode;
00024 
00025 //------------------------------------------------------------------------
00026 
00027 typedef void (*FontFileOutputFunc)(void *stream, const char *data, int len);
00028 
00029 //------------------------------------------------------------------------
00030 // FontFile
00031 //------------------------------------------------------------------------
00032 
00033 class FontFile {
00034 public:
00035 
00036   FontFile();
00037   virtual ~FontFile();
00038 
00039   // Returns the font name, as specified internally by the font file.
00040   // Returns NULL if no name is available.
00041   virtual const char *getName() = 0;
00042 
00043   // Returns the custom font encoding, or NULL if the encoding is not
00044   // available.
00045   virtual char **getEncoding() = 0;
00046 };
00047 
00048 //------------------------------------------------------------------------
00049 // Type1FontFile
00050 //------------------------------------------------------------------------
00051 
00052 class Type1FontFile: public FontFile {
00053 public:
00054 
00055   Type1FontFile(const char *file, int len);
00056   virtual ~Type1FontFile();
00057   virtual const char *getName() { return name; }
00058   virtual char **getEncoding() { return encoding; }
00059 
00060 private:
00061 
00062   char *name;
00063   char **encoding;
00064 };
00065 
00066 //------------------------------------------------------------------------
00067 // Type1CFontFile
00068 //------------------------------------------------------------------------
00069 
00070 struct Type1CTopDict;
00071 struct Type1CPrivateDict;
00072 
00073 class Type1CFontFile: public FontFile {
00074 public:
00075 
00076   Type1CFontFile(const char *fileA, int lenA);
00077   virtual ~Type1CFontFile();
00078 
00079   virtual const char *getName();
00080   virtual char **getEncoding();
00081 
00082   // Convert to a Type 1 font, suitable for embedding in a PostScript
00083   // file.  The name will be used as the PostScript font name.
00084   void convertToType1(FontFileOutputFunc outputFuncA, void *outputStreamA);
00085 
00086   // Convert to a Type 0 CIDFont, suitable for embedding in a
00087   // PostScript file.  The name will be used as the PostScript font
00088   // name.
00089   void convertToCIDType0(const char *psName,
00090              FontFileOutputFunc outputFuncA, void *outputStreamA);
00091 
00092   // Convert to a Type 0 (but non-CID) composite font, suitable for
00093   // embedding in a PostScript file.  The name will be used as the
00094   // PostScript font name.
00095   void convertToType0(const char *psName,
00096               FontFileOutputFunc outputFuncA, void *outputStreamA);
00097 
00098 private:
00099 
00100   void readNameAndEncoding();
00101   void readTopDict(Type1CTopDict *dict);
00102   void readPrivateDict(Type1CPrivateDict *privateDict,
00103                int offset, int size);
00104   Gushort *readCharset(int charset, int nGlyphs);
00105   void eexecWrite(const char *s);
00106   void eexecCvtGlyph(const char *glyphName, const Guchar *s, int n);
00107   void cvtGlyph(const Guchar *s, int n);
00108   void cvtGlyphWidth(GBool useOp);
00109   void eexecDumpNum(double x, GBool fpA);
00110   void eexecDumpOp1(int opA);
00111   void eexecDumpOp2(int opA);
00112   void eexecWriteCharstring(const Guchar *s, int n);
00113   void getDeltaInt(char *buf, const char *key, const double *opA, int n);
00114   void getDeltaReal(char *buf, const char *key, const double *opA, int n);
00115   int getIndexLen(Guchar *indexPtr);
00116   Guchar *getIndexValPtr(Guchar *indexPtr, int i);
00117   Guchar *getIndexEnd(Guchar *indexPtr);
00118   Guint getWord(Guchar *ptr, int size);
00119   double getNum(Guchar **ptr, GBool *fp);
00120   char *getString(int sid, char *buf);
00121 
00122   const char *file;
00123   int len;
00124 
00125   GString *name;
00126   char **encoding;
00127 
00128   int topOffSize;
00129   Guchar *topDictIdxPtr;
00130   Guchar *stringIdxPtr;
00131   Guchar *gsubrIdxPtr;
00132 
00133   FontFileOutputFunc outputFunc;
00134   void *outputStream;
00135   double op[48];        // operands
00136   GBool fp[48];         // true if operand is fixed point
00137   int nOps;         // number of operands
00138   double defaultWidthX;     // default glyph width
00139   double nominalWidthX;     // nominal glyph width
00140   GBool defaultWidthXFP;    // true if defaultWidthX is fixed point
00141   GBool nominalWidthXFP;    // true if nominalWidthX is fixed point
00142   Gushort r1;           // eexec encryption key
00143   GString *charBuf;     // charstring output buffer
00144   int line;         // number of eexec chars on current line
00145 };
00146 
00147 //------------------------------------------------------------------------
00148 // TrueTypeFontFile
00149 //------------------------------------------------------------------------
00150 
00151 struct TTFontTableHdr;
00152 
00153 class TrueTypeFontFile: public FontFile {
00154 public:
00155 
00156   TrueTypeFontFile(const char *fileA, int lenA);
00157   ~TrueTypeFontFile();
00158 
00159   // This always returns NULL, since it's probably better to trust the
00160   // font name in the PDF file rather than the one in the TrueType
00161   // font file.
00162   virtual const char *getName();
00163 
00164   virtual char **getEncoding();
00165 
00166   // Convert to a Type 42 font, suitable for embedding in a PostScript
00167   // file.  The name will be used as the PostScript font name (so we
00168   // don't need to depend on the 'name' table in the font).  The
00169   // encoding is needed because the PDF Font object can modify the
00170   // encoding.
00171   void convertToType42(const char *name, const char **encodingA,
00172                CharCodeToUnicode *toUnicode,
00173                GBool pdfFontHasEncoding,
00174                FontFileOutputFunc outputFunc, void *outputStream);
00175 
00176   // Convert to a Type 2 CIDFont, suitable for embedding in a
00177   // PostScript file.  The name will be used as the PostScript font
00178   // name (so we don't need to depend on the 'name' table in the
00179   // font).
00180   void convertToCIDType2(const char *name, const Gushort *cidMap, int nCIDs,
00181              FontFileOutputFunc outputFunc, void *outputStream);
00182 
00183   // Convert to a Type 0 (but non-CID) composite font, suitable for
00184   // embedding in a PostScript file.  The name will be used as the
00185   // PostScript font name (so we don't need to depend on the 'name'
00186   // table in the font).
00187   void convertToType0(const char *name, const Gushort *cidMap, int nCIDs,
00188               FontFileOutputFunc outputFunc, void *outputStream);
00189 
00190   // Write a TTF file, filling in any missing tables that are required
00191   // by the TrueType spec.  If the font already has all the required
00192   // tables, it will be written unmodified.
00193   void writeTTF(FILE *out);
00194 
00195 private:
00196 
00197   const char *file;
00198   int len;
00199 
00200   char **encoding;
00201 
00202   TTFontTableHdr *tableHdrs;
00203   int nTables;
00204   int bbox[4];
00205   int locaFmt;
00206   int nGlyphs;
00207   GBool mungedCmapSize;
00208 
00209   int getByte(int pos);
00210   int getChar(int pos);
00211   int getUShort(int pos);
00212   int getShort(int pos);
00213   Guint getULong(int pos);
00214   double getFixed(int pos);
00215   int seekTable(const char *tag);
00216   int seekTableIdx(const char *tag);
00217   void cvtEncoding(const char **encodingA, GBool pdfFontHasEncoding,
00218            FontFileOutputFunc outputFunc, void *outputStream);
00219   void cvtCharStrings(const char **encodingA, CharCodeToUnicode *toUnicode,
00220               GBool pdfFontHasEncoding,
00221               FontFileOutputFunc outputFunc, void *outputStream);
00222   int getCmapEntry(int cmapFmt, int pos, int code);
00223   void cvtSfnts(FontFileOutputFunc outputFunc, void *outputStream,
00224         GString *name);
00225   void dumpString(const char *s, int length,
00226           FontFileOutputFunc outputFunc, void *outputStream);
00227   Guint computeTableChecksum(const char *data, int length);
00228 };
00229 
00230 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys