Back to index

tetex-bin  3.0
UnicodeMap.h
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // UnicodeMap.h
00004 //
00005 // Mapping from Unicode to an encoding.
00006 //
00007 // Copyright 2001-2003 Glyph & Cog, LLC
00008 //
00009 //========================================================================
00010 
00011 #ifndef UNICODEMAP_H
00012 #define UNICODEMAP_H
00013 
00014 #include <aconf.h>
00015 
00016 #ifdef USE_GCC_PRAGMAS
00017 #pragma interface
00018 #endif
00019 
00020 #include "gtypes.h"
00021 #include "CharTypes.h"
00022 
00023 #if MULTITHREADED
00024 #include "GMutex.h"
00025 #endif
00026 
00027 class GString;
00028 
00029 //------------------------------------------------------------------------
00030 
00031 enum UnicodeMapKind {
00032   unicodeMapUser,           // read from a file
00033   unicodeMapResident,              // static list of ranges
00034   unicodeMapFunc            // function pointer
00035 };
00036 
00037 typedef int (*UnicodeMapFunc)(Unicode u, char *buf, int bufSize);
00038 
00039 struct UnicodeMapRange {
00040   Unicode start, end;              // range of Unicode chars
00041   Guint code, nBytes;              // first output code
00042 };
00043 
00044 struct UnicodeMapExt;
00045 
00046 //------------------------------------------------------------------------
00047 
00048 class UnicodeMap {
00049 public:
00050 
00051   // Create the UnicodeMap specified by <encodingName>.  Sets the
00052   // initial reference count to 1.  Returns NULL on failure.
00053   static UnicodeMap *parse(GString *encodingNameA);
00054 
00055   // Create a resident UnicodeMap.
00056   UnicodeMap(char *encodingNameA, GBool unicodeOutA,
00057             UnicodeMapRange *rangesA, int lenA);
00058 
00059   // Create a resident UnicodeMap that uses a function instead of a
00060   // list of ranges.
00061   UnicodeMap(char *encodingNameA, GBool unicodeOutA,
00062             UnicodeMapFunc funcA);
00063 
00064   ~UnicodeMap();
00065 
00066   void incRefCnt();
00067   void decRefCnt();
00068 
00069   GString *getEncodingName() { return encodingName; }
00070 
00071   GBool isUnicode() { return unicodeOut; }
00072 
00073   // Return true if this UnicodeMap matches the specified
00074   // <encodingNameA>.
00075   GBool match(GString *encodingNameA);
00076 
00077   // Map Unicode to the target encoding.  Fills in <buf> with the
00078   // output and returns the number of bytes used.  Output will be
00079   // truncated at <bufSize> bytes.  No string terminator is written.
00080   // Returns 0 if no mapping is found.
00081   int mapUnicode(Unicode u, char *buf, int bufSize);
00082 
00083 private:
00084 
00085   UnicodeMap(GString *encodingNameA);
00086 
00087   GString *encodingName;
00088   UnicodeMapKind kind;
00089   GBool unicodeOut;
00090   union {
00091     UnicodeMapRange *ranges;       // (user, resident)
00092     UnicodeMapFunc func;    // (func)
00093   };
00094   int len;                  // (user, resident)
00095   UnicodeMapExt *eMaps;            // (user)
00096   int eMapsLen;                    // (user)
00097   int refCnt;
00098 #ifdef MULTITHREADED
00099   GMutex mutex;
00100 #endif
00101 };
00102 
00103 //------------------------------------------------------------------------
00104 
00105 #define unicodeMapCacheSize 4
00106 
00107 class UnicodeMapCache {
00108 public:
00109 
00110   UnicodeMapCache();
00111   ~UnicodeMapCache();
00112 
00113   // Get the UnicodeMap for <encodingName>.  Increments its reference
00114   // count; there will be one reference for the cache plus one for the
00115   // caller of this function.  Returns NULL on failure.
00116   UnicodeMap *getUnicodeMap(GString *encodingName);
00117 
00118 private:
00119 
00120   UnicodeMap *cache[unicodeMapCacheSize];
00121 };
00122 
00123 #endif