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 <svtools/htmltokn.h>
21 #include <svtools/htmlkywd.hxx>
22 #include <svtools/htmlout.hxx>
23 #include <numrule.hxx>
24 #include <doc.hxx>
25 #include <ndtxt.hxx>
26 #include <pam.hxx>
27 
28 #include "htmlnum.hxx"
29 #include "wrthtml.hxx"
30 
31 #include <osl/diagnose.h>
32 
33 using namespace css;
34 
35 
37 {
38  m_pNextNumRuleInfo = nullptr;
39 
40  sal_uLong nPos = m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
41 
42  bool bTable = false;
43  do
44  {
45  const SwNode* pNd = m_pDoc->GetNodes()[nPos];
46  if( pNd->IsTextNode() )
47  {
48  m_pNextNumRuleInfo.reset( new SwHTMLNumRuleInfo( *pNd->GetTextNode() ) );
49 
50  // Before a table we keep the old level if the same numbering is
51  // continued after the table and no new numbering is started.
52  // The table will get the indentation that corresponds to its
53  // numbering level during import.
54  if( bTable &&
55  m_pNextNumRuleInfo->GetNumRule()==GetNumInfo().GetNumRule() &&
56  !m_pNextNumRuleInfo->IsRestart() )
57  {
58  m_pNextNumRuleInfo->SetDepth( GetNumInfo().GetDepth() );
59  }
60  }
61  else if( pNd->IsTableNode() )
62  {
63  // A table is skipped so the node after table is viewed.
64  nPos = pNd->EndOfSectionIndex() + 1;
65  bTable = true;
66  }
67  else
68  {
69  // In all other case the numbering is over.
70  m_pNextNumRuleInfo.reset(new SwHTMLNumRuleInfo);
71  }
72  }
73  while( !m_pNextNumRuleInfo );
74 }
75 
77 {
78  m_pNextNumRuleInfo.reset();
79 }
80 
81 void SwHTMLWriter::SetNextNumInfo( std::unique_ptr<SwHTMLNumRuleInfo> pNxt )
82 {
83  m_pNextNumRuleInfo = std::move(pNxt);
84 }
85 
87  const SwHTMLNumRuleInfo& rInfo )
88 {
89  SwHTMLNumRuleInfo& rPrevInfo = rWrt.GetNumInfo();
90  bool bSameRule = rPrevInfo.GetNumRule() == rInfo.GetNumRule();
91  if( bSameRule && rPrevInfo.GetDepth() >= rInfo.GetDepth() &&
92  !rInfo.IsRestart() )
93  {
94  return rWrt;
95  }
96 
97  if (rWrt.mbXHTML && !rInfo.IsNumbered())
98  {
99  // If the list only consists of non-numbered text nodes, then don't start the list.
100  bool bAtLeastOneNumbered = false;
101  sal_uLong nPos = rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
102  SwNumRule* pNumRule = nullptr;
103  while (true)
104  {
105  const SwNode* pNode = rWrt.m_pDoc->GetNodes()[nPos];
106  if (!pNode->IsTextNode())
107  {
108  break;
109  }
110 
111  const SwTextNode* pTextNode = pNode->GetTextNode();
112  if (!pTextNode->GetNumRule() || (pNumRule && pTextNode->GetNumRule() != pNumRule))
113  {
114  // Node is not in the same numbering as the previous one.
115  break;
116  }
117 
118  pNumRule = pTextNode->GetNumRule();
119  if (pTextNode->IsNumbered())
120  {
121  bAtLeastOneNumbered = true;
122  break;
123  }
124  ++nPos;
125  }
126 
127  if (!bAtLeastOneNumbered)
128  {
129  return rWrt;
130  }
131  }
132 
133  bool bStartValue = false;
134  if( !bSameRule && rInfo.GetDepth() )
135  {
136  OUString aName( rInfo.GetNumRule()->GetName() );
137  if( 0 != rWrt.m_aNumRuleNames.count( aName ) )
138  {
139  // The rule has been applied before
140  sal_Int16 eType = rInfo.GetNumRule()
141  ->Get( rInfo.GetDepth()-1 ).GetNumberingType();
142  if( SVX_NUM_CHAR_SPECIAL != eType && SVX_NUM_BITMAP != eType )
143  {
144  // If it's a numbering rule, the current number should be
145  // exported as start value, but only if there are no nodes
146  // within the numbering that have a lower level
147  bStartValue = true;
148  if( rInfo.GetDepth() > 1 )
149  {
150  sal_uLong nPos =
151  rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
152  do
153  {
154  const SwNode* pNd = rWrt.m_pDoc->GetNodes()[nPos];
155  if( pNd->IsTextNode() )
156  {
157  const SwTextNode *pTextNd = pNd->GetTextNode();
158  if( !pTextNd->GetNumRule() )
159  {
160  // node isn't numbered => check completed
161  break;
162  }
163 
164  OSL_ENSURE(! pTextNd->IsOutline(),
165  "outline not expected");
166 
167  if( pTextNd->GetActualListLevel() + 1 <
168  rInfo.GetDepth() )
169  {
170  if (rPrevInfo.GetDepth() == 0)
171  // previous node had no numbering => write start value
172  bStartValue = true;
173  else
174  // node is numbered, but level is lower
175  bStartValue = false;
176  // => check completed
177  break;
178  }
179  nPos++;
180  }
181  else if( pNd->IsTableNode() )
182  {
183  // skip table
184  nPos = pNd->EndOfSectionIndex() + 1;
185  }
186  else
187  {
188  // end node or sections start node -> check
189  // completed
190  break;
191  }
192  }
193  while( true );
194  }
195  }
196  }
197  else
198  {
199  rWrt.m_aNumRuleNames.insert( aName );
200  }
201  }
202 
203  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
204  "<PRE> was not closed before <OL>." );
205  sal_uInt16 nPrevDepth =
206  (bSameRule && !rInfo.IsRestart()) ? rPrevInfo.GetDepth() : 0;
207 
208  for( sal_uInt16 i=nPrevDepth; i<rInfo.GetDepth(); i++ )
209  {
210  rWrt.OutNewLine(); // <OL>/<UL> in a new row
211 
212  rWrt.m_aBulletGrfs[i].clear();
213  OString sOut = "<" + rWrt.GetNamespace();
214  const SwNumFormat& rNumFormat = rInfo.GetNumRule()->Get( i );
215  sal_Int16 eType = rNumFormat.GetNumberingType();
216  if( SVX_NUM_CHAR_SPECIAL == eType )
217  {
218  // ordered list: <OL>
220 
221  // determine the type by the bullet character
222  const char *pStr = nullptr;
223  switch( rNumFormat.GetBulletChar() )
224  {
227  break;
230  break;
233  break;
234  }
235 
236  if( pStr )
237  {
238  sOut += OString::Concat(" " OOO_STRING_SVTOOLS_HTML_O_type "=\"") + pStr + "\"";
239  }
240  }
241  else if( SVX_NUM_BITMAP == eType )
242  {
243  // Unordered list: <UL>
245  rWrt.Strm().WriteOString( sOut );
246  OutHTML_BulletImage( rWrt,
247  nullptr,
248  rNumFormat.GetBrush(),
249  rWrt.m_aBulletGrfs[i]);
250  }
251  else
252  {
253  // Ordered list: <OL>
255 
256  // determine the type by the format
257  char cType = 0;
258  switch( eType )
259  {
262  cType = 'A';
263  break;
266  cType = 'a';
267  break;
268  case SVX_NUM_ROMAN_UPPER:
269  cType = 'I';
270  break;
271  case SVX_NUM_ROMAN_LOWER:
272  cType = 'i';
273  break;
274  }
275  if( cType )
276  {
277  sOut += " " OOO_STRING_SVTOOLS_HTML_O_type "=\"" + OStringChar(cType) + "\"";
278  }
279 
280  sal_uInt16 nStartVal = rNumFormat.GetStart();
281  if( bStartValue && 1 == nStartVal && i == rInfo.GetDepth()-1 )
282  {
283  if ( rWrt.m_pCurrentPam->GetNode().GetTextNode()->GetNum() )
284  {
285  nStartVal = static_cast< sal_uInt16 >( rWrt.m_pCurrentPam->GetNode()
286  .GetTextNode()->GetNumberVector()[i] );
287  }
288  else
289  {
290  OSL_FAIL( "<OutHTML_NumberBulletListStart(..) - text node has no number." );
291  }
292  }
293  if( nStartVal != 1 )
294  {
295  sOut += " " OOO_STRING_SVTOOLS_HTML_O_start "=\"" + OString::number(static_cast<sal_Int32>(nStartVal)) + "\"";
296  }
297  }
298 
299  if (!sOut.isEmpty() && SVX_NUM_BITMAP != eType) // second condition to avoid adding extra ul, already done before.
300  rWrt.Strm().WriteOString( sOut );
301 
302  if( rWrt.m_bCfgOutStyles )
303  OutCSS1_NumberBulletListStyleOpt( rWrt, *rInfo.GetNumRule(), static_cast<sal_uInt8>(i) );
304 
305  rWrt.Strm().WriteChar( '>' );
306 
307  rWrt.IncIndentLevel(); // indent content of <OL>
308  }
309 
310  return rWrt;
311 }
312 
314  const SwHTMLNumRuleInfo& rNextInfo )
315 {
316  SwHTMLNumRuleInfo& rInfo = rWrt.GetNumInfo();
317  bool bSameRule = rNextInfo.GetNumRule() == rInfo.GetNumRule();
318  bool bListEnd = !bSameRule || rNextInfo.GetDepth() < rInfo.GetDepth() || rNextInfo.IsRestart();
319 
320  if (rWrt.mbXHTML)
321  {
322  if ((bListEnd && rInfo.IsNumbered()) || (!bListEnd && rNextInfo.IsNumbered()))
323  {
325  OString(rWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_li), false);
326  }
327  }
328 
329  if (!bListEnd)
330  {
331  return rWrt;
332  }
333 
334  if (rWrt.mbXHTML && !rInfo.IsNumbered())
335  {
336  // If the list only consisted of non-numbered text nodes, then don't end the list.
337  bool bAtLeastOneNumbered = false;
338  sal_uLong nPos = rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() - 1;
339  SwNumRule* pNumRule = nullptr;
340  while (true)
341  {
342  const SwNode* pNode = rWrt.m_pDoc->GetNodes()[nPos];
343  if (!pNode->IsTextNode())
344  {
345  break;
346  }
347 
348  const SwTextNode* pTextNode = pNode->GetTextNode();
349  if (!pTextNode->GetNumRule() || (pNumRule && pTextNode->GetNumRule() != pNumRule))
350  {
351  // Node is not in the same numbering as the next one.
352  break;
353  }
354 
355  pNumRule = pTextNode->GetNumRule();
356  if (pTextNode->IsNumbered())
357  {
358  bAtLeastOneNumbered = true;
359  break;
360  }
361  --nPos;
362  }
363 
364  if (!bAtLeastOneNumbered)
365  {
366  return rWrt;
367  }
368  }
369 
370  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
371  "<PRE> was not closed before </OL>." );
372  sal_uInt16 nNextDepth =
373  (bSameRule && !rNextInfo.IsRestart()) ? rNextInfo.GetDepth() : 0;
374 
375  // MIB 23.7.97: We must loop backwards, to get the right order of </OL>/</UL>
376  for( sal_uInt16 i=rInfo.GetDepth(); i>nNextDepth; i-- )
377  {
378  rWrt.DecIndentLevel(); // indent content of <OL>
379  if( rWrt.m_bLFPossible )
380  rWrt.OutNewLine(); // </OL>/</UL> in a new line
381 
382  // a list is started or ended:
383  sal_Int16 eType = rInfo.GetNumRule()->Get( i-1 ).GetNumberingType();
384  OString aTag;
385  if( SVX_NUM_CHAR_SPECIAL == eType || SVX_NUM_BITMAP == eType)
387  else
389  HTMLOutFuncs::Out_AsciiTag( rWrt.Strm(), OString(rWrt.GetNamespace() + aTag), false );
390  rWrt.m_bLFPossible = true;
391  }
392 
393  return rWrt;
394 }
395 
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvxNumType GetNumberingType() const
SVX_NUM_CHARS_UPPER_LETTER_N
SwHTMLNumRuleInfo & GetNumInfo()
Definition: wrthtml.hxx:534
bool mbXHTML
If XHTML markup should be written instead of HTML.
Definition: wrthtml.hxx:395
sal_UCS4 GetBulletChar() const
void FillNextNumInfo()
bool IsOutline() const
Returns if this text node is an outline.
Definition: ndtxt.cxx:3985
Writer & OutHTML_NumberBulletListStart(SwHTMLWriter &rWrt, const SwHTMLNumRuleInfo &rInfo)
#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:507
const OUString & GetName() const
Definition: numrule.hxx:225
static SVT_DLLPUBLIC SvStream & Out_AsciiTag(SvStream &, std::string_view rStr, bool bOn=true)
int GetActualListLevel() const
Returns the actual list level of this text node, when it is a list item.
Definition: ndtxt.cxx:4095
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1441
bool m_bLFPossible
Definition: wrthtml.hxx:380
std::set< OUString > m_aNumRuleNames
Definition: wrthtml.hxx:282
SVX_NUM_ROMAN_UPPER
SVX_NUM_ROMAN_LOWER
#define HTML_BULLETCHAR_DISC
Definition: htmlnum.hxx:32
#define OOO_STRING_SVTOOLS_HTML_li
Writer & OutCSS1_NumberBulletListStyleOpt(Writer &rWrt, const SwNumRule &rNumRule, sal_uInt8 nLevel)
Definition: css1atr.cxx:1814
DocumentType eType
SVX_NUM_BITMAP
SVX_NUM_CHAR_SPECIAL
SVX_NUM_CHARS_LOWER_LETTER
sal_uInt16 GetDepth() const
Definition: htmlnum.hxx:73
int i
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_disc
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:2807
OUString m_aBulletGrfs[MAXLEVEL]
Definition: wrthtml.hxx:295
bool IsNumbered() const
Definition: htmlnum.hxx:80
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:694
#define HTML_BULLETCHAR_CIRCLE
Definition: htmlnum.hxx:33
sal_uInt16 GetStart() const
SVX_NUM_CHARS_LOWER_LETTER_N
void ClearNextNumInfo()
const SwNumFormat & Get(sal_uInt16 i) const
Definition: number.cxx:79
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:678
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
SvStream & WriteOString(std::string_view rStr)
unsigned char sal_uInt8
bool IsNumbered(SwRootFrame const *pLayout=nullptr) const
Returns is this text node is numbered.
Definition: ndtxt.cxx:2875
void IncIndentLevel()
Definition: wrthtml.hxx:503
OUString aName
SwNodes & GetNodes()
Definition: doc.hxx:409
OString GetNamespace() const
Determines the prefix string needed to respect the requested namespace alias.
Definition: wrthtml.cxx:1490
SvStream & WriteChar(char nChar)
#define OOO_STRING_SVTOOLS_HTML_orderlist
std::shared_ptr< SwUnoCursor > m_pCurrentPam
Definition: shellio.hxx:410
Writer & OutHTML_NumberBulletListEnd(SwHTMLWriter &rWrt, const SwHTMLNumRuleInfo &rNextInfo)
SvStream & Strm()
Definition: writer.cxx:215
#define OOO_STRING_SVTOOLS_HTML_O_type
bool IsTableNode() const
Definition: node.hxx:641
bool m_bCfgOutStyles
Definition: wrthtml.hxx:340
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_circle
#define HTML_BULLETCHAR_SQUARE
Definition: htmlnum.hxx:34
bool IsTextNode() const
Definition: node.hxx:637
SwNumRule * GetNumRule()
Definition: htmlnum.hxx:69
SwDoc * m_pDoc
Definition: shellio.hxx:408
sal_uInt16 nPos
HtmlTokenId m_nLastParaToken
Definition: wrthtml.hxx:310
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:850
Writer & OutHTML_BulletImage(Writer &rWrt, const char *pTag, const SvxBrushItem *pBrush, const OUString &rGraphicURL)
Base class of the Writer document model elements.
Definition: node.hxx:80