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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* This file is part of the libvisio project.
*
* 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/.
*/
#include <librevenge-stream/librevenge-stream.h>
#include <locale.h>
#include <sstream>
#include <string>
#include <stack>
#include "libvisio_utils.h"
#include "VSD5Parser.h"
#include "VSDInternalStream.h"
#include "VSDDocumentStructure.h"
#include "VSDContentCollector.h"
#include "VSDStylesCollector.h"
libvisio::VSD5Parser::VSD5Parser(librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter)
: VSD6Parser(input, painter)
{}
libvisio::VSD5Parser::~VSD5Parser()
{}
void libvisio::VSD5Parser::readPointer(librevenge::RVNGInputStream *input, Pointer &ptr)
{
ptr.Type = readU16(input) & 0x00ff;
ptr.Format = readU16(input) & 0x00ff;
input->seek(4, librevenge::RVNG_SEEK_CUR); // Skip dword
ptr.Offset = readU32(input);
ptr.Length = readU32(input);
}
void libvisio::VSD5Parser::readPointerInfo(librevenge::RVNGInputStream *input, unsigned ptrType, unsigned shift, unsigned &listSize, int &pointerCount)
{
VSD_DEBUG_MSG(("VSD5Parser::readPointerInfo\n"));
switch (ptrType)
{
case VSD_TRAILER_STREAM:
input->seek(shift+0x82, librevenge::RVNG_SEEK_SET);
break;
case VSD_PAGE:
input->seek(shift+0x42, librevenge::RVNG_SEEK_SET);
break;
case VSD_FONT_LIST:
input->seek(shift+0x2e, librevenge::RVNG_SEEK_SET);
break;
case VSD_STYLES:
input->seek(shift+0x12, librevenge::RVNG_SEEK_SET);
break;
case VSD_STENCILS:
case VSD_SHAPE_FOREIGN:
input->seek(shift+0x1e, librevenge::RVNG_SEEK_SET);
break;
case VSD_STENCIL_PAGE:
input->seek(shift+0x36, librevenge::RVNG_SEEK_SET);
break;
default:
if (ptrType > 0x45)
input->seek(shift+0x1e, librevenge::RVNG_SEEK_SET);
else
input->seek(shift+0xa, librevenge::RVNG_SEEK_SET);
break;
}
pointerCount = readS16(input);
listSize = 0;
VSD_DEBUG_MSG(("VSD5Parser::readPointerInfo ptrType %u shift %u pointerCount %i\n", ptrType, shift, pointerCount));
}
bool libvisio::VSD5Parser::getChunkHeader(librevenge::RVNGInputStream *input)
{
unsigned char tmpChar = 0;
while (!input->isEnd() && !tmpChar)
tmpChar = readU8(input);
if (input->isEnd())
return false;
else
input->seek(-1, librevenge::RVNG_SEEK_CUR);
m_header.chunkType = getUInt(input);
m_header.id = getUInt(input);
m_header.level = readU8(input);
m_header.unknown = readU8(input);
m_header.trailer = 0;
m_header.list = getUInt(input);
m_header.dataLength = readU32(input);
return true;
}
void libvisio::VSD5Parser::handleChunkRecords(librevenge::RVNGInputStream *input)
{
long startPosition = input->tell();
long endPosition = input->tell() + m_header.dataLength;
input->seek(endPosition - 4, librevenge::RVNG_SEEK_SET);
unsigned numRecords = readU16(input);
const long headerPosition = endPosition - 4 * (numRecords + 1);
if (headerPosition <= startPosition) // no records to read
return;
unsigned endOffset = readU16(input);
if (long(endOffset) > (headerPosition - startPosition))
endOffset = unsigned(headerPosition - startPosition); // try to read something anyway
std::map<unsigned, ChunkHeader> records;
input->seek(headerPosition, librevenge::RVNG_SEEK_SET);
unsigned i = 0;
for (i = 0; i < numRecords; ++i)
{
ChunkHeader header;
header.chunkType = readU16(input);
unsigned offset = readU16(input);
unsigned tmpStart = offset;
while (tmpStart % 4)
tmpStart++;
if (tmpStart < endOffset)
{
header.dataLength = endOffset - tmpStart;
header.level = m_header.level + 1;
records[tmpStart] = header;
endOffset = offset;
}
}
i = 0;
for (std::map<unsigned, ChunkHeader>::iterator iter = records.begin(); iter != records.end(); ++iter)
{
m_header = iter->second;
m_header.id = i++;
input->seek(startPosition + iter->first, librevenge::RVNG_SEEK_SET);
handleChunk(input);
}
}
void libvisio::VSD5Parser::readGeomList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readGeomList\n"));
if (!m_shape.m_geometries.empty() && m_currentGeometryList && m_currentGeometryList->empty())
m_shape.m_geometries.erase(--m_currentGeomListCount);
m_currentGeometryList = &m_shape.m_geometries[m_currentGeomListCount++];
if (!m_isStencilStarted)
m_collector->collectUnhandledChunk(m_header.id, m_header.level);
handleChunkRecords(input);
}
void libvisio::VSD5Parser::readList(librevenge::RVNGInputStream *input)
{
if (!m_isStencilStarted)
m_collector->collectUnhandledChunk(m_header.id, m_header.level);
handleChunkRecords(input);
}
void libvisio::VSD5Parser::readCharList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readCharList\n"));
readList(input);
}
void libvisio::VSD5Parser::readParaList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readParaList\n"));
readList(input);
}
void libvisio::VSD5Parser::readShapeList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readShapeList\n"));
readList(input);
}
void libvisio::VSD5Parser::readPropList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readPropList\n"));
readList(input);
}
void libvisio::VSD5Parser::readFieldList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readFieldList\n"));
readList(input);
}
void libvisio::VSD5Parser::readNameList2(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readNameList2\n"));
readList(input);
}
void libvisio::VSD5Parser::readTabsDataList(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readTabsDataList\n"));
readList(input);
}
void libvisio::VSD5Parser::readLine(librevenge::RVNGInputStream *input)
{
input->seek(1, librevenge::RVNG_SEEK_CUR);
double strokeWidth = readDouble(input);
unsigned char colourIndex = readU8(input);
Colour c = _colourFromIndex(colourIndex);
unsigned char linePattern = readU8(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double rounding = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
unsigned char startMarker = readU8(input);
unsigned char endMarker = readU8(input);
unsigned char lineCap = readU8(input);
if (m_isInStyles)
m_collector->collectLineStyle(m_header.level, strokeWidth, c, linePattern, startMarker, endMarker, lineCap, rounding);
else
m_shape.m_lineStyle.override(VSDOptionalLineStyle(strokeWidth, c, linePattern, startMarker, endMarker, lineCap, rounding));
}
void libvisio::VSD5Parser::readParaIX(librevenge::RVNGInputStream *input)
{
unsigned charCount = readU16(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double indFirst = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double indLeft = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double indRight = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double spLine = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double spBefore = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double spAfter = readDouble(input);
unsigned char align = readU8(input);
unsigned char bullet(0);
VSDName bulletStr;
VSDName bulletFont;
double bulletFontSize(0.0);
double textPosAfterTab(0.0);
unsigned flags(0);
if (m_isInStyles)
m_collector->collectParaIXStyle(m_header.id, m_header.level, charCount, indFirst, indLeft, indRight,
spLine, spBefore, spAfter, align, bullet, bulletStr,
bulletFont, bulletFontSize, textPosAfterTab, flags);
else
{
if (m_isStencilStarted)
{
VSD_DEBUG_MSG(("Found stencil paragraph style\n"));
}
m_shape.m_paraStyle.override(VSDOptionalParaStyle(charCount, indFirst, indLeft, indRight,
spLine, spBefore, spAfter, align, bullet,
bulletStr, bulletFont, bulletFontSize,
textPosAfterTab, flags));
m_shape.m_paraList.addParaIX(m_header.id, m_header.level, charCount, indFirst, indLeft, indRight,
spLine, spBefore, spAfter, align, bullet, bulletStr, bulletFont,
bulletFontSize, textPosAfterTab, flags);
}
}
void libvisio::VSD5Parser::readCharIX(librevenge::RVNGInputStream *input)
{
unsigned charCount = readU16(input);
unsigned fontID = readU16(input);
VSDName font;
std::map<unsigned, VSDName>::const_iterator iter = m_fonts.find(fontID);
if (iter != m_fonts.end())
font = iter->second;
Colour fontColour = _colourFromIndex(readU8(input));
bool bold(false);
bool italic(false);
bool underline(false);
bool doubleunderline(false);
bool strikeout(false);
bool doublestrikeout(false);
bool allcaps(false);
bool initcaps(false);
bool smallcaps(false);
bool superscript(false);
bool subscript(false);
unsigned char fontMod = readU8(input);
if (fontMod & 1) bold = true;
if (fontMod & 2) italic = true;
if (fontMod & 4) underline = true;
if (fontMod & 8) smallcaps = true;
fontMod = readU8(input);
if (fontMod & 1) allcaps = true;
if (fontMod & 2) initcaps = true;
fontMod = readU8(input);
if (fontMod & 1) superscript = true;
if (fontMod & 2) subscript = true;
double scaleWidth = (double)(readU16(input)) / 10000.0;
input->seek(2, librevenge::RVNG_SEEK_CUR);
double fontSize = readDouble(input);
#if 0
fontMod = readU8(input);
if (fontMod & 1) doubleunderline = true;
if (fontMod & 4) strikeout = true;
if (fontMod & 0x20) doublestrikeout = true;
#endif
if (m_isInStyles)
m_collector->collectCharIXStyle(m_header.id, m_header.level, charCount, font, fontColour, fontSize,
bold, italic, underline, doubleunderline, strikeout, doublestrikeout,
allcaps, initcaps, smallcaps, superscript, subscript, scaleWidth);
else
{
if (m_isStencilStarted)
{
VSD_DEBUG_MSG(("Found stencil character style\n"));
}
m_shape.m_charStyle.override(VSDOptionalCharStyle(charCount, font, fontColour, fontSize,
bold, italic, underline, doubleunderline, strikeout, doublestrikeout,
allcaps, initcaps, smallcaps, superscript, subscript, scaleWidth));
m_shape.m_charList.addCharIX(m_header.id, m_header.level, charCount, font, fontColour, fontSize,
bold, italic, underline, doubleunderline, strikeout, doublestrikeout,
allcaps, initcaps, smallcaps, superscript, subscript, scaleWidth);
}
}
void libvisio::VSD5Parser::readFillAndShadow(librevenge::RVNGInputStream *input)
{
Colour colourFG = _colourFromIndex(readU8(input));
Colour colourBG = _colourFromIndex(readU8(input));
unsigned char fillPattern = readU8(input);
Colour shfgc = _colourFromIndex(readU8(input));
input->seek(1, librevenge::RVNG_SEEK_CUR); // Shadow Background Colour skipped
unsigned char shadowPattern = readU8(input);
if (m_isInStyles)
m_collector->collectFillStyle(m_header.level, colourFG, colourBG, fillPattern,
0.0, 0.0, shadowPattern, shfgc);
else
{
double shadowOffsetX = 0.0;
double shadowOffsetY = 0.0;
if (m_isStencilStarted && m_currentStencil)
{
VSD_DEBUG_MSG(("Found stencil fill\n"));
shadowOffsetX = m_currentStencil->m_shadowOffsetX;
shadowOffsetY = m_currentStencil->m_shadowOffsetY;
}
else
{
shadowOffsetX = m_shadowOffsetX;
shadowOffsetY = m_shadowOffsetY;
}
m_shape.m_fillStyle.override(VSDOptionalFillStyle(colourFG, colourBG, fillPattern, 0.0,
0.0, shfgc, shadowPattern, shadowOffsetX, shadowOffsetY));
}
}
void libvisio::VSD5Parser::readStyleSheet(librevenge::RVNGInputStream *input)
{
input->seek(10, librevenge::RVNG_SEEK_CUR);
unsigned lineStyle = getUInt(input);
unsigned fillStyle = getUInt(input);
unsigned textStyle = getUInt(input);
m_collector->collectStyleSheet(m_header.id, m_header.level, lineStyle, fillStyle, textStyle);
}
void libvisio::VSD5Parser::readShape(librevenge::RVNGInputStream *input)
{
m_currentGeomListCount = 0;
m_currentGeometryList = 0;
m_isShapeStarted = true;
m_shapeList.clear();
if (m_header.id != MINUS_ONE)
m_currentShapeID = m_header.id;
m_currentShapeLevel = m_header.level;
unsigned parent = 0;
unsigned masterPage = MINUS_ONE;
unsigned masterShape = MINUS_ONE;
unsigned lineStyle = MINUS_ONE;
unsigned fillStyle = MINUS_ONE;
unsigned textStyle = MINUS_ONE;
try
{
input->seek(2, librevenge::RVNG_SEEK_CUR);
parent = getUInt(input);
input->seek(2, librevenge::RVNG_SEEK_CUR);
masterPage = getUInt(input);
masterShape = getUInt(input);
lineStyle = getUInt(input);
fillStyle = getUInt(input);
textStyle = getUInt(input);
}
catch (const EndOfStreamException &)
{
}
m_shape.clear();
const VSDShape *tmpShape = m_stencils.getStencilShape(masterPage, masterShape);
if (tmpShape)
{
if (tmpShape->m_foreign)
m_shape.m_foreign = new ForeignData(*(tmpShape->m_foreign));
m_shape.m_text = tmpShape->m_text;
m_shape.m_textFormat = tmpShape->m_textFormat;
}
m_shape.m_lineStyleId = lineStyle;
m_shape.m_fillStyleId = fillStyle;
m_shape.m_textStyleId = textStyle;
m_shape.m_parent = parent;
m_shape.m_masterPage = masterPage;
m_shape.m_masterShape = masterShape;
m_shape.m_shapeId = m_currentShapeID;
m_currentShapeID = MINUS_ONE;
}
void libvisio::VSD5Parser::readPage(librevenge::RVNGInputStream *input)
{
unsigned backgroundPageID = getUInt(input);
m_collector->collectPage(m_header.id, m_header.level, backgroundPageID, m_isBackgroundPage, m_currentPageName);
}
void libvisio::VSD5Parser::readTextBlock(librevenge::RVNGInputStream *input)
{
input->seek(1, librevenge::RVNG_SEEK_CUR);
double leftMargin = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double rightMargin = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double topMargin = readDouble(input);
input->seek(1, librevenge::RVNG_SEEK_CUR);
double bottomMargin = readDouble(input);
unsigned char verticalAlign = readU8(input);
unsigned char colourIndex = readU8(input);
bool isBgFilled = !!colourIndex;
Colour c;
if (isBgFilled)
c = _colourFromIndex(colourIndex-1);
if (m_isInStyles)
m_collector->collectTextBlockStyle(m_header.level, leftMargin, rightMargin, topMargin, bottomMargin,
verticalAlign, isBgFilled, c, 0.0, (unsigned char)0);
else
m_shape.m_textBlockStyle.override(VSDOptionalTextBlockStyle(leftMargin, rightMargin, topMargin, bottomMargin,
verticalAlign, isBgFilled, c, 0.0, (unsigned char)0));
}
void libvisio::VSD5Parser::readTextField(librevenge::RVNGInputStream *input)
{
input->seek(3, librevenge::RVNG_SEEK_CUR);
if (0xe8 == readU8(input))
{
int nameId = readS16(input);
m_shape.m_fields.addTextField(m_header.id, m_header.level, nameId, 0xffff);
}
else
{
double numericValue = readDouble(input);
m_shape.m_fields.addNumericField(m_header.id, m_header.level, 0xffff, numericValue, 0xffff);
}
}
void libvisio::VSD5Parser::readNameIDX(librevenge::RVNGInputStream *input)
{
VSD_DEBUG_MSG(("VSD5Parser::readNameIDX\n"));
std::map<unsigned, VSDName> names;
unsigned recordCount = readU16(input);
for (unsigned i = 0; i < recordCount; ++i)
{
unsigned nameId = readU16(input);
unsigned elementId = readU16(input);
std::map<unsigned, VSDName>::const_iterator iter = m_names.find(nameId);
if (iter != m_names.end())
names[elementId] = iter->second;
}
m_namesMapMap[m_header.level] = names;
}
unsigned libvisio::VSD5Parser::getUInt(librevenge::RVNGInputStream *input)
{
int value = readS16(input);
return (unsigned)value;
}
int libvisio::VSD5Parser::getInt(librevenge::RVNGInputStream *input)
{
return readS16(input);
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|