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 <comphelper/diagnose_ex.hxx>
27 #include <tools/stream.hxx>
28 #include <svtools/rtfkeywd.hxx>
29 #include <svtools/rtfout.hxx>
31 #include <editeng/eeitem.hxx>
32 #include <svx/sdtaitm.hxx>
33 #include <editeng/wghtitem.hxx>
34 #include <editeng/postitem.hxx>
35 #include <editeng/udlnitem.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::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 rtl::Reference
< TableModel
> mxTable
;
65 void ExportAsRTF( SvStream
& rStrm
, SdrTableObj
& rObj
)
67 SdrTableRtfExporter
aEx( rStrm
, rObj
);
71 constexpr OUString
gsSize( u
"Size"_ustr
);
73 SdrTableRtfExporter::SdrTableRtfExporter( SvStream
& rStrm
, SdrTableObj
& rObj
)
76 , mxTable( rObj
.getUnoTable() )
80 void SdrTableRtfExporter::Write()
82 mrStrm
.WriteChar( '{' ).WriteOString( OOO_STRING_SVTOOLS_RTF_RTF
);
83 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_ANSI
).WriteOString( SAL_NEWLINE_STRING
);
85 Reference
< XTableColumns
> xColumns( mxTable
->getColumns() );
86 const sal_Int32 nColCount
= xColumns
->getCount();
88 std::vector
< sal_Int32
> aColumnStart
;
89 aColumnStart
.reserve( nColCount
);
91 // determine right offset of cells
93 for( sal_Int32 nCol
= 0; nCol
< nColCount
; nCol
++ ) try
95 Reference
< XPropertySet
> xSet( xColumns
->getByIndex(nCol
), UNO_QUERY_THROW
);
97 xSet
->getPropertyValue( gsSize
) >>= nWidth
;
98 nPos
+= o3tl::toTwips(nWidth
, o3tl::Length::mm100
);
99 aColumnStart
.push_back( nPos
);
103 TOOLS_WARN_EXCEPTION("svx", "");
107 Reference
< XTableRows
> xRows( mxTable
->getRows() );
108 const sal_Int32 nRowCount
= xRows
->getCount();
110 for( sal_Int32 nRow
= 0; nRow
< nRowCount
; nRow
++ ) try
112 Reference
< XPropertySet
> xRowSet( xRows
->getByIndex(nRow
), UNO_QUERY_THROW
);
113 WriteRow( xRowSet
, nRow
, aColumnStart
);
117 TOOLS_WARN_EXCEPTION("svx", "");
120 mrStrm
.WriteChar( '}' ).WriteOString( SAL_NEWLINE_STRING
);
123 void SdrTableRtfExporter::WriteRow( const Reference
< XPropertySet
>& xRowSet
, sal_Int32 nRow
, const std::vector
< sal_Int32
>& aColumnStart
)
125 sal_Int32 nRowHeight
= 0;
126 xRowSet
->getPropertyValue( gsSize
) >>= nRowHeight
;
128 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_TROWD
).WriteOString( OOO_STRING_SVTOOLS_RTF_TRGAPH
).WriteOString( "30" ).WriteOString( OOO_STRING_SVTOOLS_RTF_TRLEFT
).WriteOString( "-30" );
129 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_TRRH
).WriteOString( OString::number(nRowHeight
) );
131 const sal_Int32 nColCount
= mxTable
->getColumnCount();
132 for( sal_Int32 nCol
= 0; nCol
< nColCount
; nCol
++ )
134 CellRef
xCell( mxTable
->getCell( nCol
, nRow
) );
139 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CELLX
).WriteOString( OString::number(aColumnStart
[nCol
]) );
140 if ( (nCol
& 0x0F) == 0x0F )
141 mrStrm
.WriteOString( SAL_NEWLINE_STRING
); // prevent long lines
143 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_PARD
).WriteOString( OOO_STRING_SVTOOLS_RTF_PLAIN
).WriteOString( OOO_STRING_SVTOOLS_RTF_INTBL
).WriteOString( SAL_NEWLINE_STRING
);
145 sal_uInt64 nStrmPos
= mrStrm
.Tell();
146 for( sal_Int32 nCol
= 0; nCol
< nColCount
; nCol
++ )
148 WriteCell( nCol
, nRow
);
149 if ( mrStrm
.Tell() - nStrmPos
> 255 )
151 mrStrm
.WriteOString( SAL_NEWLINE_STRING
);
152 nStrmPos
= mrStrm
.Tell();
155 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_ROW
).WriteOString( SAL_NEWLINE_STRING
);
159 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol
, sal_Int32 nRow
)
161 CellRef
xCell( mxTable
->getCell( nCol
, nRow
) );
163 if( !xCell
.is() || xCell
->isMerged() )
165 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CELL
);
171 std::optional
<OutlinerParaObject
> pParaObj
= xCell
->CreateEditOutlinerParaObject();
173 if( !pParaObj
&& xCell
->GetOutlinerParaObject() )
174 pParaObj
= *xCell
->GetOutlinerParaObject();
178 // handle outliner attributes
179 SdrOutliner
& rOutliner
= mrObj
.ImpGetDrawOutliner();
180 rOutliner
.SetText(*pParaObj
);
182 aContent
= rOutliner
.GetEditEngine().GetText();
187 bool bResetAttr
= false;
189 SdrTextHorzAdjust eHAdj
= xCell
->GetTextHorizontalAdjust();
191 const SfxItemSet
& rCellSet
= xCell
->GetItemSet();
193 const SvxWeightItem
& rWeightItem
= rCellSet
.Get( EE_CHAR_WEIGHT
);
194 const SvxPostureItem
& rPostureItem
= rCellSet
.Get( EE_CHAR_ITALIC
);
195 const SvxUnderlineItem
& rUnderlineItem
= rCellSet
.Get( EE_CHAR_UNDERLINE
);
201 case SDRTEXTHORZADJUST_CENTER
: pChar
= OOO_STRING_SVTOOLS_RTF_QC
; break;
202 case SDRTEXTHORZADJUST_BLOCK
: pChar
= OOO_STRING_SVTOOLS_RTF_QJ
; break;
203 case SDRTEXTHORZADJUST_RIGHT
: pChar
= OOO_STRING_SVTOOLS_RTF_QR
; break;
204 case SDRTEXTHORZADJUST_LEFT
:
205 default: pChar
= OOO_STRING_SVTOOLS_RTF_QL
; break;
207 mrStrm
.WriteOString( pChar
);
209 if ( rWeightItem
.GetWeight() >= WEIGHT_BOLD
)
212 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_B
);
214 if ( rPostureItem
.GetPosture() != ITALIC_NONE
)
217 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_I
);
219 if ( rUnderlineItem
.GetLineStyle() != LINESTYLE_NONE
)
222 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_UL
);
225 mrStrm
.WriteChar( ' ' );
226 RTFOutFuncs::Out_String( mrStrm
, aContent
);
227 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CELL
);
230 mrStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_PLAIN
);
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */