Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / generic / print / psputil.cxx
blobecea28f1b886bfe7b43e3127e128cab7313b8e21
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 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;
92 sal_Int32
93 getValueOf (sal_Int32 nValue, sal_Char* pBuffer)
95 sal_Int32 nChar = 0;
96 if (nValue < 0)
98 pBuffer [nChar++] = '-';
99 nValue *= -1;
101 else
102 if (nValue == 0)
104 pBuffer [nChar++] = '0';
105 return nChar;
108 sal_Char pInvBuffer [32];
109 sal_Int32 nInvChar = 0;
110 while (nValue > 0)
112 pInvBuffer [nInvChar++] = '0' + nValue % 10;
113 nValue /= 10;
115 while (nInvChar > 0)
117 pBuffer [nChar++] = pInvBuffer [--nInvChar];
120 return nChar;
123 sal_Int32
124 appendStr (const sal_Char* pSrc, sal_Char* pDst)
126 sal_Int32 nBytes = strlen (pSrc);
127 strncpy (pDst, pSrc, nBytes + 1);
129 return nBytes;
133 * copy strings to file
136 bool
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;
148 bool
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;
159 bool
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;
171 bool
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;
204 else
205 aConverter = it->second;
206 return aConverter;
208 return NULL;
211 // wrapper for rtl_convertUnicodeToText that handles the usual cases for
212 // textconversion in drawtext
213 sal_Size
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 ;
219 sal_uInt32 nCvtInfo;
220 sal_Size nCvtChars;
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);
231 return nSize;
234 namespace
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: */