bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / fontsubset / xlat.cxx
blobb326ccaf0bb1538be5e536f49c100130fae702f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include "xlat.hxx"
23 namespace {
25 #define MAX_CVT_SELECT 6
27 class ConverterCache
29 public:
30 explicit ConverterCache();
31 ~ConverterCache();
32 sal_uInt16 convertOne( int nSelect, sal_Unicode );
33 private:
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)
52 if( !maContexts[i] )
53 continue;
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 ];
63 if( !aContext )
65 rtl_TextEncoding eRecodeFrom = RTL_TEXTENCODING_UNICODE;
66 switch( nSelect )
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];
92 sal_Size nTempSize;
93 sal_uInt32 nCvtInfo;
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);
106 return aCode;
109 } // anonymous namespace
111 namespace vcl
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);
142 } // namespace vcl
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */