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