1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: psputil.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
36 #include "psputil.hxx"
38 #include "tools/debug.hxx"
43 * string convenience routines
47 getHexValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
49 const static sal_Char pHex
[0x10] = {
50 '0', '1', '2', '3', '4', '5', '6', '7',
51 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
53 pBuffer
[0] = pHex
[(nValue
& 0xF0) >> 4];
54 pBuffer
[1] = pHex
[(nValue
& 0x0F) ];
60 getAlignedHexValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
63 sal_Bool bNegative
= nValue
< 0;
64 nValue
= bNegative
? -nValue
: nValue
;
66 // get required buffer size, must be a multiple of two
74 if (nValue
< 0x800000)
79 // convert the int into its hex representation, write it into the buffer
80 sal_Int32 nRet
= nPrecision
;
83 nPrecision
-= getHexValueOf (nValue
% 256, pBuffer
+ nPrecision
- 2 );
92 case '0' : pBuffer
[0] = '8'; break;
93 case '1' : pBuffer
[0] = '9'; break;
94 case '2' : pBuffer
[0] = 'A'; break;
95 case '3' : pBuffer
[0] = 'B'; break;
96 case '4' : pBuffer
[0] = 'C'; break;
97 case '5' : pBuffer
[0] = 'D'; break;
98 case '6' : pBuffer
[0] = 'E'; break;
99 case '7' : pBuffer
[0] = 'F'; break;
100 default: DBG_ERROR("Already a signed value");
110 getValueOf (sal_Int32 nValue
, sal_Char
* pBuffer
)
115 pBuffer
[nChar
++] = '-';
121 pBuffer
[nChar
++] = '0';
125 sal_Char pInvBuffer
[32];
126 sal_Int32 nInvChar
= 0;
129 pInvBuffer
[nInvChar
++] = '0' + nValue
% 10;
134 pBuffer
[nChar
++] = pInvBuffer
[--nInvChar
];
141 appendStr (const sal_Char
* pSrc
, sal_Char
* pDst
)
143 sal_Int32 nBytes
= strlen (pSrc
);
144 strncpy (pDst
, pSrc
, nBytes
+ 1);
150 appendStr (const sal_Char
* pSrc
, sal_Char
* pDst
, sal_Int32 nBytes
)
152 strncpy (pDst
, pSrc
, nBytes
);
153 pDst
[nBytes
] = '\0';
158 * copy strings to file
162 WritePS (osl::File
* pFile
, const sal_Char
* pString
)
164 sal_uInt64 nInLength
= rtl_str_getLength (pString
);
165 sal_uInt64 nOutLength
= 0;
167 if (nInLength
> 0 && pFile
)
168 pFile
->write (pString
, nInLength
, nOutLength
);
170 return nInLength
== nOutLength
;
174 WritePS (osl::File
* pFile
, const sal_Char
* pString
, sal_uInt64 nInLength
)
176 sal_uInt64 nOutLength
= 0;
178 if (nInLength
> 0 && pFile
)
179 pFile
->write (pString
, nInLength
, nOutLength
);
181 return nInLength
== nOutLength
;
185 WritePS (osl::File
* pFile
, const rtl::OString
&rString
)
187 sal_uInt64 nInLength
= rString
.getLength();
188 sal_uInt64 nOutLength
= 0;
190 if (nInLength
> 0 && pFile
)
191 pFile
->write (rString
, nInLength
, nOutLength
);
193 return nInLength
== nOutLength
;
197 WritePS (osl::File
* pFile
, const rtl::OUString
&rString
)
199 return WritePS (pFile
, rtl::OUStringToOString(rString
, RTL_TEXTENCODING_ASCII_US
));
203 * cache converter for use in postscript drawing routines
206 ConverterFactory::ConverterFactory()
210 ConverterFactory::~ConverterFactory ()
212 for( std::map
< rtl_TextEncoding
, rtl_UnicodeToTextConverter
>::const_iterator it
= m_aConverters
.begin(); it
!= m_aConverters
.end(); ++it
)
213 rtl_destroyUnicodeToTextConverter (it
->second
);
216 rtl_UnicodeToTextConverter
217 ConverterFactory::Get (rtl_TextEncoding nEncoding
)
219 if (rtl_isOctetTextEncoding( nEncoding
))
221 std::map
< rtl_TextEncoding
, rtl_UnicodeToTextConverter
>::const_iterator it
=
222 m_aConverters
.find( nEncoding
);
223 rtl_UnicodeToTextConverter aConverter
;
224 if (it
== m_aConverters
.end())
226 aConverter
= rtl_createUnicodeToTextConverter (nEncoding
);
227 m_aConverters
[nEncoding
] = aConverter
;
230 aConverter
= it
->second
;
236 // wrapper for rtl_convertUnicodeToText that handles the usual cases for
237 // textconversion in drawtext
239 ConverterFactory::Convert (const sal_Unicode
*pText
, int nTextLen
,
240 sal_uChar
*pBuffer
, sal_Size nBufferSize
, rtl_TextEncoding nEncoding
)
242 const sal_uInt32 nCvtFlags
= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
243 | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK
;
247 rtl_UnicodeToTextConverter aConverter
= Get (nEncoding
);
248 rtl_UnicodeToTextContext aContext
= rtl_createUnicodeToTextContext (aConverter
);
250 sal_Size nSize
= rtl_convertUnicodeToText (aConverter
, aContext
,
251 pText
, nTextLen
, (sal_Char
*)pBuffer
, nBufferSize
,
252 nCvtFlags
, &nCvtInfo
, &nCvtChars
);
254 rtl_destroyUnicodeToTextContext (aConverter
, aContext
);
260 GetConverterFactory ()
262 static ConverterFactory
* pCvt
= NULL
;
265 pCvt
= new ConverterFactory
;
271 } /* namespace psp */