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 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");
93 getValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
98 pBuffer
[nChar
++] = '-';
104 pBuffer
[nChar
++] = '0';
108 sal_Char pInvBuffer
[32];
109 sal_Int32 nInvChar
= 0;
112 pInvBuffer
[nInvChar
++] = '0' + nValue
% 10;
117 pBuffer
[nChar
++] = pInvBuffer
[--nInvChar
];
124 appendStr (const sal_Char
* pSrc
, sal_Char
* pDst
)
126 sal_Int32 nBytes
= strlen (pSrc
);
127 strncpy (pDst
, pSrc
, nBytes
+ 1);
133 * copy strings to file
137 WritePS (osl::File
* pFile
, const sal_Char
* pString
)
139 sal_uInt64 nInLength
= rtl_str_getLength (pString
);
140 sal_uInt64 nOutLength
= 0;
142 if (nInLength
> 0 && pFile
)
143 pFile
->write (pString
, nInLength
, nOutLength
);
145 return nInLength
== nOutLength
;
149 WritePS (osl::File
* pFile
, const sal_Char
* pString
, sal_uInt64 nInLength
)
151 sal_uInt64 nOutLength
= 0;
153 if (nInLength
> 0 && pFile
)
154 pFile
->write (pString
, nInLength
, nOutLength
);
156 return nInLength
== nOutLength
;
160 WritePS (osl::File
* pFile
, const OString
&rString
)
162 sal_uInt64 nInLength
= rString
.getLength();
163 sal_uInt64 nOutLength
= 0;
165 if (nInLength
> 0 && pFile
)
166 pFile
->write (rString
.getStr(), nInLength
, nOutLength
);
168 return nInLength
== nOutLength
;
172 WritePS (osl::File
* pFile
, const OUString
&rString
)
174 return WritePS (pFile
, OUStringToOString(rString
, RTL_TEXTENCODING_ASCII_US
));
178 * cache converter for use in postscript drawing routines
181 ConverterFactory::ConverterFactory()
185 ConverterFactory::~ConverterFactory ()
187 for( std::map
< rtl_TextEncoding
, rtl_UnicodeToTextConverter
>::const_iterator it
= m_aConverters
.begin(); it
!= m_aConverters
.end(); ++it
)
188 rtl_destroyUnicodeToTextConverter (it
->second
);
191 rtl_UnicodeToTextConverter
192 ConverterFactory::Get (rtl_TextEncoding nEncoding
)
194 if (rtl_isOctetTextEncoding( nEncoding
))
196 std::map
< rtl_TextEncoding
, rtl_UnicodeToTextConverter
>::const_iterator it
=
197 m_aConverters
.find( nEncoding
);
198 rtl_UnicodeToTextConverter aConverter
;
199 if (it
== m_aConverters
.end())
201 aConverter
= rtl_createUnicodeToTextConverter (nEncoding
);
202 m_aConverters
[nEncoding
] = aConverter
;
205 aConverter
= it
->second
;
211 // wrapper for rtl_convertUnicodeToText that handles the usual cases for
212 // textconversion in drawtext
214 ConverterFactory::Convert (const sal_Unicode
*pText
, int nTextLen
,
215 unsigned char *pBuffer
, sal_Size nBufferSize
, rtl_TextEncoding nEncoding
)
217 const sal_uInt32 nCvtFlags
= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
218 | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK
;
222 rtl_UnicodeToTextConverter aConverter
= Get (nEncoding
);
223 rtl_UnicodeToTextContext aContext
= rtl_createUnicodeToTextContext (aConverter
);
225 sal_Size nSize
= rtl_convertUnicodeToText (aConverter
, aContext
,
226 pText
, nTextLen
, reinterpret_cast<char*>(pBuffer
), nBufferSize
,
227 nCvtFlags
, &nCvtInfo
, &nCvtChars
);
229 rtl_destroyUnicodeToTextContext (aConverter
, aContext
);
236 class theConverterFactory
237 : public rtl::Static
<ConverterFactory
, theConverterFactory
>
242 ConverterFactory
& GetConverterFactory()
244 return theConverterFactory::get();
247 } /* namespace psp */
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */