1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "rtl/textcvt.h"
29 #include <tools/debug.hxx>
31 namespace { // anonymous namespace
33 // ====================================================================
35 #define MAX_CVT_SELECT 6
40 explicit ConverterCache( void );
41 ~ConverterCache( void );
42 sal_uInt16
convertOne( int nSelect
, sal_Unicode
);
43 void convertStr( int nSelect
, const sal_Unicode
* pSrc
, sal_uInt16
* pDst
, int nCount
);
45 void ensureConverter( int nSelect
);
47 rtl_UnicodeToTextConverter maConverterCache
[ MAX_CVT_SELECT
+1 ];
48 rtl_UnicodeToTextContext maContexts
[ MAX_CVT_SELECT
+1 ];
51 // ====================================================================
53 ConverterCache::ConverterCache( void)
55 for( int i
= 0; i
<= MAX_CVT_SELECT
; ++i
)
57 maConverterCache
[i
] = NULL
;
62 // --------------------------------------------------------------------
64 ConverterCache::~ConverterCache( void)
66 for( int i
= 0; i
<= MAX_CVT_SELECT
; ++i
)
70 rtl_destroyUnicodeToTextContext( maConverterCache
[i
], maContexts
[i
] );
71 rtl_destroyUnicodeToTextConverter( maConverterCache
[i
] );
75 // --------------------------------------------------------------------
77 void ConverterCache::ensureConverter( int nSelect
)
79 // DBG_ASSERT( (2<=nSelect) && (nSelect<=MAX_CVT_SELECT)), "invalid XLAT.Converter requested" );
80 rtl_UnicodeToTextContext aContext
= maContexts
[ nSelect
];
83 rtl_TextEncoding eRecodeFrom
= RTL_TEXTENCODING_UNICODE
;
86 default: nSelect
= 1; // fall through to unicode recoding
87 case 1: eRecodeFrom
= RTL_TEXTENCODING_UNICODE
; break;
88 case 2: eRecodeFrom
= RTL_TEXTENCODING_SHIFT_JIS
; break;
89 case 3: eRecodeFrom
= RTL_TEXTENCODING_GB_2312
; break;
90 case 4: eRecodeFrom
= RTL_TEXTENCODING_BIG5
; break;
91 case 5: eRecodeFrom
= RTL_TEXTENCODING_MS_949
; break;
92 case 6: eRecodeFrom
= RTL_TEXTENCODING_MS_1361
; break;
94 rtl_UnicodeToTextConverter aRecodeConverter
= rtl_createUnicodeToTextConverter( eRecodeFrom
);
95 maConverterCache
[ nSelect
] = aRecodeConverter
;
97 aContext
= rtl_createUnicodeToTextContext( aRecodeConverter
);
98 maContexts
[ nSelect
] = aContext
;
101 rtl_resetUnicodeToTextContext( maConverterCache
[ nSelect
], aContext
);
104 // --------------------------------------------------------------------
106 sal_uInt16
ConverterCache::convertOne( int nSelect
, sal_Unicode aChar
)
108 ensureConverter( nSelect
);
110 sal_Unicode aUCS2Char
= aChar
;
111 sal_Char aTempArray
[8];
115 // TODO: use direct unicode->mbcs converter should there ever be one
116 int nCodeLen
= rtl_convertUnicodeToText(
117 maConverterCache
[ nSelect
], maContexts
[ nSelect
],
118 &aUCS2Char
, 1, aTempArray
, sizeof(aTempArray
),
119 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
120 | RTL_UNICODETOTEXT_FLAGS_INVALID_0
,
121 &nCvtInfo
, &nTempSize
);
123 sal_uInt16 aCode
= aTempArray
[0];
124 for( int i
= 1; i
< nCodeLen
; ++i
)
125 aCode
= (aCode
<< 8) + (aTempArray
[i
] & 0xFF);
129 // --------------------------------------------------------------------
131 void ConverterCache::convertStr( int nSelect
, const sal_Unicode
* pSrc
, sal_uInt16
* pDst
, int nCount
)
133 ensureConverter( nSelect
);
135 for( int n
= 0; n
< nCount
; ++n
)
137 sal_Unicode aUCS2Char
= pSrc
[n
];
139 sal_Char aTempArray
[8];
143 // assume that non-unicode-fonts do not support codepoints >U+FFFF
144 // TODO: use direct unicode->mbcs converter should there ever be one
145 int nCodeLen
= rtl_convertUnicodeToText(
146 maConverterCache
[ nSelect
], maContexts
[ nSelect
],
147 &aUCS2Char
, 1, aTempArray
, sizeof(aTempArray
),
148 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
149 | RTL_UNICODETOTEXT_FLAGS_INVALID_0
,
150 &nCvtInfo
, &nTempSize
);
152 sal_uInt16 aCode
= aTempArray
[0];
153 for( int i
= 1; i
< nCodeLen
; ++i
)
154 aCode
= (aCode
<< 8) + (aTempArray
[i
] & 0xFF);
159 } // anonymous namespace
161 // ====================================================================
168 static ConverterCache aCC
;
170 sal_uInt16
TranslateChar12(sal_uInt16 src
)
172 return aCC
.convertOne( 2, src
);
175 sal_uInt16
TranslateChar13(sal_uInt16 src
)
177 return aCC
.convertOne( 3, src
);
180 sal_uInt16
TranslateChar14(sal_uInt16 src
)
182 return aCC
.convertOne( 4, src
);
185 sal_uInt16
TranslateChar15(sal_uInt16 src
)
187 return aCC
.convertOne( 5, src
);
190 sal_uInt16
TranslateChar16(sal_uInt16 src
)
192 return aCC
.convertOne( 6, src
);
195 void TranslateString12(sal_uInt16
*src
, sal_uInt16
*dst
, sal_uInt32 n
)
197 aCC
.convertStr( 2, src
, dst
, n
);
200 void TranslateString13(sal_uInt16
*src
, sal_uInt16
*dst
, sal_uInt32 n
)
202 aCC
.convertStr( 3, src
, dst
, n
);
205 void TranslateString14(sal_uInt16
*src
, sal_uInt16
*dst
, sal_uInt32 n
)
207 aCC
.convertStr( 4, src
, dst
, n
);
210 void TranslateString15(sal_uInt16
*src
, sal_uInt16
*dst
, sal_uInt32 n
)
212 aCC
.convertStr( 5, src
, dst
, n
);
215 void TranslateString16(sal_uInt16
*src
, sal_uInt16
*dst
, sal_uInt32 n
)
217 aCC
.convertStr( 6, src
, dst
, n
);