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  bool bStartValue = false;
98  if( !bSameRule && rInfo.GetDepth() )
99  {
100  OUString aName( rInfo.GetNumRule()->GetName() );
101  if( 0 != rWrt.m_aNumRuleNames.count( aName ) )
102  {
103  // The rule has been applied before
104  sal_Int16 eType = rInfo.GetNumRule()
105  ->Get( rInfo.GetDepth()-1 ).GetNumberingType();
106  if( SVX_NUM_CHAR_SPECIAL != eType && SVX_NUM_BITMAP != eType )
107  {
108  // If it's a numbering rule, the current number should be
109  // exported as start value, but only if there are no nodes
110  // within the numbering that have a lower level
111  bStartValue = true;
112  if( rInfo.GetDepth() > 1 )
113  {
114  sal_uLong nPos =
115  rWrt.m_pCurrentPam->GetPoint()->nNode.GetIndex() + 1;
116  do
117  {
118  const SwNode* pNd = rWrt.m_pDoc->GetNodes()[nPos];
119  if( pNd->IsTextNode() )
120  {
121  const SwTextNode *pTextNd = pNd->GetTextNode();
122  if( !pTextNd->GetNumRule() )
123  {
124  // node isn't numbered => check completed
125  break;
126  }
127 
128  OSL_ENSURE(! pTextNd->IsOutline(),
129  "outline not expected");
130 
131  if( pTextNd->GetActualListLevel() + 1 <
132  rInfo.GetDepth() )
133  {
134  if (rPrevInfo.GetDepth() == 0)
135  // previous node had no numbering => write start value
136  bStartValue = true;
137  else
138  // node is numbered, but level is lower
139  bStartValue = false;
140  // => check completed
141  break;
142  }
143  nPos++;
144  }
145  else if( pNd->IsTableNode() )
146  {
147  // skip table
148  nPos = pNd->EndOfSectionIndex() + 1;
149  }
150  else
151  {
152  // end node or sections start node -> check
153  // completed
154  break;
155  }
156  }
157  while( true );
158  }
159  }
160  }
161  else
162  {
163  rWrt.m_aNumRuleNames.insert( aName );
164  }
165  }
166 
167  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
168  "<PRE> was not closed before <OL>." );
169  sal_uInt16 nPrevDepth =
170  (bSameRule && !rInfo.IsRestart()) ? rPrevInfo.GetDepth() : 0;
171 
172  for( sal_uInt16 i=nPrevDepth; i<rInfo.GetDepth(); i++ )
173  {
174  rWrt.OutNewLine(); // <OL>/<UL> in a new row
175 
176  rWrt.m_aBulletGrfs[i].clear();
177  OString sOut = "<" + rWrt.GetNamespace();
178  const SwNumFormat& rNumFormat = rInfo.GetNumRule()->Get( i );
179  sal_Int16 eType = rNumFormat.GetNumberingType();
180  if( SVX_NUM_CHAR_SPECIAL == eType )
181  {
182  // ordered list: <OL>
183  sOut += OString(OOO_STRING_SVTOOLS_HTML_unorderlist);
184 
185  // determine the type by the bullet character
186  const char *pStr = nullptr;
187  switch( rNumFormat.GetBulletChar() )
188  {
191  break;
194  break;
197  break;
198  }
199 
200  if( pStr )
201  {
202  sOut += OStringLiteral(" " OOO_STRING_SVTOOLS_HTML_O_type "=\"") + pStr + "\"";
203  }
204  }
205  else if( SVX_NUM_BITMAP == eType )
206  {
207  // Unordered list: <UL>
208  sOut += OString(OOO_STRING_SVTOOLS_HTML_unorderlist);
209  rWrt.Strm().WriteOString( sOut );
210  OutHTML_BulletImage( rWrt,
211  nullptr,
212  rNumFormat.GetBrush(),
213  rWrt.m_aBulletGrfs[i]);
214  }
215  else
216  {
217  // Ordered list: <OL>
218  sOut += OString(OOO_STRING_SVTOOLS_HTML_orderlist);
219 
220  // determine the type by the format
221  char cType = 0;
222  switch( eType )
223  {
226  cType = 'A';
227  break;
230  cType = 'a';
231  break;
232  case SVX_NUM_ROMAN_UPPER:
233  cType = 'I';
234  break;
235  case SVX_NUM_ROMAN_LOWER:
236  cType = 'i';
237  break;
238  }
239  if( cType )
240  {
241  sOut += " " OOO_STRING_SVTOOLS_HTML_O_type "=\"" + OStringChar(cType) + "\"";
242  }
243 
244  sal_uInt16 nStartVal = rNumFormat.GetStart();
245  if( bStartValue && 1 == nStartVal && i == rInfo.GetDepth()-1 )
246  {
247  if ( rWrt.m_pCurrentPam->GetNode().GetTextNode()->GetNum() )
248  {
249  nStartVal = static_cast< sal_uInt16 >( rWrt.m_pCurrentPam->GetNode()
250  .GetTextNode()->GetNumberVector()[i] );
251  }
252  else
253  {
254  OSL_FAIL( "<OutHTML_NumberBulletListStart(..) - text node has no number." );
255  }
256  }
257  if( nStartVal != 1 )
258  {
259  sOut += " " OOO_STRING_SVTOOLS_HTML_O_start "=\"" + OString::number(static_cast<sal_Int32>(nStartVal)) + "\"";
260  }
261  }
262 
263  if (!sOut.isEmpty() && SVX_NUM_BITMAP != eType) // second condition to avoid adding extra ul, already done before.
264  rWrt.Strm().WriteOString( sOut );
265 
266  if( rWrt.m_bCfgOutStyles )
267  OutCSS1_NumberBulletListStyleOpt( rWrt, *rInfo.GetNumRule(), static_cast<sal_uInt8>(i) );
268 
269  rWrt.Strm().WriteChar( '>' );
270 
271  rWrt.IncIndentLevel(); // indent content of <OL>
272  }
273 
274  return rWrt;
275 }
276 
278  const SwHTMLNumRuleInfo& rNextInfo )
279 {
280  SwHTMLNumRuleInfo& rInfo = rWrt.GetNumInfo();
281  bool bSameRule = rNextInfo.GetNumRule() == rInfo.GetNumRule();
282  if( bSameRule && rNextInfo.GetDepth() >= rInfo.GetDepth() &&
283  !rNextInfo.IsRestart() )
284  {
285  return rWrt;
286  }
287 
288  OSL_ENSURE( rWrt.m_nLastParaToken == HtmlTokenId::NONE,
289  "<PRE> was not closed before </OL>." );
290  sal_uInt16 nNextDepth =
291  (bSameRule && !rNextInfo.IsRestart()) ? rNextInfo.GetDepth() : 0;
292 
293  // MIB 23.7.97: We must loop backwards, to get the right order of </OL>/</UL>
294  for( sal_uInt16 i=rInfo.GetDepth(); i>nNextDepth; i-- )
295  {
296  rWrt.DecIndentLevel(); // indent content of <OL>
297  if( rWrt.m_bLFPossible )
298  rWrt.OutNewLine(); // </OL>/</UL> in a new line
299 
300  // a list is started or ended:
301  sal_Int16 eType = rInfo.GetNumRule()->Get( i-1 ).GetNumberingType();
302  OString aTag;
303  if( SVX_NUM_CHAR_SPECIAL == eType || SVX_NUM_BITMAP == eType)
305  else
307  HTMLOutFuncs::Out_AsciiTag( rWrt.Strm(), rWrt.GetNamespace() + aTag, false );
308  rWrt.m_bLFPossible = true;
309  }
310 
311  return rWrt;
312 }
313 
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvxNumType GetNumberingType() const
SVX_NUM_CHARS_UPPER_LETTER_N
SwHTMLNumRuleInfo & GetNumInfo()
Definition: wrthtml.hxx:527
void FillNextNumInfo()
bool IsOutline() const
Returns if this text node is an outline.
Definition: ndtxt.cxx:3993
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:500
const OUString & GetName() const
Definition: numrule.hxx:222
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:4088
SwDoc * m_pDoc
Definition: docbm.cxx:1190
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1426
bool m_bLFPossible
Definition: wrthtml.hxx:379
static SVT_DLLPUBLIC SvStream & Out_AsciiTag(SvStream &, const OString &rStr, bool bOn=true)
std::set< OUString > m_aNumRuleNames
Definition: wrthtml.hxx:281
SVX_NUM_ROMAN_UPPER
SVX_NUM_ROMAN_LOWER
#define HTML_BULLETCHAR_DISC
Definition: htmlnum.hxx:32
Writer & OutCSS1_NumberBulletListStyleOpt(Writer &rWrt, const SwNumRule &rNumRule, sal_uInt8 nLevel)
Definition: css1atr.cxx:1853
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:2804
OUString m_aBulletGrfs[MAXLEVEL]
Definition: wrthtml.hxx:294
#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:496
OUString aName
SwNodes & GetNodes()
Definition: doc.hxx:404
OString GetNamespace() const
Determines the prefix string needed to respect the requested namespace alias.
Definition: wrthtml.cxx:1475
SvStream & WriteChar(char nChar)
sal_Unicode GetBulletChar() const
#define OOO_STRING_SVTOOLS_HTML_orderlist
std::shared_ptr< SwUnoCursor > m_pCurrentPam
Definition: shellio.hxx:411
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:640
bool m_bCfgOutStyles
Definition: wrthtml.hxx:339
#define OOO_STRING_SVTOOLS_HTML_ULTYPE_circle
#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:409
sal_uInt16 nPos
HtmlTokenId m_nLastParaToken
Definition: wrthtml.hxx:309
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
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:79