Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <string.h>
30 : : #include <rtl/instance.hxx>
31 : : #include "psputil.hxx"
32 : :
33 : : namespace psp {
34 : :
35 : : /*
36 : : * string convenience routines
37 : : */
38 : :
39 : : sal_Int32
40 : 0 : getHexValueOf (sal_Int32 nValue, sal_Char* pBuffer)
41 : : {
42 : : const static sal_Char pHex [0x10] = {
43 : : '0', '1', '2', '3', '4', '5', '6', '7',
44 : : '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
45 : :
46 : 0 : pBuffer[0] = pHex [(nValue & 0xF0) >> 4];
47 : 0 : pBuffer[1] = pHex [(nValue & 0x0F) ];
48 : :
49 : 0 : return 2;
50 : : }
51 : :
52 : : sal_Int32
53 : 0 : getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer)
54 : : {
55 : : // get sign
56 : 0 : sal_Bool bNegative = nValue < 0;
57 [ # # ]: 0 : nValue = bNegative ? -nValue : nValue;
58 : :
59 : : // get required buffer size, must be a multiple of two
60 : : sal_Int32 nPrecision;
61 [ # # ]: 0 : if (nValue < 0x80)
62 : 0 : nPrecision = 2;
63 : : else
64 [ # # ]: 0 : if (nValue < 0x8000)
65 : 0 : nPrecision = 4;
66 : : else
67 [ # # ]: 0 : if (nValue < 0x800000)
68 : 0 : nPrecision = 6;
69 : : else
70 : 0 : nPrecision = 8;
71 : :
72 : : // convert the int into its hex representation, write it into the buffer
73 : 0 : sal_Int32 nRet = nPrecision;
74 [ # # ]: 0 : while (nPrecision)
75 : : {
76 : 0 : nPrecision -= getHexValueOf (nValue % 256, pBuffer + nPrecision - 2 );
77 : 0 : nValue /= 256;
78 : : }
79 : :
80 : : // set sign bit
81 [ # # ]: 0 : if (bNegative)
82 : : {
83 [ # # # # : 0 : switch (pBuffer[0])
# # # #
# ]
84 : : {
85 : 0 : case '0' : pBuffer[0] = '8'; break;
86 : 0 : case '1' : pBuffer[0] = '9'; break;
87 : 0 : case '2' : pBuffer[0] = 'A'; break;
88 : 0 : case '3' : pBuffer[0] = 'B'; break;
89 : 0 : case '4' : pBuffer[0] = 'C'; break;
90 : 0 : case '5' : pBuffer[0] = 'D'; break;
91 : 0 : case '6' : pBuffer[0] = 'E'; break;
92 : 0 : case '7' : pBuffer[0] = 'F'; break;
93 : : default: OSL_FAIL("Already a signed value");
94 : : }
95 : : }
96 : :
97 : : // report precision
98 : 0 : return nRet;
99 : : }
100 : :
101 : :
102 : : sal_Int32
103 : 0 : getValueOf (sal_Int32 nValue, sal_Char* pBuffer)
104 : : {
105 : 0 : sal_Int32 nChar = 0;
106 [ # # ]: 0 : if (nValue < 0)
107 : : {
108 : 0 : pBuffer [nChar++] = '-';
109 : 0 : nValue *= -1;
110 : : }
111 : : else
112 [ # # ]: 0 : if (nValue == 0)
113 : : {
114 : 0 : pBuffer [nChar++] = '0';
115 : 0 : return nChar;
116 : : }
117 : :
118 : : sal_Char pInvBuffer [32];
119 : 0 : sal_Int32 nInvChar = 0;
120 [ # # ]: 0 : while (nValue > 0)
121 : : {
122 : 0 : pInvBuffer [nInvChar++] = '0' + nValue % 10;
123 : 0 : nValue /= 10;
124 : : }
125 [ # # ]: 0 : while (nInvChar > 0)
126 : : {
127 : 0 : pBuffer [nChar++] = pInvBuffer [--nInvChar];
128 : : }
129 : :
130 : 0 : return nChar;
131 : : }
132 : :
133 : : sal_Int32
134 : 0 : appendStr (const sal_Char* pSrc, sal_Char* pDst)
135 : : {
136 : 0 : sal_Int32 nBytes = strlen (pSrc);
137 : 0 : strncpy (pDst, pSrc, nBytes + 1);
138 : :
139 : 0 : return nBytes;
140 : : }
141 : :
142 : : /*
143 : : * copy strings to file
144 : : */
145 : :
146 : : sal_Bool
147 : 0 : WritePS (osl::File* pFile, const sal_Char* pString)
148 : : {
149 : 0 : sal_uInt64 nInLength = rtl_str_getLength (pString);
150 : 0 : sal_uInt64 nOutLength = 0;
151 : :
152 [ # # ][ # # ]: 0 : if (nInLength > 0 && pFile)
153 [ # # ]: 0 : pFile->write (pString, nInLength, nOutLength);
154 : :
155 : 0 : return nInLength == nOutLength;
156 : : }
157 : :
158 : : sal_Bool
159 : 0 : WritePS (osl::File* pFile, const sal_Char* pString, sal_uInt64 nInLength)
160 : : {
161 : 0 : sal_uInt64 nOutLength = 0;
162 : :
163 [ # # ][ # # ]: 0 : if (nInLength > 0 && pFile)
164 [ # # ]: 0 : pFile->write (pString, nInLength, nOutLength);
165 : :
166 : 0 : return nInLength == nOutLength;
167 : : }
168 : :
169 : : sal_Bool
170 : 0 : WritePS (osl::File* pFile, const rtl::OString &rString)
171 : : {
172 : 0 : sal_uInt64 nInLength = rString.getLength();
173 : 0 : sal_uInt64 nOutLength = 0;
174 : :
175 [ # # ][ # # ]: 0 : if (nInLength > 0 && pFile)
176 [ # # ]: 0 : pFile->write (rString.getStr(), nInLength, nOutLength);
177 : :
178 : 0 : return nInLength == nOutLength;
179 : : }
180 : :
181 : : sal_Bool
182 : 0 : WritePS (osl::File* pFile, const rtl::OUString &rString)
183 : : {
184 [ # # ]: 0 : return WritePS (pFile, rtl::OUStringToOString(rString, RTL_TEXTENCODING_ASCII_US));
185 : : }
186 : :
187 : : /*
188 : : * cache converter for use in postscript drawing routines
189 : : */
190 : :
191 : 0 : ConverterFactory::ConverterFactory()
192 : : {
193 : 0 : }
194 : :
195 : 0 : ConverterFactory::~ConverterFactory ()
196 : : {
197 [ # # ][ # # ]: 0 : for( std::map< rtl_TextEncoding, rtl_UnicodeToTextConverter >::const_iterator it = m_aConverters.begin(); it != m_aConverters.end(); ++it )
[ # # ][ # # ]
[ # # ]
198 [ # # ][ # # ]: 0 : rtl_destroyUnicodeToTextConverter (it->second);
199 : 0 : }
200 : :
201 : : rtl_UnicodeToTextConverter
202 : 0 : ConverterFactory::Get (rtl_TextEncoding nEncoding)
203 : : {
204 [ # # ]: 0 : if (rtl_isOctetTextEncoding( nEncoding ))
205 : : {
206 : : std::map< rtl_TextEncoding, rtl_UnicodeToTextConverter >::const_iterator it =
207 [ # # ][ # # ]: 0 : m_aConverters.find( nEncoding );
208 : : rtl_UnicodeToTextConverter aConverter;
209 [ # # ][ # # ]: 0 : if (it == m_aConverters.end())
[ # # ]
210 : : {
211 [ # # ]: 0 : aConverter = rtl_createUnicodeToTextConverter (nEncoding);
212 [ # # ]: 0 : m_aConverters[nEncoding] = aConverter;
213 : : }
214 : : else
215 [ # # ]: 0 : aConverter = it->second;
216 : 0 : return aConverter;
217 : : }
218 : 0 : return NULL;
219 : : }
220 : :
221 : : // wrapper for rtl_convertUnicodeToText that handles the usual cases for
222 : : // textconversion in drawtext
223 : : sal_Size
224 : 0 : ConverterFactory::Convert (const sal_Unicode *pText, int nTextLen,
225 : : sal_uChar *pBuffer, sal_Size nBufferSize, rtl_TextEncoding nEncoding)
226 : : {
227 : : const sal_uInt32 nCvtFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
228 : 0 : | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK ;
229 : : sal_uInt32 nCvtInfo;
230 : : sal_Size nCvtChars;
231 : :
232 [ # # ]: 0 : rtl_UnicodeToTextConverter aConverter = Get (nEncoding);
233 [ # # ]: 0 : rtl_UnicodeToTextContext aContext = rtl_createUnicodeToTextContext (aConverter);
234 : :
235 : : sal_Size nSize = rtl_convertUnicodeToText (aConverter, aContext,
236 : : pText, nTextLen, (sal_Char*)pBuffer, nBufferSize,
237 [ # # ]: 0 : nCvtFlags, &nCvtInfo, &nCvtChars);
238 : :
239 [ # # ]: 0 : rtl_destroyUnicodeToTextContext (aConverter, aContext);
240 : :
241 : 0 : return nSize;
242 : : }
243 : :
244 : : namespace
245 : : {
246 : : class theConverterFactory
247 : : : public rtl::Static<ConverterFactory, theConverterFactory>
248 : : {
249 : : };
250 : : }
251 : :
252 : 0 : ConverterFactory& GetConverterFactory()
253 : : {
254 : 0 : return theConverterFactory::get();
255 : : }
256 : :
257 : :
258 : : } /* namespace psp */
259 : :
260 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|