1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
57 #include <tools/stream.hxx>
59 #include "lwpsdwgrouploaderv0102.hxx"
60 #include "lwpdrawobj.hxx"
61 #include <lwptools.hxx>
62 #include "lwpgrfobj.hxx"
63 #include "lwpframelayout.hxx"
65 #include <xfilter/xfdrawgroup.hxx>
66 #include <sal/log.hxx>
68 LwpSdwGroupLoaderV0102::LwpSdwGroupLoaderV0102(SvStream
* pStream
, LwpGraphicObject
* pGraphicObj
)
70 , m_pGraphicObj(pGraphicObj
)
71 , m_pDrawObjVector(nullptr)
76 * @descr entry of lwp-drawing objects. the function begins to parse the sdw-drawing bento stream and create
77 * the corresponding drawing objects.
78 * @param pDrawObjVector a container which will contains the created drawing object of XF-Model.
80 void LwpSdwGroupLoaderV0102::BeginDrawObjects(std::vector
< rtl::Reference
<XFFrame
> >* pDrawObjVector
)
83 m_pDrawObjVector
= pDrawObjVector
;
86 unsigned char BinSignature
[2];
87 m_pStream
->ReadBytes(BinSignature
, 2);
88 if (BinSignature
[0] != 'S' || BinSignature
[1] != 'M')
94 unsigned short nVersion
;
95 m_pStream
->ReadUInt16(nVersion
);
102 m_pStream
->SeekRel(4);
104 unsigned short nRecCount(0);
105 m_pStream
->ReadUInt16(nRecCount
);
107 m_pStream
->SeekRel(2);
109 unsigned short left(0),top(0),right(0),bottom(0);
110 m_pStream
->ReadUInt16(left
);
111 m_pStream
->ReadUInt16(top
);
112 m_pStream
->ReadUInt16(right
);
113 m_pStream
->ReadUInt16(bottom
);
115 m_pStream
->SeekRel(2);
117 //for calculating transformation params.
118 rtl::Reference
<LwpFrameLayout
> xMyFrameLayout(dynamic_cast<LwpFrameLayout
*>(m_pGraphicObj
->GetLayout(nullptr).get()));
119 if (xMyFrameLayout
.is())
121 LwpLayoutScale
* pMyScale
= xMyFrameLayout
->GetLayoutScale();
122 LwpLayoutGeometry
* pFrameGeo
= xMyFrameLayout
->GetGeometry();
123 if (pMyScale
&& pFrameGeo
)
125 // original drawing size
126 tools::Long nWidth
= 0, nHeight
= 0;
127 m_pGraphicObj
->GetGrafOrgSize(nWidth
, nHeight
);
128 double fGrafOrgWidth
= LwpTools::ConvertFromTwips(nWidth
);
129 double fGrafOrgHeight
= LwpTools::ConvertFromTwips(nHeight
);
132 double fLeftMargin
= xMyFrameLayout
->GetMarginsValue(MARGIN_LEFT
);
133 double fTopMargin
= xMyFrameLayout
->GetMarginsValue(MARGIN_TOP
);
136 double fFrameWidth
= LwpTools::ConvertFromUnits(pFrameGeo
->GetWidth());
137 double fFrameHeight
= LwpTools::ConvertFromUnits(pFrameGeo
->GetHeight());
140 LwpPoint
& rOffset
= pMyScale
->GetOffset();
141 double fOffsetX
= LwpTools::ConvertFromUnits(rOffset
.GetX());
142 double fOffsetY
= LwpTools::ConvertFromUnits(rOffset
.GetY());
145 sal_uInt16 nScalemode
= pMyScale
->GetScaleMode();
147 if (nScalemode
& LwpLayoutScale::CUSTOM
)
149 m_aTransformData
.fScaleX
=
150 LwpTools::ConvertFromUnits(pMyScale
->GetScaleWidth()) / fGrafOrgWidth
;
151 m_aTransformData
.fScaleY
=
152 LwpTools::ConvertFromUnits(pMyScale
->GetScaleHeight()) / fGrafOrgHeight
;
154 else if (nScalemode
& LwpLayoutScale::PERCENTAGE
)
156 double fScalePercentage
= static_cast<double>(pMyScale
->GetScalePercentage()) / 1000;
157 m_aTransformData
.fScaleX
= fScalePercentage
;
158 m_aTransformData
.fScaleY
= fScalePercentage
;
160 else if (nScalemode
& LwpLayoutScale::FIT_IN_FRAME
)
162 double fWidth0
= LwpTools::ConvertFromTwips(right
);
163 double fHeight0
= LwpTools::ConvertFromTwips(bottom
);
165 double fWidth1
= LwpTools::ConvertFromUnits(pMyScale
->GetScaleWidth());
166 double fHeight1
= LwpTools::ConvertFromUnits(pMyScale
->GetScaleHeight());
168 double fScaleX
= fWidth1
/ fWidth0
;
169 double fScaleY
= fHeight1
/ fHeight0
;
171 if (nScalemode
& LwpLayoutScale::MAINTAIN_ASPECT_RATIO
)
173 m_aTransformData
.fScaleX
= std::min(fScaleX
, fScaleY
);
174 m_aTransformData
.fScaleY
= m_aTransformData
.fScaleX
;
178 m_aTransformData
.fScaleX
= fWidth1
/ fWidth0
;
179 m_aTransformData
.fScaleY
= fHeight1
/ fHeight0
;
183 // placement: centered
184 if (xMyFrameLayout
->GetScaleCenter())
186 tools::Rectangle
aBoundRect(static_cast<tools::Long
>(left
*m_aTransformData
.fScaleX
+ fLeftMargin
),
187 static_cast<tools::Long
>(top
* m_aTransformData
.fScaleY
+ fTopMargin
),
188 static_cast<tools::Long
>(right
* m_aTransformData
.fScaleX
),
189 static_cast<tools::Long
>(bottom
* m_aTransformData
.fScaleY
));
190 Point aCenter
= aBoundRect
.Center();
192 double fNewCenterX
= (LwpTools::ConvertFromTwips(left
) + fFrameWidth
/*-fOffsetX*/) / 2;
193 double fNewCenterY
= (LwpTools::ConvertFromTwips(top
) + fFrameHeight
/*-fOffsetY*/) / 2;
195 m_aTransformData
.fOffsetX
= fNewCenterX
- LwpTools::ConvertFromTwips(aCenter
.X());
196 m_aTransformData
.fOffsetY
= fNewCenterY
- LwpTools::ConvertFromTwips(aCenter
.Y());
200 m_aTransformData
.fOffsetX
= fOffsetX
;
201 m_aTransformData
.fOffsetY
= fOffsetY
;
204 m_aTransformData
.fOffsetX
+= fLeftMargin
;
205 m_aTransformData
.fOffsetY
+= fTopMargin
;
206 m_aTransformData
.fLeftMargin
= fLeftMargin
;
207 m_aTransformData
.fTopMargin
= fTopMargin
;
211 if (nRecCount
> m_pStream
->remainingSize())
213 SAL_WARN("lwp", "stream too short for claimed no of records");
214 nRecCount
= m_pStream
->remainingSize();
218 for (unsigned short i
= 0; i
< nRecCount
; i
++)
220 rtl::Reference
<XFFrame
> xXFDrawObj
= CreateDrawObject();
223 pDrawObjVector
->push_back(xXFDrawObj
);
229 * @descr when we encounter a group object the function will be called to create a XF-drawgroup object
230 * the corresponding drawing objects.
231 * @param pDrawObjVector a container which will contains the created drawing object of XF-Model.
233 rtl::Reference
<XFDrawGroup
> LwpSdwGroupLoaderV0102::CreateDrawGroupObject()
236 unsigned char BinSignature
[2];
237 if (m_pStream
->ReadBytes(BinSignature
, 2) != 2)
239 if (BinSignature
[0] != 'S' || BinSignature
[1] != 'M')
242 unsigned short nVersion(0);
243 m_pStream
->ReadUInt16(nVersion
);
247 m_pStream
->SeekRel(4);
249 unsigned short nRecCount(0);
250 m_pStream
->ReadUInt16(nRecCount
);
252 m_pStream
->SeekRel(2);
254 unsigned short left(0),top(0),right(0),bottom(0);
255 m_pStream
->ReadUInt16(left
);
256 m_pStream
->ReadUInt16(top
);
257 m_pStream
->ReadUInt16(right
);
258 m_pStream
->ReadUInt16(bottom
);
260 m_pStream
->SeekRel(2);
262 rtl::Reference
<XFDrawGroup
> xXFDrawGroup(new XFDrawGroup());
264 if (nRecCount
> m_pStream
->remainingSize())
266 SAL_WARN("lwp", "stream too short for claimed no of records");
267 nRecCount
= m_pStream
->remainingSize();
271 for (unsigned short i
= 0; i
< nRecCount
; i
++)
273 rtl::Reference
<XFFrame
> xXFDrawObj
= CreateDrawObject();
277 if (xXFDrawObj
->GetFrameType() == enumXFFrameImage
)
279 m_pDrawObjVector
->push_back(xXFDrawObj
);
283 xXFDrawGroup
->Add(xXFDrawObj
.get());
292 * @descr Create the XF-drawing objects according to the object type read from bento stream.
293 * @return the created XF-drawing objects.
295 rtl::Reference
<XFFrame
> LwpSdwGroupLoaderV0102::CreateDrawObject()
298 unsigned char recType(0);
299 m_pStream
->ReadUChar(recType
);
301 std::unique_ptr
<LwpDrawObj
> pDrawObj
;
302 rtl::Reference
<XFFrame
> xRetObject
;
306 case OT_PERPLINE
://fall-through
309 pDrawObj
.reset(new LwpDrawLine(m_pStream
, &m_aTransformData
));
314 pDrawObj
.reset(new LwpDrawPolyLine(m_pStream
, &m_aTransformData
));
319 pDrawObj
.reset(new LwpDrawPolygon(m_pStream
, &m_aTransformData
));
320 pDrawObj
->SetObjectType(OT_POLYGON
);
323 case OT_SQUARE
://fall-through
326 pDrawObj
.reset(new LwpDrawRectangle(m_pStream
, &m_aTransformData
));
329 case OT_RNDSQUARE
://fall-through
332 pDrawObj
.reset(new LwpDrawRectangle(m_pStream
, &m_aTransformData
));
333 pDrawObj
->SetObjectType(OT_RNDRECT
);
336 case OT_CIRCLE
://fall-through
339 pDrawObj
.reset(new LwpDrawEllipse(m_pStream
, &m_aTransformData
));
344 pDrawObj
.reset(new LwpDrawArc(m_pStream
, &m_aTransformData
));
349 pDrawObj
.reset(new LwpDrawTextBox(m_pStream
));
354 pDrawObj
.reset(new LwpDrawTextArt(m_pStream
, &m_aTransformData
));
355 pDrawObj
->SetObjectType(OT_TEXTART
);
360 m_pStream
->SeekRel(2);
361 // read out the object header
362 pDrawObj
.reset(new LwpDrawGroup(m_pStream
));
364 xRetObject
= CreateDrawGroupObject();
369 xRetObject
->SetAnchorType(enumXFAnchorFrame
);
373 case OT_CHART
://fall-through
374 case OT_METAFILE
://fall-through
377 LwpDrawMetafile
aMeta(m_pStream
);
381 pDrawObj
.reset(new LwpDrawBitmap(m_pStream
));
382 pDrawObj
->SetObjectType(OT_BITMAP
);
386 // we don't need create the corresponding XF-object of a group object.
387 if (pDrawObj
&& recType
!= OT_GROUP
)
389 xRetObject
= pDrawObj
->CreateXFDrawObject();
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */