Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 : #include <rtl/instance.hxx>
22 : #include "psputil.hxx"
23 :
24 : namespace psp {
25 :
26 : /*
27 : * string convenience routines
28 : */
29 :
30 : sal_Int32
31 0 : getHexValueOf (sal_Int32 nValue, sal_Char* pBuffer)
32 : {
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' };
36 :
37 0 : pBuffer[0] = pHex [(nValue & 0xF0) >> 4];
38 0 : pBuffer[1] = pHex [(nValue & 0x0F) ];
39 :
40 0 : return 2;
41 : }
42 :
43 : sal_Int32
44 0 : getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer)
45 : {
46 : // get sign
47 0 : bool bNegative = nValue < 0;
48 0 : nValue = bNegative ? -nValue : nValue;
49 :
50 : // get required buffer size, must be a multiple of two
51 : sal_Int32 nPrecision;
52 0 : if (nValue < 0x80)
53 0 : nPrecision = 2;
54 : else
55 0 : if (nValue < 0x8000)
56 0 : nPrecision = 4;
57 : else
58 0 : if (nValue < 0x800000)
59 0 : nPrecision = 6;
60 : else
61 0 : nPrecision = 8;
62 :
63 : // convert the int into its hex representation, write it into the buffer
64 0 : sal_Int32 nRet = nPrecision;
65 0 : while (nPrecision)
66 : {
67 0 : nPrecision -= getHexValueOf (nValue % 256, pBuffer + nPrecision - 2 );
68 0 : nValue /= 256;
69 : }
70 :
71 : // set sign bit
72 0 : if (bNegative)
73 : {
74 0 : switch (pBuffer[0])
75 : {
76 0 : case '0' : pBuffer[0] = '8'; break;
77 0 : case '1' : pBuffer[0] = '9'; break;
78 0 : case '2' : pBuffer[0] = 'A'; break;
79 0 : case '3' : pBuffer[0] = 'B'; break;
80 0 : case '4' : pBuffer[0] = 'C'; break;
81 0 : case '5' : pBuffer[0] = 'D'; break;
82 0 : case '6' : pBuffer[0] = 'E'; break;
83 0 : case '7' : pBuffer[0] = 'F'; break;
84 : default: OSL_FAIL("Already a signed value");
85 : }
86 : }
87 :
88 : // report precision
89 0 : return nRet;
90 : }
91 :
92 : sal_Int32
93 0 : getValueOf (sal_Int32 nValue, sal_Char* pBuffer)
94 : {
95 0 : sal_Int32 nChar = 0;
96 0 : if (nValue < 0)
97 : {
98 0 : pBuffer [nChar++] = '-';
99 0 : nValue *= -1;
100 : }
101 : else
102 0 : if (nValue == 0)
103 : {
104 0 : pBuffer [nChar++] = '0';
105 0 : return nChar;
106 : }
107 :
108 : sal_Char pInvBuffer [32];
109 0 : sal_Int32 nInvChar = 0;
110 0 : while (nValue > 0)
111 : {
112 0 : pInvBuffer [nInvChar++] = '0' + nValue % 10;
113 0 : nValue /= 10;
114 : }
115 0 : while (nInvChar > 0)
116 : {
117 0 : pBuffer [nChar++] = pInvBuffer [--nInvChar];
118 : }
119 :
120 0 : return nChar;
121 : }
122 :
123 : sal_Int32
124 0 : appendStr (const sal_Char* pSrc, sal_Char* pDst)
125 : {
126 0 : sal_Int32 nBytes = strlen (pSrc);
127 0 : strncpy (pDst, pSrc, nBytes + 1);
128 :
129 0 : return nBytes;
130 : }
131 :
132 : /*
133 : * copy strings to file
134 : */
135 :
136 : bool
137 0 : WritePS (osl::File* pFile, const sal_Char* pString)
138 : {
139 0 : sal_uInt64 nInLength = rtl_str_getLength (pString);
140 0 : sal_uInt64 nOutLength = 0;
141 :
142 0 : if (nInLength > 0 && pFile)
143 0 : pFile->write (pString, nInLength, nOutLength);
144 :
145 0 : return nInLength == nOutLength;
146 : }
147 :
148 : bool
149 0 : WritePS (osl::File* pFile, const sal_Char* pString, sal_uInt64 nInLength)
150 : {
151 0 : sal_uInt64 nOutLength = 0;
152 :
153 0 : if (nInLength > 0 && pFile)
154 0 : pFile->write (pString, nInLength, nOutLength);
155 :
156 0 : return nInLength == nOutLength;
157 : }
158 :
159 : bool
160 0 : WritePS (osl::File* pFile, const OString &rString)
161 : {
162 0 : sal_uInt64 nInLength = rString.getLength();
163 0 : sal_uInt64 nOutLength = 0;
164 :
165 0 : if (nInLength > 0 && pFile)
166 0 : pFile->write (rString.getStr(), nInLength, nOutLength);
167 :
168 0 : return nInLength == nOutLength;
169 : }
170 :
171 : bool
172 0 : WritePS (osl::File* pFile, const OUString &rString)
173 : {
174 0 : return WritePS (pFile, OUStringToOString(rString, RTL_TEXTENCODING_ASCII_US));
175 : }
176 :
177 : /*
178 : * cache converter for use in postscript drawing routines
179 : */
180 :
181 0 : ConverterFactory::ConverterFactory()
182 : {
183 0 : }
184 :
185 0 : ConverterFactory::~ConverterFactory ()
186 : {
187 0 : for( std::map< rtl_TextEncoding, rtl_UnicodeToTextConverter >::const_iterator it = m_aConverters.begin(); it != m_aConverters.end(); ++it )
188 0 : rtl_destroyUnicodeToTextConverter (it->second);
189 0 : }
190 :
191 : rtl_UnicodeToTextConverter
192 0 : ConverterFactory::Get (rtl_TextEncoding nEncoding)
193 : {
194 0 : if (rtl_isOctetTextEncoding( nEncoding ))
195 : {
196 : std::map< rtl_TextEncoding, rtl_UnicodeToTextConverter >::const_iterator it =
197 0 : m_aConverters.find( nEncoding );
198 : rtl_UnicodeToTextConverter aConverter;
199 0 : if (it == m_aConverters.end())
200 : {
201 0 : aConverter = rtl_createUnicodeToTextConverter (nEncoding);
202 0 : m_aConverters[nEncoding] = aConverter;
203 : }
204 : else
205 0 : aConverter = it->second;
206 0 : return aConverter;
207 : }
208 0 : return NULL;
209 : }
210 :
211 : // wrapper for rtl_convertUnicodeToText that handles the usual cases for
212 : // textconversion in drawtext
213 : sal_Size
214 0 : ConverterFactory::Convert (const sal_Unicode *pText, int nTextLen,
215 : unsigned char *pBuffer, sal_Size nBufferSize, rtl_TextEncoding nEncoding)
216 : {
217 : const sal_uInt32 nCvtFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
218 0 : | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK ;
219 : sal_uInt32 nCvtInfo;
220 : sal_Size nCvtChars;
221 :
222 0 : rtl_UnicodeToTextConverter aConverter = Get (nEncoding);
223 0 : rtl_UnicodeToTextContext aContext = rtl_createUnicodeToTextContext (aConverter);
224 :
225 : sal_Size nSize = rtl_convertUnicodeToText (aConverter, aContext,
226 : pText, nTextLen, (sal_Char*)pBuffer, nBufferSize,
227 0 : nCvtFlags, &nCvtInfo, &nCvtChars);
228 :
229 0 : rtl_destroyUnicodeToTextContext (aConverter, aContext);
230 :
231 0 : return nSize;
232 : }
233 :
234 : namespace
235 : {
236 : class theConverterFactory
237 : : public rtl::Static<ConverterFactory, theConverterFactory>
238 : {
239 : };
240 : }
241 :
242 0 : ConverterFactory& GetConverterFactory()
243 : {
244 0 : return theConverterFactory::get();
245 : }
246 :
247 : } /* namespace psp */
248 :
249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|