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>
25 #include <editeng/justifyitem.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 
46 ScRTFExport::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 
95 void ScRTFExport::WriteRow( SCTAB nTab, SCROW nRow )
96 {
98  rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteOString( OString::number(pDoc->GetRowHeight(nRow, nTab)) );
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 )
127  rStrm.WriteCharPtr( pChar );
128 
129  rStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteOString( OString::number(pCellX[nCol+1]) );
130  if ( (nCol & 0x0F) == 0x0F )
131  rStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // Do not let lines get too long
132  }
134 
135  sal_uLong 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 
148 void 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.meType)
164  {
165  case CELLTYPE_NONE:
166  bValueData = false;
167  break;
168  case CELLTYPE_EDIT:
169  {
170  bValueData = false;
171  const EditTextObject* pObj = aCell.mpEditText;
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  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:
198  pChar = (bValueData ? OOO_STRING_SVTOOLS_RTF_QR : OOO_STRING_SVTOOLS_RTF_QL);
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  }
207  rStrm.WriteCharPtr( pChar );
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: */
SCCOL GetColMerge() const
Definition: attrib.hxx:69
#define OOO_STRING_SVTOOLS_RTF_QL
OUString GetText(LineEnd eEnd=LINEEND_LF) const
ScAddress aStart
Definition: address.hxx:500
#define OOO_STRING_SVTOOLS_RTF_QR
void WriteRow(SCTAB nTab, SCROW nRow)
Definition: rtfexp.cxx:95
SCROW Row() const
Definition: address.hxx:262
static void GetString(const ScRefCellValue &rCell, sal_uInt32 nFormat, OUString &rString, Color **ppColor, SvNumberFormatter &rFormatter, const ScDocument *pDoc, bool bNullVals=true, bool bFormula=false, bool bUseStarFormat=false)
Definition: cellform.cxx:33
SvStream & rStrm
Definition: expbase.hxx:34
sal_uIntPtr sal_uLong
SvNumberFormatter * pFormatter
Definition: expbase.hxx:37
#define OOO_STRING_SVTOOLS_RTF_ANSI
ScAddress aEnd
Definition: address.hxx:501
#define OOO_STRING_SVTOOLS_RTF_INTBL
SvStream & WriteOString(const OString &rStr)
virtual void ScExportRTF(SvStream &, ScDocument *, const ScRange &rRange, const rtl_TextEncoding eDest) override
Definition: rtfexp.cxx:39
This is very similar to ScCellValue, except that it references the original value instead of copying ...
Definition: cellvalue.hxx:104
#define OOO_STRING_SVTOOLS_RTF_CLVERTALB
SvStream & WriteCharPtr(const char *pBuf)
constexpr TypedWhichId< ScMergeAttr > ATTR_MERGE(144)
SVT_DLLPUBLIC SvStream & Out_String(SvStream &, const OUString &, rtl_TextEncoding eDestEnc=RTL_TEXTENCODING_MS_1252)
FontItalic GetPosture() const
SC_DLLPUBLIC sal_uInt16 GetRowHeight(SCROW nRow, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4164
WEIGHT_BOLD
const EditTextObject * mpEditText
Definition: cellvalue.hxx:110
LINESTYLE_NONE
void SetText(const OUString &rStr)
#define OOO_STRING_SVTOOLS_RTF_I
#define OOO_STRING_SVTOOLS_RTF_QJ
SC_DLLPUBLIC bool HasTable(SCTAB nTab) const
Definition: document.cxx:191
SCTAB Tab() const
Definition: address.hxx:271
#define OOO_STRING_SVTOOLS_RTF_CLMGF
#define OOO_STRING_SVTOOLS_RTF_ROW
SC_DLLPUBLIC const ScPatternAttr * GetPattern(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:4738
constexpr TypedWhichId< SvxUnderlineItem > ATTR_FONT_UNDERLINE(104)
const SfxPoolItem & GetItem(sal_uInt16 nWhichP) const
Definition: patattr.hxx:70
#define OOO_STRING_SVTOOLS_RTF_TRLEFT
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:872
constexpr TypedWhichId< SvxPostureItem > ATTR_FONT_POSTURE(103)
#define OOO_STRING_SVTOOLS_RTF_PARD
virtual ~ScRTFExport() override
Definition: rtfexp.cxx:53
FontWeight GetWeight() const
sal_Int16 SCCOL
Definition: types.hxx:22
constexpr TypedWhichId< SvxVerJustifyItem > ATTR_VER_JUSTIFY(132)
void WriteTab(SCTAB nTab)
Definition: rtfexp.cxx:73
std::unique_ptr< sal_uLong[]> pCellX
Definition: rtfexp.hxx:29
ScDocument * pDoc
Definition: expbase.hxx:36
void Write()
Definition: rtfexp.cxx:57
#define OOO_STRING_SVTOOLS_RTF_TRRH
#define OOO_STRING_SVTOOLS_RTF_PLAIN
sal_uInt32 GetNumberFormat(SvNumberFormatter *) const
Definition: patattr.cxx:1257
ITALIC_NONE
SC_DLLPUBLIC bool HasValueData(SCCOL nCol, SCROW nRow, SCTAB nTab) const
Definition: document.cxx:3768
#define OOO_STRING_SVTOOLS_RTF_PAR
SCCOL Col() const
Definition: address.hxx:267
#define OOO_STRING_SVTOOLS_RTF_B
#define OOO_STRING_SVTOOLS_RTF_CLVERTALC
#define OOO_STRING_SVTOOLS_RTF_CELL
bool IsHorOverlapped() const
Definition: attrib.hxx:100
FontLineStyle GetLineStyle() const
CellType meType
Definition: cellvalue.hxx:106
#define OOO_STRING_SVTOOLS_RTF_TROWD
constexpr TypedWhichId< ScMergeFlagAttr > ATTR_MERGE_FLAG(145)
sal_Int32 SCROW
Definition: types.hxx:18
#define OOO_STRING_SVTOOLS_RTF_TRGAPH
#define OOO_STRING_SVTOOLS_RTF_CELLX
ScRange aRange
Definition: expbase.hxx:35
#define OOO_STRING_SVTOOLS_RTF_RTF
constexpr TypedWhichId< SvxWeightItem > ATTR_FONT_WEIGHT(102)
constexpr TypedWhichId< SvxHorJustifyItem > ATTR_HOR_JUSTIFY(129)
sal_uInt64 Tell() const
const char * pChar
#define OOO_STRING_SVTOOLS_RTF_UL
#define OOO_STRING_SVTOOLS_RTF_CLVERTALT
SvStream & WriteChar(char nChar)
void WriteCell(SCTAB nTab, SCROW nRow, SCCOL nCol)
Definition: rtfexp.cxx:148
#define OOO_STRING_SVTOOLS_RTF_CLMRG
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4123
ScFieldEditEngine & GetEditEngine() const
Definition: expbase.cxx:68
#define SAL_NEWLINE_STRING
#define OOO_STRING_SVTOOLS_RTF_QC
sal_Int16 SCTAB
Definition: types.hxx:23
ScRTFExport(SvStream &, ScDocument *, const ScRange &)
Definition: rtfexp.cxx:46