LibreOffice Module sc (master) 1
rtfexp.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#include <scitems.hxx>
21
22#include <editeng/wghtitem.hxx>
23#include <editeng/postitem.hxx>
24#include <editeng/udlnitem.hxx>
26#include <svtools/rtfout.hxx>
27#include <svtools/rtfkeywd.hxx>
28#include <tools/stream.hxx>
29
30#include <rtfexp.hxx>
31#include <cellvalue.hxx>
32#include <document.hxx>
33#include <patattr.hxx>
34#include <attrib.hxx>
35#include <cellform.hxx>
36#include <editutil.hxx>
37#include <ftools.hxx>
38
40 const ScRange& rRange, const rtl_TextEncoding /*eNach*/ )
41{
42 ScRTFExport aEx( rStrm, pDoc, rRange );
43 aEx.Write();
44}
45
46ScRTFExport::ScRTFExport( SvStream& rStrmP, ScDocument* pDocP, const ScRange& rRangeP )
47 :
48 ScExportBase( rStrmP, pDocP, rRangeP ),
49 pCellX( new sal_uLong[ pDoc->MaxCol()+2 ] )
50{
51}
52
54{
55}
56
58{
61
62 // Data
63 for ( SCTAB nTab = aRange.aStart.Tab(); nTab <= aRange.aEnd.Tab(); nTab++ )
64 {
65 if ( nTab > aRange.aStart.Tab() )
67 WriteTab( nTab );
68 }
69
71}
72
74{
76 if ( pDoc->HasTable( nTab ) )
77 {
78 memset( &pCellX[0], 0, (pDoc->MaxCol()+2) * sizeof(sal_uLong) );
79 SCCOL nCol;
80 SCCOL nEndCol = aRange.aEnd.Col();
81 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
82 {
83 pCellX[nCol+1] = pCellX[nCol] + pDoc->GetColWidth( nCol, nTab );
84 }
85
86 SCROW nEndRow = aRange.aEnd.Row();
87 for ( SCROW nRow = aRange.aStart.Row(); nRow <= nEndRow; nRow++ )
88 {
89 WriteRow( nTab, nRow );
90 }
91 }
93}
94
96{
99 SCCOL nCol;
100 SCCOL nEndCol = aRange.aEnd.Col();
101 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
102 {
103 const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
104 const ScMergeAttr& rMergeAttr = pAttr->GetItem( ATTR_MERGE );
105 const SvxVerJustifyItem& rVerJustifyItem= pAttr->GetItem( ATTR_VER_JUSTIFY );
106
107 const char* pChar;
108
109 if ( rMergeAttr.GetColMerge() != 0 )
111 else
112 {
113 const ScMergeFlagAttr& rMergeFlagAttr = pAttr->GetItem( ATTR_MERGE_FLAG );
114 if ( rMergeFlagAttr.IsHorOverlapped() )
116 }
117
118 switch( rVerJustifyItem.GetValue() )
119 {
120 case SvxCellVerJustify::Top: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALT; break;
121 case SvxCellVerJustify::Center: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALC; break;
122 case SvxCellVerJustify::Bottom: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break;
123 case SvxCellVerJustify::Standard: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break;
124 default: pChar = nullptr; break;
125 }
126 if ( pChar )
128
130 if ( (nCol & 0x0F) == 0x0F )
131 rStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // Do not let lines get too long
132 }
134
135 sal_uInt64 nStrmPos = rStrm.Tell();
136 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
137 {
138 WriteCell( nTab, nRow, nCol );
139 if ( rStrm.Tell() - nStrmPos > 255 )
140 { // Do not let lines get too long
142 nStrmPos = rStrm.Tell();
143 }
144 }
146}
147
148void ScRTFExport::WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol )
149{
150 const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
151
152 const ScMergeFlagAttr& rMergeFlagAttr = pAttr->GetItem( ATTR_MERGE_FLAG );
153 if ( rMergeFlagAttr.IsHorOverlapped() )
154 {
156 return ;
157 }
158
159 bool bValueData = false;
160 OUString aContent;
161 ScAddress aPos(nCol, nRow, nTab);
162 ScRefCellValue aCell(*pDoc, aPos);
163 switch (aCell.getType())
164 {
165 case CELLTYPE_NONE:
166 bValueData = false;
167 break;
168 case CELLTYPE_EDIT:
169 {
170 bValueData = false;
171 const EditTextObject* pObj = aCell.getEditText();
172 EditEngine& rEngine = GetEditEngine();
173 rEngine.SetText(*pObj);
174 aContent = rEngine.GetText(); // LineFeed in between paragraphs!
175 }
176 break;
177 default:
178 {
179 bValueData = pDoc->HasValueData(aPos);
180 sal_uInt32 nFormat = pAttr->GetNumberFormat(pFormatter);
181 const Color* pColor;
182 aContent = ScCellFormat::GetString(*pDoc, aPos, nFormat, &pColor, *pFormatter);
183 }
184 }
185
186 bool bResetAttr(false);
187
188 const SvxHorJustifyItem& rHorJustifyItem = pAttr->GetItem( ATTR_HOR_JUSTIFY );
189 const SvxWeightItem& rWeightItem = pAttr->GetItem( ATTR_FONT_WEIGHT );
190 const SvxPostureItem& rPostureItem = pAttr->GetItem( ATTR_FONT_POSTURE );
191 const SvxUnderlineItem& rUnderlineItem = pAttr->GetItem( ATTR_FONT_UNDERLINE );
192
193 const char* pChar;
194
195 switch( rHorJustifyItem.GetValue() )
196 {
197 case SvxCellHorJustify::Standard:
199 break;
200 case SvxCellHorJustify::Center: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
201 case SvxCellHorJustify::Block: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
202 case SvxCellHorJustify::Right: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
203 case SvxCellHorJustify::Left:
204 case SvxCellHorJustify::Repeat:
205 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
206 }
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 rStrm.WriteChar( ' ' );
226 RTFOutFuncs::Out_String( rStrm, aContent );
228
229 if ( bResetAttr )
231}
232
233/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetText(LineEnd eEnd=LINEEND_LF) const
void SetText(const OUString &rStr)
SCTAB Tab() const
Definition: address.hxx:283
SCROW Row() const
Definition: address.hxx:274
SCCOL Col() const
Definition: address.hxx:279
static OUString GetString(const ScRefCellValue &rCell, sal_uInt32 nFormat, const Color **ppColor, SvNumberFormatter &rFormatter, const ScDocument &rDoc, bool bNullVals=true, bool bFormula=false, bool bUseStarFormat=false)
Definition: cellform.cxx:31
SC_DLLPUBLIC sal_uInt16 GetRowHeight(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4224
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4183
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:891
SC_DLLPUBLIC bool HasValueData(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:3806
SC_DLLPUBLIC bool HasTable(SCTAB nTab) const
Definition: document.cxx:194
SC_DLLPUBLIC const ScPatternAttr * GetPattern(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:4812
SvStream & rStrm
Definition: expbase.hxx:33
ScDocument * pDoc
Definition: expbase.hxx:35
ScRange aRange
Definition: expbase.hxx:34
ScFieldEditEngine & GetEditEngine() const
Definition: expbase.cxx:68
SvNumberFormatter * pFormatter
Definition: expbase.hxx:36
virtual void ScExportRTF(SvStream &, ScDocument *, const ScRange &rRange, const rtl_TextEncoding eDest) override
Definition: rtfexp.cxx:39
SCCOL GetColMerge() const
Definition: attrib.hxx:68
bool IsHorOverlapped() const
Definition: attrib.hxx:99
sal_uInt32 GetNumberFormat(SvNumberFormatter *) const
Definition: patattr.cxx:1301
const SfxPoolItem & GetItem(sal_uInt16 nWhichP) const
Definition: patattr.hxx:72
void WriteRow(SCTAB nTab, SCROW nRow)
Definition: rtfexp.cxx:95
void WriteCell(SCTAB nTab, SCROW nRow, SCCOL nCol)
Definition: rtfexp.cxx:148
virtual ~ScRTFExport() override
Definition: rtfexp.cxx:53
ScRTFExport(SvStream &, ScDocument *, const ScRange &)
Definition: rtfexp.cxx:46
void Write()
Definition: rtfexp.cxx:57
std::unique_ptr< sal_uLong[]> pCellX
Definition: rtfexp.hxx:28
void WriteTab(SCTAB nTab)
Definition: rtfexp.cxx:73
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
sal_uInt64 Tell() const
SvStream & WriteOString(std::string_view rStr)
SvStream & WriteChar(char nChar)
SvStream & WriteCharPtr(const char *pBuf)
FontItalic GetPosture() const
FontLineStyle GetLineStyle() const
FontWeight GetWeight() const
#define SAL_NEWLINE_STRING
LINESTYLE_NONE
ITALIC_NONE
WEIGHT_BOLD
@ CELLTYPE_EDIT
Definition: global.hxx:276
@ CELLTYPE_NONE
Definition: global.hxx:272
SVT_DLLPUBLIC SvStream & Out_String(SvStream &, std::u16string_view, rtl_TextEncoding eDestEnc=RTL_TEXTENCODING_MS_1252)
void SvStream & rStrm
#define OOO_STRING_SVTOOLS_RTF_CELL
#define OOO_STRING_SVTOOLS_RTF_QC
#define OOO_STRING_SVTOOLS_RTF_B
#define OOO_STRING_SVTOOLS_RTF_RTF
#define OOO_STRING_SVTOOLS_RTF_CLMRG
#define OOO_STRING_SVTOOLS_RTF_CLMGF
#define OOO_STRING_SVTOOLS_RTF_CLVERTALC
#define OOO_STRING_SVTOOLS_RTF_UL
#define OOO_STRING_SVTOOLS_RTF_QL
#define OOO_STRING_SVTOOLS_RTF_PAR
#define OOO_STRING_SVTOOLS_RTF_QJ
#define OOO_STRING_SVTOOLS_RTF_CELLX
#define OOO_STRING_SVTOOLS_RTF_TRRH
#define OOO_STRING_SVTOOLS_RTF_QR
#define OOO_STRING_SVTOOLS_RTF_ROW
#define OOO_STRING_SVTOOLS_RTF_ANSI
#define OOO_STRING_SVTOOLS_RTF_TRLEFT
#define OOO_STRING_SVTOOLS_RTF_INTBL
#define OOO_STRING_SVTOOLS_RTF_TROWD
#define OOO_STRING_SVTOOLS_RTF_CLVERTALT
#define OOO_STRING_SVTOOLS_RTF_TRGAPH
#define OOO_STRING_SVTOOLS_RTF_PLAIN
#define OOO_STRING_SVTOOLS_RTF_CLVERTALB
#define OOO_STRING_SVTOOLS_RTF_I
#define OOO_STRING_SVTOOLS_RTF_PARD
constexpr TypedWhichId< ScMergeFlagAttr > ATTR_MERGE_FLAG(145)
constexpr TypedWhichId< SvxPostureItem > ATTR_FONT_POSTURE(103)
constexpr TypedWhichId< SvxWeightItem > ATTR_FONT_WEIGHT(102)
constexpr TypedWhichId< ScMergeAttr > ATTR_MERGE(144)
constexpr TypedWhichId< SvxHorJustifyItem > ATTR_HOR_JUSTIFY(129)
constexpr TypedWhichId< SvxVerJustifyItem > ATTR_VER_JUSTIFY(132)
constexpr TypedWhichId< SvxUnderlineItem > ATTR_FONT_UNDERLINE(104)
sal_uIntPtr sal_uLong
This is very similar to ScCellValue, except that it references the original value instead of copying ...
Definition: cellvalue.hxx:108
const EditTextObject * getEditText() const
Definition: cellvalue.hxx:136
CellType getType() const
Definition: cellvalue.hxx:133
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17
const char * pChar