1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <scitems.hxx>
22 #include <editeng/wghtitem.hxx>
23 #include <editeng/postitem.hxx>
24 #include <editeng/udlnitem.hxx>
25 #include <editeng/justifyitem.hxx>
26 #include <svtools/rtfout.hxx>
27 #include <svtools/rtfkeywd.hxx>
28 #include <tools/stream.hxx>
31 #include <cellvalue.hxx>
32 #include <document.hxx>
33 #include <patattr.hxx>
35 #include <cellform.hxx>
36 #include <editutil.hxx>
39 void ScFormatFilterPluginImpl::ScExportRTF( SvStream
& rStrm
, ScDocument
* pDoc
,
40 const ScRange
& rRange
, const rtl_TextEncoding
/*eNach*/ )
42 ScRTFExport
aEx( rStrm
, pDoc
, rRange
);
46 ScRTFExport::ScRTFExport( SvStream
& rStrmP
, ScDocument
* pDocP
, const ScRange
& rRangeP
)
48 ScExportBase( rStrmP
, pDocP
, rRangeP
),
49 m_pCellX( new sal_uLong
[ pDoc
->MaxCol()+2 ] )
53 ScRTFExport::~ScRTFExport()
57 void ScRTFExport::Write()
59 rStrm
.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF
);
60 rStrm
.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI
).WriteCharPtr( SAL_NEWLINE_STRING
);
63 for ( SCTAB nTab
= aRange
.aStart
.Tab(); nTab
<= aRange
.aEnd
.Tab(); nTab
++ )
65 if ( nTab
> aRange
.aStart
.Tab() )
66 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_PAR
);
71 rStrm
.WriteStream(m_aDocStrm
);
72 rStrm
.WriteChar( '}' ).WriteOString( SAL_NEWLINE_STRING
);
75 void ScRTFExport::WriteTab( SCTAB nTab
)
77 m_aDocStrm
.WriteChar( '{' ).WriteOString( SAL_NEWLINE_STRING
);
78 if ( pDoc
->HasTable( nTab
) )
80 memset( &m_pCellX
[0], 0, (pDoc
->MaxCol()+2) * sizeof(sal_uLong
) );
82 SCCOL nEndCol
= aRange
.aEnd
.Col();
83 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
85 m_pCellX
[nCol
+1] = m_pCellX
[nCol
] + pDoc
->GetColWidth( nCol
, nTab
);
88 SCROW nEndRow
= aRange
.aEnd
.Row();
89 for ( SCROW nRow
= aRange
.aStart
.Row(); nRow
<= nEndRow
; nRow
++ )
91 WriteRow( nTab
, nRow
);
94 m_aDocStrm
.WriteChar( '}' ).WriteOString( SAL_NEWLINE_STRING
);
97 void ScRTFExport::WriteRow( SCTAB nTab
, SCROW nRow
)
99 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_TROWD
).WriteOString( OOO_STRING_SVTOOLS_RTF_TRGAPH
).WriteOString( "30" ).WriteOString( OOO_STRING_SVTOOLS_RTF_TRLEFT
).WriteOString( "-30" );
100 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_TRRH
).WriteOString( OString::number(pDoc
->GetRowHeight(nRow
, nTab
)) );
102 SCCOL nEndCol
= aRange
.aEnd
.Col();
103 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
105 const ScPatternAttr
* pAttr
= pDoc
->GetPattern( nCol
, nRow
, nTab
);
106 const ScMergeAttr
& rMergeAttr
= pAttr
->GetItem( ATTR_MERGE
);
107 const SvxVerJustifyItem
& rVerJustifyItem
= pAttr
->GetItem( ATTR_VER_JUSTIFY
);
111 if ( rMergeAttr
.GetColMerge() != 0 )
112 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CLMGF
);
115 const ScMergeFlagAttr
& rMergeFlagAttr
= pAttr
->GetItem( ATTR_MERGE_FLAG
);
116 if ( rMergeFlagAttr
.IsHorOverlapped() )
117 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CLMRG
);
120 switch( rVerJustifyItem
.GetValue() )
122 case SvxCellVerJustify::Top
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALT
; break;
123 case SvxCellVerJustify::Center
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALC
; break;
124 case SvxCellVerJustify::Bottom
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALB
; break;
125 case SvxCellVerJustify::Standard
: pChar
= OOO_STRING_SVTOOLS_RTF_CLVERTALB
; break; //! Bottom
126 default: pChar
= nullptr; break;
129 m_aDocStrm
.WriteOString( pChar
);
131 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CELLX
).WriteOString( OString::number(m_pCellX
[nCol
+1]) );
132 if ( (nCol
& 0x0F) == 0x0F )
133 m_aDocStrm
.WriteOString( SAL_NEWLINE_STRING
); // Do not let lines get too long
135 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_PARD
).WriteOString( OOO_STRING_SVTOOLS_RTF_PLAIN
).WriteOString( OOO_STRING_SVTOOLS_RTF_INTBL
).WriteOString( SAL_NEWLINE_STRING
);
137 sal_uLong nStrmPos
= m_aDocStrm
.Tell();
138 for ( nCol
= aRange
.aStart
.Col(); nCol
<= nEndCol
; nCol
++ )
140 WriteCell( nTab
, nRow
, nCol
);
141 if ( m_aDocStrm
.Tell() - nStrmPos
> 255 )
142 { // Do not let lines get too long
143 m_aDocStrm
.WriteOString( SAL_NEWLINE_STRING
);
144 nStrmPos
= m_aDocStrm
.Tell();
147 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_ROW
).WriteOString( SAL_NEWLINE_STRING
);
150 void ScRTFExport::WriteCell( SCTAB nTab
, SCROW nRow
, SCCOL nCol
)
152 const ScPatternAttr
* pAttr
= pDoc
->GetPattern( nCol
, nRow
, nTab
);
154 const ScMergeFlagAttr
& rMergeFlagAttr
= pAttr
->GetItem( ATTR_MERGE_FLAG
);
155 if ( rMergeFlagAttr
.IsHorOverlapped() )
157 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CELL
);
161 bool bValueData
= false;
163 ScAddress
aPos(nCol
, nRow
, nTab
);
164 ScRefCellValue
aCell(*pDoc
, aPos
);
165 switch (aCell
.meType
)
173 const EditTextObject
* pObj
= aCell
.mpEditText
;
174 EditEngine
& rEngine
= GetEditEngine();
175 rEngine
.SetText(*pObj
);
176 aContent
= rEngine
.GetText(); // LineFeed in between paragraphs!
181 bValueData
= pDoc
->HasValueData(aPos
);
182 sal_uInt32 nFormat
= pAttr
->GetNumberFormat(pFormatter
);
184 aContent
= ScCellFormat::GetString(*pDoc
, aPos
, nFormat
, &pColor
, *pFormatter
);
188 bool bResetAttr(false);
190 const SvxHorJustifyItem
& rHorJustifyItem
= pAttr
->GetItem( ATTR_HOR_JUSTIFY
);
191 const SvxWeightItem
& rWeightItem
= pAttr
->GetItem( ATTR_FONT_WEIGHT
);
192 const SvxPostureItem
& rPostureItem
= pAttr
->GetItem( ATTR_FONT_POSTURE
);
193 const SvxUnderlineItem
& rUnderlineItem
= pAttr
->GetItem( ATTR_FONT_UNDERLINE
);
197 switch( rHorJustifyItem
.GetValue() )
199 case SvxCellHorJustify::Standard
:
200 pChar
= (bValueData
? OOO_STRING_SVTOOLS_RTF_QR
: OOO_STRING_SVTOOLS_RTF_QL
);
202 case SvxCellHorJustify::Center
: pChar
= OOO_STRING_SVTOOLS_RTF_QC
; break;
203 case SvxCellHorJustify::Block
: pChar
= OOO_STRING_SVTOOLS_RTF_QJ
; break;
204 case SvxCellHorJustify::Right
: pChar
= OOO_STRING_SVTOOLS_RTF_QR
; break;
205 case SvxCellHorJustify::Left
:
206 case SvxCellHorJustify::Repeat
:
207 default: pChar
= OOO_STRING_SVTOOLS_RTF_QL
; break;
209 m_aDocStrm
.WriteOString( pChar
);
211 if ( rWeightItem
.GetWeight() >= WEIGHT_BOLD
)
214 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_B
);
216 if ( rPostureItem
.GetPosture() != ITALIC_NONE
)
219 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_I
);
221 if ( rUnderlineItem
.GetLineStyle() != LINESTYLE_NONE
)
224 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_UL
);
227 m_aDocStrm
.WriteChar( ' ' );
228 RTFOutFuncs::Out_String( m_aDocStrm
, aContent
);
229 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_CELL
);
232 m_aDocStrm
.WriteOString( OOO_STRING_SVTOOLS_RTF_PLAIN
);
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */