tdf#137105: crash in table with Style Inspector active
[LibreOffice.git] / lotuswordpro / source / filter / lwpsdwgrouploaderv0102.cxx
blob5c8d9941344a469960bf873103503f8801c6d3f1
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,
29 * MA 02111-1307 USA
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)
69 : m_pStream(pStream)
70 , m_pGraphicObj(pGraphicObj)
71 , m_pDrawObjVector(nullptr)
75 LwpSdwGroupLoaderV0102::~LwpSdwGroupLoaderV0102()
79 /**
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)
86 // save the container
87 m_pDrawObjVector = pDrawObjVector;
89 //flag
90 unsigned char BinSignature[2];
91 m_pStream->ReadBytes(BinSignature, 2);
92 if (BinSignature[0] != 'S' || BinSignature[1] != 'M')
94 assert(false);
95 return;
97 //version
98 unsigned short nVersion;
99 m_pStream->ReadUInt16(nVersion);
100 if (nVersion<0x0102)
102 assert(false);
103 return;
105 // topObj, botObj
106 m_pStream->SeekRel(4);
107 //record count
108 unsigned short nRecCount(0);
109 m_pStream->ReadUInt16(nRecCount);
110 // selCount
111 m_pStream->SeekRel(2);
112 //boundrect
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);
118 // fileSize
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;
135 // get margin values
136 double fLeftMargin = xMyFrameLayout->GetMarginsValue(MARGIN_LEFT);
137 double fTopMargin = xMyFrameLayout->GetMarginsValue(MARGIN_TOP);
139 // frame size
140 double fFrameWidth = LwpTools::ConvertFromUnitsToMetric(pFrameGeo->GetWidth());
141 double fFrameHeight = LwpTools::ConvertFromUnitsToMetric(pFrameGeo->GetHeight());
143 // get frame offset
144 LwpPoint& rOffset = pMyScale->GetOffset();
145 double fOffsetX = LwpTools::ConvertFromUnitsToMetric(rOffset.GetX());
146 double fOffsetY = LwpTools::ConvertFromUnitsToMetric(rOffset.GetY());
148 // get scale mode
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;
180 else
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;
202 else
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();
221 //load draw object
222 for (unsigned short i = 0; i < nRecCount; i++)
224 XFFrame* pXFDrawObj = CreateDrawObject();
226 if (pXFDrawObj)
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()
240 //flag
241 unsigned char BinSignature[2];
242 m_pStream->ReadBytes(BinSignature, 2);
243 if (BinSignature[0] != 'S' || BinSignature[1] != 'M')
245 assert(false);
246 return nullptr;
248 //version
249 unsigned short nVersion;
250 m_pStream->ReadUInt16(nVersion);
251 if (nVersion<0x0102)
253 assert(false);
254 return nullptr;
256 // topObj, botObj
257 m_pStream->SeekRel(4);
258 //record count
259 unsigned short nRecCount(0);
260 m_pStream->ReadUInt16(nRecCount);
261 // selCount
262 m_pStream->SeekRel(2);
263 //boundrect
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);
269 // fileSize
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();
280 //load draw object
281 for (unsigned short i = 0; i < nRecCount; i++)
283 XFFrame* pXFDrawObj = CreateDrawObject();
285 if (pXFDrawObj)
287 if (pXFDrawObj->GetFrameType() == enumXFFrameImage)
289 m_pDrawObjVector->push_back(pXFDrawObj);
291 else
293 pXFDrawGroup->Add(pXFDrawObj);
298 return pXFDrawGroup;
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()
307 //record type
308 unsigned char recType(0);
309 m_pStream->ReadUChar(recType);
311 std::unique_ptr<LwpDrawObj> pDrawObj;
312 XFFrame* pRetObjct = nullptr;
314 switch(recType)
316 case OT_PERPLINE://fall-through
317 case OT_LINE:
319 pDrawObj.reset(new LwpDrawLine(m_pStream, &m_aTransformData));
320 break;
322 case OT_POLYLINE:
324 pDrawObj.reset(new LwpDrawPolyLine(m_pStream, &m_aTransformData));
325 break;
327 case OT_POLYGON:
329 pDrawObj.reset(new LwpDrawPolygon(m_pStream, &m_aTransformData));
330 pDrawObj->SetObjectType(OT_POLYGON);
331 break;
333 case OT_SQUARE://fall-through
334 case OT_RECT:
336 pDrawObj.reset(new LwpDrawRectangle(m_pStream, &m_aTransformData));
337 break;
339 case OT_RNDSQUARE://fall-through
340 case OT_RNDRECT:
342 pDrawObj.reset(new LwpDrawRectangle(m_pStream, &m_aTransformData));
343 pDrawObj->SetObjectType(OT_RNDRECT);
344 break;
346 case OT_CIRCLE://fall-through
347 case OT_OVAL:
349 pDrawObj.reset(new LwpDrawEllipse(m_pStream, &m_aTransformData));
350 break;
352 case OT_ARC:
354 pDrawObj.reset(new LwpDrawArc(m_pStream, &m_aTransformData));
355 break;
357 case OT_TEXT:
359 pDrawObj.reset(new LwpDrawTextBox(m_pStream));
360 break;
362 case OT_TEXTART:
364 pDrawObj.reset(new LwpDrawTextArt(m_pStream, &m_aTransformData));
365 pDrawObj->SetObjectType(OT_TEXTART);
366 break;
368 case OT_GROUP:
370 m_pStream->SeekRel(2);
371 // read out the object header
372 pDrawObj.reset(new LwpDrawGroup(m_pStream));
374 pRetObjct = CreateDrawGroupObject();
376 // set anchor type
377 pRetObjct->SetAnchorType(enumXFAnchorFrame);
378 break;
380 case OT_CHART://fall-through
381 case OT_METAFILE://fall-through
382 case OT_METAFILEIMG:
384 LwpDrawMetafile aMeta(m_pStream);
385 break;
387 case OT_BITMAP:
388 pDrawObj.reset(new LwpDrawBitmap(m_pStream));
389 pDrawObj->SetObjectType(OT_BITMAP);
390 break;
393 // we don't need create the corresponding XF-object of a group object.
394 if (pDrawObj && recType != OT_GROUP)
396 pRetObjct = pDrawObj->CreateXFDrawObject();
399 return pRetObjct;
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */