bump product version to 4.1.6.2
[LibreOffice.git] / vcl / generic / print / psputil.cxx
blob81da3844c5f04bd0eb6fe0a00b37746b94a75bab
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 <string.h>
21 #include <rtl/instance.hxx>
22 #include "psputil.hxx"
24 namespace psp {
27 * string convenience routines
30 sal_Int32
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) ];
40 return 2;
43 sal_Int32
44 getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer)
46 // get sign
47 sal_Bool bNegative = nValue < 0;
48 nValue = bNegative ? -nValue : nValue;
50 // get required buffer size, must be a multiple of two
51 sal_Int32 nPrecision;
52 if (nValue < 0x80)
53 nPrecision = 2;
54 else
55 if (nValue < 0x8000)
56 nPrecision = 4;
57 else
58 if (nValue < 0x800000)
59 nPrecision = 6;
60 else
61 nPrecision = 8;
63 // convert the int into its hex representation, write it into the buffer
64 sal_Int32 nRet = nPrecision;
65 while (nPrecision)
67 nPrecision -= getHexValueOf (nValue % 256, pBuffer + nPrecision - 2 );
68 nValue /= 256;
71 // set sign bit
72 if (bNegative)
74 switch (pBuffer[0])
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");
88 // report precision
89 return nRet;
93 sal_Int32
94 getValueOf (sal_Int32 nValue, sal_Char* pBuffer)
96 sal_Int32 nChar = 0;
97 if (nValue < 0)
99 pBuffer [nChar++] = '-';
100 nValue *= -1;
102 else
103 if (nValue == 0)
105 pBuffer [nChar++] = '0';
106 return nChar;
109 sal_Char pInvBuffer [32];
110 sal_Int32 nInvChar = 0;
111 while (nValue > 0)
113 pInvBuffer [nInvChar++] = '0' + nValue % 10;
114 nValue /= 10;
116 while (nInvChar > 0)
118 pBuffer [nChar++] = pInvBuffer [--nInvChar];
121 return nChar;
124 sal_Int32
125 appendStr (const sal_Char* pSrc, sal_Char* pDst)
127 sal_Int32 nBytes = strlen (pSrc);
128 strncpy (pDst, pSrc, nBytes + 1);
130 return nBytes;
134 * copy strings to file
137 sal_Bool
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;
149 sal_Bool
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;
160 sal_Bool
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;
172 sal_Bool
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;
205 else
206 aConverter = it->second;
207 return aConverter;
209 return NULL;
212 // wrapper for rtl_convertUnicodeToText that handles the usual cases for
213 // textconversion in drawtext
214 sal_Size
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 ;
220 sal_uInt32 nCvtInfo;
221 sal_Size nCvtChars;
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);
232 return nSize;
235 namespace
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: */