build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / fontsubset / xlat.cxx
blobf78674be39d5e19005f10461e8b01c499777a5ab
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"
22 #include <tools/debug.hxx>
24 namespace {
26 #define MAX_CVT_SELECT 6
28 class ConverterCache
30 public:
31 explicit ConverterCache();
32 ~ConverterCache();
33 sal_uInt16 convertOne( int nSelect, sal_Unicode );
34 void convertStr( int nSelect, const sal_Unicode* pSrc, sal_uInt16* pDst, int nCount );
35 protected:
36 void ensureConverter( int nSelect );
37 private:
38 rtl_UnicodeToTextConverter maConverterCache[ MAX_CVT_SELECT+1 ];
39 rtl_UnicodeToTextContext maContexts[ MAX_CVT_SELECT+1 ];
42 ConverterCache::ConverterCache()
44 for( int i = 0; i <= MAX_CVT_SELECT; ++i)
46 maConverterCache[i] = nullptr;
47 maContexts[i] = nullptr;
51 ConverterCache::~ConverterCache()
53 for( int i = 0; i <= MAX_CVT_SELECT; ++i)
55 if( !maContexts[i] )
56 continue;
57 rtl_destroyUnicodeToTextContext( maConverterCache[i], maContexts[i] );
58 rtl_destroyUnicodeToTextConverter( maConverterCache[i] );
62 void ConverterCache::ensureConverter( int nSelect )
64 // SAL_WARN_IF( (2>nSelect) || (nSelect>MAX_CVT_SELECT)), "vcl", "invalid XLAT.Converter requested" );
65 rtl_UnicodeToTextContext aContext = maContexts[ nSelect ];
66 if( !aContext )
68 rtl_TextEncoding eRecodeFrom = RTL_TEXTENCODING_UNICODE;
69 switch( nSelect )
71 default: nSelect = 1; SAL_FALLTHROUGH; // to unicode recoding
72 case 1: eRecodeFrom = RTL_TEXTENCODING_UNICODE; break;
73 case 2: eRecodeFrom = RTL_TEXTENCODING_SHIFT_JIS; break;
74 case 3: eRecodeFrom = RTL_TEXTENCODING_GB_2312; break;
75 case 4: eRecodeFrom = RTL_TEXTENCODING_BIG5; break;
76 case 5: eRecodeFrom = RTL_TEXTENCODING_MS_949; break;
77 case 6: eRecodeFrom = RTL_TEXTENCODING_MS_1361; break;
79 rtl_UnicodeToTextConverter aRecodeConverter = rtl_createUnicodeToTextConverter( eRecodeFrom );
80 maConverterCache[ nSelect ] = aRecodeConverter;
82 aContext = rtl_createUnicodeToTextContext( aRecodeConverter );
83 maContexts[ nSelect ] = aContext;
86 rtl_resetUnicodeToTextContext( maConverterCache[ nSelect ], aContext );
89 sal_uInt16 ConverterCache::convertOne( int nSelect, sal_Unicode aChar )
91 ensureConverter( nSelect );
93 sal_Unicode aUCS2Char = aChar;
94 sal_Char aTempArray[8];
95 sal_Size nTempSize;
96 sal_uInt32 nCvtInfo;
98 // TODO: use direct unicode->mbcs converter should there ever be one
99 int nCodeLen = rtl_convertUnicodeToText(
100 maConverterCache[ nSelect ], maContexts[ nSelect ],
101 &aUCS2Char, 1, aTempArray, sizeof(aTempArray),
102 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
103 | RTL_UNICODETOTEXT_FLAGS_INVALID_0,
104 &nCvtInfo, &nTempSize );
106 sal_uInt16 aCode = aTempArray[0];
107 for( int i = 1; i < nCodeLen; ++i )
108 aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
109 return aCode;
112 void ConverterCache::convertStr( int nSelect, const sal_Unicode* pSrc, sal_uInt16* pDst, int nCount )
114 ensureConverter( nSelect );
116 for( int n = 0; n < nCount; ++n )
118 sal_Unicode aUCS2Char = pSrc[n];
120 sal_Char aTempArray[8];
121 sal_Size nTempSize;
122 sal_uInt32 nCvtInfo;
124 // assume that non-unicode-fonts do not support codepoints >U+FFFF
125 // TODO: use direct unicode->mbcs converter should there ever be one
126 int nCodeLen = rtl_convertUnicodeToText(
127 maConverterCache[ nSelect ], maContexts[ nSelect ],
128 &aUCS2Char, 1, aTempArray, sizeof(aTempArray),
129 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0
130 | RTL_UNICODETOTEXT_FLAGS_INVALID_0,
131 &nCvtInfo, &nTempSize );
133 sal_uInt16 aCode = aTempArray[0];
134 for( int i = 1; i < nCodeLen; ++i )
135 aCode = (aCode << 8) + (aTempArray[i] & 0xFF);
136 pDst[n] = aCode;
140 } // anonymous namespace
142 namespace vcl
145 static ConverterCache aCC;
147 sal_uInt16 TranslateChar12(sal_uInt16 src)
149 return aCC.convertOne( 2, src);
152 sal_uInt16 TranslateChar13(sal_uInt16 src)
154 return aCC.convertOne( 3, src);
157 sal_uInt16 TranslateChar14(sal_uInt16 src)
159 return aCC.convertOne( 4, src);
162 sal_uInt16 TranslateChar15(sal_uInt16 src)
164 return aCC.convertOne( 5, src);
167 sal_uInt16 TranslateChar16(sal_uInt16 src)
169 return aCC.convertOne( 6, src);
172 void TranslateString12(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
174 aCC.convertStr( 2, reinterpret_cast<sal_Unicode *>(src), dst, n);
177 void TranslateString13(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
179 aCC.convertStr( 3, reinterpret_cast<sal_Unicode *>(src), dst, n);
182 void TranslateString14(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
184 aCC.convertStr( 4, reinterpret_cast<sal_Unicode *>(src), dst, n);
187 void TranslateString15(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
189 aCC.convertStr( 5, reinterpret_cast<sal_Unicode *>(src), dst, n);
192 void TranslateString16(sal_uInt16 *src, sal_uInt16 *dst, sal_uInt32 n)
194 aCC.convertStr( 6, reinterpret_cast<sal_Unicode *>(src), dst, n);
197 } // namespace vcl
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */