LibreOffice Module sw (master)  1
htmlnumwriter.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 <com/sun/star/style/NumberingType.hpp>
21 #include <hintids.hxx>
22 #include <svtools/htmltokn.h>
23 #include <svtools/htmlkywd.hxx>
24 #include <svtools/htmlout.hxx>
25 #include <svl/urihelper.hxx>
26 #include <editeng/brushitem.hxx>
27 #include <editeng/lrspitem.hxx>
28 #include <vcl/svapp.hxx>
29 #include <numrule.hxx>
30 #include <doc.hxx>
31 #include <docary.hxx>
32 #include <poolfmt.hxx>
33 #include <ndtxt.hxx>
34 #include <paratr.hxx>
35 #include <pam.hxx>
36 
37 #include "htmlnum.hxx"
38 #include "wrthtml.hxx"
39 
40 #include <SwNodeNum.hxx>
41 #include <rtl/strbuf.hxx>
42 #include <osl/diagnose.h>
43 
44 using namespace css;
45 
46 
48 {
49  m_pNextNumRuleInfo = nullptr;
50 
51  sal_uLong nPos = m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
52 
53  bool bTable = false;
54  do
55  {
56  const SwNode* pNd = m_pDoc->GetNodes()[nPos];
57  if( pNd->IsTextNode() )
58  {
59  m_pNextNumRuleInfo.reset( new SwHTMLNumRuleInfo( *pNd->GetTextNode() ) );
60 
61  // Before a table we keep the old level if the same numbering is
62  // continued after the table and no new numbering is started.
63  // The table will get the indentation that corresponds to its
64  // numbering level during import.
65  if( bTable &&
66  m_pNextNumRuleInfo->GetNumRule()==GetNumInfo().GetNumRule() &&
67  !m_pNextNumRuleInfo->IsRestart() )
68  {
69  m_pNextNumRuleInfo->SetDepth( GetNumInfo().GetDepth() );
70  }
71  }
72  else if( pNd->IsTableNode() )
73  {
74  // A table is skipped so the node after table is viewed.
75  nPos = pNd->EndOfSectionIndex() + 1;
76  bTable = true;
77  }
78  else
79  {
80  // In all other case the numbering is over.
81  m_pNextNumRuleInfo.reset(new SwHTMLNumRuleInfo);
82  }
83  }
84  while( !m_pNextNumRuleInfo );
85 }
86 
88 {
89  m_pNextNumRuleInfo.reset();
90 }
91 
92 void SwHTMLWriter::SetNextNumInfo( std::unique_ptr<SwHTMLNumRuleInfo> pNxt )
93 {
94  m_pNextNumRuleInfo = std::move(pNxt);
95 }
96 
98  const SwHTMLNumRuleInfo& rInfo )
99 {
100  SwHTMLNumRuleInfo& rPrevInfo = rWrt.GetNumInfo();
101  bool bSameRule = rPrevInfo.GetNumRule() == rInfo.GetNumRule();
102  if( bSameRule && rPrevInfo.GetDepth() >= rInfo.GetDepth() &&
103  !rInfo.IsRestart() )
104  {
105  return rWrt;
106  }
107 
108  bool bStartValue = false;
109  if( !bSameRule && rInfo.GetDepth() )
110  {
111  OUString aName( rInfo.GetNumRule()->GetName() );
112  if( 0 != rWrt.m_aNumRuleNames.count( aName ) )
113  {
114  // The rule has been applied before
115  sal_Int16 eType = rInfo.GetNumRule()
116  ->Get( rInfo.GetDepth()-1 ).GetNumberingType();
117  if( SVX_NUM_CHAR_SPECIAL != eType && SVX_NUM_BITMAP != eType )
118  {
119  // If it's a numbering rule, the current number should be
120  // exported as start value, but only if there are no nodes
121  // within the numbering that have a lower level
122  bStartValue = true;
123  if( rInfo.GetDepth() > 1 )
124  {
125  sal_uLong nPos =
126  rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
127  do
128  {
129  const SwNode* pNd = rWrt.m_pDoc->GetNodes()[nPos];
130  if( pNd->IsTextNode() )
131  {
132  const SwTextNode *pTextNd = pNd->GetTextNode();
133  if( !pTextNd->GetNumRule() )
134  {
135  // node isn't numbered => check completed
136  break;
137  }
138 
139  OSL_ENSURE(! pTextNd->IsOutline(),
140  "outline not expected");
141 
142  if( pTextNd->GetActualListLevel() + 1 <
143  rInfo.GetDepth() )
144  {
145  if (rPrevInfo.GetDepth() == 0)
146  // previous node had no numbering => write start value
147  bStartValue = true;
148  else
149  // node is numbered, but level is lower
150  bStartValue = false;
151  // => check completed
152  break;
153  }
154  nPos++;
155  }
156  else if( pNd->IsTableNode() )
157  {
158  // skip table
159  nPos = pNd->EndOfSectionIndex() + 1;
160  }
161  else
162  {
163  // end node or sections start node -> check
164  // completed
165  break;
166  }
167  }
168  while( true );
169  }
170  }
171  }
172  else
173  {
174  rWrt.m_aNumRuleNames.insert( aName );
175  }
176  }
177 
178  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
179  "<PRE> was not closed before <OL>." );
180  sal_uInt16 nPrevDepth =
181  (bSameRule && !rInfo.IsRestart()) ? rPrevInfo.GetDepth() : 0;
182 
183  for( sal_uInt16 i=nPrevDepth; i<rInfo.GetDepth(); i++ )
184  {
185  rWrt.OutNewLine(); // <OL>/<UL> in a new row
186 
187  rWrt.m_aBulletGrfs[i].clear();
188  OString sOut = "<" + rWrt.GetNamespace();
189  const SwNumFormat& rNumFormat = rInfo.GetNumRule()->Get( i );
190  sal_Int16 eType = rNumFormat.GetNumberingType();
191  if( SVX_NUM_CHAR_SPECIAL == eType )
192  {
193  // ordered list: <OL>
194  sOut += OString(OOO_STRING_SVTOOLS_HTML_unorderlist);
195 
196  // determine the type by the bullet character
197  const sal_Char *pStr = nullptr;
198  switch( rNumFormat.GetBulletChar() )
199  {
202  break;
205  break;
208  break;
209  }
210 
211  if( pStr )
212  {
213  sOut += OStringLiteral(" " OOO_STRING_SVTOOLS_HTML_O_type "=\"") + pStr + "\"";
214  }
215  }
216  else if( SVX_NUM_BITMAP == eType )
217  {
218  // Unordered list: <UL>
219  sOut += OString(OOO_STRING_SVTOOLS_HTML_unorderlist);
220  rWrt.Strm().WriteOString( sOut );
221  OutHTML_BulletImage( rWrt,
222  nullptr,
223  rNumFormat.GetBrush(),
224  rWrt.m_aBulletGrfs[i]);
225  }
226  else
227  {
228  // Ordered list: <OL>
229  sOut += OString(OOO_STRING_SVTOOLS_HTML_orderlist);
230 
231  // determine the type by the format
232  sal_Char cType = 0;
233  switch( eType )
234  {
237  cType = 'A';
238  break;
241  cType = 'a';
242  break;
243  case SVX_NUM_ROMAN_UPPER:
244  cType = 'I';
245  break;
246  case SVX_NUM_ROMAN_LOWER:
247  cType = 'i';
248  break;
249  }
250  if( cType )
251  {
252  sOut += " " OOO_STRING_SVTOOLS_HTML_O_type "=\"" + OStringChar(cType) + "\"";
253  }
254 
255  sal_uInt16 nStartVal = rNumFormat.GetStart();
256  if( bStartValue && 1 == nStartVal && i == rInfo.GetDepth()-1 )
257  {
258  if ( rWrt.m_pCurrentPam->GetNode().GetTextNode()->GetNum() )
259  {
260  nStartVal = static_cast< sal_uInt16 >( rWrt.m_pCurrentPam->GetNode()
261  .GetTextNode()->GetNumberVector()[i] );
262  }
263  else
264  {
265  OSL_FAIL( "<OutHTML_NumBulListStart(..) - text node has no number." );
266  }
267  }
268  if( nStartVal != 1 )
269  {
270  sOut += " " OOO_STRING_SVTOOLS_HTML_O_start "=\"" + OString::number(static_cast<sal_Int32>(nStartVal)) + "\"";
271  }
272  }
273 
274  if (!sOut.isEmpty() && SVX_NUM_BITMAP != eType) // second condition to avoid adding extra ul, already done before.
275  rWrt.Strm().WriteOString( sOut );
276 
277  if( rWrt.m_bCfgOutStyles )
278  OutCSS1_NumBulListStyleOpt( rWrt, *rInfo.GetNumRule(), static_cast<sal_uInt8>(i) );
279 
280  rWrt.Strm().WriteChar( '>' );
281 
282  rWrt.IncIndentLevel(); // indent content of <OL>
283  }
284 
285  return rWrt;
286 }
287 
289  const SwHTMLNumRuleInfo& rNextInfo )
290 {
291  SwHTMLNumRuleInfo& rInfo = rWrt.GetNumInfo();
292  bool bSameRule = rNextInfo.GetNumRule() == rInfo.GetNumRule();
293  if( bSameRule && rNextInfo.GetDepth() >= rInfo.GetDepth() &&
294  !rNextInfo.IsRestart() )
295  {
296  return rWrt;
297  }
298 
299  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
300  "<PRE> was not closed before </OL>." );
301  sal_uInt16 nNextDepth =
302  (bSameRule && !rNextInfo.IsRestart()) ? rNextInfo.GetDepth() : 0;
303 
304  // MIB 23.7.97: We must loop backwards, to get the right order of </OL>/</UL>
305  for( sal_uInt16 i=rInfo.GetDepth(); i>nNextDepth; i-- )
306  {
307  rWrt.DecIndentLevel(); // indent content of <OL>
308  if( rWrt.m_bLFPossible )
309  rWrt.OutNewLine(); // </OL>/</UL> in a new line
310 
311  // a list is started or ended:
312  sal_Int16 eType = rInfo.GetNumRule()->Get( i-1 ).GetNumberingType();
313  OString aTag;
314  if( SVX_NUM_CHAR_SPECIAL == eType || SVX_NUM_BITMAP == eType)
316  else
318  HTMLOutFuncs::Out_AsciiTag( rWrt.Strm(), rWrt.GetNamespace() + aTag, false );
319  rWrt.m_bLFPossible = true;
320  }
321 
322  return rWrt;
323 }
324 
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvxNumType GetNumberingType() const
SVX_NUM_CHARS_UPPER_LETTER_N
SwHTMLNumRuleInfo & GetNumInfo()
Definition: wrthtml.hxx:525
void FillNextNumInfo()
bool IsOutline() const
Returns if this text node is an outline.
Definition: ndtxt.cxx:4002
#define OOO_STRING_SVTOOLS_HTML_O_start
const SvxBrushItem * GetBrush() const
sal_uIntPtr sal_uLong
SVX_NUM_CHARS_UPPER_LETTER
bool IsRestart() const
Definition: htmlnum.hxx:78
#define OOO_STRING_SVTOOLS_HTML_unorderlist
void DecIndentLevel()
Definition: wrthtml.hxx:498
const OUString & GetName() const
Definition: numrule.hxx:221
SvStream & WriteOString(const OString &rStr)
int GetActualListLevel() const
Returns the actual list level of this text node, when it is a list item.
Definition: ndtxt.cxx:4097
Writer & OutHTML_NumBulListStart(SwHTMLWriter &rWrt, const SwHTMLNumRuleInfo &rInfo)
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1418
bool m_bLFPossible
Definition: wrthtml.hxx:379
static SVT_DLLPUBLIC SvStream & Out_AsciiTag(SvStream &, const OString &rStr, bool bOn=true)
Writer & OutHTML_NumBulListEnd(SwHTMLWriter &rWrt, const SwHTMLNumRuleInfo &rNextInfo)
std::set< OUString > m_aNumRuleNames
Definition: wrthtml.hxx:281
char sal_Char
SVX_NUM_ROMAN_UPPER
SVX_NUM_ROMAN_LOWER
#define HTML_BULLETCHAR_DISC
Definition: htmlnum.hxx:32
SVX_NUM_BITMAP
SVX_NUM_CHAR_SPECIAL
SVX_NUM_CHARS_LOWER_LETTER
sal_uInt16 GetDepth() const
Definition: htmlnum.hxx:73
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_disc
int i
void SetNextNumInfo(std::unique_ptr< SwHTMLNumRuleInfo > pNxt)
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_square
SwNumRule * GetNumRule(bool bInParent=true) const
Returns numbering rule of this text node.
Definition: ndtxt.cxx:2813
OUString m_aBulletGrfs[MAXLEVEL]
Definition: wrthtml.hxx:294
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:693
#define HTML_BULLETCHAR_CIRCLE
Definition: htmlnum.hxx:33
sal_uInt16 GetStart() const
DocumentType const eType
SVX_NUM_CHARS_LOWER_LETTER_N
void ClearNextNumInfo()
const SwNumFormat & Get(sal_uInt16 i) const
Definition: number.cxx:77
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:677
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
unsigned char sal_uInt8
void IncIndentLevel()
Definition: wrthtml.hxx:494
SwNodes & GetNodes()
Definition: doc.hxx:402
Writer & OutCSS1_NumBulListStyleOpt(Writer &rWrt, const SwNumRule &rNumRule, sal_uInt8 nLevel)
Definition: css1atr.cxx:1853
Writer & OutHTML_BulletImage(Writer &rWrt, const sal_Char *pTag, const SvxBrushItem *pBrush, const OUString &rGraphicURL)
OString GetNamespace() const
Determines the prefix string needed to respect the requested namespace alias.
Definition: wrthtml.cxx:1467
SvStream & WriteChar(char nChar)
sal_Unicode GetBulletChar() const
#define OOO_STRING_SVTOOLS_HTML_orderlist
std::shared_ptr< SwUnoCursor > m_pCurrentPam
Definition: shellio.hxx:403
OString const aName
SvStream & Strm()
Definition: writer.cxx:218
#define OOO_STRING_SVTOOLS_HTML_O_type
bool IsTableNode() const
Definition: node.hxx:640
bool m_bCfgOutStyles
Definition: wrthtml.hxx:339
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_circle
sal_Int32 nPos
#define HTML_BULLETCHAR_SQUARE
Definition: htmlnum.hxx:34
bool IsTextNode() const
Definition: node.hxx:636
SwNumRule * GetNumRule()
Definition: htmlnum.hxx:69
SwDoc * m_pDoc
Definition: shellio.hxx:401
HtmlTokenId m_nLastParaToken
Definition: wrthtml.hxx:309
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
Base class of the Writer document model elements.
Definition: node.hxx:79