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 .
21 #include <rtl/instance.hxx>
22 #include "psputil.hxx"
27 * string convenience routines
31 getHexValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
33 const static sal_Char pHex
[0x10] = {
34 '0', '1', '2', '3', '4', '5', '6', '7',
35 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
37 pBuffer
[0] = pHex
[(nValue
& 0xF0) >> 4];
38 pBuffer
[1] = pHex
[(nValue
& 0x0F) ];
44 getAlignedHexValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
47 sal_Bool bNegative
= nValue
< 0;
48 nValue
= bNegative
? -nValue
: nValue
;
50 // get required buffer size, must be a multiple of two
58 if (nValue
< 0x800000)
63 // convert the int into its hex representation, write it into the buffer
64 sal_Int32 nRet
= nPrecision
;
67 nPrecision
-= getHexValueOf (nValue
% 256, pBuffer
+ nPrecision
- 2 );
76 case '0' : pBuffer
[0] = '8'; break;
77 case '1' : pBuffer
[0] = '9'; break;
78 case '2' : pBuffer
[0] = 'A'; break;
79 case '3' : pBuffer
[0] = 'B'; break;
80 case '4' : pBuffer
[0] = 'C'; break;
81 case '5' : pBuffer
[0] = 'D'; break;
82 case '6' : pBuffer
[0] = 'E'; break;
83 case '7' : pBuffer
[0] = 'F'; break;
84 default: OSL_FAIL("Already a signed value");
94 getValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
99 pBuffer
[nChar
++] = '-';
105 pBuffer
[nChar
++] = '0';
109 sal_Char pInvBuffer
[32];
110 sal_Int32 nInvChar
= 0;
113 pInvBuffer
[nInvChar
++] = '0' + nValue
% 10;
118 pBuffer
[nChar
++] = pInvBuffer
[--nInvChar
];
125 appendStr (const sal_Char
* pSrc
, sal_Char
* pDst
)
127 sal_Int32 nBytes
= strlen (pSrc
);
128 strncpy (pDst
, pSrc
, nBytes
+ 1);
134 * copy strings to file
138 WritePS (osl::File
* pFile
, const sal_Char
* pString
)
140 sal_uInt64 nInLength
= rtl_str_getLength (pString
);
141 sal_uInt64 nOutLength
= 0;
143 if (nInLength
> 0 && pFile
)
144 pFile
->write (pString
, nInLength
, nOutLength
);
146 return nInLength
== nOutLength
;
150 WritePS (osl::File
* pFile
, const sal_Char
* pString
, sal_uInt64 nInLength
)
152 sal_uInt64 nOutLength
= 0;
154 if (nInLength
> 0 && pFile
)
155 pFile
->write (pString
, nInLength
, nOutLength
);
157 return nInLength
== nOutLength
;
161 WritePS (osl::File
* pFile
, const OString
&rString
)
163 sal_uInt64 nInLength
= rString
.getLength();
164 sal_uInt64 nOutLength
= 0;
166 if (nInLength
> 0 && pFile
)
167 pFile
->write (rString
.getStr(), nInLength
, nOutLength
);
169 return nInLength
== nOutLength
;
173 WritePS (osl::File
* pFile
, const OUString
&rString
)
175 return WritePS (pFile
, OUStringToOString(rString
, RTL_TEXTENCODING_ASCII_US
));
179 * cache converter for use in postscript drawing routines
182 ConverterFactory::ConverterFactory()
186 ConverterFactory::~ConverterFactory ()
188 for( std::map
< rtl_TextEncoding
, rtl_UnicodeToTextConverter
>::const_iterator it
= m_aConverters
.begin(); it
!= m_aConverters
.end(); ++it
)
189 rtl_destroyUnicodeToTextConverter (it
->second
);
192 rtl_UnicodeToTextConverter
193 ConverterFactory::Get (rtl_TextEncoding nEncoding
)
195 if (rtl_isOctetTextEncoding( nEncoding
))
197 std::map
< rtl_TextEncoding
, rtl_UnicodeToTextConverter
>::const_iterator it
=
198 m_aConverters
.find( nEncoding
);
199 rtl_UnicodeToTextConverter aConverter
;
200 if (it
== m_aConverters
.end())
202 aConverter
= rtl_createUnicodeToTextConverter (nEncoding
);
203 m_aConverters
[nEncoding
] = aConverter
;
206 aConverter
= it
->second
;
212 // wrapper for rtl_convertUnicodeToText that handles the usual cases for
213 // textconversion in drawtext
215 ConverterFactory::Convert (const sal_Unicode
*pText
, int nTextLen
,
216 sal_uChar
*pBuffer
, sal_Size nBufferSize
, rtl_TextEncoding nEncoding
)
218 const sal_uInt32 nCvtFlags
= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
219 | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK
;
223 rtl_UnicodeToTextConverter aConverter
= Get (nEncoding
);
224 rtl_UnicodeToTextContext aContext
= rtl_createUnicodeToTextContext (aConverter
);
226 sal_Size nSize
= rtl_convertUnicodeToText (aConverter
, aContext
,
227 pText
, nTextLen
, (sal_Char
*)pBuffer
, nBufferSize
,
228 nCvtFlags
, &nCvtInfo
, &nCvtChars
);
230 rtl_destroyUnicodeToTextContext (aConverter
, aContext
);
237 class theConverterFactory
238 : public rtl::Static
<ConverterFactory
, theConverterFactory
>
243 ConverterFactory
& GetConverterFactory()
245 return theConverterFactory::get();
249 } /* namespace psp */
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */