Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / html / htmlexp2.cxx
blob9c2113ecfd1c251e76738646f93c2db1b98826d8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svx/svditer.hxx>
21 #include <svx/svdograf.hxx>
22 #include <svx/svdoole2.hxx>
23 #include <svx/svdpage.hxx>
24 #include <svx/xoutbmp.hxx>
25 #include <svx/svdxcgv.hxx>
26 #include <sot/exchange.hxx>
27 #include <svtools/htmlkywd.hxx>
28 #include <svtools/htmlout.hxx>
29 #include <svtools/transfer.hxx>
30 #include <svtools/embedtransfer.hxx>
31 #include <svl/urihelper.hxx>
32 #include <tools/urlobj.hxx>
34 #include "htmlexp.hxx"
35 #include "global.hxx"
36 #include "document.hxx"
37 #include "drwlayer.hxx"
38 #include "ftools.hxx"
39 #include <rtl/strbuf.hxx>
41 using namespace com::sun::star;
43 void ScHTMLExport::PrepareGraphics( ScDrawLayer* pDrawLayer, SCTAB nTab,
44 SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow )
46 if ( pDrawLayer->HasObjectsInRows( nTab, nStartRow, nEndRow ) )
48 SdrPage* pDrawPage = pDrawLayer->GetPage( static_cast<sal_uInt16>(nTab) );
49 if ( pDrawPage )
51 bTabHasGraphics = true;
52 FillGraphList( pDrawPage, nTab, nStartCol, nStartRow, nEndCol, nEndRow );
53 size_t ListSize = aGraphList.size();
54 for ( size_t i = 0; i < ListSize; ++i )
56 ScHTMLGraphEntry* pE = &aGraphList[ i ];
57 if ( !pE->bInCell )
58 { // not all cells: table next to some
59 bTabAlignedLeft = true;
60 break;
67 void ScHTMLExport::FillGraphList( const SdrPage* pPage, SCTAB nTab,
68 SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow )
70 if ( pPage->GetObjCount() )
72 Rectangle aRect;
73 if ( !bAll )
74 aRect = pDoc->GetMMRect( nStartCol, nStartRow, nEndCol, nEndRow, nTab );
75 SdrObjListIter aIter( *pPage, IM_FLAT );
76 SdrObject* pObject = aIter.Next();
77 while ( pObject )
79 Rectangle aObjRect = pObject->GetCurrentBoundRect();
80 if ( (bAll || aRect.IsInside( aObjRect )) && !ScDrawLayer::IsNoteCaption(pObject) )
82 Size aSpace;
83 ScRange aR = pDoc->GetRange( nTab, aObjRect );
84 // Rectangle in mm/100
85 Size aSize( MMToPixel( aObjRect.GetSize() ) );
86 // If the image is somewhere in a merged range we must
87 // move the anchor to the upper left (THE span cell).
88 pDoc->ExtendOverlapped( aR );
89 SCCOL nCol1 = aR.aStart.Col();
90 SCROW nRow1 = aR.aStart.Row();
91 SCCOL nCol2 = aR.aEnd.Col();
92 SCROW nRow2 = aR.aEnd.Row();
93 // All cells empty under object?
94 bool bInCell = (pDoc->GetEmptyLinesInBlock(
95 nCol1, nRow1, nTab, nCol2, nRow2, nTab, DIR_TOP )
96 == static_cast< SCSIZE >( nRow2 - nRow1 )); // rows-1 !
97 if ( bInCell )
98 { // Spacing in spanning cell
99 Rectangle aCellRect = pDoc->GetMMRect(
100 nCol1, nRow1, nCol2, nRow2, nTab );
101 aSpace = MMToPixel( Size(
102 aCellRect.GetWidth() - aObjRect.GetWidth(),
103 aCellRect.GetHeight() - aObjRect.GetHeight() ));
104 aSpace.Width() += (nCol2-nCol1) * (nCellSpacing+1);
105 aSpace.Height() += (nRow2-nRow1) * (nCellSpacing+1);
106 aSpace.Width() /= 2;
107 aSpace.Height() /= 2;
109 aGraphList.push_back( ScHTMLGraphEntry( pObject,
110 aR, aSize, bInCell, aSpace ) );
112 pObject = aIter.Next();
117 void ScHTMLExport::WriteGraphEntry( ScHTMLGraphEntry* pE )
119 SdrObject* pObject = pE->pObject;
120 OStringBuffer aBuf;
121 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_width).append('=').
122 append(static_cast<sal_Int32>(pE->aSize.Width()));
123 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_height).append('=').
124 append(static_cast<sal_Int32>(pE->aSize.Height()));
125 if ( pE->bInCell )
127 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_hspace).append('=').
128 append(static_cast<sal_Int32>(pE->aSpace.Width()));
129 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_vspace).append('=').
130 append(static_cast<sal_Int32>(pE->aSpace.Height()));
132 OString aOpt = aBuf.makeStringAndClear();
133 switch ( pObject->GetObjIdentifier() )
135 case OBJ_GRAF:
137 const SdrGrafObj* pSGO = static_cast<SdrGrafObj*>(pObject);
138 const SdrGrafObjGeoData* pGeo = static_cast<SdrGrafObjGeoData*>(pSGO->GetGeoData());
139 sal_uInt16 nMirrorCase = (pGeo->aGeo.nRotationAngle == 18000 ?
140 ( pGeo->bMirrored ? 3 : 4 ) : ( pGeo->bMirrored ? 2 : 1 ));
141 bool bHMirr = ( ( nMirrorCase == 2 ) || ( nMirrorCase == 4 ) );
142 bool bVMirr = ( ( nMirrorCase == 3 ) || ( nMirrorCase == 4 ) );
143 XOutFlags nXOutFlags = XOutFlags::NONE;
144 if ( bHMirr )
145 nXOutFlags |= XOutFlags::MirrorHorz;
146 if ( bVMirr )
147 nXOutFlags |= XOutFlags::MirrorVert;
148 OUString aLinkName;
149 if ( pSGO->IsLinkedGraphic() )
150 aLinkName = pSGO->GetFileName();
151 WriteImage( aLinkName, pSGO->GetGraphic(), aOpt, nXOutFlags );
152 pE->bWritten = true;
154 break;
155 case OBJ_OLE2:
157 const Graphic* pGraphic = static_cast<SdrOle2Obj*>(pObject)->GetGraphic();
158 if ( pGraphic )
160 OUString aLinkName;
161 WriteImage( aLinkName, *pGraphic, aOpt );
162 pE->bWritten = true;
165 break;
166 default:
168 Graphic aGraph( SdrExchangeView::GetObjGraphic(
169 pDoc->GetDrawLayer(), pObject ) );
170 OUString aLinkName;
171 WriteImage( aLinkName, aGraph, aOpt );
172 pE->bWritten = true;
177 void ScHTMLExport::WriteImage( OUString& rLinkName, const Graphic& rGrf,
178 const OString& rImgOptions, XOutFlags nXOutFlags )
180 // Embedded graphic -> create an image file
181 if( rLinkName.isEmpty() )
183 if( !aStreamPath.isEmpty() )
185 // Save as a PNG
186 OUString aGrfNm( aStreamPath );
187 nXOutFlags |= XOutFlags::UseNativeIfPossible;
188 sal_uInt16 nErr = XOutBitmap::WriteGraphic( rGrf, aGrfNm,
189 "PNG", nXOutFlags );
191 // If it worked, create a URL for the IMG tag
192 if( !nErr )
194 rLinkName = URIHelper::SmartRel2Abs(
195 INetURLObject(aBaseURL),
196 aGrfNm,
197 URIHelper::GetMaybeFileHdl());
198 if ( HasCId() )
199 MakeCIdURL( rLinkName );
203 else
205 // Linked graphic - figure out the URL for the IMG tag
206 if( bCopyLocalFileToINet || HasCId() )
208 CopyLocalFileToINet( rLinkName, aStreamPath );
209 if ( HasCId() )
210 MakeCIdURL( rLinkName );
212 else
213 rLinkName = URIHelper::SmartRel2Abs(
214 INetURLObject(aBaseURL),
215 rLinkName,
216 URIHelper::GetMaybeFileHdl());
219 // If a URL was set, output the IMG tag.
220 // <IMG SRC="..."[ rImgOptions]>
221 if( !rLinkName.isEmpty() )
223 rStrm.WriteChar( '<' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_image ).WriteChar( ' ' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_O_src ).WriteCharPtr( "=\"" );
224 HTMLOutFuncs::Out_String( rStrm, URIHelper::simpleNormalizedMakeRelative(
225 aBaseURL,
226 rLinkName ), eDestEnc ).WriteChar( '\"' );
227 if ( !rImgOptions.isEmpty() )
228 rStrm.WriteCharPtr( rImgOptions.getStr() );
229 rStrm.WriteChar( '>' ).WriteCharPtr( SAL_NEWLINE_STRING ).WriteCharPtr( GetIndentStr() );
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */