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/stream.hxx>
27 #include <svtools/rtfkeywd.hxx>
28 #include <svtools/rtfout.hxx>
29 
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>
35 
36 #include <cell.hxx>
37 #include <svx/svdotable.hxx>
38 #include <svx/svdoutl.hxx>
39 #include <editeng/editeng.hxx>
40 #include <editeng/outlobj.hxx>
41 
42 
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::table;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::beans;
47 
48 namespace sdr::table {
49 
51 {
52 public:
53  SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
54  void Write();
55  void WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart );
56  void WriteCell( sal_Int32 nCol, sal_Int32 nRow );
57 
58 private:
61  Reference< XTable > mxTable;
62 };
63 
64 void ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
65 {
66  SdrTableRtfExporter aEx( rStrm, rObj );
67  aEx.Write();
68 }
69 
70 const OUStringLiteral gsSize( "Size" );
71 
73 : mrStrm( rStrm )
74 , mrObj( rObj )
75 , mxTable( rObj.getTable() )
76 {
77 }
78 
79 static long HundMMToTwips( long nIn )
80 {
81  long nRet = OutputDevice::LogicToLogic( nIn, MapUnit::Map100thMM, MapUnit::MapTwip );
82  return nRet;
83 }
84 
86 {
89 
90  Reference< XTableColumns > xColumns( mxTable->getColumns() );
91  const sal_Int32 nColCount = xColumns->getCount();
92 
93  std::vector< sal_Int32 > aColumnStart;
94  aColumnStart.reserve( nColCount );
95 
96  // determine right offset of cells
97  sal_Int32 nPos = 0;
98  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
99  {
100  Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
101  sal_Int32 nWidth = 0;
102  xSet->getPropertyValue( gsSize ) >>= nWidth;
103  nPos += HundMMToTwips( nWidth );
104  aColumnStart.push_back( nPos );
105  }
106  catch( Exception& )
107  {
108  OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
109  }
110 
111  // export rows
112  Reference< XTableRows > xRows( mxTable->getRows() );
113  const sal_Int32 nRowCount = xRows->getCount();
114 
115  for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
116  {
117  Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
118  WriteRow( xRowSet, nRow, aColumnStart );
119  }
120  catch( Exception& )
121  {
122  OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
123  }
124 
126 }
127 
128 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
129 {
130  sal_Int32 nRowHeight = 0;
131  xRowSet->getPropertyValue( gsSize ) >>= nRowHeight;
132 
134  mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(nRowHeight) );
135 
136  const sal_Int32 nColCount = mxTable->getColumnCount();
137  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
138  {
139  CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
140 
141  if( !xCell.is() )
142  continue;
143 
144  mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(aColumnStart[nCol]) );
145  if ( (nCol & 0x0F) == 0x0F )
146  mrStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // prevent long lines
147  }
149 
150  sal_uLong nStrmPos = mrStrm.Tell();
151  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
152  {
153  WriteCell( nCol, nRow );
154  if ( mrStrm.Tell() - nStrmPos > 255 )
155  {
157  nStrmPos = mrStrm.Tell();
158  }
159  }
161 }
162 
163 
164 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
165 {
166  CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
167 
168  if( !xCell.is() || xCell->isMerged() )
169  {
171  return ;
172  }
173 
174  OUString aContent;
175 
176  OutlinerParaObject* pParaObj = xCell->CreateEditOutlinerParaObject().release();
177  bool bOwnParaObj = pParaObj != nullptr;
178 
179  if( pParaObj == nullptr )
180  pParaObj = xCell->GetOutlinerParaObject();
181 
182  if(pParaObj)
183  {
184  // handle outliner attributes
185  SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
186  rOutliner.SetText(*pParaObj);
187 
188  aContent = rOutliner.GetEditEngine().GetText();
189 
190  rOutliner.Clear();
191 
192  if( bOwnParaObj )
193  delete pParaObj;
194  }
195 
196  bool bResetAttr = false;
197 
198  SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
199 
200  const SfxItemSet& rCellSet = xCell->GetItemSet();
201 
202  const SvxWeightItem& rWeightItem = rCellSet.Get( EE_CHAR_WEIGHT );
203  const SvxPostureItem& rPostureItem = rCellSet.Get( EE_CHAR_ITALIC );
204  const SvxUnderlineItem& rUnderlineItem = rCellSet.Get( EE_CHAR_UNDERLINE );
205 
206  const char* pChar;
207 
208  switch( eHAdj )
209  {
214  default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
215  }
216  mrStrm.WriteCharPtr( pChar );
217 
218  if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
219  { // bold
220  bResetAttr = true;
222  }
223  if ( rPostureItem.GetPosture() != ITALIC_NONE )
224  { // italic
225  bResetAttr = true;
227  }
228  if ( rUnderlineItem.GetLineStyle() != LINESTYLE_NONE )
229  { // underline
230  bResetAttr = true;
232  }
233 
234  mrStrm.WriteChar( ' ' );
235  RTFOutFuncs::Out_String( mrStrm, aContent );
237 
238  if ( bResetAttr )
240 }
241 
242 }
243 
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUStringLiteral gsSize("Size")
const int nColCount
#define OOO_STRING_SVTOOLS_RTF_QL
OUString GetText(LineEnd eEnd=LINEEND_LF) const
#define OOO_STRING_SVTOOLS_RTF_QR
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
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 & WriteOString(const OString &rStr)
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
static long HundMMToTwips(long nIn)
#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
#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:1144
#define SAL_NEWLINE_STRING
#define OOO_STRING_SVTOOLS_RTF_QC
sal_uInt16 nPos
SdrTableRtfExporter(SvStream &rStrmP, SdrTableObj &rObj)