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 : sal_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 :
93 : sal_Int32
94 0 : getValueOf (sal_Int32 nValue, sal_Char* pBuffer)
95 : {
96 0 : sal_Int32 nChar = 0;
97 0 : if (nValue < 0)
98 : {
99 0 : pBuffer [nChar++] = '-';
100 0 : nValue *= -1;
101 : }
102 : else
103 0 : if (nValue == 0)
104 : {
105 0 : pBuffer [nChar++] = '0';
106 0 : return nChar;
107 : }
108 :
109 : sal_Char pInvBuffer [32];
110 0 : sal_Int32 nInvChar = 0;
111 0 : while (nValue > 0)
112 : {
113 0 : pInvBuffer [nInvChar++] = '0' + nValue % 10;
114 0 : nValue /= 10;
115 : }
116 0 : while (nInvChar > 0)
117 : {
118 0 : pBuffer [nChar++] = pInvBuffer [--nInvChar];
119 : }
120 :
121 0 : return nChar;
122 : }
123 :
124 : sal_Int32
125 0 : appendStr (const sal_Char* pSrc, sal_Char* pDst)
126 : {
127 0 : sal_Int32 nBytes = strlen (pSrc);
128 0 : strncpy (pDst, pSrc, nBytes + 1);
129 :
130 0 : return nBytes;
131 : }
132 :
133 : /*
134 : * copy strings to file
135 : */
136 :
137 : sal_Bool
138 0 : WritePS (osl::File* pFile, const sal_Char* pString)
139 : {
140 0 : sal_uInt64 nInLength = rtl_str_getLength (pString);
141 0 : sal_uInt64 nOutLength = 0;
142 :
143 0 : if (nInLength > 0 && pFile)
144 0 : pFile->write (pString, nInLength, nOutLength);
145 :
146 0 : return nInLength == nOutLength;
147 : }
148 :
149 : sal_Bool
150 0 : WritePS (osl::File* pFile, const sal_Char* pString, sal_uInt64 nInLength)
151 : {
152 0 : sal_uInt64 nOutLength = 0;
153 :
154 0 : if (nInLength > 0 && pFile)
155 0 : pFile->write (pString, nInLength, nOutLength);
156 :
157 0 : return nInLength == nOutLength;
158 : }
159 :
160 : sal_Bool
161 0 : WritePS (osl::File* pFile, const rtl::OString &rString)
162 : {
163 0 : sal_uInt64 nInLength = rString.getLength();
164 0 : sal_uInt64 nOutLength = 0;
165 :
166 0 : if (nInLength > 0 && pFile)
167 0 : pFile->write (rString.getStr(), nInLength, nOutLength);
168 :
169 0 : return nInLength == nOutLength;
170 : }
171 :
172 : sal_Bool
173 0 : WritePS (osl::File* pFile, const rtl::OUString &rString)
174 : {
175 0 : return WritePS (pFile, rtl::OUStringToOString(rString, RTL_TEXTENCODING_ASCII_US));
176 : }
177 :
178 : /*
179 : * cache converter for use in postscript drawing routines
180 : */
181 :
182 0 : ConverterFactory::ConverterFactory()
183 : {
184 0 : }
185 :
186 0 : ConverterFactory::~ConverterFactory ()
187 : {
188 0 : for( std::map< rtl_TextEncoding, rtl_UnicodeToTextConverter >::const_iterator it = m_aConverters.begin(); it != m_aConverters.end(); ++it )
189 0 : rtl_destroyUnicodeToTextConverter (it->second);
190 0 : }
191 :
192 : rtl_UnicodeToTextConverter
193 0 : ConverterFactory::Get (rtl_TextEncoding nEncoding)
194 : {
195 0 : if (rtl_isOctetTextEncoding( nEncoding ))
196 : {
197 : std::map< rtl_TextEncoding, rtl_UnicodeToTextConverter >::const_iterator it =
198 0 : m_aConverters.find( nEncoding );
199 : rtl_UnicodeToTextConverter aConverter;
200 0 : if (it == m_aConverters.end())
201 : {
202 0 : aConverter = rtl_createUnicodeToTextConverter (nEncoding);
203 0 : m_aConverters[nEncoding] = aConverter;
204 : }
205 : else
206 0 : aConverter = it->second;
207 0 : return aConverter;
208 : }
209 0 : return NULL;
210 : }
211 :
212 : // wrapper for rtl_convertUnicodeToText that handles the usual cases for
213 : // textconversion in drawtext
214 : sal_Size
215 0 : ConverterFactory::Convert (const sal_Unicode *pText, int nTextLen,
216 : sal_uChar *pBuffer, sal_Size nBufferSize, rtl_TextEncoding nEncoding)
217 : {
218 : const sal_uInt32 nCvtFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
219 0 : | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK ;
220 : sal_uInt32 nCvtInfo;
221 : sal_Size nCvtChars;
222 :
223 0 : rtl_UnicodeToTextConverter aConverter = Get (nEncoding);
224 0 : rtl_UnicodeToTextContext aContext = rtl_createUnicodeToTextContext (aConverter);
225 :
226 : sal_Size nSize = rtl_convertUnicodeToText (aConverter, aContext,
227 : pText, nTextLen, (sal_Char*)pBuffer, nBufferSize,
228 0 : nCvtFlags, &nCvtInfo, &nCvtChars);
229 :
230 0 : rtl_destroyUnicodeToTextContext (aConverter, aContext);
231 :
232 0 : return nSize;
233 : }
234 :
235 : namespace
236 : {
237 : class theConverterFactory
238 : : public rtl::Static<ConverterFactory, theConverterFactory>
239 : {
240 : };
241 : }
242 :
243 0 : ConverterFactory& GetConverterFactory()
244 : {
245 0 : return theConverterFactory::get();
246 : }
247 :
248 :
249 : } /* namespace psp */
250 :
251 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|