Bump version to 6.4-15
[LibreOffice.git] / svx / source / table / tablertfexporter.cxx
blobabefdf53a101f0a50f4d70da195b3a71a133749a
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 .
21 #include <vector>
23 #include <com/sun/star/table/XTable.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <tools/stream.hxx>
27 #include <svtools/rtfkeywd.hxx>
28 #include <svtools/rtfout.hxx>
30 #include <editeng/eeitem.hxx>
31 #include <svx/sdtaitm.hxx>
32 #include <editeng/wghtitem.hxx>
33 #include <editeng/postitem.hxx>
34 #include <editeng/udlnitem.hxx>
36 #include <cell.hxx>
37 #include <celltypes.hxx>
38 #include <svx/svdotable.hxx>
39 #include <svx/svdoutl.hxx>
40 #include <editeng/editeng.hxx>
41 #include <editeng/outlobj.hxx>
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::table;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::beans;
49 namespace sdr { namespace table {
51 class SdrTableRtfExporter
53 public:
54 SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
55 void Write();
56 void WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart );
57 void WriteCell( sal_Int32 nCol, sal_Int32 nRow );
59 private:
60 SvStream& mrStrm;
61 SdrTableObj& mrObj;
62 Reference< XTable > mxTable;
65 void SdrTableObj::ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
67 SdrTableRtfExporter aEx( rStrm, rObj );
68 aEx.Write();
71 static const OUStringLiteral gsSize( "Size" );
73 SdrTableRtfExporter::SdrTableRtfExporter( SvStream& rStrm, SdrTableObj& rObj )
74 : mrStrm( rStrm )
75 , mrObj( rObj )
76 , mxTable( rObj.getTable() )
80 static long HundMMToTwips( long nIn )
82 long nRet = OutputDevice::LogicToLogic( nIn, MapUnit::Map100thMM, MapUnit::MapTwip );
83 return nRet;
86 void SdrTableRtfExporter::Write()
88 mrStrm.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF );
89 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI ).WriteCharPtr( SAL_NEWLINE_STRING );
91 Reference< XTableColumns > xColumns( mxTable->getColumns() );
92 const sal_Int32 nColCount = xColumns->getCount();
94 std::vector< sal_Int32 > aColumnStart;
95 aColumnStart.reserve( nColCount );
97 // determine right offset of cells
98 sal_Int32 nPos = 0;
99 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
101 Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
102 sal_Int32 nWidth = 0;
103 xSet->getPropertyValue( gsSize ) >>= nWidth;
104 nPos += HundMMToTwips( nWidth );
105 aColumnStart.push_back( nPos );
107 catch( Exception& )
109 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
112 // export rows
113 Reference< XTableRows > xRows( mxTable->getRows() );
114 const sal_Int32 nRowCount = xRows->getCount();
116 for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
118 Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
119 WriteRow( xRowSet, nRow, aColumnStart );
121 catch( Exception& )
123 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
126 mrStrm.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING );
129 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
131 sal_Int32 nRowHeight = 0;
132 xRowSet->getPropertyValue( gsSize ) >>= nRowHeight;
134 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH ).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT ).WriteCharPtr( "-30" );
135 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(nRowHeight) );
137 const sal_Int32 nColCount = mxTable->getColumnCount();
138 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
140 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
142 if( !xCell.is() )
143 continue;
145 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(aColumnStart[nCol]) );
146 if ( (nCol & 0x0F) == 0x0F )
147 mrStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // prevent long lines
149 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL ).WriteCharPtr( SAL_NEWLINE_STRING );
151 sal_uLong nStrmPos = mrStrm.Tell();
152 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
154 WriteCell( nCol, nRow );
155 if ( mrStrm.Tell() - nStrmPos > 255 )
157 mrStrm.WriteCharPtr( SAL_NEWLINE_STRING );
158 nStrmPos = mrStrm.Tell();
161 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW ).WriteCharPtr( SAL_NEWLINE_STRING );
165 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
167 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
169 if( !xCell.is() || xCell->isMerged() )
171 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
172 return ;
175 OUString aContent;
177 OutlinerParaObject* pParaObj = xCell->CreateEditOutlinerParaObject().release();
178 bool bOwnParaObj = pParaObj != nullptr;
180 if( pParaObj == nullptr )
181 pParaObj = xCell->GetOutlinerParaObject();
183 if(pParaObj)
185 // handle outliner attributes
186 SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
187 rOutliner.SetText(*pParaObj);
189 aContent = rOutliner.GetEditEngine().GetText();
191 rOutliner.Clear();
193 if( bOwnParaObj )
194 delete pParaObj;
197 bool bResetAttr = false;
199 SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
201 const SfxItemSet& rCellSet = xCell->GetItemSet();
203 const SvxWeightItem& rWeightItem = rCellSet.Get( EE_CHAR_WEIGHT );
204 const SvxPostureItem& rPostureItem = rCellSet.Get( EE_CHAR_ITALIC );
205 const SvxUnderlineItem& rUnderlineItem = rCellSet.Get( EE_CHAR_UNDERLINE );
207 const sal_Char* pChar;
209 switch( eHAdj )
211 case SDRTEXTHORZADJUST_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
212 case SDRTEXTHORZADJUST_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
213 case SDRTEXTHORZADJUST_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
214 case SDRTEXTHORZADJUST_LEFT:
215 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
217 mrStrm.WriteCharPtr( pChar );
219 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
220 { // bold
221 bResetAttr = true;
222 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B );
224 if ( rPostureItem.GetPosture() != ITALIC_NONE )
225 { // italic
226 bResetAttr = true;
227 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I );
229 if ( rUnderlineItem.GetLineStyle() != LINESTYLE_NONE )
230 { // underline
231 bResetAttr = true;
232 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL );
235 mrStrm.WriteChar( ' ' );
236 RTFOutFuncs::Out_String( mrStrm, aContent );
237 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
239 if ( bResetAttr )
240 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN );
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */