xref: /aoo42x/main/vcl/source/fontsubset/xlat.cxx (revision 3935342b)
19f62ea84SAndrew Rist /**************************************************************
2*3935342bSmseidel  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*3935342bSmseidel  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*3935342bSmseidel  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
19*3935342bSmseidel  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "rtl/textcvt.h"
25cdf0e10cSrcweir #include <tools/debug.hxx>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir namespace { // anonymous namespace
28cdf0e10cSrcweir 
29cdf0e10cSrcweir // ====================================================================
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #define MAX_CVT_SELECT 6
32cdf0e10cSrcweir 
33cdf0e10cSrcweir class ConverterCache
34cdf0e10cSrcweir {
35cdf0e10cSrcweir public:
36cdf0e10cSrcweir 	explicit	ConverterCache( void );
37cdf0e10cSrcweir 				~ConverterCache( void );
38cdf0e10cSrcweir 	sal_uInt16	convertOne( int nSelect, sal_Unicode );
39cdf0e10cSrcweir 	void		convertStr( int nSelect, const sal_Unicode* pSrc, sal_uInt16* pDst, int nCount );
40cdf0e10cSrcweir protected:
41cdf0e10cSrcweir 	void		ensureConverter( int nSelect );
42cdf0e10cSrcweir private:
43cdf0e10cSrcweir 	rtl_UnicodeToTextConverter maConverterCache[ MAX_CVT_SELECT+1 ];
44cdf0e10cSrcweir 	rtl_UnicodeToTextContext maContexts[ MAX_CVT_SELECT+1 ];
45cdf0e10cSrcweir };
46cdf0e10cSrcweir 
47cdf0e10cSrcweir // ====================================================================
48cdf0e10cSrcweir 
ConverterCache(void)49cdf0e10cSrcweir ConverterCache::ConverterCache( void)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	for( int i = 0; i <= MAX_CVT_SELECT; ++i)
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		maConverterCache[i] = NULL;
54cdf0e10cSrcweir 		maContexts[i] = NULL;
55cdf0e10cSrcweir 	}
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // --------------------------------------------------------------------
59cdf0e10cSrcweir 
~ConverterCache(void)60cdf0e10cSrcweir ConverterCache::~ConverterCache( void)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	for( int i = 0; i <= MAX_CVT_SELECT; ++i)
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 		if( !maContexts[i] )
65cdf0e10cSrcweir 			continue;
66cdf0e10cSrcweir 		rtl_destroyUnicodeToTextContext( maConverterCache[i], maContexts[i] );
67cdf0e10cSrcweir 		rtl_destroyUnicodeToTextConverter( maConverterCache[i] );
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir // --------------------------------------------------------------------
72cdf0e10cSrcweir 
ensureConverter(int nSelect)73cdf0e10cSrcweir void ConverterCache::ensureConverter( int nSelect )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 	// DBG_ASSERT( (2<=nSelect) && (nSelect<=MAX_CVT_SELECT)), "invalid XLAT.Converter requested" );
76cdf0e10cSrcweir 	rtl_UnicodeToTextContext aContext = maContexts[ nSelect ];
77cdf0e10cSrcweir 	if( !aContext )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		rtl_TextEncoding eRecodeFrom = RTL_TEXTENCODING_UNICODE;
80cdf0e10cSrcweir 		switch( nSelect )
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			default: nSelect = 1; // fall through to unicode recoding
83cdf0e10cSrcweir 			case 1: eRecodeFrom = RTL_TEXTENCODING_UNICODE; break;
84cdf0e10cSrcweir 			case 2: eRecodeFrom = RTL_TEXTENCODING_SHIFT_JIS; break;
85cdf0e10cSrcweir 			case 3: eRecodeFrom = RTL_TEXTENCODING_GB_2312; break;
86cdf0e10cSrcweir 			case 4: eRecodeFrom = RTL_TEXTENCODING_BIG5; break;
87cdf0e10cSrcweir 			case 5: eRecodeFrom = RTL_TEXTENCODING_MS_949; break;
88cdf0e10cSrcweir 			case 6: eRecodeFrom = RTL_TEXTENCODING_MS_1361; break;
89cdf0e10cSrcweir 		}
90cdf0e10cSrcweir 		rtl_UnicodeToTextConverter aRecodeConverter = rtl_createUnicodeToTextConverter( eRecodeFrom );
91cdf0e10cSrcweir 		maConverterCache[ nSelect ] = aRecodeConverter;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 		aContext = rtl_createUnicodeToTextContext( aRecodeConverter );
94cdf0e10cSrcweir 		maContexts[ nSelect ] = aContext;
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	rtl_resetUnicodeToTextContext( maConverterCache[ nSelect ], aContext );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir // --------------------------------------------------------------------
101cdf0e10cSrcweir 
convertOne(int nSelect,sal_Unicode aChar)102cdf0e10cSrcweir sal_uInt16 ConverterCache::convertOne( int nSelect, sal_Unicode aChar )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	ensureConverter( nSelect );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	sal_Unicode aUCS2Char = aChar;
107cdf0e10cSrcweir 	sal_Char aTempArray[8];
108cdf0e10cSrcweir 	sal_Size nTempSize;
109cdf0e10cSrcweir 	sal_uInt32 nCvtInfo;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	// TODO: use direct unicode->mbcs converter should there ever be one
112cdf0e10cSrcweir 	int nCodeLen = rtl_convertUnicodeToText(
113cdf0e10cSrcweir 			maConverterCache[ nSelect ], maContexts[ nSelect ],
114cdf0e10cSrcweir 			&aUCS2Char, 1, aTempArray, sizeof(aTempArray),
115cdf0e10cSrcweir 			RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
116cdf0e10cSrcweir 			| RTL_UNICODETOTEXT_FLAGS_INVALID_0,
117cdf0e10cSrcweir 			&nCvtInfo, &nTempSize );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	sal_uInt16 aCode = aTempArray[0];
120cdf0e10cSrcweir 	for( int i = 1; i < nCodeLen; ++i )
121cdf0e10cSrcweir 		aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
122cdf0e10cSrcweir 	return aCode;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // --------------------------------------------------------------------
126cdf0e10cSrcweir 
convertStr(int nSelect,const sal_Unicode * pSrc,sal_uInt16 * pDst,int nCount)127cdf0e10cSrcweir void ConverterCache::convertStr( int nSelect, const sal_Unicode* pSrc, sal_uInt16* pDst, int nCount )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	ensureConverter( nSelect );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	for( int n = 0; n < nCount; ++n )
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir 		sal_Unicode aUCS2Char = pSrc[n];
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 		sal_Char aTempArray[8];
136cdf0e10cSrcweir 		sal_Size nTempSize;
137cdf0e10cSrcweir 		sal_uInt32 nCvtInfo;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 		// assume that non-unicode-fonts do not support codepoints >U+FFFF
140cdf0e10cSrcweir 		// TODO: use direct unicode->mbcs converter should there ever be one
141cdf0e10cSrcweir 		int nCodeLen = rtl_convertUnicodeToText(
142cdf0e10cSrcweir 			maConverterCache[ nSelect ], maContexts[ nSelect ],
143cdf0e10cSrcweir 			&aUCS2Char, 1, aTempArray, sizeof(aTempArray),
144cdf0e10cSrcweir 			RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
145cdf0e10cSrcweir 			| RTL_UNICODETOTEXT_FLAGS_INVALID_0,
146cdf0e10cSrcweir 			&nCvtInfo, &nTempSize );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		sal_uInt16 aCode = aTempArray[0];
149cdf0e10cSrcweir 		for( int i = 1; i < nCodeLen; ++i )
150cdf0e10cSrcweir 			aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
151cdf0e10cSrcweir 		pDst[n] = aCode;
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir } // anonymous namespace
156cdf0e10cSrcweir 
157cdf0e10cSrcweir // ====================================================================
158cdf0e10cSrcweir 
159cdf0e10cSrcweir #include "xlat.hxx"
160cdf0e10cSrcweir 
161cdf0e10cSrcweir namespace vcl
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 
164cdf0e10cSrcweir static ConverterCache aCC;
165cdf0e10cSrcweir 
TranslateChar12(sal_uInt16 src)166cdf0e10cSrcweir sal_uInt16 TranslateChar12(sal_uInt16 src)
167cdf0e10cSrcweir {
168*3935342bSmseidel 	return aCC.convertOne( 2, src);
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
TranslateChar13(sal_uInt16 src)171cdf0e10cSrcweir sal_uInt16 TranslateChar13(sal_uInt16 src)
172cdf0e10cSrcweir {
173*3935342bSmseidel 	return aCC.convertOne( 3, src);
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
TranslateChar14(sal_uInt16 src)176cdf0e10cSrcweir sal_uInt16 TranslateChar14(sal_uInt16 src)
177cdf0e10cSrcweir {
178*3935342bSmseidel 	return aCC.convertOne( 4, src);
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
TranslateChar15(sal_uInt16 src)181cdf0e10cSrcweir sal_uInt16 TranslateChar15(sal_uInt16 src)
182cdf0e10cSrcweir {
183*3935342bSmseidel 	return aCC.convertOne( 5, src);
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
TranslateChar16(sal_uInt16 src)186cdf0e10cSrcweir sal_uInt16 TranslateChar16(sal_uInt16 src)
187cdf0e10cSrcweir {
188*3935342bSmseidel 	return aCC.convertOne( 6, src);
189cdf0e10cSrcweir }
190*3935342bSmseidel 
TranslateString12(sal_uInt16 * src,sal_uInt16 * dst,sal_uInt32 n)191cdf0e10cSrcweir void TranslateString12(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	aCC.convertStr( 2, src, dst, n);
194cdf0e10cSrcweir }
195*3935342bSmseidel 
TranslateString13(sal_uInt16 * src,sal_uInt16 * dst,sal_uInt32 n)196cdf0e10cSrcweir void TranslateString13(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir 	aCC.convertStr( 3, src, dst, n);
199cdf0e10cSrcweir }
200*3935342bSmseidel 
TranslateString14(sal_uInt16 * src,sal_uInt16 * dst,sal_uInt32 n)201cdf0e10cSrcweir void TranslateString14(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir 	aCC.convertStr( 4, src, dst, n);
204cdf0e10cSrcweir }
205*3935342bSmseidel 
TranslateString15(sal_uInt16 * src,sal_uInt16 * dst,sal_uInt32 n)206cdf0e10cSrcweir void TranslateString15(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir 	aCC.convertStr( 5, src, dst, n);
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
TranslateString16(sal_uInt16 * src,sal_uInt16 * dst,sal_uInt32 n)211cdf0e10cSrcweir void TranslateString16(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	aCC.convertStr( 6, src, dst, n);
214cdf0e10cSrcweir }
215*3935342bSmseidel 
216cdf0e10cSrcweir } // namespace vcl
217cdf0e10cSrcweir 
218*3935342bSmseidel /* vim: set noet sw=4 ts=4: */
219