1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include "xmloff/MarkerStyle.hxx"
27cdf0e10cSrcweir #include "xexptran.hxx"
28cdf0e10cSrcweir #include <xmloff/attrlist.hxx>
29cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
30cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
31cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
32cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
33cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
34cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
35cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36cdf0e10cSrcweir #include <rtl/ustring.hxx>
37cdf0e10cSrcweir #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir using ::rtl::OUString;
41cdf0e10cSrcweir using ::rtl::OUStringBuffer;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace ::xmloff::token;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //-------------------------------------------------------------
47cdf0e10cSrcweir // Import
48cdf0e10cSrcweir //-------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir XMLMarkerStyleImport::XMLMarkerStyleImport( SvXMLImport& rImp )
51cdf0e10cSrcweir     : rImport( rImp )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir XMLMarkerStyleImport::~XMLMarkerStyleImport()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir sal_Bool XMLMarkerStyleImport::importXML(
60cdf0e10cSrcweir     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
61cdf0e10cSrcweir     uno::Any& rValue,
62cdf0e10cSrcweir     OUString& rStrName )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	sal_Bool bHasViewBox    = sal_False;
65cdf0e10cSrcweir 	sal_Bool bHasPathData   = sal_False;
66cdf0e10cSrcweir 	OUString aDisplayName;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	SdXMLImExViewBox* pViewBox = NULL;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
71cdf0e10cSrcweir     SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	OUString strPathData;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
76cdf0e10cSrcweir 	for( sal_Int16 i = 0; i < nAttrCount; i++ )
77cdf0e10cSrcweir 	{
78cdf0e10cSrcweir 		OUString aStrFullAttrName = xAttrList->getNameByIndex( i );
79cdf0e10cSrcweir 		OUString aStrAttrName;
80cdf0e10cSrcweir 		rNamespaceMap.GetKeyByAttrName( aStrFullAttrName, &aStrAttrName );
81cdf0e10cSrcweir 		OUString aStrValue = xAttrList->getValueByIndex( i );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 		if( IsXMLToken( aStrAttrName, XML_NAME ) )
84cdf0e10cSrcweir 		{
85cdf0e10cSrcweir 			rStrName = aStrValue;
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 		else if( IsXMLToken( aStrAttrName, XML_DISPLAY_NAME ) )
88cdf0e10cSrcweir 		{
89cdf0e10cSrcweir 			aDisplayName = aStrValue;
90cdf0e10cSrcweir 		}
91cdf0e10cSrcweir 		else if( IsXMLToken( aStrAttrName, XML_VIEWBOX ) )
92cdf0e10cSrcweir 		{
93cdf0e10cSrcweir 			pViewBox = new SdXMLImExViewBox( aStrValue, rUnitConverter );
94cdf0e10cSrcweir 			bHasViewBox = sal_True;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 		}
97cdf0e10cSrcweir 		else if( IsXMLToken( aStrAttrName, XML_D ) )
98cdf0e10cSrcweir 		{
99cdf0e10cSrcweir 			strPathData = aStrValue;
100cdf0e10cSrcweir 			bHasPathData = sal_True;
101cdf0e10cSrcweir 		}
102cdf0e10cSrcweir 	}
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	if( bHasViewBox && bHasPathData )
105cdf0e10cSrcweir 	{
106cdf0e10cSrcweir 		SdXMLImExSvgDElement aPoints(strPathData, *pViewBox, awt::Point( 0, 0 ),
107cdf0e10cSrcweir 			awt::Size( pViewBox->GetWidth(), pViewBox->GetHeight() ),
108cdf0e10cSrcweir 			rUnitConverter );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 		if(aPoints.IsCurve())
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			drawing::PolyPolygonBezierCoords aSourcePolyPolygon(
113cdf0e10cSrcweir 				aPoints.GetPointSequenceSequence(),
114cdf0e10cSrcweir 				aPoints.GetFlagSequenceSequence());
115cdf0e10cSrcweir 			rValue <<= aSourcePolyPolygon;
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir 		else
118cdf0e10cSrcweir 		{
119cdf0e10cSrcweir 			drawing::PolyPolygonBezierCoords aSourcePolyPolygon;
120cdf0e10cSrcweir 			aSourcePolyPolygon.Coordinates = aPoints.GetPointSequenceSequence();
121cdf0e10cSrcweir 			aSourcePolyPolygon.Flags.realloc(aSourcePolyPolygon.Coordinates.getLength());
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 			// Zeiger auf innere sequences holen
124cdf0e10cSrcweir 			const drawing::PointSequence* pInnerSequence = aSourcePolyPolygon.Coordinates.getConstArray();
125cdf0e10cSrcweir 			drawing::FlagSequence* pInnerSequenceFlags = aSourcePolyPolygon.Flags.getArray();
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 			for(sal_Int32 a(0); a < aSourcePolyPolygon.Coordinates.getLength(); a++)
128cdf0e10cSrcweir 			{
129cdf0e10cSrcweir 				pInnerSequenceFlags->realloc(pInnerSequence->getLength());
130cdf0e10cSrcweir 				drawing::PolygonFlags* pPolyFlags = pInnerSequenceFlags->getArray();
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 				for(sal_Int32 b(0); b < pInnerSequence->getLength(); b++)
133cdf0e10cSrcweir 					*pPolyFlags++ = drawing::PolygonFlags_NORMAL;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 				// next run
136cdf0e10cSrcweir 				pInnerSequence++;
137cdf0e10cSrcweir 				pInnerSequenceFlags++;
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 			rValue <<= aSourcePolyPolygon;
141cdf0e10cSrcweir 		}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 		if( aDisplayName.getLength() )
144cdf0e10cSrcweir 		{
145cdf0e10cSrcweir 			rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_MARKER_ID, rStrName,
146cdf0e10cSrcweir 										aDisplayName );
147cdf0e10cSrcweir 			rStrName = aDisplayName;
148cdf0e10cSrcweir 		}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	if( pViewBox )
153cdf0e10cSrcweir 		delete pViewBox;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	return bHasViewBox && bHasPathData;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 
159cdf0e10cSrcweir //-------------------------------------------------------------
160cdf0e10cSrcweir // Export
161cdf0e10cSrcweir //-------------------------------------------------------------
162cdf0e10cSrcweir 
163cdf0e10cSrcweir #ifndef SVX_LIGHT
164cdf0e10cSrcweir 
165cdf0e10cSrcweir XMLMarkerStyleExport::XMLMarkerStyleExport( SvXMLExport& rExp )
166cdf0e10cSrcweir     : rExport( rExp )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir XMLMarkerStyleExport::~XMLMarkerStyleExport()
171cdf0e10cSrcweir {
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir sal_Bool XMLMarkerStyleExport::exportXML(
175cdf0e10cSrcweir     const OUString& rStrName,
176cdf0e10cSrcweir     const uno::Any& rValue )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	sal_Bool bRet(sal_False);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	if(rStrName.getLength())
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		drawing::PolyPolygonBezierCoords aBezier;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 		if(rValue >>= aBezier)
185cdf0e10cSrcweir 		{
186cdf0e10cSrcweir 			OUString aStrValue;
187cdf0e10cSrcweir 			OUStringBuffer aOut;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 			/////////////////
190cdf0e10cSrcweir 			// Name
191cdf0e10cSrcweir 			sal_Bool bEncoded = sal_False;
192cdf0e10cSrcweir 			OUString aStrName( rStrName );
193cdf0e10cSrcweir 			rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
194cdf0e10cSrcweir 								  rExport.EncodeStyleName( aStrName,
195cdf0e10cSrcweir 														   &bEncoded ) );
196cdf0e10cSrcweir 			if( bEncoded )
197cdf0e10cSrcweir 				rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
198cdf0e10cSrcweir 									  aStrName );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 			/////////////////
201cdf0e10cSrcweir 			// Viewbox (viewBox="0 0 1500 1000")
202cdf0e10cSrcweir 			sal_Int32 nMinX(0x7fffffff);
203cdf0e10cSrcweir 			sal_Int32 nMaxX(0x80000000);
204cdf0e10cSrcweir 			sal_Int32 nMinY(0x7fffffff);
205cdf0e10cSrcweir 			sal_Int32 nMaxY(0x80000000);
206cdf0e10cSrcweir 			sal_Int32 nOuterCnt(aBezier.Coordinates.getLength());
207cdf0e10cSrcweir 			drawing::PointSequence* pOuterSequence = aBezier.Coordinates.getArray();
208cdf0e10cSrcweir 			sal_Int32 a, b;
209cdf0e10cSrcweir 			sal_Bool bClosed(sal_False);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             for (a = 0; a < nOuterCnt; a++)
212cdf0e10cSrcweir 			{
213cdf0e10cSrcweir 				drawing::PointSequence* pSequence = pOuterSequence++;
214cdf0e10cSrcweir 				const awt::Point *pPoints = pSequence->getConstArray();
215cdf0e10cSrcweir 				sal_Int32 nPointCount(pSequence->getLength());
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 				if(nPointCount)
218cdf0e10cSrcweir 				{
219cdf0e10cSrcweir 					const awt::Point aStart = pPoints[0];
220cdf0e10cSrcweir 					const awt::Point aEnd = pPoints[nPointCount - 1];
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 					if(aStart.X == aEnd.X && aStart.Y == aEnd.Y)
223cdf0e10cSrcweir 					{
224cdf0e10cSrcweir 						bClosed = sal_True;
225cdf0e10cSrcweir 					}
226cdf0e10cSrcweir 				}
227cdf0e10cSrcweir 
228cdf0e10cSrcweir                 for (b = 0; b < nPointCount; b++)
229cdf0e10cSrcweir 				{
230cdf0e10cSrcweir 					const awt::Point aPoint = pPoints[b];
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 					if( aPoint.X < nMinX )
233cdf0e10cSrcweir 						nMinX = aPoint.X;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 					if( aPoint.X > nMaxX )
236cdf0e10cSrcweir 						nMaxX = aPoint.X;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 					if( aPoint.Y < nMinY )
239cdf0e10cSrcweir 						nMinY = aPoint.Y;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 					if( aPoint.Y > nMaxY )
242cdf0e10cSrcweir 						nMaxY = aPoint.Y;
243cdf0e10cSrcweir 				}
244cdf0e10cSrcweir 			}
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 			sal_Int32 nDifX(nMaxX - nMinX);
247cdf0e10cSrcweir 			sal_Int32 nDifY(nMaxY - nMinY);
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 			SdXMLImExViewBox aViewBox( 0, 0, nDifX, nDifY );
250cdf0e10cSrcweir 			rExport.AddAttribute( XML_NAMESPACE_SVG, XML_VIEWBOX, aViewBox.GetExportString() );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 			/////////////////
253cdf0e10cSrcweir 			// Pathdata
254cdf0e10cSrcweir 			pOuterSequence = aBezier.Coordinates.getArray();
255cdf0e10cSrcweir 			drawing::FlagSequence*  pOuterFlags = aBezier.Flags.getArray();
256cdf0e10cSrcweir 			SdXMLImExSvgDElement aSvgDElement(aViewBox);
257cdf0e10cSrcweir 
258cdf0e10cSrcweir             for (a = 0; a < nOuterCnt; a++)
259cdf0e10cSrcweir 			{
260cdf0e10cSrcweir 				drawing::PointSequence* pSequence = pOuterSequence++;
261cdf0e10cSrcweir 				drawing::FlagSequence* pFlags = pOuterFlags++;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 				aSvgDElement.AddPolygon(pSequence, pFlags,
264cdf0e10cSrcweir 					awt::Point( 0, 0 ),
265cdf0e10cSrcweir 					awt::Size( aViewBox.GetWidth(), aViewBox.GetHeight() ),
266cdf0e10cSrcweir 					bClosed);
267cdf0e10cSrcweir 			}
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 			// write point array
270cdf0e10cSrcweir 			rExport.AddAttribute(XML_NAMESPACE_SVG, XML_D, aSvgDElement.GetExportString());
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 			/////////////////
273cdf0e10cSrcweir 			// Do Write
274cdf0e10cSrcweir             SvXMLElementExport rElem( rExport, XML_NAMESPACE_DRAW, XML_MARKER,
275cdf0e10cSrcweir                                       sal_True, sal_False );
276cdf0e10cSrcweir 		}
277cdf0e10cSrcweir 	}
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	return bRet;
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir #endif // #ifndef SVX_LIGHT
283