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