1 //========================================================================
2 //
3 // CharCodeToUnicode.h
4 //
5 // Mapping from character codes to Unicode.
6 //
7 // Copyright 2001-2003 Glyph & Cog, LLC
8 //
9 //========================================================================
10 
11 #ifndef CHARCODETOUNICODE_H
12 #define CHARCODETOUNICODE_H
13 
14 #include <aconf.h>
15 
16 #ifdef USE_GCC_PRAGMAS
17 #pragma interface
18 #endif
19 
20 #include "CharTypes.h"
21 
22 #if MULTITHREADED
23 #include "GMutex.h"
24 #endif
25 
26 struct CharCodeToUnicodeString;
27 
28 //------------------------------------------------------------------------
29 
30 class CharCodeToUnicode {
31 public:
32 
33   // Create an identity mapping (Unicode = CharCode).
34   static CharCodeToUnicode *makeIdentityMapping();
35 
36   // Read the CID-to-Unicode mapping for <collection> from the file
37   // specified by <fileName>.  Sets the initial reference count to 1.
38   // Returns NULL on failure.
39   static CharCodeToUnicode *parseCIDToUnicode(GString *fileName,
40 					      GString *collection);
41 
42   // Create a Unicode-to-Unicode mapping from the file specified by
43   // <fileName>.  Sets the initial reference count to 1.  Returns NULL
44   // on failure.
45   static CharCodeToUnicode *parseUnicodeToUnicode(GString *fileName);
46 
47   // Create the CharCode-to-Unicode mapping for an 8-bit font.
48   // <toUnicode> is an array of 256 Unicode indexes.  Sets the initial
49   // reference count to 1.
50   static CharCodeToUnicode *make8BitToUnicode(Unicode *toUnicode);
51 
52   // Parse a ToUnicode CMap for an 8- or 16-bit font.
53   static CharCodeToUnicode *parseCMap(GString *buf, int nBits);
54 
55   // Parse a ToUnicode CMap for an 8- or 16-bit font, merging it into
56   // <this>.
57   void mergeCMap(GString *buf, int nBits);
58 
59   ~CharCodeToUnicode();
60 
61   void incRefCnt();
62   void decRefCnt();
63 
64   // Return true if this mapping matches the specified <tagA>.
65   GBool match(GString *tagA);
66 
67   // Set the mapping for <c>.
68   void setMapping(CharCode c, Unicode *u, int len);
69 
70   // Map a CharCode to Unicode.
71   int mapToUnicode(CharCode c, Unicode *u, int size);
72 
73   // Return the mapping's length, i.e., one more than the max char
74   // code supported by the mapping.
getLength()75   CharCode getLength() { return mapLen; }
76 
isIdentity()77   GBool isIdentity() { return !map; }
78 
79 private:
80 
81   void parseCMap1(int (*getCharFunc)(void *), void *data, int nBits);
82   void addMapping(CharCode code, char *uStr, int n, int offset);
83   CharCodeToUnicode();
84   CharCodeToUnicode(GString *tagA);
85   CharCodeToUnicode(GString *tagA, Unicode *mapA,
86 		    CharCode mapLenA, GBool copyMap,
87 		    CharCodeToUnicodeString *sMapA,
88 		    int sMapLenA, int sMapSizeA);
89 
90   GString *tag;
91   Unicode *map;
92   CharCode mapLen;
93   CharCodeToUnicodeString *sMap;
94   int sMapLen, sMapSize;
95   int refCnt;
96 #if MULTITHREADED
97   GMutex mutex;
98 #endif
99 };
100 
101 //------------------------------------------------------------------------
102 
103 class CharCodeToUnicodeCache {
104 public:
105 
106   CharCodeToUnicodeCache(int sizeA);
107   ~CharCodeToUnicodeCache();
108 
109   // Get the CharCodeToUnicode object for <tag>.  Increments its
110   // reference count; there will be one reference for the cache plus
111   // one for the caller of this function.  Returns NULL on failure.
112   CharCodeToUnicode *getCharCodeToUnicode(GString *tag);
113 
114   // Insert <ctu> into the cache, in the most-recently-used position.
115   void add(CharCodeToUnicode *ctu);
116 
117 private:
118 
119   CharCodeToUnicode **cache;
120   int size;
121 };
122 
123 #endif
124