LibreOffice Module vcl (master) 1
xlat.cxx
Go to the documentation of this file.
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
23namespace {
24
25#define MAX_CVT_SELECT 6
26
27class ConverterCache
28{
29public:
30 explicit ConverterCache();
31 ~ConverterCache();
32 sal_uInt16 convertOne( int nSelect, sal_Unicode );
33private:
34 void ensureConverter( int nSelect );
35 rtl_UnicodeToTextConverter maConverterCache[ MAX_CVT_SELECT+1 ];
36 rtl_UnicodeToTextContext maContexts[ MAX_CVT_SELECT+1 ];
37};
38
39ConverterCache::ConverterCache()
40{
41 for( int i = 0; i <= MAX_CVT_SELECT; ++i)
42 {
43 maConverterCache[i] = nullptr;
44 maContexts[i] = nullptr;
45 }
46}
47
48ConverterCache::~ConverterCache()
49{
50 for( int i = 0; i <= MAX_CVT_SELECT; ++i)
51 {
52 if( !maContexts[i] )
53 continue;
54 rtl_destroyUnicodeToTextContext( maConverterCache[i], maContexts[i] );
55 rtl_destroyUnicodeToTextConverter( maConverterCache[i] );
56 }
57}
58
59void ConverterCache::ensureConverter( int nSelect )
60{
61 // SAL_WARN_IF( (2>nSelect) || (nSelect>MAX_CVT_SELECT)), "vcl", "invalid XLAT.Converter requested" );
62 rtl_UnicodeToTextContext aContext = maContexts[ nSelect ];
63 if( !aContext )
64 {
65 rtl_TextEncoding eRecodeFrom = RTL_TEXTENCODING_UNICODE;
66 switch( nSelect )
67 {
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;
75 }
76 rtl_UnicodeToTextConverter aRecodeConverter = rtl_createUnicodeToTextConverter( eRecodeFrom );
77 maConverterCache[ nSelect ] = aRecodeConverter;
78
79 aContext = rtl_createUnicodeToTextContext( aRecodeConverter );
80 maContexts[ nSelect ] = aContext;
81 }
82
83 rtl_resetUnicodeToTextContext( maConverterCache[ nSelect ], aContext );
84}
85
86sal_uInt16 ConverterCache::convertOne( int nSelect, sal_Unicode aChar )
87{
88 ensureConverter( nSelect );
89
90 sal_Unicode aUCS2Char = aChar;
91 char aTempArray[8];
92 sal_Size nTempSize;
93 sal_uInt32 nCvtInfo;
94
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 );
102
103 sal_uInt16 aCode = aTempArray[0];
104 for( int i = 1; i < nCodeLen; ++i )
105 aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
106 return aCode;
107}
108
109} // anonymous namespace
110
111namespace vcl
112{
113
114static ConverterCache aCC;
115
116sal_uInt16 TranslateChar12(sal_uInt16 src)
117{
118 return aCC.convertOne( 2, src);
119}
120
121sal_uInt16 TranslateChar13(sal_uInt16 src)
122{
123 return aCC.convertOne( 3, src);
124}
125
126sal_uInt16 TranslateChar14(sal_uInt16 src)
127{
128 return aCC.convertOne( 4, src);
129}
130
131sal_uInt16 TranslateChar15(sal_uInt16 src)
132{
133 return aCC.convertOne( 5, src);
134}
135
136sal_uInt16 TranslateChar16(sal_uInt16 src)
137{
138 return aCC.convertOne( 6, src);
139}
140
141
142} // namespace vcl
143
144/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int i
sal_uInt16 TranslateChar15(sal_uInt16 src)
Definition: xlat.cxx:131
static ConverterCache aCC
Definition: xlat.cxx:114
sal_uInt16 TranslateChar14(sal_uInt16 src)
Definition: xlat.cxx:126
sal_uInt16 TranslateChar12(sal_uInt16 src)
Definition: xlat.cxx:116
sal_uInt16 TranslateChar13(sal_uInt16 src)
Definition: xlat.cxx:121
sal_uInt16 TranslateChar16(sal_uInt16 src)
Definition: xlat.cxx:136
sal_uInt16 sal_Unicode
#define MAX_CVT_SELECT
Definition: xlat.cxx:25