home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / indispensabili / utility / apdf / xpdf-0.80 / xpdf / gfxfont.h < prev    next >
C/C++ Source or Header  |  1998-11-27  |  7KB  |  246 lines

  1. //========================================================================
  2. //
  3. // GfxFont.h
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifndef GFXFONT_H
  10. #define GFXFONT_H
  11.  
  12. #ifdef __GNUC__
  13. #pragma interface
  14. #endif
  15.  
  16. #include "gtypes.h"
  17. #include "GString.h"
  18. #include "Object.h"
  19.  
  20. class Dict;
  21.  
  22. //------------------------------------------------------------------------
  23. // GfxFontEncoding
  24. //------------------------------------------------------------------------
  25.  
  26. #define gfxFontEncHashSize 419
  27.  
  28. class GfxFontEncoding {
  29. public:
  30.  
  31.   // Construct an empty encoding.
  32.   GfxFontEncoding();
  33.  
  34.   // Construct an encoding from an array of char names.
  35.   GfxFontEncoding(char **encoding1, int encSize);
  36.  
  37.   // Destructor.
  38.   ~GfxFontEncoding();
  39.  
  40.   // Add a char to the encoding.
  41.   void addChar(int code, char *name);
  42.  
  43.   // Return the character name associated with <code>.
  44.   char *getCharName(int code) { return encoding[code]; }
  45.  
  46.   // Return the code associated with <name>.
  47.   int getCharCode(char *name);
  48.  
  49. private:
  50.  
  51.   int hash(char *name);
  52.   void addChar1(int code, char *name);
  53.  
  54.   char **encoding;        // code --> name mapping
  55.   GBool freeEnc;        // should we free the encoding array?
  56.   short                // name --> code hash table
  57.     hashTab[gfxFontEncHashSize];
  58. };
  59.  
  60. //------------------------------------------------------------------------
  61. // GfxFontCharSet16
  62. //------------------------------------------------------------------------
  63.  
  64. enum GfxFontCharSet16 {
  65.   font16AdobeJapan12            // Adobe-Japan1-2
  66. };
  67.  
  68. //------------------------------------------------------------------------
  69. // GfxFontEncoding16
  70. //------------------------------------------------------------------------
  71.  
  72. struct GfxFontEncoding16 {
  73.   Guchar codeLen[256];        // length of codes, in bytes, indexed by
  74.                 //   first byte of code
  75.   Gushort map1[256];        // one-byte code mapping:
  76.                 //   map1[code] --> 16-bit char selector
  77.   Gushort *map2;        // two-byte code mapping
  78.                 //   map2[2*i]   --> first code in range
  79.                 //   map2[2*i+1] --> 16-bit char selector
  80.                 //                   for map2[2*i]
  81.   int map2Len;            // length of map2 array (divided by 2)
  82. };
  83.  
  84. //------------------------------------------------------------------------
  85. // GfxFontWidths16
  86. //------------------------------------------------------------------------
  87.  
  88. struct GfxFontWidthExcep {
  89.   int first;            // chars <first>..<last> have
  90.   int last;            //   width <width>
  91.   double width;
  92. };
  93.  
  94. struct GfxFontWidths16 {
  95.   double defWidth;        // default char width
  96.   GfxFontWidthExcep *exceps;    // exceptions
  97.   int numExceps;        // number of valid entries in exceps
  98. };
  99.  
  100. //------------------------------------------------------------------------
  101. // GfxFont
  102. //------------------------------------------------------------------------
  103.  
  104. #define fontFixedWidth (1 << 0)
  105. #define fontSerif      (1 << 1)
  106. #define fontSymbolic   (1 << 2)
  107. #define fontItalic     (1 << 6)
  108. #define fontBold       (1 << 18)
  109.  
  110. enum GfxFontType {
  111.   fontUnknownType,
  112.   fontType1,
  113.   fontType3,
  114.   fontTrueType,
  115.   fontType0
  116. };
  117.  
  118. class GfxFont {
  119. public:
  120.  
  121.   // Constructor.
  122.   GfxFont(char *tag1, Ref id1, Dict *fontDict);
  123.  
  124.   // Destructor.
  125.   ~GfxFont();
  126.  
  127.   // Get font tag.
  128.   GString *getTag() { return tag; }
  129.  
  130.   // Get font dictionary ID.
  131.   Ref getID() { return id; }
  132.  
  133.   // Does this font match the tag?
  134.   GBool matches(char *tag1) { return !tag->cmp(tag1); }
  135.  
  136.   // Get base font name.
  137.   GString *getName() { return name; }
  138.  
  139.   // Get font type.
  140.   GfxFontType getType() { return type; }
  141.  
  142.   // Does this font use 16-bit characters?
  143.   GBool is16Bit() { return is16; }
  144.  
  145.   // Get embedded font ID, i.e., a ref for the font file stream.
  146.   // Returns false if there is no embedded font.
  147.   GBool getEmbeddedFontID(Ref *embID)
  148.     { *embID = embFontID; return embFontID.num >= 0; }
  149.  
  150.   // Get the PostScript font name for the embedded font.  Returns
  151.   // NULL if there is no embedded font.
  152.   char *getEmbeddedFontName()
  153.     { return embFontName ? embFontName->getCString() : (char *)NULL; }
  154.  
  155.   // Get the name of the external font file.  Returns NULL if there
  156.   // is no external font file.
  157.   char *getExtFontFile()
  158.     { return extFontFile ? extFontFile->getCString() : (char *)NULL; }
  159.  
  160.   // Get font descriptor flags.
  161.   GBool isFixedWidth() { return flags & fontFixedWidth; }
  162.   GBool isSerif() { return flags & fontSerif; }
  163.   GBool isSymbolic() { return flags & fontSymbolic; }
  164.   GBool isItalic() { return flags & fontItalic; }
  165.   GBool isBold() { return flags & fontBold; }
  166.  
  167.   // Get width of a character or string.
  168.   double getWidth(Guchar c) { return widths[c]; }
  169.   double getWidth(GString *s);
  170.   double getWidth16(int c);
  171.   double getWidth16(GString *s);
  172.  
  173.   // Return the character name associated with <code>.
  174.   char *getCharName(int code) { return encoding->getCharName(code); }
  175.  
  176.   // Return the code associated with <name>.
  177.   int getCharCode(char *charName) { return encoding->getCharCode(charName); }
  178.  
  179.   // Return the 16-bit character set and encoding.
  180.   GfxFontCharSet16 getCharSet16() { return enc16.charSet; }
  181.   GfxFontEncoding16 *getEncoding16() { return enc16.enc; }
  182.  
  183.   // Return the font matrix.
  184.   double *getFontMatrix() { return fontMat; }
  185.  
  186. private:
  187.  
  188.   void makeEncoding(Dict *fontDict, GfxFontEncoding *builtinEncoding);
  189.   GfxFontEncoding *makeEncoding1(Object obj, Dict *fontDesc,
  190.                  GfxFontEncoding *builtinEncoding);
  191.   void getType1Encoding(Stream *str);
  192.   void makeWidths(Dict *fontDict, GfxFontEncoding *builtinEncoding,
  193.           Gushort *builtinWidths);
  194.   void getType0EncAndWidths(Dict *fontDict);
  195.  
  196.   GString *tag;            // PDF font tag
  197.   Ref id;            // reference (used as unique ID)
  198.   GString *name;        // font name
  199.   int flags;            // font descriptor flags
  200.   GfxFontType type;        // type of font
  201.   GBool is16;            // set if font uses 16-bit chars
  202.   GString *embFontName;        // name of embedded font
  203.   Ref embFontID;        // ref to embedded font file stream
  204.   GString *extFontFile;        // external font file name
  205.   double fontMat[6];        // font matrix
  206.   union {
  207.     GfxFontEncoding *encoding;    // 8-bit font encoding
  208.     struct {
  209.       GfxFontCharSet16 charSet;    // 16-bit character set
  210.       GfxFontEncoding16 *enc;    // 16-bit encoding (CMap)
  211.     } enc16;
  212.   };
  213.   union {
  214.     double widths[256];        // width of each char for 8-bit font
  215.     GfxFontWidths16 widths16;    // char widths for 16-bit font
  216.   };
  217. };
  218.  
  219. //------------------------------------------------------------------------
  220. // GfxFontDict
  221. //------------------------------------------------------------------------
  222.  
  223. class GfxFontDict {
  224. public:
  225.  
  226.   // Build the font dictionary, given the PDF font dictionary.
  227.   GfxFontDict(Dict *fontDict);
  228.  
  229.   // Destructor.
  230.   ~GfxFontDict();
  231.  
  232.   // Get the specified font.
  233.   GfxFont *lookup(char *tag);
  234.  
  235.   // Iterative access.
  236.   int getNumFonts() { return numFonts; }
  237.   GfxFont *getFont(int i) { return fonts[i]; }
  238.  
  239. private:
  240.  
  241.   GfxFont **fonts;        // list of fonts
  242.   int numFonts;            // number of fonts
  243. };
  244.  
  245. #endif
  246.