1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "rtl/textcvt.h"
#include "xlat.hxx"
#include <tools/debug.hxx>
namespace {
#define MAX_CVT_SELECT 6
class ConverterCache
{
public:
explicit ConverterCache( void );
~ConverterCache( void );
sal_uInt16 convertOne( int nSelect, sal_Unicode );
void convertStr( int nSelect, const sal_Unicode* pSrc, sal_uInt16* pDst, int nCount );
protected:
void ensureConverter( int nSelect );
private:
rtl_UnicodeToTextConverter maConverterCache[ MAX_CVT_SELECT+1 ];
rtl_UnicodeToTextContext maContexts[ MAX_CVT_SELECT+1 ];
};
ConverterCache::ConverterCache( void)
{
for( int i = 0; i <= MAX_CVT_SELECT; ++i)
{
maConverterCache[i] = NULL;
maContexts[i] = NULL;
}
}
ConverterCache::~ConverterCache( void)
{
for( int i = 0; i <= MAX_CVT_SELECT; ++i)
{
if( !maContexts[i] )
continue;
rtl_destroyUnicodeToTextContext( maConverterCache[i], maContexts[i] );
rtl_destroyUnicodeToTextConverter( maConverterCache[i] );
}
}
void ConverterCache::ensureConverter( int nSelect )
{
// DBG_ASSERT( (2<=nSelect) && (nSelect<=MAX_CVT_SELECT)), "invalid XLAT.Converter requested" );
rtl_UnicodeToTextContext aContext = maContexts[ nSelect ];
if( !aContext )
{
rtl_TextEncoding eRecodeFrom = RTL_TEXTENCODING_UNICODE;
switch( nSelect )
{
default: nSelect = 1; // fall through to unicode recoding
case 1: eRecodeFrom = RTL_TEXTENCODING_UNICODE; break;
case 2: eRecodeFrom = RTL_TEXTENCODING_SHIFT_JIS; break;
case 3: eRecodeFrom = RTL_TEXTENCODING_GB_2312; break;
case 4: eRecodeFrom = RTL_TEXTENCODING_BIG5; break;
case 5: eRecodeFrom = RTL_TEXTENCODING_MS_949; break;
case 6: eRecodeFrom = RTL_TEXTENCODING_MS_1361; break;
}
rtl_UnicodeToTextConverter aRecodeConverter = rtl_createUnicodeToTextConverter( eRecodeFrom );
maConverterCache[ nSelect ] = aRecodeConverter;
aContext = rtl_createUnicodeToTextContext( aRecodeConverter );
maContexts[ nSelect ] = aContext;
}
rtl_resetUnicodeToTextContext( maConverterCache[ nSelect ], aContext );
}
sal_uInt16 ConverterCache::convertOne( int nSelect, sal_Unicode aChar )
{
ensureConverter( nSelect );
sal_Unicode aUCS2Char = aChar;
sal_Char aTempArray[8];
sal_Size nTempSize;
sal_uInt32 nCvtInfo;
// TODO: use direct unicode->mbcs converter should there ever be one
int nCodeLen = rtl_convertUnicodeToText(
maConverterCache[ nSelect ], maContexts[ nSelect ],
&aUCS2Char, 1, aTempArray, sizeof(aTempArray),
RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
| RTL_UNICODETOTEXT_FLAGS_INVALID_0,
&nCvtInfo, &nTempSize );
sal_uInt16 aCode = aTempArray[0];
for( int i = 1; i < nCodeLen; ++i )
aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
return aCode;
}
void ConverterCache::convertStr( int nSelect, const sal_Unicode* pSrc, sal_uInt16* pDst, int nCount )
{
ensureConverter( nSelect );
for( int n = 0; n < nCount; ++n )
{
sal_Unicode aUCS2Char = pSrc[n];
sal_Char aTempArray[8];
sal_Size nTempSize;
sal_uInt32 nCvtInfo;
// assume that non-unicode-fonts do not support codepoints >U+FFFF
// TODO: use direct unicode->mbcs converter should there ever be one
int nCodeLen = rtl_convertUnicodeToText(
maConverterCache[ nSelect ], maContexts[ nSelect ],
&aUCS2Char, 1, aTempArray, sizeof(aTempArray),
RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
| RTL_UNICODETOTEXT_FLAGS_INVALID_0,
&nCvtInfo, &nTempSize );
sal_uInt16 aCode = aTempArray[0];
for( int i = 1; i < nCodeLen; ++i )
aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
pDst[n] = aCode;
}
}
} // anonymous namespace
namespace vcl
{
static ConverterCache aCC;
sal_uInt16 TranslateChar12(sal_uInt16 src)
{
return aCC.convertOne( 2, src);
}
sal_uInt16 TranslateChar13(sal_uInt16 src)
{
return aCC.convertOne( 3, src);
}
sal_uInt16 TranslateChar14(sal_uInt16 src)
{
return aCC.convertOne( 4, src);
}
sal_uInt16 TranslateChar15(sal_uInt16 src)
{
return aCC.convertOne( 5, src);
}
sal_uInt16 TranslateChar16(sal_uInt16 src)
{
return aCC.convertOne( 6, src);
}
void TranslateString12(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
{
aCC.convertStr( 2, src, dst, n);
}
void TranslateString13(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
{
aCC.convertStr( 3, src, dst, n);
}
void TranslateString14(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
{
aCC.convertStr( 4, src, dst, n);
}
void TranslateString15(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
{
aCC.convertStr( 5, src, dst, n);
}
void TranslateString16(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
{
aCC.convertStr( 6, src, dst, n);
}
} // namespace vcl
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|