tdf#131098 docx export: write fill property of graphic
[LibreOffice.git] / lotuswordpro / source / filter / lwpsdwgrouploaderv0102.cxx
blobc5aa189c3596135b7009146b96c232fc7ed7832b
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 /**
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)
82 // save the container
83 m_pDrawObjVector = pDrawObjVector;
85 //flag
86 unsigned char BinSignature[2];
87 m_pStream->ReadBytes(BinSignature, 2);
88 if (BinSignature[0] != 'S' || BinSignature[1] != 'M')
90 assert(false);
91 return;
93 //version
94 unsigned short nVersion;
95 m_pStream->ReadUInt16(nVersion);
96 if (nVersion<0x0102)
98 assert(false);
99 return;
101 // topObj, botObj
102 m_pStream->SeekRel(4);
103 //record count
104 unsigned short nRecCount(0);
105 m_pStream->ReadUInt16(nRecCount);
106 // selCount
107 m_pStream->SeekRel(2);
108 //boundrect
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);
114 // fileSize
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);
131 // get margin values
132 double fLeftMargin = xMyFrameLayout->GetMarginsValue(MARGIN_LEFT);
133 double fTopMargin = xMyFrameLayout->GetMarginsValue(MARGIN_TOP);
135 // frame size
136 double fFrameWidth = LwpTools::ConvertFromUnits(pFrameGeo->GetWidth());
137 double fFrameHeight = LwpTools::ConvertFromUnits(pFrameGeo->GetHeight());
139 // get frame offset
140 LwpPoint& rOffset = pMyScale->GetOffset();
141 double fOffsetX = LwpTools::ConvertFromUnits(rOffset.GetX());
142 double fOffsetY = LwpTools::ConvertFromUnits(rOffset.GetY());
144 // get scale mode
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;
176 else
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());
198 else
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();
217 //load draw object
218 for (unsigned short i = 0; i < nRecCount; i++)
220 rtl::Reference<XFFrame> xXFDrawObj = CreateDrawObject();
221 if (xXFDrawObj)
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()
235 //flag
236 unsigned char BinSignature[2];
237 if (m_pStream->ReadBytes(BinSignature, 2) != 2)
238 return nullptr;
239 if (BinSignature[0] != 'S' || BinSignature[1] != 'M')
240 return nullptr;
241 //version
242 unsigned short nVersion(0);
243 m_pStream->ReadUInt16(nVersion);
244 if (nVersion<0x0102)
245 return nullptr;
246 // topObj, botObj
247 m_pStream->SeekRel(4);
248 //record count
249 unsigned short nRecCount(0);
250 m_pStream->ReadUInt16(nRecCount);
251 // selCount
252 m_pStream->SeekRel(2);
253 //boundrect
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);
259 // fileSize
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();
270 //load draw object
271 for (unsigned short i = 0; i < nRecCount; i++)
273 rtl::Reference<XFFrame> xXFDrawObj = CreateDrawObject();
275 if (xXFDrawObj)
277 if (xXFDrawObj->GetFrameType() == enumXFFrameImage)
279 m_pDrawObjVector->push_back(xXFDrawObj);
281 else
283 xXFDrawGroup->Add(xXFDrawObj.get());
288 return xXFDrawGroup;
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()
297 //record type
298 unsigned char recType(0);
299 m_pStream->ReadUChar(recType);
301 std::unique_ptr<LwpDrawObj> pDrawObj;
302 rtl::Reference<XFFrame> xRetObject;
304 switch(recType)
306 case OT_PERPLINE://fall-through
307 case OT_LINE:
309 pDrawObj.reset(new LwpDrawLine(m_pStream, &m_aTransformData));
310 break;
312 case OT_POLYLINE:
314 pDrawObj.reset(new LwpDrawPolyLine(m_pStream, &m_aTransformData));
315 break;
317 case OT_POLYGON:
319 pDrawObj.reset(new LwpDrawPolygon(m_pStream, &m_aTransformData));
320 pDrawObj->SetObjectType(OT_POLYGON);
321 break;
323 case OT_SQUARE://fall-through
324 case OT_RECT:
326 pDrawObj.reset(new LwpDrawRectangle(m_pStream, &m_aTransformData));
327 break;
329 case OT_RNDSQUARE://fall-through
330 case OT_RNDRECT:
332 pDrawObj.reset(new LwpDrawRectangle(m_pStream, &m_aTransformData));
333 pDrawObj->SetObjectType(OT_RNDRECT);
334 break;
336 case OT_CIRCLE://fall-through
337 case OT_OVAL:
339 pDrawObj.reset(new LwpDrawEllipse(m_pStream, &m_aTransformData));
340 break;
342 case OT_ARC:
344 pDrawObj.reset(new LwpDrawArc(m_pStream, &m_aTransformData));
345 break;
347 case OT_TEXT:
349 pDrawObj.reset(new LwpDrawTextBox(m_pStream));
350 break;
352 case OT_TEXTART:
354 pDrawObj.reset(new LwpDrawTextArt(m_pStream, &m_aTransformData));
355 pDrawObj->SetObjectType(OT_TEXTART);
356 break;
358 case OT_GROUP:
360 m_pStream->SeekRel(2);
361 // read out the object header
362 pDrawObj.reset(new LwpDrawGroup(m_pStream));
364 xRetObject = CreateDrawGroupObject();
366 if (xRetObject)
368 // set anchor type
369 xRetObject->SetAnchorType(enumXFAnchorFrame);
371 break;
373 case OT_CHART://fall-through
374 case OT_METAFILE://fall-through
375 case OT_METAFILEIMG:
377 LwpDrawMetafile aMeta(m_pStream);
378 break;
380 case OT_BITMAP:
381 pDrawObj.reset(new LwpDrawBitmap(m_pStream));
382 pDrawObj->SetObjectType(OT_BITMAP);
383 break;
386 // we don't need create the corresponding XF-object of a group object.
387 if (pDrawObj && recType != OT_GROUP)
389 xRetObject = pDrawObj->CreateXFDrawObject();
392 return xRetObject;
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */