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  while (true)
103  {
104  const SwNode* pNode = rWrt.m_pDoc->GetNodes()[nPos];
105  if (!pNode->IsTextNode())
106  {
107  break;
108  }
109 
110  const SwTextNode* pTextNode = pNode->GetTextNode();
111  if (!pTextNode->GetNumRule())
112  {
113  break;
114  }
115 
116  if (pTextNode->IsNumbered())
117  {
118  bAtLeastOneNumbered = true;
119  break;
120  }
121  ++nPos;
122  }
123 
124  if (!bAtLeastOneNumbered)
125  {
126  return rWrt;
127  }
128  }
129 
130  bool bStartValue = false;
131  if( !bSameRule && rInfo.GetDepth() )
132  {
133  OUString aName( rInfo.GetNumRule()->GetName() );
134  if( 0 != rWrt.m_aNumRuleNames.count( aName ) )
135  {
136  // The rule has been applied before
137  sal_Int16 eType = rInfo.GetNumRule()
138  ->Get( rInfo.GetDepth()-1 ).GetNumberingType();
139  if( SVX_NUM_CHAR_SPECIAL != eType && SVX_NUM_BITMAP != eType )
140  {
141  // If it's a numbering rule, the current number should be
142  // exported as start value, but only if there are no nodes
143  // within the numbering that have a lower level
144  bStartValue = true;
145  if( rInfo.GetDepth() > 1 )
146  {
147  sal_uLong nPos =
148  rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
149  do
150  {
151  const SwNode* pNd = rWrt.m_pDoc->GetNodes()[nPos];
152  if( pNd->IsTextNode() )
153  {
154  const SwTextNode *pTextNd = pNd->GetTextNode();
155  if( !pTextNd->GetNumRule() )
156  {
157  // node isn't numbered => check completed
158  break;
159  }
160 
161  OSL_ENSURE(! pTextNd->IsOutline(),
162  "outline not expected");
163 
164  if( pTextNd->GetActualListLevel() + 1 <
165  rInfo.GetDepth() )
166  {
167  if (rPrevInfo.GetDepth() == 0)
168  // previous node had no numbering => write start value
169  bStartValue = true;
170  else
171  // node is numbered, but level is lower
172  bStartValue = false;
173  // => check completed
174  break;
175  }
176  nPos++;
177  }
178  else if( pNd->IsTableNode() )
179  {
180  // skip table
181  nPos = pNd->EndOfSectionIndex() + 1;
182  }
183  else
184  {
185  // end node or sections start node -> check
186  // completed
187  break;
188  }
189  }
190  while( true );
191  }
192  }
193  }
194  else
195  {
196  rWrt.m_aNumRuleNames.insert( aName );
197  }
198  }
199 
200  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
201  "<PRE> was not closed before <OL>." );
202  sal_uInt16 nPrevDepth =
203  (bSameRule && !rInfo.IsRestart()) ? rPrevInfo.GetDepth() : 0;
204 
205  for( sal_uInt16 i=nPrevDepth; i<rInfo.GetDepth(); i++ )
206  {
207  rWrt.OutNewLine(); // <OL>/<UL> in a new row
208 
209  rWrt.m_aBulletGrfs[i].clear();
210  OString sOut = "<" + rWrt.GetNamespace();
211  const SwNumFormat& rNumFormat = rInfo.GetNumRule()->Get( i );
212  sal_Int16 eType = rNumFormat.GetNumberingType();
213  if( SVX_NUM_CHAR_SPECIAL == eType )
214  {
215  // ordered list: <OL>
217 
218  // determine the type by the bullet character
219  const char *pStr = nullptr;
220  switch( rNumFormat.GetBulletChar() )
221  {
224  break;
227  break;
230  break;
231  }
232 
233  if( pStr )
234  {
235  sOut += OString::Concat(" " OOO_STRING_SVTOOLS_HTML_O_type "=\"") + pStr + "\"";
236  }
237  }
238  else if( SVX_NUM_BITMAP == eType )
239  {
240  // Unordered list: <UL>
242  rWrt.Strm().WriteOString( sOut );
243  OutHTML_BulletImage( rWrt,
244  nullptr,
245  rNumFormat.GetBrush(),
246  rWrt.m_aBulletGrfs[i]);
247  }
248  else
249  {
250  // Ordered list: <OL>
252 
253  // determine the type by the format
254  char cType = 0;
255  switch( eType )
256  {
259  cType = 'A';
260  break;
263  cType = 'a';
264  break;
265  case SVX_NUM_ROMAN_UPPER:
266  cType = 'I';
267  break;
268  case SVX_NUM_ROMAN_LOWER:
269  cType = 'i';
270  break;
271  }
272  if( cType )
273  {
274  sOut += " " OOO_STRING_SVTOOLS_HTML_O_type "=\"" + OStringChar(cType) + "\"";
275  }
276 
277  sal_uInt16 nStartVal = rNumFormat.GetStart();
278  if( bStartValue && 1 == nStartVal && i == rInfo.GetDepth()-1 )
279  {
280  if ( rWrt.m_pCurrentPam->GetNode().GetTextNode()->GetNum() )
281  {
282  nStartVal = static_cast< sal_uInt16 >( rWrt.m_pCurrentPam->GetNode()
283  .GetTextNode()->GetNumberVector()[i] );
284  }
285  else
286  {
287  OSL_FAIL( "<OutHTML_NumberBulletListStart(..) - text node has no number." );
288  }
289  }
290  if( nStartVal != 1 )
291  {
292  sOut += " " OOO_STRING_SVTOOLS_HTML_O_start "=\"" + OString::number(static_cast<sal_Int32>(nStartVal)) + "\"";
293  }
294  }
295 
296  if (!sOut.isEmpty() && SVX_NUM_BITMAP != eType) // second condition to avoid adding extra ul, already done before.
297  rWrt.Strm().WriteOString( sOut );
298 
299  if( rWrt.m_bCfgOutStyles )
300  OutCSS1_NumberBulletListStyleOpt( rWrt, *rInfo.GetNumRule(), static_cast<sal_uInt8>(i) );
301 
302  rWrt.Strm().WriteChar( '>' );
303 
304  rWrt.IncIndentLevel(); // indent content of <OL>
305  }
306 
307  return rWrt;
308 }
309 
311  const SwHTMLNumRuleInfo& rNextInfo )
312 {
313  SwHTMLNumRuleInfo& rInfo = rWrt.GetNumInfo();
314  bool bSameRule = rNextInfo.GetNumRule() == rInfo.GetNumRule();
315  bool bListEnd = !bSameRule || rNextInfo.GetDepth() < rInfo.GetDepth() || rNextInfo.IsRestart();
316 
317  if (rWrt.mbXHTML)
318  {
319  if ((bListEnd && rInfo.IsNumbered()) || (!bListEnd && rNextInfo.IsNumbered()))
320  {
322  OString(rWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_li), false);
323  }
324  }
325 
326  if (!bListEnd)
327  {
328  return rWrt;
329  }
330 
331  if (rWrt.mbXHTML && !rInfo.IsNumbered())
332  {
333  // If the list only consisted of non-numbered text nodes, then don't end the list.
334  bool bAtLeastOneNumbered = false;
335  sal_uLong nPos = rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() - 1;
336  while (true)
337  {
338  const SwNode* pNode = rWrt.m_pDoc->GetNodes()[nPos];
339  if (!pNode->IsTextNode())
340  {
341  break;
342  }
343 
344  const SwTextNode* pTextNode = pNode->GetTextNode();
345  if (!pTextNode->GetNumRule())
346  {
347  break;
348  }
349 
350  if (pTextNode->IsNumbered())
351  {
352  bAtLeastOneNumbered = true;
353  break;
354  }
355  --nPos;
356  }
357 
358  if (!bAtLeastOneNumbered)
359  {
360  return rWrt;
361  }
362  }
363 
364  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
365  "<PRE> was not closed before </OL>." );
366  sal_uInt16 nNextDepth =
367  (bSameRule && !rNextInfo.IsRestart()) ? rNextInfo.GetDepth() : 0;
368 
369  // MIB 23.7.97: We must loop backwards, to get the right order of </OL>/</UL>
370  for( sal_uInt16 i=rInfo.GetDepth(); i>nNextDepth; i-- )
371  {
372  rWrt.DecIndentLevel(); // indent content of <OL>
373  if( rWrt.m_bLFPossible )
374  rWrt.OutNewLine(); // </OL>/</UL> in a new line
375 
376  // a list is started or ended:
377  sal_Int16 eType = rInfo.GetNumRule()->Get( i-1 ).GetNumberingType();
378  OString aTag;
379  if( SVX_NUM_CHAR_SPECIAL == eType || SVX_NUM_BITMAP == eType)
381  else
383  HTMLOutFuncs::Out_AsciiTag( rWrt.Strm(), OString(rWrt.GetNamespace() + aTag), false );
384  rWrt.m_bLFPossible = true;
385  }
386 
387  return rWrt;
388 }
389 
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvxNumType GetNumberingType() const
SVX_NUM_CHARS_UPPER_LETTER_N
SwHTMLNumRuleInfo & GetNumInfo()
Definition: wrthtml.hxx:537
bool mbXHTML
If XHTML markup should be written instead of HTML.
Definition: wrthtml.hxx:396
sal_UCS4 GetBulletChar() const
void FillNextNumInfo()
bool IsOutline() const
Returns if this text node is an outline.
Definition: ndtxt.cxx:3977
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:510
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:4091
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1470
bool m_bLFPossible
Definition: wrthtml.hxx:381
std::set< OUString > m_aNumRuleNames
Definition: wrthtml.hxx:283
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:2798
OUString m_aBulletGrfs[MAXLEVEL]
Definition: wrthtml.hxx:296
bool IsNumbered() const
Definition: htmlnum.hxx:80
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:697
#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:681
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:2866
void IncIndentLevel()
Definition: wrthtml.hxx:506
OUString aName
SwNodes & GetNodes()
Definition: doc.hxx:408
OString GetNamespace() const
Determines the prefix string needed to respect the requested namespace alias.
Definition: wrthtml.cxx:1519
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:644
bool m_bCfgOutStyles
Definition: wrthtml.hxx:341
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_circle
#define HTML_BULLETCHAR_SQUARE
Definition: htmlnum.hxx:34
bool IsTextNode() const
Definition: node.hxx:640
SwNumRule * GetNumRule()
Definition: htmlnum.hxx:69
SwDoc * m_pDoc
Definition: shellio.hxx:408
sal_uInt16 nPos
HtmlTokenId m_nLastParaToken
Definition: wrthtml.hxx:311
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:851
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