| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 2000 |
| 3 |
* Orignially by Vlad Harchev <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
* 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef XAP_ENCMGR_H |
| 22 |
#define XAP_ENCMGR_H |
| 23 |
|
| 24 |
/* pre-emptive dismissal; ut_types.h is needed by just about everything, |
| 25 |
* so even if it's commented out in-file that's still a lot of work for |
| 26 |
* the preprocessor to do... |
| 27 |
*/ |
| 28 |
#ifndef UT_TYPES_H |
| 29 |
#include "ut_types.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef HAVE_EXPAT |
| 33 |
#ifndef gchar |
| 34 |
typedef gchar gchar; |
| 35 |
#endif |
| 36 |
#include <expat.h> |
| 37 |
#else |
| 38 |
#define XML_Encoding void |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "ut_bijection.h" |
| 42 |
#include "ut_iconv.h" |
| 43 |
#include "ut_xml.h" |
| 44 |
|
| 45 |
struct ABI_EXPORT XAP_LangInfo |
| 46 |
{ |
| 47 |
/*no memeber can have NULL value. If string is empty, then value is |
| 48 |
not defined. All fields are strings to simplify searches. |
| 49 |
*/ |
| 50 |
enum fieldidx { longname_idx, /*this field is not empty*/ |
| 51 |
isoshortname_idx /*ISO*/, |
| 52 |
countrycode_idx, /*e.g. the "US" in "en-US" */ |
| 53 |
winlangcode_idx, /*0x400 + atoi() it to get a value*/ |
| 54 |
macname_idx, /*e.g. "langRussian" or empty*/ |
| 55 |
maclangcode_idx, /*atoi() it to get a value*/ |
| 56 |
max_idx = maclangcode_idx }; |
| 57 |
|
| 58 |
const char* fields[max_idx+1]; |
| 59 |
|
| 60 |
size_t outerQuoteIdx; |
| 61 |
size_t innerQuoteIdx; |
| 62 |
}; |
| 63 |
|
| 64 |
struct ABI_EXPORT XAP_SmartQuoteStyle |
| 65 |
{ |
| 66 |
UT_UCSChar leftQuote; |
| 67 |
UT_UCSChar rightQuote; |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
class ABI_EXPORT XAP_EncodingManager |
| 72 |
{ |
| 73 |
public: |
| 74 |
/* |
| 75 |
These shouldn't return NULL. Don't g_free or write to returned strings. |
| 76 |
The strings should be uppercased (extra font tarballs assume this). |
| 77 |
*/ |
| 78 |
virtual const char* getNativeEncodingName() const; |
| 79 |
virtual const char* getNativeSystemEncodingName() const; |
| 80 |
virtual const char* getNative8BitEncodingName() const; |
| 81 |
virtual const char* getNativeNonUnicodeEncodingName() const; |
| 82 |
|
| 83 |
/* |
| 84 |
These can return NULL. Don't g_free or write to returned strings. |
| 85 |
The strings should be uppercased (extra font tarballs assume this). |
| 86 |
*/ |
| 87 |
virtual const char* getNativeUnicodeEncodingName() const; |
| 88 |
virtual const char* getUCS2BEName() const; |
| 89 |
virtual const char* getUCS2LEName() const; |
| 90 |
virtual const char* getUCS4BEName() const; |
| 91 |
virtual const char* getUCS4LEName() const; |
| 92 |
|
| 93 |
/* |
| 94 |
This should return true for any Unicode locale: |
| 95 |
UTF-8 on *nix, UCS-2 on Windows, etc |
| 96 |
*/ |
| 97 |
inline virtual bool isUnicodeLocale() const {return m_bIsUnicodeLocale;} |
| 98 |
|
| 99 |
/* |
| 100 |
This shouldn't return NULL. Don't g_free or write to returned string. |
| 101 |
Returns ISO two-letter name like "en" |
| 102 |
*/ |
| 103 |
virtual const char* getLanguageISOName() const; |
| 104 |
|
| 105 |
/* |
| 106 |
This can return NULL. Don't g_free or write to returned string. |
| 107 |
Returns ISO two-letter territory name like "UK". |
| 108 |
*/ |
| 109 |
|
| 110 |
virtual const char* getLanguageISOTerritory() const; |
| 111 |
|
| 112 |
#if 0 |
| 113 |
/* |
| 114 |
for exporting to Tex - in order to provide proper argument for |
| 115 |
{inputenc}, e.g. \usepackage[koi8-r]{inputenc} |
| 116 |
If NULL is returned, then package 'inputenc' is not used at all. |
| 117 |
*/ |
| 118 |
virtual const char* getNativeTexEncodingName() const; |
| 119 |
#else |
| 120 |
virtual void placeholder() {};//to be removed |
| 121 |
#endif |
| 122 |
/* |
| 123 |
Should return "\n"-terminated prologue that loads required packages, |
| 124 |
etc. |
| 125 |
*/ |
| 126 |
virtual const char* getTexPrologue() const; |
| 127 |
|
| 128 |
/*these return 0 if they can't convert*/ |
| 129 |
/* |
| 130 |
This won't work for c>0xff. Use UT_Mbtowc! |
| 131 |
*/ |
| 132 |
virtual UT_UCSChar try_nativeToU(UT_UCSChar c) const; |
| 133 |
/* |
| 134 |
If returned value is > 0xff, then multibyte seq is returned by iconv |
| 135 |
and return value mean nothing (except that it notes that singlebyte |
| 136 |
encoding can't be used for this character. |
| 137 |
*/ |
| 138 |
virtual UT_UCSChar try_UToNative(UT_UCSChar c) const; |
| 139 |
virtual UT_UCSChar try_UToLatin1(UT_UCSChar c) const; |
| 140 |
|
| 141 |
/*these are used for reading/writing of doc and rtf files. */ |
| 142 |
virtual UT_UCSChar try_WindowsToU(UT_UCSChar c) const; |
| 143 |
virtual UT_UCSChar try_UToWindows(UT_UCSChar c) const; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
virtual char fallbackChar(UT_UCSChar c) const; |
| 149 |
static XAP_EncodingManager *get_instance(); |
| 150 |
void Delete_instance(); |
| 151 |
|
| 152 |
/* This tries to approximate the character with the string, e.g. |
| 153 |
horizontal-elipsis -> "...". Returns # of chars written or 0 if can't. |
| 154 |
The returned string will be in ascii (i.e. it will be representable |
| 155 |
in any encoding). |
| 156 |
If 'max_length' is 1, then approrixmation with exactly one character is |
| 157 |
requested. If 'max_length' is not 1, then it's rather large (e.g. 16) - |
| 158 |
so there is no need to check whether there is enough g_free space in the |
| 159 |
buffer. |
| 160 |
*/ |
| 161 |
virtual UT_uint32 approximate(char* out,UT_uint32 max_length,UT_UCSChar c) const; |
| 162 |
|
| 163 |
/* |
| 164 |
This should return 0 if it's unknown. Used only when exporting the |
| 165 |
document. |
| 166 |
*/ |
| 167 |
virtual UT_uint32 getWinLanguageCode() const; |
| 168 |
|
| 169 |
/* |
| 170 |
0 means Ascii. See _CHARSET macros (e.g RUSSIAN_CHARSET) in wingdi.h |
| 171 |
from Win32 SDK. Used only when exporting RTF. |
| 172 |
*/ |
| 173 |
virtual UT_uint32 getWinCharsetCode() const; |
| 174 |
|
| 175 |
/*can be called several times - e.g. by constructor of port-specific |
| 176 |
implementation. */ |
| 177 |
virtual void initialize(); |
| 178 |
/* |
| 179 |
returns 1 if current langauge is CJK (chinese, japanese, korean) |
| 180 |
*/ |
| 181 |
inline virtual bool cjk_locale() const { return is_cjk_; } |
| 182 |
|
| 183 |
/* |
| 184 |
returns true if there is no distinction between upper and lower |
| 185 |
letters. |
| 186 |
*/ |
| 187 |
virtual bool single_case() const; |
| 188 |
|
| 189 |
/* |
| 190 |
returns true if all letters are non-CJK. Under non-cjk locales |
| 191 |
it returns 1. Under cjk locales, returns 1 if all chars <0xff |
| 192 |
in that range. |
| 193 |
*/ |
| 194 |
virtual bool noncjk_letters(const UT_UCSChar* str,int len) const; |
| 195 |
|
| 196 |
/* |
| 197 |
* Returns true if a break between c[0] and c[1] is permissible. |
| 198 |
*/ |
| 199 |
virtual bool canBreakBetween(const UT_UCS4Char c[2]) const; |
| 200 |
|
| 201 |
/* |
| 202 |
This should be as precise as possible. |
| 203 |
*/ |
| 204 |
virtual bool is_cjk_letter(UT_UCSChar c) const; |
| 205 |
|
| 206 |
/* |
| 207 |
This is rather smart wrapper for wvLIDToCodePageConverter. |
| 208 |
Not all CP* are known by current iconv's, so this function |
| 209 |
will try first to return charset string that iconv knows. |
| 210 |
*/ |
| 211 |
virtual const char* charsetFromCodepage(int lid) const; |
| 212 |
|
| 213 |
/* |
| 214 |
This is convert charset to codepage. |
| 215 |
*/ |
| 216 |
virtual const char* CodepageFromCharset(const char *charset) const; |
| 217 |
|
| 218 |
/* |
| 219 |
returns charsetFromCodepage( getWinLanguageCode() ) |
| 220 |
*/ |
| 221 |
virtual const char* WindowsCharsetName() const; |
| 222 |
|
| 223 |
/* these use try_ methods, and if they fail, fallbackChar() is returned*/ |
| 224 |
UT_UCSChar nativeToU(UT_UCSChar c) const; |
| 225 |
UT_UCSChar UToNative(UT_UCSChar c) const; |
| 226 |
UT_UCSChar WindowsToU(UT_UCSChar c) const; |
| 227 |
UT_UCSChar UToWindows(UT_UCSChar c) const; |
| 228 |
|
| 229 |
/* |
| 230 |
this will convert the string 'in' from charset 'charset' to |
| 231 |
native charset. This is mostly shorthand function. |
| 232 |
it returns ptr to translated string - it will be either |
| 233 |
'in' or will be in static storage that shouldn't be freed. |
| 234 |
Allowed values for 'charset' include "" and NULL in which case |
| 235 |
'in' be returned. Don't ask to translate strings longer than 2K. |
| 236 |
This code is mostly used for translating localized menuitems to |
| 237 |
the proper charset. |
| 238 |
|
| 239 |
Of course it uses iconv internally. |
| 240 |
*/ |
| 241 |
const char* strToNative(const char* in,const char* charset,bool bReverse=false, bool bUseSysEncoding=false) const; |
| 242 |
|
| 243 |
/* |
| 244 |
Same as above, but it will use buffer provided by caller. |
| 245 |
*/ |
| 246 |
const char* strToNative(const char* in,const char* charset,char* buf,int bufsz, bool bReverse=false, bool bUseSysEncoding=false) const; |
| 247 |
|
| 248 |
/*this is used by code that reads xml using expat*/ |
| 249 |
static int XAP_XML_UnknownEncodingHandler(void *encodingHandlerData, |
| 250 |
const gchar *name, |
| 251 |
XML_Encoding *info); |
| 252 |
|
| 253 |
/*it's terminated with the record with NULL in language name. */ |
| 254 |
static const XAP_LangInfo langinfo[]; |
| 255 |
|
| 256 |
/*it's terminated with a record with all NULLs. */ |
| 257 |
static const XAP_SmartQuoteStyle smartQuoteStyles[]; |
| 258 |
|
| 259 |
/* |
| 260 |
Precise meaning: |
| 261 |
swap_utos: the following seq should produce a seq in buf that |
| 262 |
iconv will understand correctly when converting |
| 263 |
from UCS to mbs. |
| 264 |
unsigned short V; |
| 265 |
char buf[2]; |
| 266 |
b0 = V&0xff, b1 = V>>8; |
| 267 |
buf[swap_utos]=b0; |
| 268 |
buf[!swap_utos]=b1; |
| 269 |
swap_stou: the following seq should produce a correct value V |
| 270 |
that iconv will understand correctly when converting |
| 271 |
from mbs to UCS (i.e. return value). |
| 272 |
iconv(cd,&inptr,&inlen,&outptr,&outlen); |
| 273 |
unsigned short V; |
| 274 |
b0 = outptr_orig[swap_stou],b1 = outptr_orig[!swap_stou]; |
| 275 |
V = b0 | (b1<<8); |
| 276 |
*/ |
| 277 |
static bool swap_utos; |
| 278 |
static bool swap_stou; |
| 279 |
|
| 280 |
/* these are utility functions. Since all fields are strings, |
| 281 |
we can use the same routine. Returns NULL if nothing was found. */ |
| 282 |
static const XAP_LangInfo* findLangInfo(const char* key, |
| 283 |
XAP_LangInfo::fieldidx column); |
| 284 |
|
| 285 |
static const XAP_LangInfo* findLangInfoByLocale(const char* locale); |
| 286 |
|
| 287 |
/*word uses non-ascii names of fonts in .doc*/ |
| 288 |
static UT_Bijection cjk_word_fontname_mapping; |
| 289 |
/* CJK users need slightly different set of fontsizes*/ |
| 290 |
static UT_Bijection fontsizes_mapping; |
| 291 |
protected: |
| 292 |
void describe(); |
| 293 |
XAP_EncodingManager(); |
| 294 |
virtual ~XAP_EncodingManager(); |
| 295 |
|
| 296 |
private: |
| 297 |
static XAP_EncodingManager* _instance; |
| 298 |
|
| 299 |
const char* TexPrologue; |
| 300 |
UT_uint32 WinLanguageCode,WinCharsetCode; |
| 301 |
bool is_cjk_,m_bIsUnicodeLocale; |
| 302 |
}; |
| 303 |
|
| 304 |
/* |
| 305 |
This one returns NULL-terminated vector of strings in static buffers (i.e. |
| 306 |
don't try to g_free anything). On next call, filled data will be lost. |
| 307 |
returns the following strings surrounded by prefix and suffix: |
| 308 |
if (!skip_fallback) |
| 309 |
""; |
| 310 |
//next ones also include 'sep' to the left of them |
| 311 |
"%s" XAP_E..M..::instance->getLanguageISOName() |
| 312 |
"%s" XAP_E..M..::getNativeEncodingName() |
| 313 |
"%s-%s" XAP_E..M..::getLanguageISOName(),XAP_E..M..::getLanguageISOTerritory() |
| 314 |
"%s-%s.%s" XAP_E..M..::getLanguageISOName(), \ |
| 315 |
XAP_E..M..::getLanguageISOTerritory(), getNativeEncodingName() |
| 316 |
*/ |
| 317 |
const char** localeinfo_combinations(const char* prefix,const char* suffix,const char* sep, bool skip_fallback=0); |
| 318 |
|
| 319 |
/*these one are used by ispell*/ |
| 320 |
/* placate win32 compiler */ |
| 321 |
extern "C" { |
| 322 |
extern int XAP_EncodingManager__swap_stou,XAP_EncodingManager__swap_utos; |
| 323 |
const char * xap_encoding_manager_get_language_iso_name(void); |
| 324 |
} |
| 325 |
|
| 326 |
#endif /* XAP_ENCMGR_H */ |