Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / html / htmlexp2.cxx
blob475fea96c8f8ead2039cb76dcdf4049a1ac36a71
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 <svtools/htmlkywd.hxx>
27 #include <svtools/htmlout.hxx>
28 #include <svl/urihelper.hxx>
29 #include <tools/stream.hxx>
30 #include <tools/urlobj.hxx>
32 #include <htmlexp.hxx>
33 #include <global.hxx>
34 #include <document.hxx>
35 #include <drwlayer.hxx>
36 #include <rtl/strbuf.hxx>
38 using namespace com::sun::star;
40 void ScHTMLExport::PrepareGraphics( ScDrawLayer* pDrawLayer, SCTAB nTab,
41 SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow )
43 if ( !pDrawLayer->HasObjectsInRows( nTab, nStartRow, nEndRow ) )
44 return;
46 SdrPage* pDrawPage = pDrawLayer->GetPage( static_cast<sal_uInt16>(nTab) );
47 if ( !pDrawPage )
48 return;
50 bTabHasGraphics = true;
51 FillGraphList( pDrawPage, nTab, nStartCol, nStartRow, nEndCol, nEndRow );
52 size_t ListSize = aGraphList.size();
53 for ( size_t i = 0; i < ListSize; ++i )
55 ScHTMLGraphEntry* pE = &aGraphList[ i ];
56 if ( !pE->bInCell )
57 { // not all cells: table next to some
58 bTabAlignedLeft = true;
59 break;
64 void ScHTMLExport::FillGraphList( const SdrPage* pPage, SCTAB nTab,
65 SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow )
67 if ( !pPage->GetObjCount() )
68 return;
70 tools::Rectangle aRect;
71 if ( !bAll )
72 aRect = pDoc->GetMMRect( nStartCol, nStartRow, nEndCol, nEndRow, nTab );
73 SdrObjListIter aIter( pPage, SdrIterMode::Flat );
74 SdrObject* pObject = aIter.Next();
75 while ( pObject )
77 tools::Rectangle aObjRect = pObject->GetCurrentBoundRect();
78 if ( (bAll || aRect.Contains( aObjRect )) && !ScDrawLayer::IsNoteCaption(pObject) )
80 Size aSpace;
81 ScRange aR = pDoc->GetRange( nTab, aObjRect );
82 // Rectangle in mm/100
83 Size aSize( MMToPixel( aObjRect.GetSize() ) );
84 // If the image is somewhere in a merged range we must
85 // move the anchor to the upper left (THE span cell).
86 pDoc->ExtendOverlapped( aR );
87 SCCOL nCol1 = aR.aStart.Col();
88 SCROW nRow1 = aR.aStart.Row();
89 SCCOL nCol2 = aR.aEnd.Col();
90 SCROW nRow2 = aR.aEnd.Row();
91 // All cells empty under object?
92 bool bInCell = pDoc->IsEmptyData( nCol1, nRow1, nCol2, nRow2, nTab );
93 if ( bInCell )
94 { // Spacing in spanning cell
95 tools::Rectangle aCellRect = pDoc->GetMMRect(
96 nCol1, nRow1, nCol2, nRow2, nTab );
97 aSpace = MMToPixel( Size(
98 aCellRect.GetWidth() - aObjRect.GetWidth(),
99 aCellRect.GetHeight() - aObjRect.GetHeight() ));
100 aSpace.AdjustWidth((nCol2-nCol1) * (nCellSpacing+1) );
101 aSpace.AdjustHeight((nRow2-nRow1) * (nCellSpacing+1) );
102 aSpace.setWidth( aSpace.Width() / 2 );
103 aSpace.setHeight( aSpace.Height() / 2 );
105 aGraphList.emplace_back( pObject,
106 aR, aSize, bInCell, aSpace );
108 pObject = aIter.Next();
112 void ScHTMLExport::WriteGraphEntry( ScHTMLGraphEntry* pE )
114 SdrObject* pObject = pE->pObject;
115 OStringBuffer aBuf;
116 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_width).append('=').
117 append(static_cast<sal_Int32>(pE->aSize.Width()));
118 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_height).append('=').
119 append(static_cast<sal_Int32>(pE->aSize.Height()));
120 if ( pE->bInCell )
122 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_hspace).append('=').
123 append(static_cast<sal_Int32>(pE->aSpace.Width()));
124 aBuf.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_vspace).append('=').
125 append(static_cast<sal_Int32>(pE->aSpace.Height()));
127 OString aOpt = aBuf.makeStringAndClear();
128 switch ( pObject->GetObjIdentifier() )
130 case SdrObjKind::Graphic:
132 const SdrGrafObj* pSGO = static_cast<SdrGrafObj*>(pObject);
133 std::unique_ptr<SdrGrafObjGeoData> pGeo(static_cast<SdrGrafObjGeoData*>(pSGO->GetGeoData().release()));
134 sal_uInt16 nMirrorCase = (pGeo->maGeo.nRotationAngle == 18000_deg100 ?
135 ( pGeo->bMirrored ? 3 : 4 ) : ( pGeo->bMirrored ? 2 : 1 ));
136 bool bHMirr = ( ( nMirrorCase == 2 ) || ( nMirrorCase == 4 ) );
137 bool bVMirr = ( ( nMirrorCase == 3 ) || ( nMirrorCase == 4 ) );
138 XOutFlags nXOutFlags = XOutFlags::NONE;
139 if ( bHMirr )
140 nXOutFlags |= XOutFlags::MirrorHorz;
141 if ( bVMirr )
142 nXOutFlags |= XOutFlags::MirrorVert;
143 OUString aLinkName;
144 if ( pSGO->IsLinkedGraphic() )
145 aLinkName = pSGO->GetFileName();
146 WriteImage( aLinkName, pSGO->GetGraphic(), aOpt, nXOutFlags );
147 pE->bWritten = true;
149 break;
150 case SdrObjKind::OLE2:
152 const Graphic* pGraphic = static_cast<SdrOle2Obj*>(pObject)->GetGraphic();
153 if ( pGraphic )
155 OUString aLinkName;
156 WriteImage( aLinkName, *pGraphic, aOpt );
157 pE->bWritten = true;
160 break;
161 default:
163 Graphic aGraph(SdrExchangeView::GetObjGraphic(*pObject));
164 OUString aLinkName;
165 WriteImage( aLinkName, aGraph, aOpt );
166 pE->bWritten = true;
171 void ScHTMLExport::WriteImage( OUString& rLinkName, const Graphic& rGrf,
172 std::string_view rImgOptions, XOutFlags nXOutFlags )
174 // Embedded graphic -> create an image file
175 if( rLinkName.isEmpty() )
177 if( !aStreamPath.isEmpty() )
179 // Save as a PNG
180 OUString aGrfNm( aStreamPath );
181 nXOutFlags |= XOutFlags::UseNativeIfPossible;
182 ErrCode nErr = XOutBitmap::WriteGraphic( rGrf, aGrfNm,
183 "PNG", nXOutFlags );
185 // If it worked, create a URL for the IMG tag
186 if( !nErr )
188 rLinkName = URIHelper::SmartRel2Abs(
189 INetURLObject(aBaseURL),
190 aGrfNm,
191 URIHelper::GetMaybeFileHdl());
195 else
197 // Linked graphic - figure out the URL for the IMG tag
198 if( bCopyLocalFileToINet )
200 CopyLocalFileToINet( rLinkName, aStreamPath );
202 else
203 rLinkName = URIHelper::SmartRel2Abs(
204 INetURLObject(aBaseURL),
205 rLinkName,
206 URIHelper::GetMaybeFileHdl());
209 // If a URL was set, output the IMG tag.
210 // <IMG SRC="..."[ rImgOptions]>
211 if( !rLinkName.isEmpty() )
213 rStrm.WriteChar( '<' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_image ).WriteChar( ' ' ).WriteCharPtr( OOO_STRING_SVTOOLS_HTML_O_src ).WriteCharPtr( "=\"" );
214 HTMLOutFuncs::Out_String( rStrm, URIHelper::simpleNormalizedMakeRelative(
215 aBaseURL,
216 rLinkName ) ).WriteChar( '\"' );
217 if ( !rImgOptions.empty() )
218 rStrm.WriteOString( rImgOptions );
219 rStrm.WriteChar( '>' ).WriteCharPtr( SAL_NEWLINE_STRING ).WriteCharPtr( GetIndentStr() );
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */