1 | //======================================================================== |
2 | // |
3 | // CMap.h |
4 | // |
5 | // Copyright 2001-2003 Glyph & Cog, LLC |
6 | // |
7 | //======================================================================== |
8 | |
9 | //======================================================================== |
10 | // |
11 | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | // |
13 | // All changes made under the Poppler project to this file are licensed |
14 | // under GPL version 2 or later |
15 | // |
16 | // Copyright (C) 2008 Koji Otani <sho@bbr.jp> |
17 | // Copyright (C) 2009, 2018-2020, 2022 Albert Astals Cid <aacid@kde.org> |
18 | // Copyright (C) 2012, 2017 Adrian Johnson <ajohnson@redneon.com> |
19 | // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de> |
20 | // |
21 | // To see a description of the changes please see the Changelog file that |
22 | // came with your tarball or type make ChangeLog if you are building from git |
23 | // |
24 | //======================================================================== |
25 | |
26 | #ifndef CMAP_H |
27 | #define CMAP_H |
28 | |
29 | #include <array> |
30 | #include <atomic> |
31 | #include <memory> |
32 | |
33 | #include "poppler-config.h" |
34 | #include "CharTypes.h" |
35 | |
36 | class GooString; |
37 | class Object; |
38 | struct CMapVectorEntry; |
39 | class CMapCache; |
40 | class Stream; |
41 | |
42 | //------------------------------------------------------------------------ |
43 | |
44 | class CMap |
45 | { |
46 | public: |
47 | // Parse a CMap from <obj>, which can be a name or a stream. Sets |
48 | // the initial reference count to 1. Returns NULL on failure. |
49 | static std::shared_ptr<CMap> parse(CMapCache *cache, const GooString *collectionA, Object *obj); |
50 | |
51 | // Create the CMap specified by <collection> and <cMapName>. Sets |
52 | // the initial reference count to 1. Returns NULL on failure. |
53 | static std::shared_ptr<CMap> parse(CMapCache *cache, const GooString *collectionA, const GooString *cMapNameA); |
54 | |
55 | // Parse a CMap from <str>. Sets the initial reference count to 1. |
56 | // Returns NULL on failure. |
57 | static std::shared_ptr<CMap> parse(CMapCache *cache, const GooString *collectionA, Stream *str); |
58 | |
59 | ~CMap(); |
60 | |
61 | CMap(const CMap &) = delete; |
62 | CMap &operator=(const CMap &) = delete; |
63 | |
64 | // Return collection name (<registry>-<ordering>). |
65 | const GooString *getCollection() const { return collection; } |
66 | |
67 | const GooString *getCMapName() const { return cMapName; } |
68 | |
69 | // Return true if this CMap matches the specified <collectionA>, and |
70 | // <cMapNameA>. |
71 | bool match(const GooString *collectionA, const GooString *cMapNameA); |
72 | |
73 | // Return the CID corresponding to the character code starting at |
74 | // <s>, which contains <len> bytes. Sets *<c> to the char code, and |
75 | // *<nUsed> to the number of bytes used by the char code. |
76 | CID getCID(const char *s, int len, CharCode *c, int *nUsed); |
77 | |
78 | // Return the writing mode (0=horizontal, 1=vertical). |
79 | int getWMode() const { return wMode; } |
80 | |
81 | void setReverseMap(unsigned int *rmap, unsigned int rmapSize, unsigned int ncand); |
82 | |
83 | private: |
84 | void parse2(CMapCache *cache, int (*getCharFunc)(void *), void *data); |
85 | CMap(GooString *collectionA, GooString *cMapNameA); |
86 | CMap(GooString *collectionA, GooString *cMapNameA, int wModeA); |
87 | void useCMap(CMapCache *cache, const char *useName); |
88 | void useCMap(CMapCache *cache, Object *obj); |
89 | void copyVector(CMapVectorEntry *dest, CMapVectorEntry *src); |
90 | void addCIDs(unsigned int start, unsigned int end, unsigned int nBytes, CID firstCID); |
91 | void freeCMapVector(CMapVectorEntry *vec); |
92 | void setReverseMapVector(unsigned int startCode, CMapVectorEntry *vec, unsigned int *rmap, unsigned int rmapSize, unsigned int ncand); |
93 | |
94 | GooString *collection; |
95 | GooString *cMapName; |
96 | bool isIdent; // true if this CMap is an identity mapping, |
97 | // or is based on one (via usecmap) |
98 | int wMode; // writing mode (0=horizontal, 1=vertical) |
99 | CMapVectorEntry *vector; // vector for first byte (NULL for |
100 | // identity CMap) |
101 | }; |
102 | |
103 | //------------------------------------------------------------------------ |
104 | |
105 | #define cMapCacheSize 4 |
106 | |
107 | class CMapCache |
108 | { |
109 | public: |
110 | CMapCache(); |
111 | ~CMapCache() = default; |
112 | |
113 | CMapCache(const CMapCache &) = delete; |
114 | CMapCache &operator=(const CMapCache &) = delete; |
115 | |
116 | // Get the <cMapName> CMap for the specified character collection. |
117 | // Increments its reference count; there will be one reference for |
118 | // the cache plus one for the caller of this function. |
119 | // Stream is a stream containing the CMap, can be NULL and |
120 | // this means the CMap will be searched in the CMap files |
121 | // Returns NULL on failure. |
122 | std::shared_ptr<CMap> getCMap(const GooString *collection, const GooString *cMapName); |
123 | |
124 | private: |
125 | std::array<std::shared_ptr<CMap>, cMapCacheSize> cache; |
126 | }; |
127 | |
128 | #endif |
129 | |