Get the style color and number just once
[LibreOffice.git] / sal / textenc / tcvtbyte.cxx
blobee727d9f910fd623c5faf383b31619faa1a4b253
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <rtl/textcvt.h>
24 #include "handleundefinedunicodetotextchar.hxx"
25 #include "tcvtbyte.hxx"
26 #include "tenchelp.hxx"
28 sal_Size ImplSymbolToUnicode( SAL_UNUSED_PARAMETER const void*,
29 SAL_UNUSED_PARAMETER void*,
30 const char* pSrcBuf, sal_Size nSrcBytes,
31 sal_Unicode* pDestBuf, sal_Size nDestChars,
32 SAL_UNUSED_PARAMETER sal_uInt32,
33 sal_uInt32* pInfo, sal_Size* pSrcCvtBytes )
35 sal_Unicode* pEndDestBuf;
36 const char* pEndSrcBuf;
38 *pInfo = 0;
39 pEndDestBuf = pDestBuf+nDestChars;
40 pEndSrcBuf = pSrcBuf+nSrcBytes;
41 while ( pSrcBuf < pEndSrcBuf )
43 if ( pDestBuf == pEndDestBuf )
45 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
46 break;
49 /* 0-31 (all Control-Character get the same Unicode value) */
50 unsigned char c = static_cast<unsigned char>(*pSrcBuf);
51 if ( c <= 0x1F )
52 *pDestBuf = static_cast<sal_Unicode>(c);
53 else
54 *pDestBuf = static_cast<sal_Unicode>(c)+0xF000;
55 pDestBuf++;
56 pSrcBuf++;
59 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
60 return (nDestChars - (pEndDestBuf-pDestBuf));
63 sal_Size ImplUnicodeToSymbol( SAL_UNUSED_PARAMETER const void*,
64 SAL_UNUSED_PARAMETER void*,
65 const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
66 char* pDestBuf, sal_Size nDestBytes,
67 sal_uInt32 nFlags, sal_uInt32* pInfo,
68 sal_Size* pSrcCvtChars )
70 sal_Unicode c;
71 char* pEndDestBuf;
72 const sal_Unicode* pEndSrcBuf;
74 *pInfo = 0;
75 pEndDestBuf = pDestBuf+nDestBytes;
76 pEndSrcBuf = pSrcBuf+nSrcChars;
77 while ( pSrcBuf < pEndSrcBuf )
79 if ( pDestBuf == pEndDestBuf )
81 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
82 break;
85 c = *pSrcBuf;
86 if ( (c >= 0xF000) && (c <= 0xF0FF) )
88 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c-0xF000));
89 pDestBuf++;
90 pSrcBuf++;
92 // Normally 0x001F, but in many cases also symbol characters
93 // are stored in the first 256 bytes, so that we don't change
94 // these values
95 else if ( c <= 0x00FF )
97 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c));
98 pDestBuf++;
99 pSrcBuf++;
101 else
103 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE )
105 /* !!! */
106 /* Only ascii characters < 0x1F */
109 /* Handle undefined and surrogates characters */
110 /* (all surrogates characters are undefined) */
111 if (!sal::detail::textenc::handleUndefinedUnicodeToTextChar(
112 &pSrcBuf, pEndSrcBuf, &pDestBuf, pEndDestBuf, nFlags,
113 pInfo))
114 break;
118 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
119 return (nDestBytes - (pEndDestBuf-pDestBuf));
122 sal_Size ImplUpperCharToUnicode( const void* pData,
123 SAL_UNUSED_PARAMETER void*,
124 const char* pSrcBuf, sal_Size nSrcBytes,
125 sal_Unicode* pDestBuf, sal_Size nDestChars,
126 SAL_UNUSED_PARAMETER sal_uInt32, sal_uInt32* pInfo,
127 sal_Size* pSrcCvtBytes )
129 sal_Unicode cConv;
130 const ImplByteConvertData* pConvertData = static_cast<const ImplByteConvertData*>(pData);
131 sal_Unicode* pEndDestBuf;
132 const char* pEndSrcBuf;
134 *pInfo = 0;
135 pEndDestBuf = pDestBuf+nDestChars;
136 pEndSrcBuf = pSrcBuf+nSrcBytes;
137 if ( pDestBuf == pEndDestBuf )
139 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
140 *pSrcCvtBytes = 0;
141 return 0;
143 while ( pSrcBuf < pEndSrcBuf )
145 unsigned char c = static_cast<unsigned char>(*pSrcBuf);
146 if (c < 0x80)
147 cConv = c;
148 else
149 // c <= 0xFF is implied.
150 cConv = pConvertData->mpToUniTab1[c - 0x80];
152 *pDestBuf = cConv;
153 pDestBuf++;
154 pSrcBuf++;
157 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
158 return (nDestChars - (pEndDestBuf-pDestBuf));
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */