merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / printergfx / psputil.cxx
blobe9fe8b0836466a9d2945486dc1b3ac0920effb13
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: psputil.cxx,v $
10 * $Revision: 1.6 $
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"
34 #include <string.h>
36 #include "psputil.hxx"
38 #include "tools/debug.hxx"
40 namespace psp {
43 * string convenience routines
46 sal_Int32
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) ];
56 return 2;
59 sal_Int32
60 getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer)
62 // get sign
63 sal_Bool bNegative = nValue < 0;
64 nValue = bNegative ? -nValue : nValue;
66 // get required buffer size, must be a multiple of two
67 sal_Int32 nPrecision;
68 if (nValue < 0x80)
69 nPrecision = 2;
70 else
71 if (nValue < 0x8000)
72 nPrecision = 4;
73 else
74 if (nValue < 0x800000)
75 nPrecision = 6;
76 else
77 nPrecision = 8;
79 // convert the int into its hex representation, write it into the buffer
80 sal_Int32 nRet = nPrecision;
81 while (nPrecision)
83 nPrecision -= getHexValueOf (nValue % 256, pBuffer + nPrecision - 2 );
84 nValue /= 256;
87 // set sign bit
88 if (bNegative)
90 switch (pBuffer[0])
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");
104 // report precision
105 return nRet;
109 sal_Int32
110 getValueOf (sal_Int32 nValue, sal_Char* pBuffer)
112 sal_Int32 nChar = 0;
113 if (nValue < 0)
115 pBuffer [nChar++] = '-';
116 nValue *= -1;
118 else
119 if (nValue == 0)
121 pBuffer [nChar++] = '0';
122 return nChar;
125 sal_Char pInvBuffer [32];
126 sal_Int32 nInvChar = 0;
127 while (nValue > 0)
129 pInvBuffer [nInvChar++] = '0' + nValue % 10;
130 nValue /= 10;
132 while (nInvChar > 0)
134 pBuffer [nChar++] = pInvBuffer [--nInvChar];
137 return nChar;
140 sal_Int32
141 appendStr (const sal_Char* pSrc, sal_Char* pDst)
143 sal_Int32 nBytes = strlen (pSrc);
144 strncpy (pDst, pSrc, nBytes + 1);
146 return nBytes;
149 sal_Int32
150 appendStr (const sal_Char* pSrc, sal_Char* pDst, sal_Int32 nBytes)
152 strncpy (pDst, pSrc, nBytes);
153 pDst [nBytes] = '\0';
154 return nBytes;
158 * copy strings to file
161 sal_Bool
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;
173 sal_Bool
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;
184 sal_Bool
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;
196 sal_Bool
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;
229 else
230 aConverter = it->second;
231 return aConverter;
233 return NULL;
236 // wrapper for rtl_convertUnicodeToText that handles the usual cases for
237 // textconversion in drawtext
238 sal_Size
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 ;
244 sal_uInt32 nCvtInfo;
245 sal_Size nCvtChars;
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);
256 return nSize;
259 ConverterFactory*
260 GetConverterFactory ()
262 static ConverterFactory* pCvt = NULL;
264 if (pCvt == NULL)
265 pCvt = new ConverterFactory;
267 return pCvt;
271 } /* namespace psp */