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 
26 #include <tools/diagnose_ex.h>
27 #include <tools/stream.hxx>
28 #include <tools/UnitConversion.hxx>
29 #include <svtools/rtfkeywd.hxx>
30 #include <svtools/rtfout.hxx>
31 
32 #include <editeng/eeitem.hxx>
33 #include <svx/sdtaitm.hxx>
34 #include <editeng/wghtitem.hxx>
35 #include <editeng/postitem.hxx>
36 #include <editeng/udlnitem.hxx>
37 
38 #include <cell.hxx>
39 #include <svx/svdotable.hxx>
40 #include <svx/svdoutl.hxx>
41 #include <editeng/editeng.hxx>
42 #include <editeng/outlobj.hxx>
43 
44 
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::table;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::beans;
49 
50 namespace sdr::table {
51 
53 {
54 public:
55  SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
56  void Write();
57  void WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart );
58  void WriteCell( sal_Int32 nCol, sal_Int32 nRow );
59 
60 private:
63  Reference< XTable > mxTable;
64 };
65 
66 void ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
67 {
68  SdrTableRtfExporter aEx( rStrm, rObj );
69  aEx.Write();
70 }
71 
72 constexpr OUStringLiteral gsSize( u"Size" );
73 
75 : mrStrm( rStrm )
76 , mrObj( rObj )
77 , mxTable( rObj.getTable() )
78 {
79 }
80 
82 {
85 
86  Reference< XTableColumns > xColumns( mxTable->getColumns() );
87  const sal_Int32 nColCount = xColumns->getCount();
88 
89  std::vector< sal_Int32 > aColumnStart;
90  aColumnStart.reserve( nColCount );
91 
92  // determine right offset of cells
93  sal_Int32 nPos = 0;
94  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
95  {
96  Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
97  sal_Int32 nWidth = 0;
98  xSet->getPropertyValue( gsSize ) >>= nWidth;
99  nPos += o3tl::toTwips(nWidth, o3tl::Length::mm100);
100  aColumnStart.push_back( nPos );
101  }
102  catch( Exception& )
103  {
104  TOOLS_WARN_EXCEPTION("svx", "");
105  }
106 
107  // export rows
108  Reference< XTableRows > xRows( mxTable->getRows() );
109  const sal_Int32 nRowCount = xRows->getCount();
110 
111  for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
112  {
113  Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
114  WriteRow( xRowSet, nRow, aColumnStart );
115  }
116  catch( Exception& )
117  {
118  TOOLS_WARN_EXCEPTION("svx", "");
119  }
120 
122 }
123 
124 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
125 {
126  sal_Int32 nRowHeight = 0;
127  xRowSet->getPropertyValue( gsSize ) >>= nRowHeight;
128 
130  mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(nRowHeight) );
131 
132  const sal_Int32 nColCount = mxTable->getColumnCount();
133  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
134  {
135  CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
136 
137  if( !xCell.is() )
138  continue;
139 
140  mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(aColumnStart[nCol]) );
141  if ( (nCol & 0x0F) == 0x0F )
142  mrStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // prevent long lines
143  }
145 
146  sal_uLong nStrmPos = mrStrm.Tell();
147  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
148  {
149  WriteCell( nCol, nRow );
150  if ( mrStrm.Tell() - nStrmPos > 255 )
151  {
153  nStrmPos = mrStrm.Tell();
154  }
155  }
157 }
158 
159 
160 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
161 {
162  CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
163 
164  if( !xCell.is() || xCell->isMerged() )
165  {
167  return ;
168  }
169 
170  OUString aContent;
171 
172  std::optional<OutlinerParaObject> pParaObj = xCell->CreateEditOutlinerParaObject();
173 
174  if( !pParaObj && xCell->GetOutlinerParaObject() )
175  pParaObj = *xCell->GetOutlinerParaObject();
176 
177  if(pParaObj)
178  {
179  // handle outliner attributes
180  SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
181  rOutliner.SetText(*pParaObj);
182 
183  aContent = rOutliner.GetEditEngine().GetText();
184 
185  rOutliner.Clear();
186  }
187 
188  bool bResetAttr = false;
189 
190  SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
191 
192  const SfxItemSet& rCellSet = xCell->GetItemSet();
193 
194  const SvxWeightItem& rWeightItem = rCellSet.Get( EE_CHAR_WEIGHT );
195  const SvxPostureItem& rPostureItem = rCellSet.Get( EE_CHAR_ITALIC );
196  const SvxUnderlineItem& rUnderlineItem = rCellSet.Get( EE_CHAR_UNDERLINE );
197 
198  const char* pChar;
199 
200  switch( eHAdj )
201  {
206  default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
207  }
208  mrStrm.WriteCharPtr( pChar );
209 
210  if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
211  { // bold
212  bResetAttr = true;
214  }
215  if ( rPostureItem.GetPosture() != ITALIC_NONE )
216  { // italic
217  bResetAttr = true;
219  }
220  if ( rUnderlineItem.GetLineStyle() != LINESTYLE_NONE )
221  { // underline
222  bResetAttr = true;
224  }
225 
226  mrStrm.WriteChar( ' ' );
227  RTFOutFuncs::Out_String( mrStrm, aContent );
229 
230  if ( bResetAttr )
232 }
233 
234 }
235 
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define OOO_STRING_SVTOOLS_RTF_QL
OUString GetText(LineEnd eEnd=LINEEND_LF) const
#define OOO_STRING_SVTOOLS_RTF_QR
constexpr auto toTwips(N number, Length from)
constexpr OUStringLiteral gsSize(u"Size")
SdrTextHorzAdjust
Definition: sdtaitm.hxx:53
sal_uIntPtr sal_uLong
constexpr TypedWhichId< SvxPostureItem > EE_CHAR_ITALIC(EE_CHAR_START+7)
#define OOO_STRING_SVTOOLS_RTF_ANSI
#define OOO_STRING_SVTOOLS_RTF_INTBL
SvStream & WriteCharPtr(const char *pBuf)
SVT_DLLPUBLIC SvStream & Out_String(SvStream &, const OUString &, rtl_TextEncoding eDestEnc=RTL_TEXTENCODING_MS_1252)
FontItalic GetPosture() const
WEIGHT_BOLD
LINESTYLE_NONE
#define OOO_STRING_SVTOOLS_RTF_I
#define OOO_STRING_SVTOOLS_RTF_QJ
#define OOO_STRING_SVTOOLS_RTF_ROW
constexpr TypedWhichId< SvxWeightItem > EE_CHAR_WEIGHT(EE_CHAR_START+4)
#define OOO_STRING_SVTOOLS_RTF_TRLEFT
#define TOOLS_WARN_EXCEPTION(area, stream)
#define OOO_STRING_SVTOOLS_RTF_PARD
void SetText(const OutlinerParaObject &)
FontWeight GetWeight() const
#define OOO_STRING_SVTOOLS_RTF_TRRH
#define OOO_STRING_SVTOOLS_RTF_PLAIN
ITALIC_NONE
const EditEngine & GetEditEngine() const
#define OOO_STRING_SVTOOLS_RTF_B
void WriteRow(const Reference< XPropertySet > &xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 > &aColumnStart)
#define OOO_STRING_SVTOOLS_RTF_CELL
FontLineStyle GetLineStyle() const
#define OOO_STRING_SVTOOLS_RTF_TROWD
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_TRGAPH
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
#define OOO_STRING_SVTOOLS_RTF_CELLX
SvStream & WriteOString(std::string_view rStr)
#define OOO_STRING_SVTOOLS_RTF_RTF
sal_uInt64 Tell() const
const char * pChar
#define OOO_STRING_SVTOOLS_RTF_UL
SvStream & WriteChar(char nChar)
void WriteCell(sal_Int32 nCol, sal_Int32 nRow)
constexpr TypedWhichId< SvxUnderlineItem > EE_CHAR_UNDERLINE(EE_CHAR_START+5)
void Clear()
SdrOutliner & ImpGetDrawOutliner() const
Definition: svdotext.cxx:1163
#define SAL_NEWLINE_STRING
#define OOO_STRING_SVTOOLS_RTF_QC
sal_uInt16 nPos
SdrTableRtfExporter(SvStream &rStrmP, SdrTableObj &rObj)