File indexing completed on 2025-12-10 10:23:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
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
0056
0057
0058 enum GfxFontType
0059 {
0060
0061 fontUnknownType,
0062 fontType1,
0063 fontType1C,
0064 fontType1COT,
0065 fontType3,
0066 fontTrueType,
0067 fontTrueTypeOT,
0068
0069 fontCIDType0,
0070 fontCIDType0C,
0071 fontCIDType0COT,
0072 fontCIDType2,
0073 fontCIDType2OT
0074 };
0075
0076
0077
0078
0079
0080 struct GfxFontCIDWidthExcep
0081 {
0082 CID first;
0083 CID last;
0084 double width;
0085 };
0086
0087 struct GfxFontCIDWidthExcepV
0088 {
0089 CID first;
0090 CID last;
0091 double height;
0092 double vx, vy;
0093 };
0094
0095 struct GfxFontCIDWidths
0096 {
0097 double defWidth;
0098 double defHeight;
0099 double defVY;
0100 GfxFontCIDWidthExcep *exceps;
0101 int nExceps;
0102 GfxFontCIDWidthExcepV *
0103 excepsV;
0104 int nExcepsV;
0105 };
0106
0107
0108
0109
0110
0111 enum GfxFontLocType
0112 {
0113 gfxFontLocEmbedded,
0114 gfxFontLocExternal,
0115 gfxFontLocResident
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
0130
0131 void setPath(GooString *pathA);
0132 const GooString *pathAsGooString() const;
0133
0134 GfxFontLocType locType;
0135 GfxFontType fontType;
0136 Ref embFontID;
0137
0138 std::string path;
0139
0140
0141
0142 int fontNum;
0143
0144 int substIdx;
0145
0146
0147 };
0148
0149
0150
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,
0183 W500,
0184 W600,
0185 W700,
0186 W800,
0187 W900
0188 };
0189
0190
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
0200 const std::string &getTag() const { return tag; }
0201
0202
0203 const Ref *getID() const { return &id; }
0204
0205
0206 bool matches(const char *tagA) const { return tag == tagA; }
0207
0208
0209 GooString *getFamily() const { return family; }
0210
0211
0212 Stretch getStretch() const { return stretch; }
0213
0214
0215 Weight getWeight() const { return weight; }
0216
0217
0218
0219 const std::optional<std::string> &getName() const { return name; }
0220
0221 bool isSubset() const;
0222
0223
0224 std::string getNameWithoutSubsetTag() const;
0225
0226
0227 GfxFontType getType() const { return type; }
0228 virtual bool isCIDFont() const { return false; }
0229
0230
0231
0232 bool getEmbeddedFontID(Ref *embID) const
0233 {
0234 *embID = embFontID;
0235 return embFontID != Ref::INVALID();
0236 }
0237
0238
0239
0240 bool invalidateEmbeddedFont()
0241 {
0242 if (embFontID != Ref::INVALID()) {
0243 embFontID = Ref::INVALID();
0244 return true;
0245 }
0246 return false;
0247 }
0248
0249
0250
0251 const GooString *getEmbeddedFontName() const { return embFontName; }
0252
0253
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
0262 virtual const CharCodeToUnicode *getToUnicode() const = 0;
0263
0264
0265 const double *getFontMatrix() const { return fontMat; }
0266
0267
0268 const double *getFontBBox() const { return fontBBox; }
0269
0270
0271 double getAscent() const { return ascent; }
0272 double getDescent() const { return descent; }
0273
0274
0275 virtual int getWMode() const { return 0; }
0276
0277
0278
0279 std::optional<GfxFontLoc> locateFont(XRef *xref, PSOutputDev *ps);
0280
0281
0282 std::optional<std::vector<unsigned char>> readEmbFontFile(XRef *xref);
0283
0284
0285
0286
0287
0288
0289
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
0293 bool hasToUnicodeCMap() const { return hasToUnicode; }
0294
0295
0296 const std::string &getEncodingName() const { return encodingName; }
0297
0298
0299
0300
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;
0312 const Ref id;
0313 std::optional<std::string> name;
0314 GooString *family;
0315 Stretch stretch;
0316 Weight weight;
0317 const GfxFontType type;
0318 int flags;
0319 GooString *embFontName;
0320 Ref embFontID;
0321 double fontMat[6];
0322 double fontBBox[4];
0323 double missingWidth;
0324 double ascent;
0325 double descent;
0326 bool ok;
0327 bool hasToUnicode;
0328 std::string encodingName;
0329 };
0330
0331
0332
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
0343 char **getEncoding() { return enc; }
0344
0345
0346 const CharCodeToUnicode *getToUnicode() const override;
0347
0348
0349 const char *getCharName(int code) const { return enc[code]; }
0350
0351
0352 bool getHasEncoding() const { return hasEncoding; }
0353
0354
0355 bool getUsesMacRomanEnc() const { return usesMacRomanEnc; }
0356
0357
0358 double getWidth(unsigned char c) const { return widths[c]; }
0359
0360
0361
0362 int *getCodeToGIDMap(FoFiTrueType *ff);
0363
0364
0365 Dict *getCharProcs();
0366
0367
0368 Object getCharProc(int code);
0369 Object getCharProcNF(int code);
0370
0371
0372 Dict *getResources();
0373
0374 private:
0375 ~Gfx8BitFont() override;
0376
0377 const Base14FontMapEntry *base14;
0378 char *enc[256];
0379 char encFree[256];
0380
0381 CharCodeToUnicode *ctu;
0382 bool hasEncoding;
0383 bool usesMacRomanEnc;
0384 double widths[256];
0385 Object charProcs;
0386 Object resources;
0387
0388 friend class GfxFont;
0389 };
0390
0391
0392
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
0405 int getWMode() const override;
0406
0407
0408 const CharCodeToUnicode *getToUnicode() const override;
0409
0410
0411 const GooString *getCollection() const;
0412
0413
0414
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;
0427
0428 GooString *collection;
0429 std::shared_ptr<CMap> cMap;
0430 CharCodeToUnicode *ctu;
0431 bool ctuUsesCharCode;
0432
0433 GfxFontCIDWidths widths;
0434 int *cidToGID;
0435
0436 unsigned int cidToGIDLen;
0437 };
0438
0439
0440
0441
0442
0443 class GfxFontDict
0444 {
0445 public:
0446
0447 GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict);
0448
0449 GfxFontDict(const GfxFontDict &) = delete;
0450 GfxFontDict &operator=(const GfxFontDict &) = delete;
0451
0452
0453 std::shared_ptr<GfxFont> lookup(const char *tag) const;
0454
0455
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