tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / sal / textenc / handleundefinedunicodetotextchar.cxx
blob78b2464b70c6a4eea904da245be51b05d0e17a98
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <rtl/character.hxx>
13 #include <rtl/textcvt.h>
14 #include <sal/types.h>
16 #include "handleundefinedunicodetotextchar.hxx"
17 #include "tenchelp.hxx"
19 namespace {
21 bool ImplIsUnicodeIgnoreChar(sal_Unicode c, sal_uInt32 nFlags)
23 return
24 ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0
25 && ImplIsZeroWidth(c))
26 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0
27 && ImplIsControlOrFormat(c))
28 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0
29 && ImplIsPrivateUse(c));
32 bool ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags,
33 char * pBuf,
34 sal_Size nMaxLen)
36 if (nMaxLen == 0)
37 return false;
38 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
40 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
41 *pBuf = 0x00;
42 break;
44 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
45 default: /* RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT */
46 *pBuf = 0x3F;
47 break;
49 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
50 *pBuf = 0x5F;
51 break;
53 return true;
56 bool ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags,
57 char * pBuf,
58 sal_Size nMaxLen)
60 if (nMaxLen == 0)
61 return false;
62 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK)
64 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
65 *pBuf = 0x00;
66 break;
68 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
69 default: /* RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT */
70 *pBuf = 0x3F;
71 break;
73 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
74 *pBuf = 0x5F;
75 break;
77 return true;
82 bool sal::detail::textenc::handleUndefinedUnicodeToTextChar(
83 sal_Unicode const ** ppSrcBuf, sal_Unicode const * pEndSrcBuf,
84 char ** ppDestBuf, char const * pEndDestBuf, sal_uInt32 nFlags,
85 sal_uInt32 * pInfo)
87 sal_Unicode c = **ppSrcBuf;
89 /* Should the private character map to one byte */
90 if ( (c >= RTL_TEXTCVT_BYTE_PRIVATE_START) && (c <= RTL_TEXTCVT_BYTE_PRIVATE_END) )
92 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0 )
94 **ppDestBuf = static_cast<char>(static_cast<unsigned char>(c-RTL_TEXTCVT_BYTE_PRIVATE_START));
95 (*ppDestBuf)++;
96 (*ppSrcBuf)++;
97 return true;
101 /* Should this character ignored (Private, Non Spacing, Control) */
102 if ( ImplIsUnicodeIgnoreChar( c, nFlags ) )
104 (*ppSrcBuf)++;
105 return true;
108 /* Surrogates Characters should result in */
109 /* one replacement character */
110 if (rtl::isHighSurrogate(c))
112 if ( ((*ppSrcBuf) + 1) == pEndSrcBuf )
114 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
115 return false;
118 c = *((*ppSrcBuf)+1);
119 if (rtl::isLowSurrogate(c))
120 (*ppSrcBuf)++;
121 else
123 *pInfo |= RTL_UNICODETOTEXT_INFO_INVALID;
124 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR )
126 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
127 return false;
129 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE )
131 (*ppSrcBuf)++;
132 return true;
134 if (ImplGetInvalidAsciiMultiByte(nFlags,
135 *ppDestBuf,
136 pEndDestBuf - *ppDestBuf))
138 ++*ppSrcBuf;
139 ++*ppDestBuf;
140 return true;
142 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
143 return false;
147 *pInfo |= RTL_UNICODETOTEXT_INFO_UNDEFINED;
148 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR )
150 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
151 return false;
153 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE )
154 (*ppSrcBuf)++;
155 else if (ImplGetUndefinedAsciiMultiByte(nFlags,
156 *ppDestBuf,
157 pEndDestBuf - *ppDestBuf))
159 ++*ppSrcBuf;
160 ++*ppDestBuf;
162 else
164 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
165 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
166 return false;
169 return true;
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */