1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 #include "rtl/textcvt.h"
31 #include "sal/types.h"
33 static sal_Bool
ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags
,
37 static sal_Bool
ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags
,
41 static int ImplIsUnicodeIgnoreChar(sal_Unicode c
, sal_uInt32 nFlags
);
43 sal_Bool
ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags
,
49 switch (nFlags
& RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK
)
51 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
:
55 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
:
56 default: /* RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT */
60 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE
:
67 sal_Bool
ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags
,
73 switch (nFlags
& RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK
)
75 case RTL_UNICODETOTEXT_FLAGS_INVALID_0
:
79 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK
:
80 default: /* RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT */
84 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE
:
91 int ImplIsUnicodeIgnoreChar( sal_Unicode c
, sal_uInt32 nFlags
)
94 ((nFlags
& RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE
) != 0
95 && ImplIsZeroWidth(c
))
96 || ((nFlags
& RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE
) != 0
97 && ImplIsControlOrFormat(c
))
98 || ((nFlags
& RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE
) != 0
99 && ImplIsPrivateUse(c
));
102 /* ======================================================================= */
104 sal_Unicode
ImplGetUndefinedUnicodeChar(sal_uChar cChar
, sal_uInt32 nFlags
)
106 return ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK
)
107 == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE
) ?
108 RTL_TEXTCVT_BYTE_PRIVATE_START
+ cChar
:
109 RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER
;
112 /* ----------------------------------------------------------------------- */
115 ImplHandleUndefinedUnicodeToTextChar(ImplTextConverterData
const * pData
,
116 sal_Unicode
const ** ppSrcBuf
,
117 sal_Unicode
const * pEndSrcBuf
,
118 sal_Char
** ppDestBuf
,
119 sal_Char
const * pEndDestBuf
,
123 sal_Unicode c
= **ppSrcBuf
;
125 (void) pData
; /* unused */
127 /* Should the private character map to one byte */
128 if ( (c
>= RTL_TEXTCVT_BYTE_PRIVATE_START
) && (c
<= RTL_TEXTCVT_BYTE_PRIVATE_END
) )
130 if ( nFlags
& RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0
)
132 **ppDestBuf
= (sal_Char
)(sal_uChar
)(c
-RTL_TEXTCVT_BYTE_PRIVATE_START
);
139 /* Should this character ignored (Private, Non Spacing, Control) */
140 if ( ImplIsUnicodeIgnoreChar( c
, nFlags
) )
146 /* Surrogates Characters should result in */
147 /* one replacement character */
148 if (ImplIsHighSurrogate(c
))
150 if ( *ppSrcBuf
== pEndSrcBuf
)
152 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
| RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL
;
156 c
= *((*ppSrcBuf
)+1);
157 if (ImplIsLowSurrogate(c
))
161 *pInfo
|= RTL_UNICODETOTEXT_INFO_INVALID
;
162 if ( (nFlags
& RTL_UNICODETOTEXT_FLAGS_INVALID_MASK
) == RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
)
164 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
;
167 else if ( (nFlags
& RTL_UNICODETOTEXT_FLAGS_INVALID_MASK
) == RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE
)
172 else if (ImplGetInvalidAsciiMultiByte(nFlags
,
174 pEndDestBuf
- *ppDestBuf
))
182 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
183 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
189 *pInfo
|= RTL_UNICODETOTEXT_INFO_UNDEFINED
;
190 if ( (nFlags
& RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK
) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
)
192 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
;
195 else if ( (nFlags
& RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK
) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE
)
197 else if (ImplGetUndefinedAsciiMultiByte(nFlags
,
199 pEndDestBuf
- *ppDestBuf
))
206 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
207 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;