Line data Source code
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 { namespace table {
50 :
51 0 : class SdrTableRtfExporter
52 : {
53 : public:
54 : SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
55 : sal_uLong 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:
60 : SvStream& mrStrm;
61 : SdrTableObj& mrObj;
62 : Reference< XTable > mxTable;
63 : const OUString msSize;
64 : };
65 :
66 0 : void SdrTableObj::ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
67 : {
68 0 : SdrTableRtfExporter aEx( rStrm, rObj );
69 0 : aEx.Write();
70 0 : }
71 :
72 0 : SdrTableRtfExporter::SdrTableRtfExporter( SvStream& rStrm, SdrTableObj& rObj )
73 : : mrStrm( rStrm )
74 : , mrObj( rObj )
75 : , mxTable( rObj.getTable() )
76 0 : , msSize( "Size" )
77 : {
78 0 : }
79 :
80 0 : long HundMMToTwips( long nIn )
81 : {
82 0 : long nRet = OutputDevice::LogicToLogic( nIn, MAP_100TH_MM, MAP_TWIP );
83 0 : return nRet;
84 : }
85 :
86 0 : sal_uLong SdrTableRtfExporter::Write()
87 : {
88 0 : mrStrm.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF );
89 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI ).WriteCharPtr( SAL_NEWLINE_STRING );
90 :
91 0 : Reference< XTableColumns > xColumns( mxTable->getColumns() );
92 0 : const sal_Int32 nColCount = xColumns->getCount();
93 :
94 0 : std::vector< sal_Int32 > aColumnStart;
95 0 : aColumnStart.reserve( nColCount );
96 :
97 : // determine right offset of cells
98 0 : sal_Int32 nPos = 0;
99 0 : for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
100 : {
101 0 : Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
102 0 : sal_Int32 nWidth = 0;
103 0 : xSet->getPropertyValue( msSize ) >>= nWidth;
104 0 : nPos += HundMMToTwips( nWidth );
105 0 : aColumnStart.push_back( nPos );
106 : }
107 0 : catch( Exception& e )
108 : {
109 : (void)e;
110 : OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
111 : }
112 :
113 : // export rows
114 0 : Reference< XTableRows > xRows( mxTable->getRows() );
115 0 : const sal_Int32 nRowCount = xRows->getCount();
116 :
117 0 : for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
118 : {
119 0 : Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
120 0 : WriteRow( xRowSet, nRow, aColumnStart );
121 : }
122 0 : catch( Exception& e )
123 : {
124 : (void)e;
125 : OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
126 : }
127 :
128 0 : mrStrm.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING );
129 0 : return mrStrm.GetError();
130 : }
131 :
132 0 : void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
133 : {
134 0 : sal_Int32 nRowHeight = 0;
135 0 : xRowSet->getPropertyValue( msSize ) >>= nRowHeight;
136 :
137 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH ).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT ).WriteCharPtr( "-30" );
138 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteCharPtr( OString::number(nRowHeight).getStr() );
139 :
140 0 : const sal_Int32 nColCount = mxTable->getColumnCount();
141 0 : for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
142 : {
143 0 : CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
144 :
145 0 : if( !xCell.is() )
146 0 : continue;
147 :
148 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteCharPtr( OString::number(aColumnStart[nCol]).getStr() );
149 0 : if ( (nCol & 0x0F) == 0x0F )
150 0 : mrStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // Zeilen nicht zu lang werden lassen
151 0 : }
152 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL ).WriteCharPtr( SAL_NEWLINE_STRING );
153 :
154 0 : sal_uLong nStrmPos = mrStrm.Tell();
155 0 : for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
156 : {
157 0 : WriteCell( nCol, nRow );
158 0 : if ( mrStrm.Tell() - nStrmPos > 255 )
159 : {
160 0 : mrStrm.WriteCharPtr( SAL_NEWLINE_STRING );
161 0 : nStrmPos = mrStrm.Tell();
162 : }
163 : }
164 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW ).WriteCharPtr( SAL_NEWLINE_STRING );
165 0 : }
166 :
167 :
168 0 : void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
169 : {
170 0 : CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
171 :
172 0 : if( !xCell.is() || xCell->isMerged() )
173 : {
174 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
175 0 : return ;
176 : }
177 :
178 0 : OUString aContent;
179 :
180 0 : OutlinerParaObject* pParaObj = xCell->GetEditOutlinerParaObject();
181 0 : bool bOwnParaObj = pParaObj != 0;
182 :
183 0 : if( pParaObj == 0 )
184 0 : pParaObj = xCell->GetOutlinerParaObject();
185 :
186 0 : if(pParaObj)
187 : {
188 : // handle outliner attributes
189 0 : SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
190 0 : rOutliner.SetText(*pParaObj);
191 :
192 0 : aContent = rOutliner.GetEditEngine().GetText( LINEEND_LF );
193 :
194 0 : rOutliner.Clear();
195 :
196 0 : if( bOwnParaObj )
197 0 : delete pParaObj;
198 : }
199 :
200 0 : bool bResetAttr = false;
201 :
202 0 : SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
203 :
204 0 : const SfxItemSet& rCellSet = xCell->GetItemSet();
205 :
206 0 : const SvxWeightItem& rWeightItem = static_cast<const SvxWeightItem&> ( rCellSet.Get( EE_CHAR_WEIGHT ) );
207 0 : const SvxPostureItem& rPostureItem = static_cast<const SvxPostureItem&> ( rCellSet.Get( EE_CHAR_ITALIC ) );
208 0 : const SvxUnderlineItem& rUnderlineItem = static_cast<const SvxUnderlineItem&>( rCellSet.Get( EE_CHAR_UNDERLINE ) );
209 :
210 : const sal_Char* pChar;
211 :
212 0 : switch( eHAdj )
213 : {
214 0 : case SDRTEXTHORZADJUST_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
215 0 : case SDRTEXTHORZADJUST_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
216 0 : case SDRTEXTHORZADJUST_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
217 : case SDRTEXTHORZADJUST_LEFT:
218 0 : default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
219 : }
220 0 : mrStrm.WriteCharPtr( pChar );
221 :
222 0 : if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
223 : { // bold
224 0 : bResetAttr = true;
225 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B );
226 : }
227 0 : if ( rPostureItem.GetPosture() != ITALIC_NONE )
228 : { // italic
229 0 : bResetAttr = true;
230 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I );
231 : }
232 0 : if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
233 : { // underline
234 0 : bResetAttr = true;
235 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL );
236 : }
237 :
238 0 : mrStrm.WriteChar( ' ' );
239 0 : RTFOutFuncs::Out_String( mrStrm, aContent );
240 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
241 :
242 0 : if ( bResetAttr )
243 0 : mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN );
244 : }
245 :
246 390 : } }
247 :
248 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|