1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
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
54 SdrTableRtfExporter( SvStream
& rStrmP
, SdrTableObj
& rObj
);
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
);
62 Reference
< XTable
> mxTable
;
63 const OUString msSize
;
66 void SdrTableObj::ExportAsRTF( SvStream
& rStrm
, SdrTableObj
& rObj
)
68 SdrTableRtfExporter
aEx( rStrm
, rObj
);
72 SdrTableRtfExporter::SdrTableRtfExporter( SvStream
& rStrm
, SdrTableObj
& rObj
)
75 , mxTable( rObj
.getTable() )
80 long HundMMToTwips( long nIn
)
82 long nRet
= OutputDevice::LogicToLogic( nIn
, MAP_100TH_MM
, MAP_TWIP
);
86 sal_uLong
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
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( msSize
) >>= nWidth
;
104 nPos
+= HundMMToTwips( nWidth
);
105 aColumnStart
.push_back( nPos
);
107 catch( Exception
& e
)
110 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
114 Reference
< XTableRows
> xRows( mxTable
->getRows() );
115 const sal_Int32 nRowCount
= xRows
->getCount();
117 for( sal_Int32 nRow
= 0; nRow
< nRowCount
; nRow
++ ) try
119 Reference
< XPropertySet
> xRowSet( xRows
->getByIndex(nRow
), UNO_QUERY_THROW
);
120 WriteRow( xRowSet
, nRow
, aColumnStart
);
122 catch( Exception
& e
)
125 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
128 mrStrm
.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING
);
129 return mrStrm
.GetError();
132 void SdrTableRtfExporter::WriteRow( const Reference
< XPropertySet
>& xRowSet
, sal_Int32 nRow
, const std::vector
< sal_Int32
>& aColumnStart
)
134 sal_Int32 nRowHeight
= 0;
135 xRowSet
->getPropertyValue( msSize
) >>= nRowHeight
;
137 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH
).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT
).WriteCharPtr( "-30" );
138 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH
).WriteCharPtr( OString::number(nRowHeight
).getStr() );
140 const sal_Int32 nColCount
= mxTable
->getColumnCount();
141 for( sal_Int32 nCol
= 0; nCol
< nColCount
; nCol
++ )
143 CellRef
xCell( dynamic_cast< Cell
* >( mxTable
->getCellByPosition( nCol
, nRow
).get() ) );
148 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX
).WriteCharPtr( OString::number(aColumnStart
[nCol
]).getStr() );
149 if ( (nCol
& 0x0F) == 0x0F )
150 mrStrm
.WriteCharPtr( SAL_NEWLINE_STRING
); // Zeilen nicht zu lang werden lassen
152 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL
).WriteCharPtr( SAL_NEWLINE_STRING
);
154 sal_uLong nStrmPos
= mrStrm
.Tell();
155 for( sal_Int32 nCol
= 0; nCol
< nColCount
; nCol
++ )
157 WriteCell( nCol
, nRow
);
158 if ( mrStrm
.Tell() - nStrmPos
> 255 )
160 mrStrm
.WriteCharPtr( SAL_NEWLINE_STRING
);
161 nStrmPos
= mrStrm
.Tell();
164 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW
).WriteCharPtr( SAL_NEWLINE_STRING
);
168 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol
, sal_Int32 nRow
)
170 CellRef
xCell( dynamic_cast< Cell
* >( mxTable
->getCellByPosition( nCol
, nRow
).get() ) );
172 if( !xCell
.is() || xCell
->isMerged() )
174 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL
);
180 OutlinerParaObject
* pParaObj
= xCell
->GetEditOutlinerParaObject();
181 bool bOwnParaObj
= pParaObj
!= 0;
184 pParaObj
= xCell
->GetOutlinerParaObject();
188 // handle outliner attributes
189 SdrOutliner
& rOutliner
= mrObj
.ImpGetDrawOutliner();
190 rOutliner
.SetText(*pParaObj
);
192 aContent
= rOutliner
.GetEditEngine().GetText( LINEEND_LF
);
200 bool bResetPar
, bResetAttr
;
201 bResetPar
= bResetAttr
= false;
203 SdrTextHorzAdjust eHAdj
= xCell
->GetTextHorizontalAdjust();
205 const SfxItemSet
& rCellSet
= xCell
->GetItemSet();
207 const SvxWeightItem
& rWeightItem
= (const SvxWeightItem
&) rCellSet
.Get( EE_CHAR_WEIGHT
);
208 const SvxPostureItem
& rPostureItem
= (const SvxPostureItem
&) rCellSet
.Get( EE_CHAR_ITALIC
);
209 const SvxUnderlineItem
& rUnderlineItem
= (const SvxUnderlineItem
&) rCellSet
.Get( EE_CHAR_UNDERLINE
);
211 const sal_Char
* pChar
;
215 case SDRTEXTHORZADJUST_CENTER
: pChar
= OOO_STRING_SVTOOLS_RTF_QC
; break;
216 case SDRTEXTHORZADJUST_BLOCK
: pChar
= OOO_STRING_SVTOOLS_RTF_QJ
; break;
217 case SDRTEXTHORZADJUST_RIGHT
: pChar
= OOO_STRING_SVTOOLS_RTF_QR
; break;
218 case SDRTEXTHORZADJUST_LEFT
:
219 default: pChar
= OOO_STRING_SVTOOLS_RTF_QL
; break;
221 mrStrm
.WriteCharPtr( pChar
);
223 if ( rWeightItem
.GetWeight() >= WEIGHT_BOLD
)
226 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B
);
228 if ( rPostureItem
.GetPosture() != ITALIC_NONE
)
231 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I
);
233 if ( rUnderlineItem
.GetLineStyle() != UNDERLINE_NONE
)
236 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL
);
239 mrStrm
.WriteChar( ' ' );
240 RTFOutFuncs::Out_String( mrStrm
, aContent
);
241 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL
);
244 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD
).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL
);
246 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
);
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */