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 <celltypes.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 static const OUStringLiteral gsSize( "Size" );
72 
74 : mrStrm( rStrm )
75 , mrObj( rObj )
76 , mxTable( rObj.getTable() )
77 {
78 }
79 
80 static long HundMMToTwips( long nIn )
81 {
82  long nRet = OutputDevice::LogicToLogic( nIn, MapUnit::Map100thMM, MapUnit::MapTwip );
83  return nRet;
84 }
85 
87 {
90 
91  Reference< XTableColumns > xColumns( mxTable->getColumns() );
92  const sal_Int32 nColCount = xColumns->getCount();
93 
94  std::vector< sal_Int32 > aColumnStart;
95  aColumnStart.reserve( nColCount );
96 
97  // determine right offset of cells
98  sal_Int32 nPos = 0;
99  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
100  {
101  Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
102  sal_Int32 nWidth = 0;
103  xSet->getPropertyValue( gsSize ) >>= nWidth;
104  nPos += HundMMToTwips( nWidth );
105  aColumnStart.push_back( nPos );
106  }
107  catch( Exception& )
108  {
109  OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
110  }
111 
112  // export rows
113  Reference< XTableRows > xRows( mxTable->getRows() );
114  const sal_Int32 nRowCount = xRows->getCount();
115 
116  for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
117  {
118  Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
119  WriteRow( xRowSet, nRow, aColumnStart );
120  }
121  catch( Exception& )
122  {
123  OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
124  }
125 
127 }
128 
129 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
130 {
131  sal_Int32 nRowHeight = 0;
132  xRowSet->getPropertyValue( gsSize ) >>= nRowHeight;
133 
135  mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(nRowHeight) );
136 
137  const sal_Int32 nColCount = mxTable->getColumnCount();
138  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
139  {
140  CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
141 
142  if( !xCell.is() )
143  continue;
144 
145  mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(aColumnStart[nCol]) );
146  if ( (nCol & 0x0F) == 0x0F )
147  mrStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // prevent long lines
148  }
150 
151  sal_uLong nStrmPos = mrStrm.Tell();
152  for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
153  {
154  WriteCell( nCol, nRow );
155  if ( mrStrm.Tell() - nStrmPos > 255 )
156  {
158  nStrmPos = mrStrm.Tell();
159  }
160  }
162 }
163 
164 
165 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
166 {
167  CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
168 
169  if( !xCell.is() || xCell->isMerged() )
170  {
172  return ;
173  }
174 
175  OUString aContent;
176 
177  OutlinerParaObject* pParaObj = xCell->CreateEditOutlinerParaObject().release();
178  bool bOwnParaObj = pParaObj != nullptr;
179 
180  if( pParaObj == nullptr )
181  pParaObj = xCell->GetOutlinerParaObject();
182 
183  if(pParaObj)
184  {
185  // handle outliner attributes
186  SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
187  rOutliner.SetText(*pParaObj);
188 
189  aContent = rOutliner.GetEditEngine().GetText();
190 
191  rOutliner.Clear();
192 
193  if( bOwnParaObj )
194  delete pParaObj;
195  }
196 
197  bool bResetAttr = false;
198 
199  SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
200 
201  const SfxItemSet& rCellSet = xCell->GetItemSet();
202 
203  const SvxWeightItem& rWeightItem = rCellSet.Get( EE_CHAR_WEIGHT );
204  const SvxPostureItem& rPostureItem = rCellSet.Get( EE_CHAR_ITALIC );
205  const SvxUnderlineItem& rUnderlineItem = rCellSet.Get( EE_CHAR_UNDERLINE );
206 
207  const char* pChar;
208 
209  switch( eHAdj )
210  {
215  default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
216  }
217  mrStrm.WriteCharPtr( pChar );
218 
219  if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
220  { // bold
221  bResetAttr = true;
223  }
224  if ( rPostureItem.GetPosture() != ITALIC_NONE )
225  { // italic
226  bResetAttr = true;
228  }
229  if ( rUnderlineItem.GetLineStyle() != LINESTYLE_NONE )
230  { // underline
231  bResetAttr = true;
233  }
234 
235  mrStrm.WriteChar( ' ' );
236  RTFOutFuncs::Out_String( mrStrm, aContent );
238 
239  if ( bResetAttr )
241 }
242 
243 }
244 
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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
#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 EE_CHAR_ITALIC
#define OOO_STRING_SVTOOLS_RTF_ROW
#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 EE_CHAR_UNDERLINE
#define OOO_STRING_SVTOOLS_RTF_TRRH
#define OOO_STRING_SVTOOLS_RTF_PLAIN
ITALIC_NONE
static const OUStringLiteral gsSize("Size")
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 EE_CHAR_WEIGHT
#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)
void Clear()
SdrOutliner & ImpGetDrawOutliner() const
Definition: svdotext.cxx:1161
#define SAL_NEWLINE_STRING
#define OOO_STRING_SVTOOLS_RTF_QC
sal_uInt16 nPos
SdrTableRtfExporter(SvStream &rStrmP, SdrTableObj &rObj)