1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/textcvt.h>
25 #define MAX_CVT_SELECT 6
30 explicit ConverterCache();
32 sal_uInt16
convertOne( int nSelect
, sal_Unicode
);
34 void ensureConverter( int nSelect
);
35 rtl_UnicodeToTextConverter maConverterCache
[ MAX_CVT_SELECT
+1 ];
36 rtl_UnicodeToTextContext maContexts
[ MAX_CVT_SELECT
+1 ];
39 ConverterCache::ConverterCache()
41 for( int i
= 0; i
<= MAX_CVT_SELECT
; ++i
)
43 maConverterCache
[i
] = nullptr;
44 maContexts
[i
] = nullptr;
48 ConverterCache::~ConverterCache()
50 for( int i
= 0; i
<= MAX_CVT_SELECT
; ++i
)
54 rtl_destroyUnicodeToTextContext( maConverterCache
[i
], maContexts
[i
] );
55 rtl_destroyUnicodeToTextConverter( maConverterCache
[i
] );
59 void ConverterCache::ensureConverter( int nSelect
)
61 // SAL_WARN_IF( (2>nSelect) || (nSelect>MAX_CVT_SELECT)), "vcl", "invalid XLAT.Converter requested" );
62 rtl_UnicodeToTextContext aContext
= maContexts
[ nSelect
];
65 rtl_TextEncoding eRecodeFrom
= RTL_TEXTENCODING_UNICODE
;
68 default: nSelect
= 1; [[fallthrough
]]; // to unicode recoding
69 case 1: eRecodeFrom
= RTL_TEXTENCODING_UNICODE
; break;
70 case 2: eRecodeFrom
= RTL_TEXTENCODING_SHIFT_JIS
; break;
71 case 3: eRecodeFrom
= RTL_TEXTENCODING_GB_2312
; break;
72 case 4: eRecodeFrom
= RTL_TEXTENCODING_BIG5
; break;
73 case 5: eRecodeFrom
= RTL_TEXTENCODING_MS_949
; break;
74 case 6: eRecodeFrom
= RTL_TEXTENCODING_MS_1361
; break;
76 rtl_UnicodeToTextConverter aRecodeConverter
= rtl_createUnicodeToTextConverter( eRecodeFrom
);
77 maConverterCache
[ nSelect
] = aRecodeConverter
;
79 aContext
= rtl_createUnicodeToTextContext( aRecodeConverter
);
80 maContexts
[ nSelect
] = aContext
;
83 rtl_resetUnicodeToTextContext( maConverterCache
[ nSelect
], aContext
);
86 sal_uInt16
ConverterCache::convertOne( int nSelect
, sal_Unicode aChar
)
88 ensureConverter( nSelect
);
90 sal_Unicode aUCS2Char
= aChar
;
91 sal_Char aTempArray
[8];
95 // TODO: use direct unicode->mbcs converter should there ever be one
96 int nCodeLen
= rtl_convertUnicodeToText(
97 maConverterCache
[ nSelect
], maContexts
[ nSelect
],
98 &aUCS2Char
, 1, aTempArray
, sizeof(aTempArray
),
99 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
100 | RTL_UNICODETOTEXT_FLAGS_INVALID_0
,
101 &nCvtInfo
, &nTempSize
);
103 sal_uInt16 aCode
= aTempArray
[0];
104 for( int i
= 1; i
< nCodeLen
; ++i
)
105 aCode
= (aCode
<< 8) + (aTempArray
[i
] & 0xFF);
109 } // anonymous namespace
114 static ConverterCache aCC
;
116 sal_uInt16
TranslateChar12(sal_uInt16 src
)
118 return aCC
.convertOne( 2, src
);
121 sal_uInt16
TranslateChar13(sal_uInt16 src
)
123 return aCC
.convertOne( 3, src
);
126 sal_uInt16
TranslateChar14(sal_uInt16 src
)
128 return aCC
.convertOne( 4, src
);
131 sal_uInt16
TranslateChar15(sal_uInt16 src
)
133 return aCC
.convertOne( 5, src
);
136 sal_uInt16
TranslateChar16(sal_uInt16 src
)
138 return aCC
.convertOne( 6, src
);
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */