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
;
65 void SdrTableObj::ExportAsRTF( SvStream
& rStrm
, SdrTableObj
& rObj
)
67 SdrTableRtfExporter
aEx( rStrm
, rObj
);
71 static const OUStringLiteral
gsSize( "Size" );
73 SdrTableRtfExporter::SdrTableRtfExporter( SvStream
& rStrm
, SdrTableObj
& rObj
)
76 , mxTable( rObj
.getTable() )
80 static long HundMMToTwips( long nIn
)
82 long nRet
= OutputDevice::LogicToLogic( nIn
, MapUnit::Map100thMM
, MapUnit::MapTwip
);
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
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
);
109 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
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
);
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() ) );
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
);
177 OutlinerParaObject
* pParaObj
= xCell
->CreateEditOutlinerParaObject().release();
178 bool bOwnParaObj
= pParaObj
!= nullptr;
180 if( pParaObj
== nullptr )
181 pParaObj
= xCell
->GetOutlinerParaObject();
185 // handle outliner attributes
186 SdrOutliner
& rOutliner
= mrObj
.ImpGetDrawOutliner();
187 rOutliner
.SetText(*pParaObj
);
189 aContent
= rOutliner
.GetEditEngine().GetText();
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
;
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
)
222 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B
);
224 if ( rPostureItem
.GetPosture() != ITALIC_NONE
)
227 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I
);
229 if ( rUnderlineItem
.GetLineStyle() != LINESTYLE_NONE
)
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
);
240 mrStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN
);
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */