forked from totravel/minidocx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminidocx.cpp
2723 lines (2325 loc) · 76.2 KB
/
minidocx.cpp
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* minidocx 0.5.0 - C++ library for creating Microsoft Word Document (.docx).
* --------------------------------------------------------
* Copyright (C) 2022-2024, by Xie Zequn ([email protected])
* Report bugs and download new versions at https://github.com/totravel/minidocx
*/
#include "minidocx.hpp"
#include <algorithm>
#include <cstring> // std::strlen(), std::strcmp()
#include <cstdlib> // std::free()
#include <cctype> // std::isspace()
#include "zip.h"
#include "pugixml.hpp"
// Raw string literal R is danger removed Borland not supported him
#define _RELS "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"word/document.xml\"/></Relationships>"
#define DOCUMENT_XML "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:document xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" xmlns:cx=\"http://schemas.microsoft.com/office/drawing/2014/chartex\" xmlns:cx1=\"http://schemas.microsoft.com/office/drawing/2015/9/8/chartex\" xmlns:cx2=\"http://schemas.microsoft.com/office/drawing/2015/10/21/chartex\" xmlns:cx3=\"http://schemas.microsoft.com/office/drawing/2016/5/9/chartex\" xmlns:cx4=\"http://schemas.microsoft.com/office/drawing/2016/5/10/chartex\" xmlns:cx5=\"http://schemas.microsoft.com/office/drawing/2016/5/11/chartex\" xmlns:cx6=\"http://schemas.microsoft.com/office/drawing/2016/5/12/chartex\" xmlns:cx7=\"http://schemas.microsoft.com/office/drawing/2016/5/13/chartex\" xmlns:cx8=\"http://schemas.microsoft.com/office/drawing/2016/5/14/chartex\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:aink=\"http://schemas.microsoft.com/office/drawing/2016/ink\" xmlns:am3d=\"http://schemas.microsoft.com/office/drawing/2017/model3d\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:oel=\"http://schemas.microsoft.com/office/2019/extlst\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\" xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\" xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\" xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\" xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14\"><w:body><w:sectPr><w:pgSz w:w=\"11906\" w:h=\"16838\" /><w:pgMar w:top=\"1440\" w:right=\"1800\" w:bottom=\"1440\" w:left=\"1800\" w:header=\"851\" w:footer=\"992\" w:gutter=\"0\" /><w:cols w:space=\"425\" /><w:docGrid w:type=\"lines\" w:linePitch=\"312\" /></w:sectPr></w:body></w:document>"
#define CONTENT_TYPES_XML "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\" /><Default Extension=\"xml\" ContentType=\"application/xml\" /><Override PartName=\"/word/document.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\" /><Override PartName=\"/word/footer1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml\" /><Override PartName=\"/word/settings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml\" /></Types>"
#define DOCUMENT_XML_RELS "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer\" Target=\"footer1.xml\" /><Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\" /></Relationships>"
#define FOOTER1_XML "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:ftr xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" xmlns:cx=\"http://schemas.microsoft.com/office/drawing/2014/chartex\" xmlns:cx1=\"http://schemas.microsoft.com/office/drawing/2015/9/8/chartex\" xmlns:cx2=\"http://schemas.microsoft.com/office/drawing/2015/10/21/chartex\" xmlns:cx3=\"http://schemas.microsoft.com/office/drawing/2016/5/9/chartex\" xmlns:cx4=\"http://schemas.microsoft.com/office/drawing/2016/5/10/chartex\" xmlns:cx5=\"http://schemas.microsoft.com/office/drawing/2016/5/11/chartex\" xmlns:cx6=\"http://schemas.microsoft.com/office/drawing/2016/5/12/chartex\" xmlns:cx7=\"http://schemas.microsoft.com/office/drawing/2016/5/13/chartex\" xmlns:cx8=\"http://schemas.microsoft.com/office/drawing/2016/5/14/chartex\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:aink=\"http://schemas.microsoft.com/office/drawing/2016/ink\" xmlns:am3d=\"http://schemas.microsoft.com/office/drawing/2017/model3d\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:oel=\"http://schemas.microsoft.com/office/2019/extlst\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\" xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\" xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\" xmlns:w16du=\"http://schemas.microsoft.com/office/word/2023/wordml/word16du\" xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\" xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14\"><w:p><w:pPr><w:jc w:val=\"center\" /></w:pPr><w:r><w:fldChar w:fldCharType=\"begin\" /></w:r><w:r><w:instrText>PAGE \\* MERGEFORMAT</w:instrText></w:r><w:r><w:fldChar w:fldCharType=\"end\" /></w:r></w:p></w:ftr>"
#define SETTINGS_XML "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><w:settings xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\" xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\" xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\" xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\" xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\" xmlns:sl=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh\"></w:settings>"
namespace docx
{
int Pt2Twip(const double pt)
{
return pt * 20;
}
double Twip2Pt(const int twip)
{
return twip / 20.0;
}
double Inch2Pt(const double inch)
{
return inch * 72;
}
double Pt2Inch(const double pt)
{
return pt / 72;
}
double MM2Inch(const int mm)
{
return mm / 25.4;
}
int Inch2MM(const double inch)
{
return inch * 25.4;
}
double CM2Inch(const double cm)
{
return cm / 2.54;
}
double Inch2CM(const double inch)
{
return inch * 2.54;
}
int Inch2Twip(const double inch)
{
return inch * 1440;
}
double Twip2Inch(const int twip)
{
return twip / 1440.0;
}
int MM2Twip(const int mm)
{
return Inch2Twip(MM2Inch(mm));
}
int Twip2MM(const int twip)
{
return Inch2MM(Twip2Inch(twip));
}
int CM2Twip(const double cm)
{
return Inch2Twip(CM2Inch(cm));
}
double Twip2CM(const int twip)
{
return Inch2CM(Twip2Inch(twip));
}
struct xml_string_writer : pugi::xml_writer
{
std::string result;
virtual void write(const void* data, size_t size)
{
result.append(static_cast<const char*>(data), size);
}
};
pugi::xml_node GetLastChild(pugi::xml_node node, const char* name)
{
pugi::xml_node child = node.last_child();
while (!child.empty() && std::strcmp(name, child.name()) != 0) {
child = child.previous_sibling(name);
}
return child;
}
void SetBorders(pugi::xml_node& w_bdrs, const char* elemName, const Box::BorderStyle style, const double width, const char* color)
{
pugi::xml_node w_bdr = w_bdrs.child(elemName);
if (!w_bdr) {
w_bdr = w_bdrs.append_child(elemName);
}
const char* val;
switch (style) {
case Box::BorderStyle::Single:
val = "single";
break;
case Box::BorderStyle::Dotted:
val = "dotted";
break;
case Box::BorderStyle::DotDash:
val = "dotDash";
break;
case Box::BorderStyle::Dashed:
val = "dashed";
break;
case Box::BorderStyle::Double:
val = "double";
break;
case Box::BorderStyle::Wave:
val = "wave";
break;
case Box::BorderStyle::None:
val = "none";
break;
}
pugi::xml_attribute w_val = w_bdr.attribute("w:val");
if (!w_val) {
w_val = w_bdr.append_attribute("w:val");
}
w_val.set_value(val);
pugi::xml_attribute w_sz = w_bdr.attribute("w:sz");
if (!w_sz) {
w_sz = w_bdr.append_attribute("w:sz");
}
w_sz.set_value(width * 8);
pugi::xml_attribute w_color = w_bdr.attribute("w:color");
if (!w_color) {
w_color = w_bdr.append_attribute("w:color");
}
w_color.set_value(color);
}
struct Document::Impl
{
pugi::xml_document doc_;
pugi::xml_node w_body_;
pugi::xml_node w_sectPr_;
pugi::xml_document settings_;
pugi::xml_node w_settings_;
unsigned int nextBookmarkId_;
std::vector<Bookmark> bookmarks_;
};
struct Bookmark::Impl
{
unsigned int id_;
std::string name_;
pugi::xml_node w_bookmarkStart_;
pugi::xml_node w_bookmarkEnd_;
};
struct Paragraph::Impl
{
pugi::xml_node w_body_;
pugi::xml_node w_p_;
pugi::xml_node w_pPr_;
};
struct TextFrame::Impl
{
pugi::xml_node w_framePr_;
};
struct Section::Impl
{
pugi::xml_node w_body_;
pugi::xml_node w_p_; // current paragraph
pugi::xml_node w_pPr_;
pugi::xml_node w_p_last_; // the last paragraph of the section
pugi::xml_node w_pPr_last_;
pugi::xml_node w_sectPr_;
};
struct Table::Impl
{
pugi::xml_node w_body_;
pugi::xml_node w_tbl_;
pugi::xml_node w_tblPr_;
pugi::xml_node w_tblGrid_;
int rows_;
int cols_;
Grid grid_; // logical grid
Impl() : rows_(0), cols_(0) {}
};
struct Run::Impl
{
pugi::xml_node w_p_;
pugi::xml_node w_r_;
pugi::xml_node w_rPr_;
};
struct TableCell::Impl
{
Cell* c_;
pugi::xml_node w_tr_;
pugi::xml_node w_tc_;
pugi::xml_node w_tcPr_;
};
std::ostream& operator<<(std::ostream& out, const Document& doc)
{
if (doc.impl_) {
xml_string_writer writer;
doc.impl_->w_body_.print(writer, " ");
out << writer.result;
}
else {
out << "<document />";
}
return out;
}
std::ostream& operator<<(std::ostream& out, const Paragraph& p)
{
if (p.impl_) {
xml_string_writer writer;
p.impl_->w_p_.print(writer, " ");
out << writer.result;
}
else {
out << "<paragraph />";
}
return out;
}
std::ostream& operator<<(std::ostream& out, const Run& r)
{
if (r.impl_) {
xml_string_writer writer;
r.impl_->w_r_.print(writer, " ");
out << writer.result;
}
else {
out << "<run />";
}
return out;
}
std::ostream& operator<<(std::ostream& out, const Section& s)
{
if (s.impl_) {
xml_string_writer writer;
s.impl_->w_p_.print(writer, " ");
out << writer.result;
}
else {
out << "<section />";
}
return out;
}
// class Bookmark
Bookmark::Bookmark() : impl_(NULL)
{
}
Bookmark::Bookmark(Impl* impl) : impl_(impl)
{
}
Bookmark::Bookmark(const Bookmark& rhs)
{
impl_ = new Impl;
impl_->id_ = rhs.impl_->id_;
impl_->name_ = rhs.impl_->name_;
impl_->w_bookmarkStart_ = rhs.impl_->w_bookmarkStart_;
impl_->w_bookmarkEnd_ = rhs.impl_->w_bookmarkEnd_;
}
Bookmark::~Bookmark()
{
if (impl_ != NULL) {
delete impl_;
impl_ = NULL;
}
}
bool Bookmark::operator==(const Bookmark& rhs)
{
if (!impl_ && !rhs.impl_) return true;
if (impl_ && rhs.impl_) return impl_->id_ == rhs.impl_->id_;
return false;
}
unsigned int Bookmark::GetId() const
{
return impl_->id_;
}
std::string Bookmark::GetName() const
{
return impl_->name_;
}
// class Document
Document::Document()
{
impl_ = new Impl;
impl_->doc_.load_buffer(DOCUMENT_XML, std::strlen(DOCUMENT_XML), pugi::parse_declaration);
impl_->w_body_ = impl_->doc_.child("w:document").child("w:body");
impl_->w_sectPr_ = impl_->w_body_.child("w:sectPr");
impl_->settings_.load_buffer(SETTINGS_XML, std::strlen(SETTINGS_XML), pugi::parse_declaration);
impl_->w_settings_ = impl_->settings_.child("w:settings");
impl_->nextBookmarkId_ = 0;
}
Document::~Document()
{
if (impl_ != NULL) {
delete impl_;
impl_ = NULL;
}
}
bool Document::Save(const std::string& path)
{
if (!impl_) return false;
struct zip_t* zip = zip_open(path.c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
if (zip == NULL) {
return false;
}
xml_string_writer writer;
const char* buf = NULL;
zip_entry_open(zip, "_rels/.rels");
zip_entry_write(zip, _RELS, std::strlen(_RELS));
zip_entry_close(zip);
zip_entry_open(zip, "word/document.xml");
impl_->doc_.save(writer, "", pugi::format_raw);
buf = writer.result.c_str();
zip_entry_write(zip, buf, std::strlen(buf));
zip_entry_close(zip);
zip_entry_open(zip, "word/settings.xml");
writer.result = "";
impl_->settings_.save(writer, "", pugi::format_raw);
buf = writer.result.c_str();
zip_entry_write(zip, buf, std::strlen(buf));
zip_entry_close(zip);
zip_entry_open(zip, "word/_rels/document.xml.rels");
zip_entry_write(zip, DOCUMENT_XML_RELS, std::strlen(DOCUMENT_XML_RELS));
zip_entry_close(zip);
zip_entry_open(zip, "word/footer1.xml");
zip_entry_write(zip, FOOTER1_XML, std::strlen(FOOTER1_XML));
zip_entry_close(zip);
zip_entry_open(zip, "[Content_Types].xml");
zip_entry_write(zip, CONTENT_TYPES_XML, std::strlen(CONTENT_TYPES_XML));
zip_entry_close(zip);
zip_close(zip);
return true;
}
bool Document::Open(const std::string& path)
{
if (!impl_) return false;
struct zip_t* zip = zip_open(path.c_str(), ZIP_DEFAULT_COMPRESSION_LEVEL, 'r');
if (zip == NULL) {
return false;
}
if (zip_entry_open(zip, "word/document.xml") < 0) {
zip_close(zip);
return false;
}
void* buf = NULL;
size_t bufsize;
zip_entry_read(zip, &buf, &bufsize);
zip_entry_close(zip);
impl_->doc_.load_buffer(buf, bufsize, pugi::parse_declaration);
impl_->w_body_ = impl_->doc_.child("w:document").child("w:body");
impl_->w_sectPr_ = impl_->w_body_.child("w:sectPr");
std::free(buf);
if (zip_entry_open(zip, "word/settings.xml") >= 0) {
zip_entry_read(zip, &buf, &bufsize);
zip_entry_close(zip);
impl_->settings_.load_buffer(buf, bufsize, pugi::parse_declaration);
impl_->w_settings_ = impl_->settings_.child("w:settings");
std::free(buf);
}
zip_close(zip);
FindBookmarks();
return true;
}
Paragraph Document::FirstParagraph()
{
if (!impl_) return Paragraph();
pugi::xml_node w_p = impl_->w_body_.child("w:p");
if (!w_p) return Paragraph();
Paragraph::Impl* impl = new Paragraph::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_p_ = w_p;
impl->w_pPr_ = w_p.child("w:pPr");
return Paragraph(impl);
}
Paragraph Document::LastParagraph()
{
if (!impl_) return Paragraph();
pugi::xml_node w_p = GetLastChild(impl_->w_body_, "w:p");
if (!w_p) return Paragraph();
Paragraph::Impl* impl = new Paragraph::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_p_ = w_p;
impl->w_pPr_ = w_p.child("w:pPr");
return Paragraph(impl);
}
Section Document::FirstSection()
{
Paragraph firstParagraph = FirstParagraph();
if (firstParagraph) return firstParagraph.GetSection();
Section::Impl* impl = new Section::Impl;
impl->w_body_ = impl_->w_body_;
Section section(impl);
section.FindSectionProperties();
return section;
}
Section Document::LastSection()
{
Paragraph lastParagraph = LastParagraph();
if (lastParagraph) return lastParagraph.GetSection();
Section::Impl* impl = new Section::Impl;
impl->w_body_ = impl_->w_body_;
Section section(impl);
section.FindSectionProperties();
return section;
}
Paragraph Document::AppendParagraph()
{
if (!impl_) return Paragraph();
pugi::xml_node w_p = impl_->w_body_.insert_child_before("w:p", impl_->w_sectPr_);
pugi::xml_node w_pPr = w_p.append_child("w:pPr");
Paragraph::Impl* impl = new Paragraph::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_p_ = w_p;
impl->w_pPr_ = w_pPr;
return Paragraph(impl);
}
Paragraph Document::AppendParagraph(const std::string& text)
{
Paragraph p = AppendParagraph();
p.AppendRun(text);
return p;
}
Paragraph Document::AppendParagraph(const std::string& text,
const double fontSize)
{
Paragraph p = AppendParagraph();
p.AppendRun(text, fontSize);
return p;
}
Paragraph Document::AppendParagraph(const std::string& text,
const double fontSize,
const std::string& fontAscii,
const std::string& fontEastAsia)
{
Paragraph p = AppendParagraph();
p.AppendRun(text, fontSize, fontAscii, fontEastAsia);
return p;
}
Paragraph Document::PrependParagraph()
{
if (!impl_) return Paragraph();
pugi::xml_node w_p = impl_->w_body_.prepend_child("w:p");
pugi::xml_node w_pPr = w_p.append_child("w:pPr");
Paragraph::Impl* impl = new Paragraph::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_p_ = w_p;
impl->w_pPr_ = w_pPr;
return Paragraph(impl);
}
Paragraph Document::PrependParagraph(const std::string& text)
{
Paragraph p = PrependParagraph();
p.AppendRun(text);
return p;
}
Paragraph Document::PrependParagraph(const std::string& text,
const double fontSize)
{
Paragraph p = PrependParagraph();
p.AppendRun(text, fontSize);
return p;
}
Paragraph Document::PrependParagraph(const std::string& text,
const double fontSize,
const std::string& fontAscii,
const std::string& fontEastAsia)
{
Paragraph p = PrependParagraph();
p.AppendRun(text, fontSize, fontAscii, fontEastAsia);
return p;
}
Paragraph Document::InsertParagraphBefore(const Paragraph& p)
{
if (!impl_) return Paragraph();
pugi::xml_node w_p = impl_->w_body_.insert_child_before("w:p", p.impl_->w_p_);
pugi::xml_node w_pPr = w_p.append_child("w:pPr");
Paragraph::Impl* impl = new Paragraph::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_p_ = w_p;
impl->w_pPr_ = w_pPr;
return Paragraph(impl);
}
Paragraph Document::InsertParagraphAfter(const Paragraph& p)
{
if (!impl_) return Paragraph();
pugi::xml_node w_p = impl_->w_body_.insert_child_after("w:p", p.impl_->w_p_);
pugi::xml_node w_pPr = w_p.append_child("w:pPr");
Paragraph::Impl* impl = new Paragraph::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_p_ = w_p;
impl->w_pPr_ = w_pPr;
return Paragraph(impl);
}
bool Document::RemoveParagraph(Paragraph& p)
{
if (!impl_) return false;
return impl_->w_body_.remove_child(p.impl_->w_p_);
}
Paragraph Document::AppendPageBreak()
{
Paragraph p = AppendParagraph();
p.AppendPageBreak();
return p;
}
Paragraph Document::AppendSectionBreak()
{
Paragraph p = AppendParagraph();
p.InsertSectionBreak();
return p;
}
Table Document::AppendTable(const int rows, const int cols)
{
if (!impl_) return Table();
pugi::xml_node w_tbl = impl_->w_body_.insert_child_before("w:tbl", impl_->w_sectPr_);
pugi::xml_node w_tblPr = w_tbl.append_child("w:tblPr");
pugi::xml_node w_tblGrid = w_tbl.append_child("w:tblGrid");
Table::Impl* impl = new Table::Impl;
impl->w_body_ = impl_->w_body_;
impl->w_tbl_ = w_tbl;
impl->w_tblPr_ = w_tblPr;
impl->w_tblGrid_ = w_tblGrid;
Table tbl(impl);
tbl.Create_(rows, cols);
tbl.SetAllBorders();
tbl.SetWidthPercent(100);
tbl.SetCellMarginLeft(CM2Twip(0.19));
tbl.SetCellMarginRight(CM2Twip(0.19));
tbl.SetAlignment(Table::Alignment::Centered);
return tbl;
}
void Document::RemoveTable(Table& tbl)
{
if (!impl_) return;
impl_->w_body_.remove_child(tbl.impl_->w_tbl_);
}
TextFrame Document::AppendTextFrame(const int w, const int h)
{
if (!impl_) return TextFrame();
pugi::xml_node w_p = impl_->w_body_.insert_child_before("w:p", impl_->w_sectPr_);
pugi::xml_node w_pPr = w_p.append_child("w:pPr");
pugi::xml_node w_framePr = w_pPr.append_child("w:framePr");
Paragraph::Impl* paragraph_impl = new Paragraph::Impl;
paragraph_impl->w_body_ = impl_->w_body_;
paragraph_impl->w_p_ = w_p;
paragraph_impl->w_pPr_ = w_pPr;
TextFrame::Impl* impl = new TextFrame::Impl;
impl->w_framePr_ = w_framePr;
TextFrame textFrame = TextFrame(impl, paragraph_impl);
textFrame.SetSize(w, h);
textFrame.SetBorders();
return textFrame;
}
void Document::SetReadOnly(const bool enabled)
{
if (!impl_) return;
if (!enabled) {
pugi::xml_node documentProtection = impl_->w_settings_.child("w:documentProtection");
if (documentProtection) {
impl_->w_settings_.remove_child(documentProtection);
}
return;
}
pugi::xml_node documentProtection = impl_->w_settings_.child("w:documentProtection");
if (!documentProtection) {
documentProtection = impl_->w_settings_.append_child("w:documentProtection");
}
pugi::xml_attribute documentProtectionEdit = documentProtection.attribute("w:edit");
if (!documentProtectionEdit) {
documentProtectionEdit = documentProtection.append_attribute("w:edit");
}
documentProtectionEdit.set_value("readOnly");
pugi::xml_attribute documentProtectionEnforcement = documentProtection.attribute("w:enforcement");
if (!documentProtectionEnforcement) {
documentProtectionEnforcement = documentProtection.append_attribute("w:enforcement");
}
documentProtectionEnforcement.set_value("1");
}
std::map<std::string, std::string> Document::GetVars()
{
std::map<std::string, std::string> vars;
if (!impl_) return vars;
pugi::xml_node docVars = impl_->w_settings_.child("w:docVars");
if (!docVars) {
return vars;
}
for (pugi::xml_node p = docVars.child("w:docVar"); p; p = p.next_sibling("w:docVar")) {
vars.insert(std::pair<std::string, std::string>(p.attribute("w:name").value(), p.attribute("w:val").value()));
}
return vars;
}
void Document::SetVars(const std::map<std::string, std::string>& vars)
{
if (!impl_) return;
pugi::xml_node docVars = impl_->w_settings_.child("w:docVars");
if (!docVars) {
docVars = impl_->w_settings_.append_child("w:docVars");
}
else {
docVars.remove_children();
}
std::map<std::string, std::string>::const_iterator it;
for (it = vars.begin(); it != vars.end(); it++) {
pugi::xml_node docVar = docVars.append_child("w:docVar");
docVar.append_attribute("w:name") = it->first.c_str();
docVar.append_attribute("w:val") = it->second.c_str();
}
}
void Document::AddVars(const std::map<std::string, std::string>& vars)
{
if (!impl_) return;
pugi::xml_node docVars = impl_->w_settings_.child("w:docVars");
if (!docVars) {
docVars = impl_->w_settings_.append_child("w:docVars");
}
std::map<std::string, std::string>::const_iterator it;
for (it = vars.begin(); it != vars.end(); it++) {
pugi::xml_node docVar = docVars.append_child("w:docVar");
docVar.append_attribute("w:name") = it->first.c_str();
docVar.append_attribute("w:val") = it->second.c_str();
}
}
void Document::FindBookmarks()
{
if (!impl_) return;
unsigned int maxBookmarkId = 0;
for (pugi::xml_node i = impl_->w_body_.first_child(); i; i = i.next_sibling()) {
if (std::strcmp(i.name(), "w:p") != 0) continue;
for (pugi::xml_node j = i.first_child(); j; j = j.next_sibling()) {
if (std::strcmp(j.name(), "w:bookmarkStart") == 0) {
Bookmark::Impl* impl = new Bookmark::Impl;
impl->id_ = j.attribute("w:id").as_uint();
impl->name_ = j.attribute("w:name").as_string();
impl->w_bookmarkStart_ = j;
if (maxBookmarkId < impl->id_)
maxBookmarkId = impl->id_;
impl_->bookmarks_.push_back(impl);
}
else if (std::strcmp(j.name(), "w:bookmarkEnd") == 0) {
const unsigned int id = j.attribute("w:id").as_uint();
for (std::vector<Bookmark>::iterator it = impl_->bookmarks_.begin(); it != impl_->bookmarks_.end(); ++it) {
if ((*it).GetId() == id) {
it->impl_->w_bookmarkEnd_ = j;
break;
}
}
}
else {
continue;
}
}
}
if (impl_->bookmarks_.size() > 0)
impl_->nextBookmarkId_ = maxBookmarkId++;
}
std::vector<Bookmark> Document::GetBookmarks()
{
if (!impl_) return std::vector<Bookmark>();
return impl_->bookmarks_;
}
Bookmark Document::AddBookmark(const std::string& name, const Run& start, const Run& end)
{
if (!impl_) return Bookmark();
Bookmark::Impl* impl = new Bookmark::Impl;
impl->id_ = impl_->nextBookmarkId_++;
impl->name_ = name;
pugi::xml_node w_bookmarkStart = start.impl_->w_p_.insert_child_before("w:bookmarkStart", start.impl_->w_r_);
w_bookmarkStart.append_attribute("w:id") = impl->id_;
w_bookmarkStart.append_attribute("w:name") = impl->name_.c_str();
pugi::xml_node w_bookmarkEnd = end.impl_->w_p_.insert_child_after("w:bookmarkEnd", end.impl_->w_r_);
w_bookmarkEnd.append_attribute("w:id") = impl->id_;
impl->w_bookmarkStart_ = w_bookmarkStart;
impl->w_bookmarkStart_ = w_bookmarkEnd;
Bookmark b(impl);
impl_->bookmarks_.push_back(b);
return b;
}
void Document::RemoveBookmark(Bookmark& bookmark)
{
if (!impl_) return;
std::vector<Bookmark>::iterator it = std::find(
impl_->bookmarks_.begin(), impl_->bookmarks_.end(), bookmark);
if (it != impl_->bookmarks_.end())
impl_->bookmarks_.erase(it);
bookmark.impl_->w_bookmarkStart_.parent().remove_child(bookmark.impl_->w_bookmarkStart_);
bookmark.impl_->w_bookmarkEnd_.parent().remove_child(bookmark.impl_->w_bookmarkEnd_);
}
// class Paragraph
Paragraph::Paragraph() : impl_(NULL)
{
}
Paragraph::Paragraph(Impl* impl) : impl_(impl)
{
}
Paragraph::Paragraph(const Paragraph& p)
{
impl_ = new Impl;
impl_->w_body_ = p.impl_->w_body_;
impl_->w_p_ = p.impl_->w_p_;
impl_->w_pPr_ = p.impl_->w_pPr_;
}
Paragraph::~Paragraph()
{
if (impl_ != NULL) {
delete impl_;
impl_ = NULL;
}
}
Run Paragraph::FirstRun()
{
if (!impl_) return Run();
pugi::xml_node w_r = impl_->w_p_.child("w:r");
if (!w_r) return Run();
Run::Impl* impl = new Run::Impl;
impl->w_p_ = impl_->w_p_;
impl->w_r_ = w_r;
impl->w_rPr_ = w_r.child("w:rPr");
return Run(impl);
}
Run Paragraph::AppendRun()
{
if (!impl_) return Run();
pugi::xml_node w_r = impl_->w_p_.append_child("w:r");
pugi::xml_node w_rPr = w_r.append_child("w:rPr");
Run::Impl* impl = new Run::Impl;
impl->w_p_ = impl_->w_p_;
impl->w_r_ = w_r;
impl->w_rPr_ = w_rPr;
return Run(impl);
}
Run Paragraph::AppendRun(const std::string& text)
{
Run r = AppendRun();
if (!text.empty()) {
r.AppendText(text);
}
return r;
}
Run Paragraph::AppendRun(const std::string& text, const double fontSize)
{
Run r = AppendRun(text);
if (fontSize != 0) {
r.SetFontSize(fontSize);
}
return r;
}
Run Paragraph::AppendRun(const std::string& text, const double fontSize, const std::string& fontAscii, const std::string& fontEastAsia)
{
Run r = AppendRun(text, fontSize);
if (!fontAscii.empty()) {
r.SetFont(fontAscii, fontEastAsia);
}
return r;
}
Run Paragraph::AppendPageBreak()
{
if (!impl_) return Run();
pugi::xml_node w_r = impl_->w_p_.append_child("w:r");
pugi::xml_node w_br = w_r.append_child("w:br");
w_br.append_attribute("w:type") = "page";
Run::Impl* impl = new Run::Impl;
impl->w_p_ = impl_->w_p_;
impl->w_r_ = w_r;
impl->w_rPr_ = w_br;
return Run(impl);
}
void Paragraph::SetAlignment(const Alignment alignment)
{
if (!impl_) return;
const char* val;
switch (alignment) {
case Alignment::Left:
val = "start";
break;
case Alignment::Right:
val = "end";
break;
case Alignment::Centered:
val = "center";
break;
case Alignment::Justified:
val = "both";
break;
case Alignment::Distributed:
val = "distribute";
break;
}
pugi::xml_node jc = impl_->w_pPr_.child("w:jc");
if (!jc) {
jc = impl_->w_pPr_.append_child("w:jc");
}
pugi::xml_attribute jcVal = jc.attribute("w:val");
if (!jcVal) {
jcVal = jc.append_attribute("w:val");
}
jcVal.set_value(val);
}
void Paragraph::SetLineSpacingSingle()
{
SetLineSpacing(240, "auto");
}
void Paragraph::SetLineSpacingLines(const double at)
{
// A normal single-spaced paragaph has a w:line value of 240, or 12 points.
//
// If the value of lineRule is auto, then the value of line
// is interpreted as 240th of a line, e.g. 360 = 1.5 lines.
SetLineSpacing(at * 240, "auto");
}
void Paragraph::SetLineSpacingAtLeast(const int at)
{
// If the value of the lineRule attribute is atLeast or exactly,
// then the value of the line attribute is interpreted as 240th of a point.
// (Not really. Actually, values are in twentieths of a point, e.g. 240 = 12 pt.)
SetLineSpacing(at, "atLeast");
}
void Paragraph::SetLineSpacingExactly(const int at)
{
SetLineSpacing(at, "exact");
}
void Paragraph::SetLineSpacing(const int at, const char* lineRule)
{
if (!impl_) return;
pugi::xml_node spacing = impl_->w_pPr_.child("w:spacing");