LibreOffice Module svx (master) 1
tablertfexporter.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include <vector>
22
23#include <com/sun/star/table/XTable.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25
27#include <tools/stream.hxx>
28#include <svtools/rtfkeywd.hxx>
29#include <svtools/rtfout.hxx>
30
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>
36
37#include <cell.hxx>
38#include <svx/svdotable.hxx>
39#include <svx/svdoutl.hxx>
40#include <editeng/editeng.hxx>
41#include <editeng/outlobj.hxx>
42
43
44using namespace ::com::sun::star::uno;
45using namespace ::com::sun::star::table;
46using namespace ::com::sun::star::container;
47using namespace ::com::sun::star::beans;
48
49namespace sdr::table {
50
52{
53public:
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 );
58
59private:
62 Reference< XTable > mxTable;
63};
64
65void ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
66{
67 SdrTableRtfExporter aEx( rStrm, rObj );
68 aEx.Write();
69}
70
71constexpr OUStringLiteral gsSize( u"Size" );
72
74: mrStrm( rStrm )
75, mrObj( rObj )
76, mxTable( rObj.getTable() )
77{
78}
79
81{
84
85 Reference< XTableColumns > xColumns( mxTable->getColumns() );
86 const sal_Int32 nColCount = xColumns->getCount();
87
88 std::vector< sal_Int32 > aColumnStart;
89 aColumnStart.reserve( nColCount );
90
91 // determine right offset of cells
92 sal_Int32 nPos = 0;
93 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
94 {
95 Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
96 sal_Int32 nWidth = 0;
97 xSet->getPropertyValue( gsSize ) >>= nWidth;
99 aColumnStart.push_back( nPos );
100 }
101 catch( Exception& )
102 {
103 TOOLS_WARN_EXCEPTION("svx", "");
104 }
105
106 // export rows
107 Reference< XTableRows > xRows( mxTable->getRows() );
108 const sal_Int32 nRowCount = xRows->getCount();
109
110 for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
111 {
112 Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
113 WriteRow( xRowSet, nRow, aColumnStart );
114 }
115 catch( Exception& )
116 {
117 TOOLS_WARN_EXCEPTION("svx", "");
118 }
119
121}
122
123void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
124{
125 sal_Int32 nRowHeight = 0;
126 xRowSet->getPropertyValue( gsSize ) >>= nRowHeight;
127
129 mrStrm.WriteOString( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(nRowHeight) );
130
131 const sal_Int32 nColCount = mxTable->getColumnCount();
132 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
133 {
134 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
135
136 if( !xCell.is() )
137 continue;
138
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
142 }
144
145 sal_uInt64 nStrmPos = mrStrm.Tell();
146 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
147 {
148 WriteCell( nCol, nRow );
149 if ( mrStrm.Tell() - nStrmPos > 255 )
150 {
152 nStrmPos = mrStrm.Tell();
153 }
154 }
156}
157
158
159void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
160{
161 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
162
163 if( !xCell.is() || xCell->isMerged() )
164 {
166 return ;
167 }
168
169 OUString aContent;
170
171 std::optional<OutlinerParaObject> pParaObj = xCell->CreateEditOutlinerParaObject();
172
173 if( !pParaObj && xCell->GetOutlinerParaObject() )
174 pParaObj = *xCell->GetOutlinerParaObject();
175
176 if(pParaObj)
177 {
178 // handle outliner attributes
179 SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
180 rOutliner.SetText(*pParaObj);
181
182 aContent = rOutliner.GetEditEngine().GetText();
183
184 rOutliner.Clear();
185 }
186
187 bool bResetAttr = false;
188
189 SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
190
191 const SfxItemSet& rCellSet = xCell->GetItemSet();
192
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 );
196
197 const char* pChar;
198
199 switch( eHAdj )
200 {
205 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
206 }
208
209 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
210 { // bold
211 bResetAttr = true;
213 }
214 if ( rPostureItem.GetPosture() != ITALIC_NONE )
215 { // italic
216 bResetAttr = true;
218 }
219 if ( rUnderlineItem.GetLineStyle() != LINESTYLE_NONE )
220 { // underline
221 bResetAttr = true;
223 }
224
225 mrStrm.WriteChar( ' ' );
226 RTFOutFuncs::Out_String( mrStrm, aContent );
228
229 if ( bResetAttr )
231}
232
233}
234
235/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetText(LineEnd eEnd=LINEEND_LF) const
void SetText(const OutlinerParaObject &)
const EditEngine & GetEditEngine() const
void Clear()
SdrOutliner & ImpGetDrawOutliner() const
Definition: svdotext.cxx:1194
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
sal_uInt64 Tell() const
SvStream & WriteOString(std::string_view rStr)
SvStream & WriteChar(char nChar)
FontItalic GetPosture() const
FontLineStyle GetLineStyle() const
FontWeight GetWeight() const
void WriteCell(sal_Int32 nCol, sal_Int32 nRow)
void WriteRow(const Reference< XPropertySet > &xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 > &aColumnStart)
SdrTableRtfExporter(SvStream &rStrmP, SdrTableObj &rObj)
#define SAL_NEWLINE_STRING
#define TOOLS_WARN_EXCEPTION(area, stream)
constexpr TypedWhichId< SvxUnderlineItem > EE_CHAR_UNDERLINE(EE_CHAR_START+5)
constexpr TypedWhichId< SvxWeightItem > EE_CHAR_WEIGHT(EE_CHAR_START+4)
constexpr TypedWhichId< SvxPostureItem > EE_CHAR_ITALIC(EE_CHAR_START+7)
LINESTYLE_NONE
ITALIC_NONE
WEIGHT_BOLD
sal_uInt16 nPos
SVT_DLLPUBLIC SvStream & Out_String(SvStream &, std::u16string_view, rtl_TextEncoding eDestEnc=RTL_TEXTENCODING_MS_1252)
@ Exception
void SvStream & rStrm
constexpr auto toTwips(N number, Length from)
constexpr OUStringLiteral gsSize(u"Size")
void ExportAsRTF(SvStream &rStrm, SdrTableObj &rObj)
Hack for clipboard with calc and writer, export and import table content as rtf table.
#define OOO_STRING_SVTOOLS_RTF_CELL
#define OOO_STRING_SVTOOLS_RTF_QC
#define OOO_STRING_SVTOOLS_RTF_B
#define OOO_STRING_SVTOOLS_RTF_RTF
#define OOO_STRING_SVTOOLS_RTF_UL
#define OOO_STRING_SVTOOLS_RTF_QL
#define OOO_STRING_SVTOOLS_RTF_QJ
#define OOO_STRING_SVTOOLS_RTF_CELLX
#define OOO_STRING_SVTOOLS_RTF_TRRH
#define OOO_STRING_SVTOOLS_RTF_QR
#define OOO_STRING_SVTOOLS_RTF_ROW
#define OOO_STRING_SVTOOLS_RTF_ANSI
#define OOO_STRING_SVTOOLS_RTF_TRLEFT
#define OOO_STRING_SVTOOLS_RTF_INTBL
#define OOO_STRING_SVTOOLS_RTF_TROWD
#define OOO_STRING_SVTOOLS_RTF_TRGAPH
#define OOO_STRING_SVTOOLS_RTF_PLAIN
#define OOO_STRING_SVTOOLS_RTF_I
#define OOO_STRING_SVTOOLS_RTF_PARD
SdrTextHorzAdjust
Definition: sdtaitm.hxx:53
@ SDRTEXTHORZADJUST_LEFT
Definition: sdtaitm.hxx:53
@ SDRTEXTHORZADJUST_BLOCK
Definition: sdtaitm.hxx:56
@ SDRTEXTHORZADJUST_CENTER
Definition: sdtaitm.hxx:54
@ SDRTEXTHORZADJUST_RIGHT
Definition: sdtaitm.hxx:55
const char * pChar