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 <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 
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;
48 
49 namespace sdr::table {
50 
52 {
53 public:
54  SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
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 
59 private:
62  Reference< XTable > mxTable;
63 };
64 
65 void ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
66 {
67  SdrTableRtfExporter aEx( rStrm, rObj );
68  aEx.Write();
69 }
70 
71 constexpr 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;
98  nPos += o3tl::toTwips(nWidth, o3tl::Length::mm100);
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 
123 void 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.WriteCharPtr( 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.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(aColumnStart[nCol]) );
140  if ( (nCol & 0x0F) == 0x0F )
141  mrStrm.WriteCharPtr( 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 
159 void 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  }
207  mrStrm.WriteCharPtr( pChar );
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: */
#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
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:1164
#define SAL_NEWLINE_STRING
#define OOO_STRING_SVTOOLS_RTF_QC
sal_uInt16 nPos
SdrTableRtfExporter(SvStream &rStrmP, SdrTableObj &rObj)