Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-10 10:23:46

0001 //========================================================================
0002 //
0003 // GfxFont.h
0004 //
0005 // Copyright 1996-2003 Glyph & Cog, LLC
0006 //
0007 //========================================================================
0008 
0009 //========================================================================
0010 //
0011 // Modified under the Poppler project - http://poppler.freedesktop.org
0012 //
0013 // All changes made under the Poppler project to this file are licensed
0014 // under GPL version 2 or later
0015 //
0016 // Copyright (C) 2005, 2008, 2015, 2017-2022 Albert Astals Cid <aacid@kde.org>
0017 // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
0018 // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
0019 // Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org>
0020 // Copyright (C) 2007 Jeff Muizelaar <jeff@infidigm.net>
0021 // Copyright (C) 2007 Koji Otani <sho@bbr.jp>
0022 // Copyright (C) 2011 Axel Strübing <axel.struebing@freenet.de>
0023 // Copyright (C) 2011, 2012, 2014 Adrian Johnson <ajohnson@redneon.com>
0024 // Copyright (C) 2015, 2018 Jason Crain <jason@aquaticape.us>
0025 // Copyright (C) 2015 Thomas Freitag <Thomas.Freitag@alfa.de>
0026 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
0027 // Copyright (C) 2021, 2022 Oliver Sander <oliver.sander@tu-dresden.de>
0028 //
0029 // To see a description of the changes please see the Changelog file that
0030 // came with your tarball or type make ChangeLog if you are building from git
0031 //
0032 //========================================================================
0033 
0034 #ifndef GFXFONT_H
0035 #define GFXFONT_H
0036 
0037 #include <memory>
0038 #include <optional>
0039 
0040 #include "goo/GooString.h"
0041 #include "Object.h"
0042 #include "CharTypes.h"
0043 #include "poppler_private_export.h"
0044 
0045 class Dict;
0046 class CMap;
0047 class CharCodeToUnicode;
0048 class FoFiTrueType;
0049 class PSOutputDev;
0050 struct GfxFontCIDWidths;
0051 struct Base14FontMapEntry;
0052 class FNVHash;
0053 
0054 //------------------------------------------------------------------------
0055 // GfxFontType
0056 //------------------------------------------------------------------------
0057 
0058 enum GfxFontType
0059 {
0060     //----- Gfx8BitFont
0061     fontUnknownType,
0062     fontType1,
0063     fontType1C,
0064     fontType1COT,
0065     fontType3,
0066     fontTrueType,
0067     fontTrueTypeOT,
0068     //----- GfxCIDFont
0069     fontCIDType0,
0070     fontCIDType0C,
0071     fontCIDType0COT,
0072     fontCIDType2,
0073     fontCIDType2OT
0074 };
0075 
0076 //------------------------------------------------------------------------
0077 // GfxFontCIDWidths
0078 //------------------------------------------------------------------------
0079 
0080 struct GfxFontCIDWidthExcep
0081 {
0082     CID first; // this record applies to
0083     CID last; //   CIDs <first>..<last>
0084     double width; // char width
0085 };
0086 
0087 struct GfxFontCIDWidthExcepV
0088 {
0089     CID first; // this record applies to
0090     CID last; //   CIDs <first>..<last>
0091     double height; // char height
0092     double vx, vy; // origin position
0093 };
0094 
0095 struct GfxFontCIDWidths
0096 {
0097     double defWidth; // default char width
0098     double defHeight; // default char height
0099     double defVY; // default origin position
0100     GfxFontCIDWidthExcep *exceps; // exceptions
0101     int nExceps; // number of valid entries in exceps
0102     GfxFontCIDWidthExcepV * // exceptions for vertical font
0103             excepsV;
0104     int nExcepsV; // number of valid entries in excepsV
0105 };
0106 
0107 //------------------------------------------------------------------------
0108 // GfxFontLoc
0109 //------------------------------------------------------------------------
0110 
0111 enum GfxFontLocType
0112 {
0113     gfxFontLocEmbedded, // font embedded in PDF file
0114     gfxFontLocExternal, // external font file
0115     gfxFontLocResident // font resident in PS printer
0116 };
0117 
0118 class POPPLER_PRIVATE_EXPORT GfxFontLoc
0119 {
0120 public:
0121     GfxFontLoc();
0122     ~GfxFontLoc();
0123 
0124     GfxFontLoc(const GfxFontLoc &) = delete;
0125     GfxFontLoc(GfxFontLoc &&) noexcept;
0126     GfxFontLoc &operator=(const GfxFontLoc &) = delete;
0127     GfxFontLoc &operator=(GfxFontLoc &&other) noexcept;
0128 
0129     // Set the 'path' string from a GooString on the heap.
0130     // Ownership of the object is taken.
0131     void setPath(GooString *pathA);
0132     const GooString *pathAsGooString() const;
0133 
0134     GfxFontLocType locType;
0135     GfxFontType fontType;
0136     Ref embFontID; // embedded stream obj ID
0137                    //   (if locType == gfxFontLocEmbedded)
0138     std::string path; // font file path
0139                       //   (if locType == gfxFontLocExternal)
0140                       // PS font name
0141                       //   (if locType == gfxFontLocResident)
0142     int fontNum; // for TrueType collections
0143                  //   (if locType == gfxFontLocExternal)
0144     int substIdx; // substitute font index
0145                   //   (if locType == gfxFontLocExternal,
0146                   //   and a Base-14 substitution was made)
0147 };
0148 
0149 //------------------------------------------------------------------------
0150 // GfxFont
0151 //------------------------------------------------------------------------
0152 
0153 #define fontFixedWidth (1 << 0)
0154 #define fontSerif (1 << 1)
0155 #define fontSymbolic (1 << 2)
0156 #define fontItalic (1 << 6)
0157 #define fontBold (1 << 18)
0158 
0159 class POPPLER_PRIVATE_EXPORT GfxFont
0160 {
0161 public:
0162     enum Stretch
0163     {
0164         StretchNotDefined,
0165         UltraCondensed,
0166         ExtraCondensed,
0167         Condensed,
0168         SemiCondensed,
0169         Normal,
0170         SemiExpanded,
0171         Expanded,
0172         ExtraExpanded,
0173         UltraExpanded
0174     };
0175 
0176     enum Weight
0177     {
0178         WeightNotDefined,
0179         W100,
0180         W200,
0181         W300,
0182         W400, // Normal
0183         W500,
0184         W600,
0185         W700, // Bold
0186         W800,
0187         W900
0188     };
0189 
0190     // Build a GfxFont object.
0191     static std::unique_ptr<GfxFont> makeFont(XRef *xref, const char *tagA, Ref idA, Dict *fontDict);
0192 
0193     GfxFont(const GfxFont &) = delete;
0194     GfxFont &operator=(const GfxFont &other) = delete;
0195     virtual ~GfxFont();
0196 
0197     bool isOk() const { return ok; }
0198 
0199     // Get font tag.
0200     const std::string &getTag() const { return tag; }
0201 
0202     // Get font dictionary ID.
0203     const Ref *getID() const { return &id; }
0204 
0205     // Does this font match the tag?
0206     bool matches(const char *tagA) const { return tag == tagA; }
0207 
0208     // Get font family name.
0209     GooString *getFamily() const { return family; }
0210 
0211     // Get font stretch.
0212     Stretch getStretch() const { return stretch; }
0213 
0214     // Get font weight.
0215     Weight getWeight() const { return weight; }
0216 
0217     // Get the original font name (ignornig any munging that might have
0218     // been done to map to a canonical Base-14 font name).
0219     const std::optional<std::string> &getName() const { return name; }
0220 
0221     bool isSubset() const;
0222 
0223     // Returns the original font name without the subset tag (if it has one)
0224     std::string getNameWithoutSubsetTag() const;
0225 
0226     // Get font type.
0227     GfxFontType getType() const { return type; }
0228     virtual bool isCIDFont() const { return false; }
0229 
0230     // Get embedded font ID, i.e., a ref for the font file stream.
0231     // Returns false if there is no embedded font.
0232     bool getEmbeddedFontID(Ref *embID) const
0233     {
0234         *embID = embFontID;
0235         return embFontID != Ref::INVALID();
0236     }
0237 
0238     // Invalidate an embedded font
0239     // Returns false if there is no embedded font.
0240     bool invalidateEmbeddedFont()
0241     {
0242         if (embFontID != Ref::INVALID()) {
0243             embFontID = Ref::INVALID();
0244             return true;
0245         }
0246         return false;
0247     }
0248 
0249     // Get the PostScript font name for the embedded font.  Returns
0250     // NULL if there is no embedded font.
0251     const GooString *getEmbeddedFontName() const { return embFontName; }
0252 
0253     // Get font descriptor flags.
0254     int getFlags() const { return flags; }
0255     bool isFixedWidth() const { return flags & fontFixedWidth; }
0256     bool isSerif() const { return flags & fontSerif; }
0257     bool isSymbolic() const { return flags & fontSymbolic; }
0258     bool isItalic() const { return flags & fontItalic; }
0259     bool isBold() const { return flags & fontBold; }
0260 
0261     // Return the Unicode map.
0262     virtual const CharCodeToUnicode *getToUnicode() const = 0;
0263 
0264     // Return the font matrix.
0265     const double *getFontMatrix() const { return fontMat; }
0266 
0267     // Return the font bounding box.
0268     const double *getFontBBox() const { return fontBBox; }
0269 
0270     // Return the ascent and descent values.
0271     double getAscent() const { return ascent; }
0272     double getDescent() const { return descent; }
0273 
0274     // Return the writing mode (0=horizontal, 1=vertical).
0275     virtual int getWMode() const { return 0; }
0276 
0277     // Locate the font file for this font.  If <ps> is not null, includes PS
0278     // printer-resident fonts.  Returns std::optional without a value on failure.
0279     std::optional<GfxFontLoc> locateFont(XRef *xref, PSOutputDev *ps);
0280 
0281     // Read an external or embedded font file into a buffer.
0282     std::optional<std::vector<unsigned char>> readEmbFontFile(XRef *xref);
0283 
0284     // Get the next char from a string <s> of <len> bytes, returning the
0285     // char <code>, its Unicode mapping <u>, its displacement vector
0286     // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>).  <uSize>
0287     // is the number of entries available in <u>, and <uLen> is set to
0288     // the number actually used.  Returns the number of bytes used by
0289     // the char code.
0290     virtual int getNextChar(const char *s, int len, CharCode *code, Unicode const **u, int *uLen, double *dx, double *dy, double *ox, double *oy) const = 0;
0291 
0292     // Does this font have a toUnicode map?
0293     bool hasToUnicodeCMap() const { return hasToUnicode; }
0294 
0295     // Return the name of the encoding
0296     const std::string &getEncodingName() const { return encodingName; }
0297 
0298     // Return AGLFN names of ligatures in the Standard and Expert encodings
0299     // for use with fonts that are not compatible with the Standard 14 fonts.
0300     // http://sourceforge.net/adobe/aglfn/wiki/AGL%20Specification/
0301     static const char *getAlternateName(const char *name);
0302 
0303 protected:
0304     GfxFont(const char *tagA, Ref idA, std::optional<std::string> &&nameA, GfxFontType typeA, Ref embFontIDA);
0305 
0306     static GfxFontType getFontType(XRef *xref, Dict *fontDict, Ref *embID);
0307     void readFontDescriptor(XRef *xref, Dict *fontDict);
0308     CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits, CharCodeToUnicode *ctu);
0309     static std::optional<GfxFontLoc> getExternalFont(GooString *path, bool cid);
0310 
0311     const std::string tag; // PDF font tag
0312     const Ref id; // reference (used as unique ID)
0313     std::optional<std::string> name; // font name
0314     GooString *family; // font family
0315     Stretch stretch; // font stretch
0316     Weight weight; // font weight
0317     const GfxFontType type; // type of font
0318     int flags; // font descriptor flags
0319     GooString *embFontName; // name of embedded font
0320     Ref embFontID; // ref to embedded font file stream
0321     double fontMat[6]; // font matrix (Type 3 only)
0322     double fontBBox[4]; // font bounding box (Type 3 only)
0323     double missingWidth; // "default" width
0324     double ascent; // max height above baseline
0325     double descent; // max depth below baseline
0326     bool ok;
0327     bool hasToUnicode;
0328     std::string encodingName;
0329 };
0330 
0331 //------------------------------------------------------------------------
0332 // Gfx8BitFont
0333 //------------------------------------------------------------------------
0334 
0335 class POPPLER_PRIVATE_EXPORT Gfx8BitFont : public GfxFont
0336 {
0337 public:
0338     Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, std::optional<std::string> &&nameA, GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
0339 
0340     int getNextChar(const char *s, int len, CharCode *code, Unicode const **u, int *uLen, double *dx, double *dy, double *ox, double *oy) const override;
0341 
0342     // Return the encoding.
0343     char **getEncoding() { return enc; }
0344 
0345     // Return the Unicode map.
0346     const CharCodeToUnicode *getToUnicode() const override;
0347 
0348     // Return the character name associated with <code>.
0349     const char *getCharName(int code) const { return enc[code]; }
0350 
0351     // Returns true if the PDF font specified an encoding.
0352     bool getHasEncoding() const { return hasEncoding; }
0353 
0354     // Returns true if the PDF font specified MacRomanEncoding.
0355     bool getUsesMacRomanEnc() const { return usesMacRomanEnc; }
0356 
0357     // Get width of a character.
0358     double getWidth(unsigned char c) const { return widths[c]; }
0359 
0360     // Return a char code-to-GID mapping for the provided font file.
0361     // (This is only useful for TrueType fonts.)
0362     int *getCodeToGIDMap(FoFiTrueType *ff);
0363 
0364     // Return the Type 3 CharProc dictionary, or NULL if none.
0365     Dict *getCharProcs();
0366 
0367     // Return the Type 3 CharProc for the character associated with <code>.
0368     Object getCharProc(int code);
0369     Object getCharProcNF(int code);
0370 
0371     // Return the Type 3 Resources dictionary, or NULL if none.
0372     Dict *getResources();
0373 
0374 private:
0375     ~Gfx8BitFont() override;
0376 
0377     const Base14FontMapEntry *base14; // for Base-14 fonts only; NULL otherwise
0378     char *enc[256]; // char code --> char name
0379     char encFree[256]; // boolean for each char name: if set,
0380                        //   the string is malloc'ed
0381     CharCodeToUnicode *ctu; // char code --> Unicode
0382     bool hasEncoding;
0383     bool usesMacRomanEnc;
0384     double widths[256]; // character widths
0385     Object charProcs; // Type 3 CharProcs dictionary
0386     Object resources; // Type 3 Resources dictionary
0387 
0388     friend class GfxFont;
0389 };
0390 
0391 //------------------------------------------------------------------------
0392 // GfxCIDFont
0393 //------------------------------------------------------------------------
0394 
0395 class POPPLER_PRIVATE_EXPORT GfxCIDFont : public GfxFont
0396 {
0397 public:
0398     GfxCIDFont(XRef *xref, const char *tagA, Ref idA, std::optional<std::string> &&nameA, GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
0399 
0400     bool isCIDFont() const override { return true; }
0401 
0402     int getNextChar(const char *s, int len, CharCode *code, Unicode const **u, int *uLen, double *dx, double *dy, double *ox, double *oy) const override;
0403 
0404     // Return the writing mode (0=horizontal, 1=vertical).
0405     int getWMode() const override;
0406 
0407     // Return the Unicode map.
0408     const CharCodeToUnicode *getToUnicode() const override;
0409 
0410     // Get the collection name (<registry>-<ordering>).
0411     const GooString *getCollection() const;
0412 
0413     // Return the CID-to-GID mapping table.  These should only be called
0414     // if type is fontCIDType2.
0415     int *getCIDToGID() const { return cidToGID; }
0416     unsigned int getCIDToGIDLen() const { return cidToGIDLen; }
0417 
0418     int *getCodeToGIDMap(FoFiTrueType *ff, int *codeToGIDLen);
0419 
0420     double getWidth(char *s, int len) const;
0421 
0422 private:
0423     ~GfxCIDFont() override;
0424 
0425     int mapCodeToGID(FoFiTrueType *ff, int cmapi, Unicode unicode, bool wmode);
0426     double getWidth(CID cid) const; // Get width of a character.
0427 
0428     GooString *collection; // collection name
0429     std::shared_ptr<CMap> cMap; // char code --> CID
0430     CharCodeToUnicode *ctu; // CID --> Unicode
0431     bool ctuUsesCharCode; // true: ctu maps char code to Unicode;
0432                           //   false: ctu maps CID to Unicode
0433     GfxFontCIDWidths widths; // character widths
0434     int *cidToGID; // CID --> GID mapping (for embedded
0435                    //   TrueType fonts)
0436     unsigned int cidToGIDLen;
0437 };
0438 
0439 //------------------------------------------------------------------------
0440 // GfxFontDict
0441 //------------------------------------------------------------------------
0442 
0443 class GfxFontDict
0444 {
0445 public:
0446     // Build the font dictionary, given the PDF font dictionary.
0447     GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
0448 
0449     GfxFontDict(const GfxFontDict &) = delete;
0450     GfxFontDict &operator=(const GfxFontDict &) = delete;
0451 
0452     // Get the specified font.
0453     std::shared_ptr<GfxFont> lookup(const char *tag) const;
0454 
0455     // Iterative access.
0456     int getNumFonts() const { return fonts.size(); }
0457     const std::shared_ptr<GfxFont> &getFont(int i) const { return fonts[i]; }
0458 
0459 private:
0460     int hashFontObject(Object *obj);
0461     void hashFontObject1(const Object *obj, FNVHash *h);
0462 
0463     std::vector<std::shared_ptr<GfxFont>> fonts;
0464 };
0465 
0466 #endif