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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
|
/* -*- 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 <string.h>
#include <libxml/xmlIO.h>
#include <libxml/xmlstring.h>
#include <librevenge-stream/librevenge-stream.h>
#include <boost/algorithm/string.hpp>
#include "VDXParser.h"
#include "libvisio_utils.h"
#include "VSDContentCollector.h"
#include "VSDStylesCollector.h"
#include "VSDXMLHelper.h"
#include "VSDXMLTokenMap.h"
libvisio::VDXParser::VDXParser(librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter)
: VSDXMLParserBase(), m_input(input), m_painter(painter)
{
}
libvisio::VDXParser::~VDXParser()
{
}
bool libvisio::VDXParser::parseMain()
{
if (!m_input)
return false;
try
{
std::vector<std::map<unsigned, XForm> > groupXFormsSequence;
std::vector<std::map<unsigned, unsigned> > groupMembershipsSequence;
std::vector<std::list<unsigned> > documentPageShapeOrders;
VSDStylesCollector stylesCollector(groupXFormsSequence, groupMembershipsSequence, documentPageShapeOrders);
m_collector = &stylesCollector;
m_input->seek(0, librevenge::RVNG_SEEK_SET);
if (!processXmlDocument(m_input))
return false;
VSDStyles styles = stylesCollector.getStyleSheets();
VSDContentCollector contentCollector(m_painter, groupXFormsSequence, groupMembershipsSequence, documentPageShapeOrders, styles, m_stencils);
m_collector = &contentCollector;
m_input->seek(0, librevenge::RVNG_SEEK_SET);
if (!processXmlDocument(m_input))
return false;
return true;
}
catch (...)
{
return false;
}
}
bool libvisio::VDXParser::extractStencils()
{
m_extractStencils = true;
return parseMain();
}
bool libvisio::VDXParser::processXmlDocument(librevenge::RVNGInputStream *input)
{
if (!input)
return false;
xmlTextReaderPtr reader = xmlReaderForStream(input, 0, 0, XML_PARSE_NOBLANKS|XML_PARSE_NOENT|XML_PARSE_NONET|XML_PARSE_RECOVER);
if (!reader)
return false;
int ret = xmlTextReaderRead(reader);
while (1 == ret)
{
processXmlNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
return true;
}
void libvisio::VDXParser::processXmlNode(xmlTextReaderPtr reader)
{
if (!reader)
return;
int tokenId = getElementToken(reader);
int tokenType = xmlTextReaderNodeType(reader);
_handleLevelChange((unsigned)getElementDepth(reader));
switch (tokenId)
{
case XML_COLORS:
if (XML_READER_TYPE_ELEMENT == tokenType)
readColours(reader);
break;
case XML_FACENAMES:
if (XML_READER_TYPE_ELEMENT == tokenType)
readFonts(reader);
break;
case XML_FILL:
if (XML_READER_TYPE_ELEMENT == tokenType)
readFillAndShadow(reader);
break;
case XML_LINE:
if (XML_READER_TYPE_ELEMENT == tokenType)
readLine(reader);
break;
case XML_MASTER:
if (XML_READER_TYPE_ELEMENT == tokenType)
handleMasterStart(reader);
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
handleMasterEnd(reader);
break;
case XML_MASTERS:
if (XML_READER_TYPE_ELEMENT == tokenType)
handleMastersStart(reader);
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
handleMastersEnd(reader);
break;
case XML_PAGE:
if (XML_READER_TYPE_ELEMENT == tokenType)
handlePageStart(reader);
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
handlePageEnd(reader);
break;
case XML_PAGEPROPS:
if (XML_READER_TYPE_ELEMENT == tokenType)
readPageProps(reader);
break;
case XML_PAGES:
if (XML_READER_TYPE_ELEMENT == tokenType)
handlePagesStart(reader);
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
handlePagesEnd(reader);
break;
case XML_PAGESHEET:
if (XML_READER_TYPE_ELEMENT == tokenType)
readPageSheet(reader);
break;
case XML_SHAPE:
if (XML_READER_TYPE_ELEMENT == tokenType)
readShape(reader);
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
{
if (m_isStencilStarted)
m_currentStencil->addStencilShape(m_shape.m_shapeId, m_shape);
else
_flushShape();
m_shape.clear();
if (m_shapeStack.empty())
m_isShapeStarted = false;
}
break;
case XML_SHAPES:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (m_isShapeStarted)
{
m_shapeStack.push(m_shape);
m_shapeLevelStack.push(m_currentShapeLevel);
m_currentShapeLevel = 0;
}
}
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
{
if (!m_shapeStack.empty() && !m_shapeLevelStack.empty())
{
m_shape = m_shapeStack.top();
m_shapeStack.pop();
m_currentShapeLevel = m_shapeLevelStack.top();
m_shapeLevelStack.pop();
}
else
{
m_isShapeStarted = false;
while (!m_shapeLevelStack.empty())
m_shapeLevelStack.pop();
while (!m_shapeStack.empty())
m_shapeStack.pop();
}
}
break;
case XML_SOLUTIONXML:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
int ret = 0;
do
{
ret = xmlTextReaderRead(reader);
#if 0
// SolutionXML inside VDX file can have invalid namespace URIs
xmlResetLastError();
#endif
tokenId = getElementToken(reader);
tokenType = xmlTextReaderNodeType(reader);
}
while ((XML_SOLUTIONXML != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
}
break;
case XML_STYLESHEET:
if (XML_READER_TYPE_ELEMENT == tokenType)
readStyleSheet(reader);
break;
case XML_STYLESHEETS:
if (XML_READER_TYPE_ELEMENT == tokenType)
m_isInStyles = true;
else if (XML_READER_TYPE_END_ELEMENT == tokenType)
{
_handleLevelChange(0);
m_isInStyles = false;
}
break;
case XML_FOREIGN:
if (XML_READER_TYPE_ELEMENT == tokenType)
readForeignInfo(reader);
break;
case XML_FOREIGNDATA:
if (XML_READER_TYPE_ELEMENT == tokenType)
readForeignData(reader);
break;
case XML_XFORM:
if (XML_READER_TYPE_ELEMENT == tokenType)
readXFormData(reader);
break;
case XML_MISC:
if (XML_READER_TYPE_ELEMENT == tokenType)
readMisc(reader);
break;
case XML_GEOM:
if (XML_READER_TYPE_ELEMENT == tokenType)
readGeometry(reader);
break;
case XML_PARA:
if (XML_READER_TYPE_ELEMENT == tokenType)
readParaIX(reader);
break;
case XML_CHAR:
if (XML_READER_TYPE_ELEMENT == tokenType)
readCharIX(reader);
break;
case XML_TEXT:
if (XML_READER_TYPE_ELEMENT == tokenType)
readText(reader);
break;
case XML_TEXTBLOCK:
if (XML_READER_TYPE_ELEMENT == tokenType)
readTextBlock(reader);
break;
case XML_TEXTXFORM:
if (XML_READER_TYPE_ELEMENT == tokenType)
readTxtXForm(reader);
break;
default:
break;
}
#ifdef DEBUG
const xmlChar *name = xmlTextReaderConstName(reader);
const xmlChar *value = xmlTextReaderConstValue(reader);
int isEmptyElement = xmlTextReaderIsEmptyElement(reader);
for (int i=0; i<getElementDepth(reader); ++i)
{
VSD_DEBUG_MSG((" "));
}
VSD_DEBUG_MSG(("%i %i %s", isEmptyElement, tokenType, name ? (const char *)name : ""));
if (xmlTextReaderNodeType(reader) == 1)
{
while (xmlTextReaderMoveToNextAttribute(reader))
{
const xmlChar *name1 = xmlTextReaderConstName(reader);
const xmlChar *value1 = xmlTextReaderConstValue(reader);
printf(" %s=\"%s\"", name1, value1);
}
}
if (!value)
VSD_DEBUG_MSG(("\n"));
else
{
VSD_DEBUG_MSG((" %s\n", value));
}
#endif
}
// Functions reading the DiagramML document content
void libvisio::VDXParser::readLine(xmlTextReaderPtr reader)
{
boost::optional<double> strokeWidth;
boost::optional<Colour> colour;
boost::optional<unsigned char> linePattern;
boost::optional<unsigned char> startMarker;
boost::optional<unsigned char> endMarker;
boost::optional<unsigned char> lineCap;
unsigned level = (unsigned)getElementDepth(reader);
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readLine: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_LINEWEIGHT:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(strokeWidth, reader);
break;
case XML_LINECOLOR:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readExtendedColourData(colour, reader);
break;
case XML_LINEPATTERN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(linePattern, reader);
break;
case XML_BEGINARROW:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(startMarker, reader);
break;
case XML_ENDARROW:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(endMarker, reader);
break;
case XML_LINECAP:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(lineCap, reader);
break;
default:
break;
}
}
while ((XML_LINE != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
if (m_isInStyles)
m_collector->collectLineStyle(level, strokeWidth, colour, linePattern, startMarker, endMarker, lineCap);
else
m_shape.m_lineStyle.override(VSDOptionalLineStyle(strokeWidth, colour, linePattern, startMarker, endMarker, lineCap));
}
void libvisio::VDXParser::readFillAndShadow(xmlTextReaderPtr reader)
{
boost::optional<Colour> fillColourFG;
boost::optional<double> fillFGTransparency;
boost::optional<Colour> fillColourBG;
boost::optional<double> fillBGTransparency;
boost::optional<unsigned char> fillPattern;
boost::optional<Colour> shadowColourFG;
boost::optional<Colour> shadowColourBG;
boost::optional<unsigned char> shadowPattern;
boost::optional<double> shadowOffsetX;
boost::optional<double> shadowOffsetY;
unsigned level = (unsigned)getElementDepth(reader);
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readFillAndShadow: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_FILLFOREGND:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readExtendedColourData(fillColourFG, reader);
break;
case XML_FILLBKGND:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readExtendedColourData(fillColourBG, reader);
break;
case XML_FILLPATTERN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(fillPattern, reader);
break;
case XML_SHDWFOREGND:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readExtendedColourData(shadowColourFG, reader);
break;
case XML_SHDWBKGND:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readExtendedColourData(shadowColourBG, reader);
break;
case XML_SHDWPATTERN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(shadowPattern, reader);
break;
case XML_FILLFOREGNDTRANS:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(fillFGTransparency, reader);
break;
case XML_FILLBKGNDTRANS:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(fillBGTransparency, reader);
break;
case XML_SHAPESHDWOFFSETX:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(shadowOffsetX, reader);
break;
case XML_SHAPESHDWOFFSETY:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(shadowOffsetY, reader);
break;
default:
break;
}
}
while ((XML_FILL != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
if (m_isInStyles)
m_collector->collectFillStyle(level, fillColourFG, fillColourBG, fillPattern, fillFGTransparency,
fillBGTransparency, shadowPattern, shadowColourFG, shadowOffsetX, shadowOffsetY);
else
{
if (m_isStencilStarted)
{
VSD_DEBUG_MSG(("Found stencil fill\n"));
}
m_shape.m_fillStyle.override(VSDOptionalFillStyle(fillColourFG, fillColourBG, fillPattern, fillFGTransparency, fillBGTransparency,
shadowColourFG, shadowPattern, shadowOffsetX, shadowOffsetY));
}
}
void libvisio::VDXParser::readMisc(xmlTextReaderPtr reader)
{
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readMisc: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_HIDETEXT:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readBoolData(m_shape.m_misc.m_hideText, reader);
break;
default:
break;
}
}
while ((XML_MISC != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
}
void libvisio::VDXParser::readXFormData(xmlTextReaderPtr reader)
{
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readXFormData: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_PINX:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.pinX, reader);
break;
case XML_PINY:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.pinY, reader);
break;
case XML_WIDTH:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.width, reader);
break;
case XML_HEIGHT:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.height, reader);
break;
case XML_LOCPINX:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.pinLocX, reader);
break;
case XML_LOCPINY:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.pinLocY, reader);
break;
case XML_ANGLE:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(m_shape.m_xform.angle, reader);
break;
case XML_FLIPX:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readBoolData(m_shape.m_xform.flipX, reader);
break;
case XML_FLIPY:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readBoolData(m_shape.m_xform.flipY, reader);
break;
default:
break;
}
}
while ((XML_XFORM != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
}
void libvisio::VDXParser::readTxtXForm(xmlTextReaderPtr reader)
{
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readXFormData: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_TXTPINX:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->pinX, reader);
}
break;
case XML_TXTPINY:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->pinY, reader);
}
break;
case XML_TXTWIDTH:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->width, reader);
}
break;
case XML_TXTHEIGHT:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->height, reader);
}
break;
case XML_TXTLOCPINX:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->pinLocX, reader);
}
break;
case XML_TXTLOCPINY:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->pinLocY, reader);
}
break;
case XML_TXTANGLE:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_txtxform)
m_shape.m_txtxform = new XForm();
ret = readDoubleData(m_shape.m_txtxform->angle, reader);
}
break;
default:
break;
}
}
while ((XML_TEXTXFORM != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
}
void libvisio::VDXParser::readPageProps(xmlTextReaderPtr reader)
{
double pageWidth = 0.0;
double pageHeight = 0.0;
double shadowOffsetX = 0.0;
double shadowOffsetY = 0.0;
double pageScale = 1.0;
double drawingScale = 1.0;
unsigned level = (unsigned)getElementDepth(reader);
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readPageProps: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_PAGEWIDTH:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret =readDoubleData(pageWidth, reader);
break;
case XML_PAGEHEIGHT:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(pageHeight, reader);
break;
case XML_SHDWOFFSETX:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(shadowOffsetX, reader);
break;
case XML_SHDWOFFSETY:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(shadowOffsetY, reader);
break;
case XML_PAGESCALE:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(pageScale, reader);
break;
case XML_DRAWINGSCALE:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(drawingScale, reader);
break;
default:
break;
}
}
while ((XML_PAGEPROPS != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
if (m_isStencilStarted)
{
m_currentStencil->m_shadowOffsetX = shadowOffsetX;
m_currentStencil->m_shadowOffsetY = shadowOffsetY;
}
else if (m_isPageStarted)
{
double scale = drawingScale > 0 || drawingScale < 0 ? pageScale/drawingScale : 1.0;
m_collector->collectPageProps(0, level, pageWidth, pageHeight, shadowOffsetX, shadowOffsetY, scale);
}
}
void libvisio::VDXParser::readFonts(xmlTextReaderPtr reader)
{
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readFonts: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
if (XML_FACENAME == tokenId)
{
xmlChar *id = xmlTextReaderGetAttribute(reader, BAD_CAST("ID"));
xmlChar *name = xmlTextReaderGetAttribute(reader, BAD_CAST("Name"));
if (id && name)
{
unsigned idx = (unsigned)xmlStringToLong(id);
librevenge::RVNGBinaryData textStream(name, xmlStrlen(name));
m_fonts[idx] = VSDName(textStream, libvisio::VSD_TEXT_UTF8);
}
xmlFree(name);
xmlFree(id);
}
}
while ((XML_FACENAMES != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
}
void libvisio::VDXParser::readTextBlock(xmlTextReaderPtr reader)
{
double leftMargin = 0.0;
double rightMargin = 0.0;
double topMargin = 0.0;
double bottomMargin = 0.0;
unsigned char verticalAlign = 0;
long bgClrId = 0;
Colour bgColour;
double defaultTabStop = 0.0;
unsigned char textDirection = 0;
unsigned level = (unsigned)getElementDepth(reader);
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_LEFTMARGIN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(leftMargin, reader);
break;
case XML_RIGHTMARGIN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(rightMargin, reader);
break;
case XML_TOPMARGIN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(topMargin, reader);
break;
case XML_BOTTOMMARGIN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(bottomMargin, reader);
break;
case XML_VERTICALALIGN:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(verticalAlign, reader);
break;
case XML_TEXTBKGND:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
ret = readExtendedColourData(bgColour, bgClrId, reader);
if (bgClrId < 0)
bgClrId = 0;
if (bgClrId)
{
std::map<unsigned, Colour>::const_iterator iter = m_colours.find(bgClrId-1);
if (iter != m_colours.end())
bgColour = iter->second;
else
bgColour = Colour();
}
}
break;
case XML_DEFAULTTABSTOP:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readDoubleData(defaultTabStop, reader);
break;
case XML_TEXTDIRECTION:
if (XML_READER_TYPE_ELEMENT == tokenType)
ret = readByteData(textDirection, reader);
break;
case XML_TEXTBKGNDTRANS:
default:
break;
}
}
while ((XML_TEXTBLOCK != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
if (m_isInStyles)
m_collector->collectTextBlockStyle(level, leftMargin, rightMargin, topMargin, bottomMargin,
verticalAlign, !!bgClrId, bgColour, defaultTabStop, textDirection);
else
m_shape.m_textBlockStyle.override(VSDOptionalTextBlockStyle(leftMargin, rightMargin, topMargin, bottomMargin,
verticalAlign, !!bgClrId, bgColour, defaultTabStop, textDirection));
}
xmlChar *libvisio::VDXParser::readStringData(xmlTextReaderPtr reader)
{
int ret = xmlTextReaderRead(reader);
if (1 == ret && XML_READER_TYPE_TEXT == xmlTextReaderNodeType(reader))
{
xmlChar *stringValue = xmlTextReaderValue(reader);
ret = xmlTextReaderRead(reader);
if (1 == ret && stringValue)
{
VSD_DEBUG_MSG(("VDXParser::readStringData stringValue %s\n", (const char *)stringValue));
return stringValue;
}
}
return 0;
}
int libvisio::VDXParser::getElementToken(xmlTextReaderPtr reader)
{
return VSDXMLTokenMap::getTokenId(xmlTextReaderConstName(reader));
}
int libvisio::VDXParser::getElementDepth(xmlTextReaderPtr reader)
{
return xmlTextReaderDepth(reader);
}
void libvisio::VDXParser::getBinaryData(xmlTextReaderPtr reader)
{
const int ret = xmlTextReaderRead(reader);
if (1 == ret && XML_READER_TYPE_TEXT == xmlTextReaderNodeType(reader))
{
const xmlChar *data = xmlTextReaderConstValue(reader);
if (data)
{
if (!m_shape.m_foreign)
m_shape.m_foreign = new ForeignData();
m_shape.m_foreign->data.clear();
m_shape.m_foreign->data.appendBase64Data(librevenge::RVNGString((const char *)data));
}
}
}
void libvisio::VDXParser::readForeignInfo(xmlTextReaderPtr reader)
{
int ret = 1;
int tokenId = XML_TOKEN_INVALID;
int tokenType = -1;
do
{
ret = xmlTextReaderRead(reader);
tokenId = getElementToken(reader);
if (XML_TOKEN_INVALID == tokenId)
{
VSD_DEBUG_MSG(("VDXParser::readForeignInfo: unknown token %s\n", xmlTextReaderConstName(reader)));
}
tokenType = xmlTextReaderNodeType(reader);
switch (tokenId)
{
case XML_IMGOFFSETX:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_foreign)
m_shape.m_foreign = new ForeignData();
ret = readDoubleData(m_shape.m_foreign->offsetX, reader);
}
break;
case XML_IMGOFFSETY:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_foreign)
m_shape.m_foreign = new ForeignData();
ret = readDoubleData(m_shape.m_foreign->offsetY, reader);
}
break;
case XML_IMGWIDTH:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_foreign)
m_shape.m_foreign = new ForeignData();
ret = readDoubleData(m_shape.m_foreign->width, reader);
}
break;
case XML_IMGHEIGHT:
if (XML_READER_TYPE_ELEMENT == tokenType)
{
if (!m_shape.m_foreign)
m_shape.m_foreign = new ForeignData();
ret = readDoubleData(m_shape.m_foreign->height, reader);
}
break;
default:
break;
}
}
while ((XML_FOREIGN != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|