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