]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/UnicodeMap.h
Import of Xpdf 2.00 for merge
[evince.git] / pdf / xpdf / UnicodeMap.h
1 //========================================================================
2 //
3 // UnicodeMap.h
4 //
5 // Mapping from Unicode to an encoding.
6 //
7 // Copyright 2001-2002 Glyph & Cog, LLC
8 //
9 //========================================================================
10
11 #ifndef UNICODEMAP_H
12 #define UNICODEMAP_H
13
14 #include <aconf.h>
15
16 #ifdef USE_GCC_PRAGMAS
17 #pragma interface
18 #endif
19
20 #include "gtypes.h"
21 #include "CharTypes.h"
22
23 class GString;
24
25 //------------------------------------------------------------------------
26
27 enum UnicodeMapKind {
28   unicodeMapUser,               // read from a file
29   unicodeMapResident,           // static list of ranges
30   unicodeMapFunc                // function pointer
31 };
32
33 typedef int (*UnicodeMapFunc)(Unicode u, char *buf, int bufSize);
34
35 struct UnicodeMapRange {
36   Unicode start, end;           // range of Unicode chars
37   Guint code, nBytes;           // first output code
38 };
39
40 struct UnicodeMapExt;
41
42 //------------------------------------------------------------------------
43
44 class UnicodeMap {
45 public:
46
47   // Create the UnicodeMap specified by <encodingName>.  Sets the
48   // initial reference count to 1.  Returns NULL on failure.
49   static UnicodeMap *parse(GString *encodingNameA);
50
51   // Create a resident UnicodeMap.
52   UnicodeMap(char *encodingNameA, GBool unicodeOutA,
53              UnicodeMapRange *rangesA, int lenA);
54
55   // Create a resident UnicodeMap that uses a function instead of a
56   // list of ranges.
57   UnicodeMap(char *encodingNameA, GBool unicodeOutA,
58              UnicodeMapFunc funcA);
59
60   ~UnicodeMap();
61
62   void incRefCnt();
63   void decRefCnt();
64
65   GString *getEncodingName() { return encodingName; }
66
67   GBool isUnicode() { return unicodeOut; }
68
69   // Return true if this UnicodeMap matches the specified
70   // <encodingNameA>.
71   GBool match(GString *encodingNameA);
72
73   // Map Unicode to the target encoding.  Fills in <buf> with the
74   // output and returns the number of bytes used.  Output will be
75   // truncated at <bufSize> bytes.  No string terminator is written.
76   // Returns 0 if no mapping is found.
77   int mapUnicode(Unicode u, char *buf, int bufSize);
78
79 private:
80
81   UnicodeMap(GString *encodingNameA);
82
83   GString *encodingName;
84   UnicodeMapKind kind;
85   GBool unicodeOut;
86   union {
87     UnicodeMapRange *ranges;    // (user, resident)
88     UnicodeMapFunc func;        // (func)
89   };
90   int len;                      // (user, resident)
91   UnicodeMapExt *eMaps;         // (user)
92   int eMapsLen;                 // (user)
93   int refCnt;
94 };
95
96 //------------------------------------------------------------------------
97
98 #define unicodeMapCacheSize 4
99
100 class UnicodeMapCache {
101 public:
102
103   UnicodeMapCache();
104   ~UnicodeMapCache();
105
106   // Get the UnicodeMap for <encodingName>.  Increments its reference
107   // count; there will be one reference for the cache plus one for the
108   // caller of this function.  Returns NULL on failure.
109   UnicodeMap *getUnicodeMap(GString *encodingName);
110
111 private:
112
113   UnicodeMap *cache[unicodeMapCacheSize];
114 };
115
116 #endif