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)
75 LwpSdwGroupLoaderV0102::~LwpSdwGroupLoaderV0102()
80 * @descr entry of lwp-drawing objects. the function begins to parse the sdw-drawing bento stream and create
81 * the corresponding drawing objects.
82 * @param pDrawObjVector a container which will contains the created drawing object of XF-Model.
84 void LwpSdwGroupLoaderV0102::BeginDrawObjects(std::vector
< rtl::Reference
<XFFrame
> >* pDrawObjVector
)
87 m_pDrawObjVector
= pDrawObjVector
;
90 unsigned char BinSignature
[2];
91 m_pStream
->ReadBytes(BinSignature
, 2);
92 if (BinSignature
[0] != 'S' || BinSignature
[1] != 'M')
98 unsigned short nVersion
;
99 m_pStream
->ReadUInt16(nVersion
);
106 m_pStream
->SeekRel(4);
108 unsigned short nRecCount(0);
109 m_pStream
->ReadUInt16(nRecCount
);
111 m_pStream
->SeekRel(2);
113 unsigned short left(0),top(0),right(0),bottom(0);
114 m_pStream
->ReadUInt16(left
);
115 m_pStream
->ReadUInt16(top
);
116 m_pStream
->ReadUInt16(right
);
117 m_pStream
->ReadUInt16(bottom
);
119 m_pStream
->SeekRel(2);
121 //for calculating transformation params.
122 rtl::Reference
<LwpFrameLayout
> xMyFrameLayout(dynamic_cast<LwpFrameLayout
*>(m_pGraphicObj
->GetLayout(nullptr).get()));
123 if (xMyFrameLayout
.is())
125 LwpLayoutScale
* pMyScale
= xMyFrameLayout
->GetLayoutScale();
126 LwpLayoutGeometry
* pFrameGeo
= xMyFrameLayout
->GetGeometry();
127 if (pMyScale
&& pFrameGeo
)
129 // original drawing size
130 long nWidth
= 0, nHeight
= 0;
131 m_pGraphicObj
->GetGrafOrgSize(nWidth
, nHeight
);
132 double fGrafOrgWidth
= static_cast<double>(nWidth
)/TWIPS_PER_CM
;
133 double fGrafOrgHeight
= static_cast<double>(nHeight
)/TWIPS_PER_CM
;
136 double fLeftMargin
= xMyFrameLayout
->GetMarginsValue(MARGIN_LEFT
);
137 double fTopMargin
= xMyFrameLayout
->GetMarginsValue(MARGIN_TOP
);
140 double fFrameWidth
= LwpTools::ConvertFromUnitsToMetric(pFrameGeo
->GetWidth());
141 double fFrameHeight
= LwpTools::ConvertFromUnitsToMetric(pFrameGeo
->GetHeight());
144 LwpPoint
& rOffset
= pMyScale
->GetOffset();
145 double fOffsetX
= LwpTools::ConvertFromUnitsToMetric(rOffset
.GetX());
146 double fOffsetY
= LwpTools::ConvertFromUnitsToMetric(rOffset
.GetY());
149 sal_uInt16 nScalemode
= pMyScale
->GetScaleMode();
151 if (nScalemode
& LwpLayoutScale::CUSTOM
)
153 m_aTransformData
.fScaleX
=
154 LwpTools::ConvertFromUnitsToMetric(pMyScale
->GetScaleWidth()) / fGrafOrgWidth
;
155 m_aTransformData
.fScaleY
=
156 LwpTools::ConvertFromUnitsToMetric(pMyScale
->GetScaleHeight()) / fGrafOrgHeight
;
158 else if (nScalemode
& LwpLayoutScale::PERCENTAGE
)
160 double fScalePercentage
= static_cast<double>(pMyScale
->GetScalePercentage()) / 1000;
161 m_aTransformData
.fScaleX
= fScalePercentage
;
162 m_aTransformData
.fScaleY
= fScalePercentage
;
164 else if (nScalemode
& LwpLayoutScale::FIT_IN_FRAME
)
166 double fWidth0
= static_cast<double>(right
) / TWIPS_PER_CM
;
167 double fHeight0
= static_cast<double>(bottom
) / TWIPS_PER_CM
;
169 double fWidth1
= LwpTools::ConvertFromUnitsToMetric(pMyScale
->GetScaleWidth());
170 double fHeight1
= LwpTools::ConvertFromUnitsToMetric(pMyScale
->GetScaleHeight());
172 double fScaleX
= fWidth1
/ fWidth0
;
173 double fScaleY
= fHeight1
/ fHeight0
;
175 if (nScalemode
& LwpLayoutScale::MAINTAIN_ASPECT_RATIO
)
177 m_aTransformData
.fScaleX
= std::min(fScaleX
, fScaleY
);
178 m_aTransformData
.fScaleY
= m_aTransformData
.fScaleX
;
182 m_aTransformData
.fScaleX
= fWidth1
/ fWidth0
;
183 m_aTransformData
.fScaleY
= fHeight1
/ fHeight0
;
187 // placement: centered
188 if (xMyFrameLayout
->GetScaleCenter())
190 tools::Rectangle
aBoundRect(static_cast<long>(left
*m_aTransformData
.fScaleX
+ fLeftMargin
),
191 static_cast<long>(top
* m_aTransformData
.fScaleY
+ fTopMargin
),
192 static_cast<long>(right
* m_aTransformData
.fScaleX
),
193 static_cast<long>(bottom
* m_aTransformData
.fScaleY
));
194 Point aCenter
= aBoundRect
.Center();
196 double fNewCenterX
= (double(left
)/TWIPS_PER_CM
+ fFrameWidth
/*-fOffsetX*/) / 2;
197 double fNewCenterY
= (double(top
)/TWIPS_PER_CM
+ fFrameHeight
/*-fOffsetY*/) / 2;
199 m_aTransformData
.fOffsetX
= fNewCenterX
- static_cast<double>(aCenter
.X())/TWIPS_PER_CM
;
200 m_aTransformData
.fOffsetY
= fNewCenterY
-static_cast<double>(aCenter
.Y())/TWIPS_PER_CM
;
204 m_aTransformData
.fOffsetX
= fOffsetX
;
205 m_aTransformData
.fOffsetY
= fOffsetY
;
208 m_aTransformData
.fOffsetX
+= fLeftMargin
;
209 m_aTransformData
.fOffsetY
+= fTopMargin
;
210 m_aTransformData
.fLeftMargin
= fLeftMargin
;
211 m_aTransformData
.fTopMargin
= fTopMargin
;
215 if (nRecCount
> m_pStream
->remainingSize())
217 SAL_WARN("lwp", "stream too short for claimed no of records");
218 nRecCount
= m_pStream
->remainingSize();
222 for (unsigned short i
= 0; i
< nRecCount
; i
++)
224 XFFrame
* pXFDrawObj
= CreateDrawObject();
228 pDrawObjVector
->push_back(pXFDrawObj
);
234 * @descr when we encounter a group object the function will be called to create a XF-drawgroup object
235 * the corresponding drawing objects.
236 * @param pDrawObjVector a container which will contains the created drawing object of XF-Model.
238 XFDrawGroup
* LwpSdwGroupLoaderV0102::CreateDrawGroupObject()
241 unsigned char BinSignature
[2];
242 m_pStream
->ReadBytes(BinSignature
, 2);
243 if (BinSignature
[0] != 'S' || BinSignature
[1] != 'M')
249 unsigned short nVersion
;
250 m_pStream
->ReadUInt16(nVersion
);
257 m_pStream
->SeekRel(4);
259 unsigned short nRecCount(0);
260 m_pStream
->ReadUInt16(nRecCount
);
262 m_pStream
->SeekRel(2);
264 unsigned short left(0),top(0),right(0),bottom(0);
265 m_pStream
->ReadUInt16(left
);
266 m_pStream
->ReadUInt16(top
);
267 m_pStream
->ReadUInt16(right
);
268 m_pStream
->ReadUInt16(bottom
);
270 m_pStream
->SeekRel(2);
272 XFDrawGroup
* pXFDrawGroup
= new XFDrawGroup();
274 if (nRecCount
> m_pStream
->remainingSize())
276 SAL_WARN("lwp", "stream too short for claimed no of records");
277 nRecCount
= m_pStream
->remainingSize();
281 for (unsigned short i
= 0; i
< nRecCount
; i
++)
283 XFFrame
* pXFDrawObj
= CreateDrawObject();
287 if (pXFDrawObj
->GetFrameType() == enumXFFrameImage
)
289 m_pDrawObjVector
->push_back(pXFDrawObj
);
293 pXFDrawGroup
->Add(pXFDrawObj
);
302 * @descr Create the XF-drawing objects according to the object type read from bento stream.
303 * @return the created XF-drawing objects.
305 XFFrame
* LwpSdwGroupLoaderV0102::CreateDrawObject()
308 unsigned char recType(0);
309 m_pStream
->ReadUChar(recType
);
311 std::unique_ptr
<LwpDrawObj
> pDrawObj
;
312 XFFrame
* pRetObjct
= nullptr;
316 case OT_PERPLINE
://fall-through
319 pDrawObj
.reset(new LwpDrawLine(m_pStream
, &m_aTransformData
));
324 pDrawObj
.reset(new LwpDrawPolyLine(m_pStream
, &m_aTransformData
));
329 pDrawObj
.reset(new LwpDrawPolygon(m_pStream
, &m_aTransformData
));
330 pDrawObj
->SetObjectType(OT_POLYGON
);
333 case OT_SQUARE
://fall-through
336 pDrawObj
.reset(new LwpDrawRectangle(m_pStream
, &m_aTransformData
));
339 case OT_RNDSQUARE
://fall-through
342 pDrawObj
.reset(new LwpDrawRectangle(m_pStream
, &m_aTransformData
));
343 pDrawObj
->SetObjectType(OT_RNDRECT
);
346 case OT_CIRCLE
://fall-through
349 pDrawObj
.reset(new LwpDrawEllipse(m_pStream
, &m_aTransformData
));
354 pDrawObj
.reset(new LwpDrawArc(m_pStream
, &m_aTransformData
));
359 pDrawObj
.reset(new LwpDrawTextBox(m_pStream
));
364 pDrawObj
.reset(new LwpDrawTextArt(m_pStream
, &m_aTransformData
));
365 pDrawObj
->SetObjectType(OT_TEXTART
);
370 m_pStream
->SeekRel(2);
371 // read out the object header
372 pDrawObj
.reset(new LwpDrawGroup(m_pStream
));
374 pRetObjct
= CreateDrawGroupObject();
377 pRetObjct
->SetAnchorType(enumXFAnchorFrame
);
380 case OT_CHART
://fall-through
381 case OT_METAFILE
://fall-through
384 LwpDrawMetafile
aMeta(m_pStream
);
388 pDrawObj
.reset(new LwpDrawBitmap(m_pStream
));
389 pDrawObj
->SetObjectType(OT_BITMAP
);
393 // we don't need create the corresponding XF-object of a group object.
394 if (pDrawObj
&& recType
!= OT_GROUP
)
396 pRetObjct
= pDrawObj
->CreateXFDrawObject();
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */