File: WP6StylesListener.cpp

package info (click to toggle)
libwpd 0.10.3-2
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,948 kB
  • sloc: cpp: 28,095; sh: 4,433; makefile: 616; ansic: 4
file content (391 lines) | stat: -rw-r--r-- 12,461 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* libwpd
 * Version: MPL 2.0 / LGPLv2.1+
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Major Contributor(s):
 * Copyright (C) 2003 William Lachance (wrlach@gmail.com)
 * Copyright (C) 2003 Marc Maurer (uwog@uwog.net)
 * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License Version 2.1 or later
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
 * applicable instead of those above.
 *
 * For further information visit http://libwpd.sourceforge.net
 */

/* "This product is not manufactured, approved, or supported by
 * Corel Corporation or Corel Corporation Limited."
 */

#include "WP6StylesListener.h"
#include "WPXTable.h"
#include "WP6FileStructure.h"
#include "WPXFileStructure.h"
#include "libwpd_internal.h"
#include "WP6SubDocument.h"
#include "WP6PrefixDataPacket.h"

// WP6StylesListener: creates intermediate table and page span representations, given a
// sequence of messages passed to it by the parser.

WP6StylesListener::WP6StylesListener(std::list<WPXPageSpan> &pageList, WPXTableList tableList) :
	WP6Listener(),
	WPXStylesListener(pageList),
	m_currentPage(),
	m_tableList(tableList),
	m_currentTable(nullptr),
	m_tempMarginLeft(1.0),
	m_tempMarginRight(1.0),
	m_currentPageHasContent(false),
	m_isTableDefined(false),
	m_isSubDocument(false),
	m_subDocuments(),
	m_pageListHardPageMark(m_pageList.end())
{
}

void WP6StylesListener::endDocument()
{
	insertBreak(WPX_SOFT_PAGE_BREAK); // pretend we just had a soft page break (for the last page)
}

void WP6StylesListener::endSubDocument()
{
	insertBreak(WPX_SOFT_PAGE_BREAK); // pretend we just had a soft page break (for the last page)
}

void WP6StylesListener::insertBreak(const unsigned char breakType)
{
	if (m_isSubDocument)
		return;

	if (!isUndoOn())
	{
		switch (breakType)
		{
		case WPX_PAGE_BREAK:
		case WPX_SOFT_PAGE_BREAK:
			if ((m_pageList.size() > 0) && (m_currentPage==m_pageList.back())
			        && (m_pageListHardPageMark != m_pageList.end()))
			{
				m_pageList.back().setPageSpan(m_pageList.back().getPageSpan() + 1);
			}
			else
			{
				m_pageList.push_back(m_currentPage);
				if (m_pageListHardPageMark == m_pageList.end())
					--m_pageListHardPageMark;
			}
			m_currentPage = WPXPageSpan(m_pageList.back(), 0.0, 0.0);
			m_currentPage.setPageSpan(1);
			m_currentPageHasContent = false;
			break;
		default:
			break;
		}
		if (breakType == WPX_PAGE_BREAK)
		{
			m_pageListHardPageMark = m_pageList.end();
			m_currentPage.setMarginLeft(m_tempMarginLeft);
			m_currentPage.setMarginRight(m_tempMarginRight);
		}
	}
}

void WP6StylesListener::pageNumberingChange(const WPXPageNumberPosition pageNumberingPosition, const unsigned short pageNumberFontPointSize, const unsigned short pageNumberFontPID)
{
	if (!isUndoOn())
	{
		m_currentPage.setPageNumberPosition(pageNumberingPosition);

		if (pageNumberFontPID)
		{
			librevenge::RVNGString pidFontName = WP6Listener::getFontNameForPID(pageNumberFontPID);
			if (!pidFontName.empty())
				m_currentPage.setPageNumberingFontName(pidFontName);
		}

		m_currentPage.setPageNumberingFontSize(wpuToFontPointSize(pageNumberFontPointSize));
	}
}

void WP6StylesListener::pageMarginChange(const unsigned char side, const unsigned short margin)
{
	if (!isUndoOn())
	{
		auto marginInch = (double)((double)margin / (double)WPX_NUM_WPUS_PER_INCH);
		switch (side)
		{
		case WPX_TOP:
			m_currentPage.setMarginTop(marginInch);
			break;
		case WPX_BOTTOM:
			m_currentPage.setMarginBottom(marginInch);
			break;
		default:
			break;
		}
	}
}

void WP6StylesListener::pageFormChange(const unsigned short length, const unsigned short width, const WPXFormOrientation orientation)
{
	if (!isUndoOn())
	{
		auto lengthInch = (double)((double)length / (double)WPX_NUM_WPUS_PER_INCH);
		auto widthInch = (double)((double)width / (double)WPX_NUM_WPUS_PER_INCH);
		if (!m_currentPageHasContent)
		{
			m_currentPage.setFormLength(lengthInch);
			m_currentPage.setFormWidth(widthInch);
			m_currentPage.setFormOrientation(orientation);
		}
	}
}

void WP6StylesListener::marginChange(const unsigned char side, const unsigned short margin)
{
	if (!isUndoOn())
	{
		if (m_isSubDocument)
			return; // do not deal with L/R margins in headers, footer and notes

		std::list<WPXPageSpan>::iterator Iter;
		auto marginInch = (double)((double)margin / (double)WPX_NUM_WPUS_PER_INCH);
		switch (side)
		{
		case WPX_LEFT:
			if (!m_currentPageHasContent && (m_pageListHardPageMark == m_pageList.end()))
				m_currentPage.setMarginLeft(marginInch);
			else if (m_currentPage.getMarginLeft() > marginInch)
			{
				// Change the margin for the current page and for all pages in the list since the last Hard Break
				m_currentPage.setMarginLeft(marginInch);
				for (Iter = m_pageListHardPageMark; Iter != m_pageList.end(); ++Iter)
				{
					(*Iter).setMarginLeft(marginInch);
				}
			}
			m_tempMarginLeft = marginInch;
			break;
		case WPX_RIGHT:
			if (!m_currentPageHasContent && (m_pageListHardPageMark == m_pageList.end()))
				m_currentPage.setMarginRight(marginInch);
			else if (m_currentPage.getMarginRight() > marginInch)
			{
				// Change the margin for the current page and for all pages in the list since the last Hard Break
				m_currentPage.setMarginRight(marginInch);
				for (Iter = m_pageListHardPageMark; Iter != m_pageList.end(); ++Iter)
				{
					(*Iter).setMarginRight(marginInch);
				}
			}
			m_tempMarginRight = marginInch;
			break;
		default:
			break;
		}

	}

}

void WP6StylesListener::headerFooterGroup(const unsigned char headerFooterType, const unsigned char occurrenceBits, const unsigned short textPID)
{
	if (!isUndoOn())
	{
		WPD_DEBUG_MSG(("WordPerfect: headerFooterGroup (headerFooterType: %i, occurrenceBits: %i, textPID: %i)\n",
		               headerFooterType, occurrenceBits, textPID));
		bool tempCurrentPageHasContent = m_currentPageHasContent;
		if (headerFooterType <= WP6_HEADER_FOOTER_GROUP_FOOTER_B) // ignore watermarks for now
		{
			WPXHeaderFooterType wpxType = ((headerFooterType <= WP6_HEADER_FOOTER_GROUP_HEADER_B) ? HEADER : FOOTER);

			WPXHeaderFooterOccurrence wpxOccurrence;
			if (occurrenceBits & WP6_HEADER_FOOTER_GROUP_EVEN_BIT && occurrenceBits & WP6_HEADER_FOOTER_GROUP_ODD_BIT)
				wpxOccurrence = ALL;
			else if (occurrenceBits & WP6_HEADER_FOOTER_GROUP_EVEN_BIT)
				wpxOccurrence = EVEN;
			else
				wpxOccurrence = ODD;

			WPXTableList tableList;
			m_currentPage.setHeaderFooter(wpxType, headerFooterType, wpxOccurrence,
			                              ((textPID && WP6Listener::getPrefixDataPacket(textPID)) ? WP6Listener::getPrefixDataPacket(textPID)->getSubDocument() : nullptr), tableList);
			_handleSubDocument(((textPID && WP6Listener::getPrefixDataPacket(textPID)) ? WP6Listener::getPrefixDataPacket(textPID)->getSubDocument().get() : nullptr), WPX_SUBDOCUMENT_HEADER_FOOTER, tableList);
		}
		m_currentPageHasContent = tempCurrentPageHasContent;
	}
}

void WP6StylesListener::suppressPageCharacteristics(const unsigned char suppressCode)
{
	if (!isUndoOn())
	{
		WPD_DEBUG_MSG(("WordPerfect: suppressPageCharacteristics (suppressCode: %u)\n", suppressCode));

		if (suppressCode & WP6_PAGE_GROUP_SUPPRESS_PAGE_NUMBER)
			m_currentPage.setPageNumberSuppression(true);

		if (suppressCode & WP6_PAGE_GROUP_SUPPRESS_HEADER_A)
			m_currentPage.setHeadFooterSuppression(WPX_HEADER_A, true);
		if (suppressCode & WP6_PAGE_GROUP_SUPPRESS_HEADER_B)
			m_currentPage.setHeadFooterSuppression(WPX_HEADER_B, true);
		if (suppressCode & WP6_PAGE_GROUP_SUPPRESS_FOOTER_A)
			m_currentPage.setHeadFooterSuppression(WPX_FOOTER_A, true);
		if (suppressCode & WP6_PAGE_GROUP_SUPPRESS_FOOTER_B)
			m_currentPage.setHeadFooterSuppression(WPX_FOOTER_B, true);
	}
}

void WP6StylesListener::setPageNumber(const unsigned short pageNumber)
{
	if (!isUndoOn())
	{
		m_currentPage.setPageNumber(pageNumber);
	}
}

void WP6StylesListener::setPageNumberingType(const WPXNumberingType pageNumberingType)
{
	if (!isUndoOn())
	{
		m_currentPage.setPageNumberingType(pageNumberingType);
	}
}

void WP6StylesListener::defineTable(const unsigned char /* position */, const unsigned short /* leftOffset */)
{
	if (!isUndoOn())
	{
		m_currentPageHasContent = true;
		m_currentTable = std::make_shared<WPXTable>();
		m_tableList.add(m_currentTable);
		m_isTableDefined = true;
	}
}

void WP6StylesListener::startTable()
{
	if (!isUndoOn() && !m_isTableDefined)
	{
		m_currentPageHasContent = true;
		m_currentTable = std::make_shared<WPXTable>();
		m_tableList.add(m_currentTable);
		m_isTableDefined = false;
	}

}

void WP6StylesListener::endTable()
{
	if (!isUndoOn())
	{
		m_isTableDefined = false;
		m_currentTable = nullptr;
	}
}

void WP6StylesListener::insertRow(const unsigned short /* rowHeight */, const bool /* isMinimumHeight */, const bool /* isHeaderRow */)
{
	if (!isUndoOn() && m_currentTable)
	{
		m_currentPageHasContent = true;
		m_currentTable->insertRow();
	}
}

void WP6StylesListener::insertCell(const unsigned char colSpan, const unsigned char rowSpan, const unsigned char borderBits,
                                   const RGBSColor * /* cellFgColor */, const RGBSColor * /* cellBgColor */,
                                   const RGBSColor * /* cellBorderColor */, const WPXVerticalAlignment /* cellVerticalAlignment */,
                                   const bool /* useCellAttributes */, const unsigned /* cellAttributes */)
{
	if (!isUndoOn() && m_currentTable)
	{
		m_currentPageHasContent = true;
		m_currentTable->insertCell(colSpan, rowSpan, borderBits);
	}
}

void WP6StylesListener::noteOn(const unsigned short textPID)
{
	if (!isUndoOn())
	{
		m_currentPageHasContent = true;
		_handleSubDocument(((textPID && WP6Listener::getPrefixDataPacket(textPID)) ? WP6Listener::getPrefixDataPacket(textPID)->getSubDocument().get() : nullptr), WPX_SUBDOCUMENT_NOTE, m_tableList);
	}
}

void WP6StylesListener::insertTextBox(const WP6SubDocument *subDocument)
{
	if (!isUndoOn() && subDocument)
	{
		m_currentPageHasContent = true;
		_handleSubDocument(subDocument, WPX_SUBDOCUMENT_TEXT_BOX, m_tableList);
	}
}

void WP6StylesListener::commentAnnotation(const unsigned short textPID)
{
	if (!isUndoOn())
	{
		m_currentPageHasContent = true;
		_handleSubDocument(((textPID && WP6Listener::getPrefixDataPacket(textPID)) ? WP6Listener::getPrefixDataPacket(textPID)->getSubDocument().get() : nullptr), WPX_SUBDOCUMENT_COMMENT_ANNOTATION, m_tableList);
	}
}

void WP6StylesListener::_handleSubDocument(const WPXSubDocument *subDocument, WPXSubDocumentType subDocumentType, WPXTableList tableList,
                                           int /* nextTableIndice */)
{
	// We don't want to actual insert anything in the case of a sub-document, but we
	// do want to capture whatever table-related information is within it..
	std::set <const WPXSubDocument *> oldSubDocuments;
	oldSubDocuments = m_subDocuments;
	// prevent entering in an endless loop
	if ((subDocument) && (oldSubDocuments.find(subDocument) == oldSubDocuments.end()))
	{
		m_subDocuments.insert(subDocument);
		bool oldIsSubDocument = m_isSubDocument;
		m_isSubDocument = true;
		auto oldCurrentTable = m_currentTable;
		if (subDocumentType == WPX_SUBDOCUMENT_HEADER_FOOTER)
		{
			bool oldCurrentPageHasContent = m_currentPageHasContent;
			WPXTableList oldTableList = m_tableList;
			m_tableList = tableList;

			if (subDocument)
				static_cast<const WP6SubDocument *>(subDocument)->parse(this);
			endTable();

			m_tableList = oldTableList;
			m_currentPageHasContent = oldCurrentPageHasContent;
		}
		else
		{
			if (subDocument)
				static_cast<const WP6SubDocument *>(subDocument)->parse(this);
			endTable();
		}
		m_currentTable = oldCurrentTable;
		m_isSubDocument = oldIsSubDocument;
		m_subDocuments = oldSubDocuments;
	}
}

void WP6StylesListener::undoChange(const unsigned char undoType, const unsigned short /* undoLevel */)
{
	if (undoType == WP6_UNDO_GROUP_INVALID_TEXT_START)
		setUndoOn(true);
	else if (undoType == WP6_UNDO_GROUP_INVALID_TEXT_END)
		setUndoOn(false);
}
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */