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 "xmloff/MarkerStyle.hxx"
30 : : #include "xexptran.hxx"
31 : : #include <xmloff/attrlist.hxx>
32 : : #include <xmloff/nmspmap.hxx>
33 : : #include <xmloff/xmluconv.hxx>
34 : : #include "xmloff/xmlnmspe.hxx"
35 : : #include <xmloff/xmltoken.hxx>
36 : : #include <xmloff/xmlexp.hxx>
37 : : #include <xmloff/xmlimp.hxx>
38 : : #include <rtl/ustrbuf.hxx>
39 : : #include <rtl/ustring.hxx>
40 : : #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
41 : :
42 : : using namespace ::com::sun::star;
43 : : using ::rtl::OUString;
44 : : using ::rtl::OUStringBuffer;
45 : :
46 : : using namespace ::xmloff::token;
47 : :
48 : :
49 : : //-------------------------------------------------------------
50 : : // Import
51 : : //-------------------------------------------------------------
52 : :
53 : 182 : XMLMarkerStyleImport::XMLMarkerStyleImport( SvXMLImport& rImp )
54 : 182 : : rImport( rImp )
55 : : {
56 : 182 : }
57 : :
58 : 182 : XMLMarkerStyleImport::~XMLMarkerStyleImport()
59 : : {
60 : 182 : }
61 : :
62 : 182 : sal_Bool XMLMarkerStyleImport::importXML(
63 : : const uno::Reference< xml::sax::XAttributeList >& xAttrList,
64 : : uno::Any& rValue,
65 : : OUString& rStrName )
66 : : {
67 : 182 : sal_Bool bHasViewBox = sal_False;
68 : 182 : sal_Bool bHasPathData = sal_False;
69 : 182 : OUString aDisplayName;
70 : :
71 : 182 : SdXMLImExViewBox* pViewBox = NULL;
72 : :
73 : 182 : SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
74 : 182 : SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
75 : :
76 : 182 : OUString strPathData;
77 : :
78 [ + - ][ + - ]: 182 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
[ + - ]
79 [ + + ]: 739 : for( sal_Int16 i = 0; i < nAttrCount; i++ )
80 : : {
81 [ + - ][ + - ]: 557 : OUString aStrFullAttrName = xAttrList->getNameByIndex( i );
82 : 557 : OUString aStrAttrName;
83 [ + - ]: 557 : rNamespaceMap.GetKeyByAttrName( aStrFullAttrName, &aStrAttrName );
84 [ + - ][ + - ]: 557 : OUString aStrValue = xAttrList->getValueByIndex( i );
85 : :
86 [ + - ][ + + ]: 557 : if( IsXMLToken( aStrAttrName, XML_NAME ) )
87 : : {
88 : 182 : rStrName = aStrValue;
89 : : }
90 [ + - ][ + + ]: 375 : else if( IsXMLToken( aStrAttrName, XML_DISPLAY_NAME ) )
91 : : {
92 : 11 : aDisplayName = aStrValue;
93 : : }
94 [ + - ][ + + ]: 364 : else if( IsXMLToken( aStrAttrName, XML_VIEWBOX ) )
95 : : {
96 [ + - ][ + - ]: 182 : pViewBox = new SdXMLImExViewBox( aStrValue, rUnitConverter );
97 : 182 : bHasViewBox = sal_True;
98 : :
99 : : }
100 [ + - ][ + - ]: 182 : else if( IsXMLToken( aStrAttrName, XML_D ) )
101 : : {
102 : 182 : strPathData = aStrValue;
103 : 182 : bHasPathData = sal_True;
104 : : }
105 : 557 : }
106 : :
107 [ + - ][ + - ]: 182 : if( bHasViewBox && bHasPathData )
108 : : {
109 : : SdXMLImExSvgDElement aPoints(strPathData, *pViewBox, awt::Point( 0, 0 ),
110 [ + - ]: 182 : awt::Size( pViewBox->GetWidth(), pViewBox->GetHeight() ), rImport );
111 : :
112 [ + + ]: 182 : if(aPoints.IsCurve())
113 : : {
114 : : drawing::PolyPolygonBezierCoords aSourcePolyPolygon(
115 : 16 : aPoints.GetPointSequenceSequence(),
116 [ + - ]: 32 : aPoints.GetFlagSequenceSequence());
117 [ + - ][ + - ]: 16 : rValue <<= aSourcePolyPolygon;
118 : : }
119 : : else
120 : : {
121 [ + - ]: 166 : drawing::PolyPolygonBezierCoords aSourcePolyPolygon;
122 [ + - ]: 166 : aSourcePolyPolygon.Coordinates = aPoints.GetPointSequenceSequence();
123 [ + - ]: 166 : aSourcePolyPolygon.Flags.realloc(aSourcePolyPolygon.Coordinates.getLength());
124 : :
125 : : // Zeiger auf innere sequences holen
126 : 166 : const drawing::PointSequence* pInnerSequence = aSourcePolyPolygon.Coordinates.getConstArray();
127 [ + - ]: 166 : drawing::FlagSequence* pInnerSequenceFlags = aSourcePolyPolygon.Flags.getArray();
128 : :
129 [ + + ]: 364 : for(sal_Int32 a(0); a < aSourcePolyPolygon.Coordinates.getLength(); a++)
130 : : {
131 [ + - ]: 198 : pInnerSequenceFlags->realloc(pInnerSequence->getLength());
132 [ + - ]: 198 : drawing::PolygonFlags* pPolyFlags = pInnerSequenceFlags->getArray();
133 : :
134 [ + + ]: 2043 : for(sal_Int32 b(0); b < pInnerSequence->getLength(); b++)
135 : 1845 : *pPolyFlags++ = drawing::PolygonFlags_NORMAL;
136 : :
137 : : // next run
138 : 198 : pInnerSequence++;
139 : 198 : pInnerSequenceFlags++;
140 : : }
141 : :
142 [ + - ][ + - ]: 166 : rValue <<= aSourcePolyPolygon;
143 : : }
144 : :
145 [ + + ]: 182 : if( !aDisplayName.isEmpty() )
146 : : {
147 : : rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_MARKER_ID, rStrName,
148 [ + - ]: 11 : aDisplayName );
149 : 11 : rStrName = aDisplayName;
150 [ + - ]: 182 : }
151 : :
152 : : }
153 : :
154 [ + - ]: 182 : if( pViewBox )
155 [ + - ]: 182 : delete pViewBox;
156 : :
157 [ + - ][ + - ]: 182 : return bHasViewBox && bHasPathData;
158 : : }
159 : :
160 : :
161 : : //-------------------------------------------------------------
162 : : // Export
163 : : //-------------------------------------------------------------
164 : :
165 : 46 : XMLMarkerStyleExport::XMLMarkerStyleExport( SvXMLExport& rExp )
166 : 46 : : rExport( rExp )
167 : : {
168 : 46 : }
169 : :
170 : 46 : XMLMarkerStyleExport::~XMLMarkerStyleExport()
171 : : {
172 : 46 : }
173 : :
174 : 12 : sal_Bool XMLMarkerStyleExport::exportXML(
175 : : const OUString& rStrName,
176 : : const uno::Any& rValue )
177 : : {
178 : 12 : sal_Bool bRet(sal_False);
179 : :
180 [ + - ]: 12 : if(!rStrName.isEmpty())
181 : : {
182 [ + - ]: 12 : drawing::PolyPolygonBezierCoords aBezier;
183 : :
184 [ + - ][ + - ]: 12 : if(rValue >>= aBezier)
185 : : {
186 : 12 : OUString aStrValue;
187 : 12 : OUStringBuffer aOut;
188 : :
189 : : /////////////////
190 : : // Name
191 : 12 : sal_Bool bEncoded = sal_False;
192 : 12 : OUString aStrName( rStrName );
193 : : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
194 : : rExport.EncodeStyleName( aStrName,
195 [ + - ][ + - ]: 12 : &bEncoded ) );
196 [ - + ]: 12 : if( bEncoded )
197 : : rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
198 [ # # ]: 0 : aStrName );
199 : :
200 : : /////////////////
201 : : // Viewbox (viewBox="0 0 1500 1000")
202 : 12 : sal_Int32 nMinX(0x7fffffff);
203 : 12 : sal_Int32 nMaxX(0x80000000);
204 : 12 : sal_Int32 nMinY(0x7fffffff);
205 : 12 : sal_Int32 nMaxY(0x80000000);
206 : 12 : sal_Int32 nOuterCnt(aBezier.Coordinates.getLength());
207 [ + - ]: 12 : drawing::PointSequence* pOuterSequence = aBezier.Coordinates.getArray();
208 : : sal_Int32 a, b;
209 : 12 : sal_Bool bClosed(sal_False);
210 : :
211 [ + + ]: 24 : for (a = 0; a < nOuterCnt; a++)
212 : : {
213 : 12 : drawing::PointSequence* pSequence = pOuterSequence++;
214 : 12 : const awt::Point *pPoints = pSequence->getConstArray();
215 : 12 : sal_Int32 nPointCount(pSequence->getLength());
216 : :
217 [ + - ]: 12 : if(nPointCount)
218 : : {
219 : 12 : const awt::Point aStart = pPoints[0];
220 : 12 : const awt::Point aEnd = pPoints[nPointCount - 1];
221 : :
222 [ + - ][ + - ]: 12 : if(aStart.X == aEnd.X && aStart.Y == aEnd.Y)
223 : : {
224 : 12 : bClosed = sal_True;
225 : : }
226 : : }
227 : :
228 [ + + ]: 60 : for (b = 0; b < nPointCount; b++)
229 : : {
230 : 48 : const awt::Point aPoint = pPoints[b];
231 : :
232 [ + + ]: 48 : if( aPoint.X < nMinX )
233 : 24 : nMinX = aPoint.X;
234 : :
235 [ + + ]: 48 : if( aPoint.X > nMaxX )
236 : 24 : nMaxX = aPoint.X;
237 : :
238 [ + + ]: 48 : if( aPoint.Y < nMinY )
239 : 12 : nMinY = aPoint.Y;
240 : :
241 [ + + ]: 48 : if( aPoint.Y > nMaxY )
242 : 24 : nMaxY = aPoint.Y;
243 : : }
244 : : }
245 : :
246 : 12 : sal_Int32 nDifX(nMaxX - nMinX);
247 : 12 : sal_Int32 nDifY(nMaxY - nMinY);
248 : :
249 [ + - ]: 12 : SdXMLImExViewBox aViewBox( 0, 0, nDifX, nDifY );
250 [ + - ][ + - ]: 12 : rExport.AddAttribute( XML_NAMESPACE_SVG, XML_VIEWBOX, aViewBox.GetExportString() );
251 : :
252 : : /////////////////
253 : : // Pathdata
254 [ + - ]: 12 : pOuterSequence = aBezier.Coordinates.getArray();
255 [ + - ]: 12 : drawing::FlagSequence* pOuterFlags = aBezier.Flags.getArray();
256 [ + - ]: 12 : SdXMLImExSvgDElement aSvgDElement(aViewBox, rExport);
257 : :
258 [ + + ]: 24 : for (a = 0; a < nOuterCnt; a++)
259 : : {
260 : 12 : drawing::PointSequence* pSequence = pOuterSequence++;
261 : 12 : drawing::FlagSequence* pFlags = pOuterFlags++;
262 : :
263 : : aSvgDElement.AddPolygon(pSequence, pFlags,
264 : : awt::Point( 0, 0 ),
265 : 12 : awt::Size( aViewBox.GetWidth(), aViewBox.GetHeight() ),
266 [ + - ]: 12 : bClosed);
267 : : }
268 : :
269 : : // write point array
270 [ + - ]: 12 : rExport.AddAttribute(XML_NAMESPACE_SVG, XML_D, aSvgDElement.GetExportString());
271 : :
272 : : /////////////////
273 : : // Do Write
274 : : SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_MARKER,
275 [ + - ][ + - ]: 12 : sal_True, sal_False );
[ + - ]
276 [ + - ]: 12 : }
277 : : }
278 : :
279 : 12 : return bRet;
280 : : }
281 : :
282 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|