-
Notifications
You must be signed in to change notification settings - Fork 25
/
structdrawing.go
1531 lines (1433 loc) · 36.1 KB
/
structdrawing.go
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
/*
Copyright (c) 2020 gingfrederik
Copyright (c) 2021 Gonzalo Fernandez-Victorio
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
Copyright (c) 2023 Fumiama Minamoto (源文雨)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package docx
import (
"crypto/md5"
"encoding/hex"
"encoding/xml"
"io"
"strconv"
"strings"
"sync/atomic"
)
//nolint:revive,stylecheck
const (
// A4_EMU_MAX_WIDTH is the max display width of an A4 paper
A4_EMU_MAX_WIDTH = 5274310
)
//nolint:revive,stylecheck
const (
XMLNS_DRAWINGML_MAIN = `http://schemas.openxmlformats.org/drawingml/2006/main`
XMLNS_DRAWINGML_PICTURE = `http://schemas.openxmlformats.org/drawingml/2006/picture`
)
// Drawing element contains photos
type Drawing struct {
XMLName xml.Name `xml:"w:drawing,omitempty"`
Inline *WPInline
Anchor *WPAnchor
file *Docx
}
// UnmarshalXML ...
func (r *Drawing) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "inline":
r.Inline = new(WPInline)
r.Inline.file = r.file
err = d.DecodeElement(r.Inline, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
case "anchor":
r.Anchor = new(WPAnchor)
r.Anchor.file = r.file
err = d.DecodeElement(r.Anchor, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
func (r *Drawing) copymedia(to *Docx) *Drawing {
if r.Inline != nil {
return &Drawing{
Inline: r.Inline.copymedia(to),
file: to,
}
}
if r.Anchor != nil {
return &Drawing{
Anchor: r.Anchor.copymedia(to),
file: to,
}
}
return &Drawing{file: to}
}
// WPInline is an element that represents an inline image within a text paragraph.
//
// It contains information about the image's size and position,
// as well as any non-visual properties associated with the image.
// The <wp:inline> element can contain child elements such as <wp:extent> to specify
// the dimensions of the image and <wp:cNvGraphicFramePr> to specify the non-visual
// properties of the image. Inline images are often used in documents where the images
// are meant to be treated as part of the text flow, such as in a newsletter or a product brochure.
type WPInline struct {
XMLName xml.Name `xml:"wp:inline,omitempty"`
DistT int64 `xml:"distT,attr"`
DistB int64 `xml:"distB,attr"`
DistL int64 `xml:"distL,attr"`
DistR int64 `xml:"distR,attr"`
// AnchorID string `xml:"wp14:anchorId,attr,omitempty"`
// EditID string `xml:"wp14:editId,attr,omitempty"`
Extent *WPExtent
EffectExtent *WPEffectExtent
DocPr *WPDocPr
CNvGraphicFramePr *WPCNvGraphicFramePr
Graphic *AGraphic
file *Docx
}
// UnmarshalXML ...
func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "distT":
r.DistT, err = GetInt64(attr.Value)
case "distB":
r.DistB, err = GetInt64(attr.Value)
case "distL":
r.DistL, err = GetInt64(attr.Value)
case "distR":
r.DistR, err = GetInt64(attr.Value)
default:
// ignore other attributes
}
if err != nil {
return err
}
}
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "extent":
r.Extent = new(WPExtent)
for _, v := range tt.Attr {
switch v.Name.Local {
case "cx":
r.Extent.CX, err = GetInt64(v.Value)
case "cy":
r.Extent.CY, err = GetInt64(v.Value)
}
if err != nil {
return err
}
}
case "effectExtent":
r.EffectExtent = new(WPEffectExtent)
for _, v := range tt.Attr {
switch v.Name.Local {
case "l":
r.EffectExtent.L, err = GetInt64(v.Value)
case "t":
r.EffectExtent.T, err = GetInt64(v.Value)
case "r":
r.EffectExtent.R, err = GetInt64(v.Value)
case "b":
r.EffectExtent.B, err = GetInt64(v.Value)
}
if err != nil {
return err
}
}
case "docPr":
r.DocPr = new(WPDocPr)
err = d.DecodeElement(r.DocPr, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
case "cNvGraphicFramePr":
var value WPCNvGraphicFramePr
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
r.CNvGraphicFramePr = &value
case "graphic":
var value AGraphic
value.file = r.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
r.Graphic = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// String ...
func (r *WPInline) String() string {
sb := strings.Builder{}
if r.Graphic.GraphicData.Pic != nil {
sb.WriteString("![inlnim ")
switch {
case r.DocPr != nil:
sb.WriteString(r.DocPr.Name)
case r.Graphic.GraphicData.Pic.NonVisualPicProperties != nil:
sb.WriteString(r.Graphic.GraphicData.Pic.NonVisualPicProperties.NonVisualDrawingProperties.Name)
default:
sb.WriteString(r.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
}
sb.WriteString("](")
if r.Graphic.GraphicData.Pic.BlipFill != nil {
tgt, err := r.file.ReferTarget(r.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
if err != nil {
sb.WriteString(err.Error())
} else {
h := md5.Sum(r.file.Media(tgt[6:]).Data)
sb.WriteString(hex.EncodeToString(h[:]))
}
}
sb.WriteByte(')')
return sb.String()
}
if r.Graphic.GraphicData.Shape != nil {
sb.WriteString("![inlnsp ")
switch {
case r.DocPr != nil:
sb.WriteString(r.DocPr.Name)
case r.Graphic.GraphicData.Shape.CNvPr != nil:
sb.WriteString(r.Graphic.GraphicData.Shape.CNvPr.Name)
case r.Graphic.GraphicData.Shape.SpPr != nil:
sb.WriteString(r.Graphic.GraphicData.Shape.SpPr.PrstGeom.Prst)
default:
sb.WriteString("nil")
}
sb.WriteString("](")
if r.Graphic.GraphicData.Shape.SpPr != nil {
sb.WriteString(r.Graphic.GraphicData.Shape.SpPr.PrstGeom.Prst)
}
sb.WriteByte(')')
return sb.String()
}
if r.Graphic.GraphicData.Canvas != nil {
sb.WriteString("![inlncv ")
if r.DocPr != nil {
sb.WriteString(r.DocPr.Name)
} else {
sb.WriteString("nil")
}
sb.WriteString("]()")
return sb.String()
}
return "![inln?](unknown)"
}
func (r *WPInline) copymedia(to *Docx) *WPInline {
if r.Graphic.GraphicData.Pic != nil {
if r.Graphic.GraphicData.Pic.BlipFill != nil {
tgt, err := r.file.ReferTarget(r.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
if err != nil {
return nil
}
format := tgt[strings.LastIndex(tgt, ".")+1:]
idn := int(atomic.AddUintptr(&to.docID, 1))
id := int(to.IncreaseID("图片"))
ids := strconv.Itoa(id)
m := r.file.Media(tgt[6:])
if m == nil {
return nil
}
rid := to.addImage(format, m.Data)
inln := *r
grph := *r.Graphic
inln.Graphic = &grph
grphdata := *r.Graphic.GraphicData
grph.GraphicData = &grphdata
pic := *r.Graphic.GraphicData.Pic
grphdata.Pic = &pic
grphdata.file = to
grph.file = to
inln.file = to
inln.DocPr = &WPDocPr{
ID: idn,
Name: "图片 " + ids,
}
pic.NonVisualPicProperties = &PICNonVisualPicProperties{
NonVisualDrawingProperties: NonVisualProperties{
ID: id,
Name: "图片 " + ids,
},
CNvPicPr: r.Graphic.GraphicData.Pic.NonVisualPicProperties.CNvPicPr,
}
pic.BlipFill = &PICBlipFill{
Blip: ABlip{
Embed: rid,
Cstate: r.Graphic.GraphicData.Pic.BlipFill.Blip.Cstate,
},
Stretch: r.Graphic.GraphicData.Pic.BlipFill.Stretch,
}
return &inln
}
return nil
}
if r.Graphic.GraphicData.Shape != nil { // shape has no media
return r
}
if r.Graphic.GraphicData.Canvas != nil { //TODO: copy canvas media
return r
}
return nil
}
// WPExtent represents the extent of a drawing in a Word document.
//
// CX CY 's unit is English Metric Units, which is 1/914400 inch
type WPExtent struct {
XMLName xml.Name `xml:"wp:extent,omitempty"`
CX int64 `xml:"cx,attr"`
CY int64 `xml:"cy,attr"`
}
// UnmarshalXML ...
func (r *WPExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
for _, attr := range start.Attr {
switch attr.Name.Local {
case "cx":
r.CX, err = GetInt64(attr.Value)
case "cy":
r.CY, err = GetInt64(attr.Value)
}
if err != nil {
return err
}
}
// Consume the end element
_, err = d.Token()
return err
}
// WPEffectExtent represents the effect extent of a drawing in a Word document.
type WPEffectExtent struct {
XMLName xml.Name `xml:"wp:effectExtent,omitempty"`
L int64 `xml:"l,attr"`
T int64 `xml:"t,attr"`
R int64 `xml:"r,attr"`
B int64 `xml:"b,attr"`
}
// UnmarshalXML ...
func (r *WPEffectExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
for _, attr := range start.Attr {
switch attr.Name.Local {
case "l":
r.L, err = GetInt64(attr.Value)
case "t":
r.T, err = GetInt64(attr.Value)
case "r":
r.R, err = GetInt64(attr.Value)
case "b":
r.B, err = GetInt64(attr.Value)
}
if err != nil {
return err
}
}
// Consume the end element
_, err = d.Token()
return err
}
// WPDocPr represents the document properties of a drawing in a Word document.
type WPDocPr struct {
XMLName xml.Name `xml:"wp:docPr,omitempty"`
ID int `xml:"id,attr"`
Name string `xml:"name,attr,omitempty"`
}
// UnmarshalXML ...
func (r *WPDocPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "id":
id, err := GetInt(attr.Value)
if err != nil {
return err
}
r.ID = id
case "name":
r.Name = attr.Value
default:
// ignore other attributes
}
}
// Consume the end element
_, err := d.Token()
return err
}
// WPCNvGraphicFramePr represents the non-visual properties of a graphic frame.
type WPCNvGraphicFramePr struct {
XMLName xml.Name `xml:"wp:cNvGraphicFramePr,omitempty"`
Locks AGraphicFrameLocks
}
// UnmarshalXML ...
func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "graphicFrameLocks":
w.Locks.XMLA = getAtt(tt.Attr, "a")
v := getAtt(tt.Attr, "noChangeAspect")
if v == "" {
continue
}
w.Locks.NoChangeAspect, err = GetInt(v)
if err != nil {
return err
}
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// AGraphicFrameLocks represents the locks applied to a graphic frame.
type AGraphicFrameLocks struct {
XMLName xml.Name `xml:"a:graphicFrameLocks,omitempty"`
XMLA string `xml:"xmlns:a,attr,omitempty"`
NoChangeAspect int `xml:"noChangeAspect,attr,omitempty"`
}
// AGraphic represents a graphic in a Word document.
type AGraphic struct {
XMLName xml.Name `xml:"a:graphic,omitempty"`
XMLA string `xml:"xmlns:a,attr,omitempty"`
GraphicData *AGraphicData
file *Docx
}
// UnmarshalXML ...
func (a *AGraphic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "a":
a.XMLA = attr.Value
default:
// ignore other attributes
}
}
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "graphicData":
var value AGraphicData
value.file = a.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
value.URI = getAtt(tt.Attr, "uri")
a.GraphicData = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// AGraphicData represents the data of a graphic in a Word document.
type AGraphicData struct {
XMLName xml.Name `xml:"a:graphicData,omitempty"`
URI string `xml:"uri,attr"`
Pic *Picture
Shape *WordprocessingShape
Canvas *WordprocessingCanvas
Group *WordprocessingGroup
file *Docx
}
// UnmarshalXML ...
func (a *AGraphicData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "uri":
a.URI = attr.Value
default:
// ignore other attributes
}
}
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "pic":
var value Picture
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
value.XMLPIC = getAtt(tt.Attr, "pic")
a.Pic = &value
case "wsp":
var value WordprocessingShape
value.file = a.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
a.Shape = &value
case "wpc":
var value WordprocessingCanvas
value.file = a.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
a.Canvas = &value
case "wgp":
var value WordprocessingGroup
value.file = a.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
a.Group = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// Picture represents a picture in a Word document.
type Picture struct {
XMLName xml.Name `xml:"pic:pic,omitempty"`
XMLPIC string `xml:"xmlns:pic,attr,omitempty"`
NonVisualPicProperties *PICNonVisualPicProperties
BlipFill *PICBlipFill
SpPr *PICSpPr
}
// UnmarshalXML ...
func (p *Picture) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "nvPicPr":
var value PICNonVisualPicProperties
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.NonVisualPicProperties = &value
case "blipFill":
var value PICBlipFill
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.BlipFill = &value
case "spPr":
var value PICSpPr
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.SpPr = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// PICNonVisualPicProperties represents the non-visual properties of a picture in a Word document.
type PICNonVisualPicProperties struct {
XMLName xml.Name `xml:"pic:nvPicPr,omitempty"`
NonVisualDrawingProperties NonVisualProperties `xml:"pic:cNvPr,omitempty"`
CNvPicPr PicCNvPicPr
}
// UnmarshalXML ...
func (p *PICNonVisualPicProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "cNvPr":
v := getAtt(tt.Attr, "id")
if v == "" {
continue
}
p.NonVisualDrawingProperties.ID, err = GetInt(v)
if err != nil {
return err
}
p.NonVisualDrawingProperties.Name = getAtt(tt.Attr, "name")
case "cNvPicPr":
err = d.DecodeElement(&p.CNvPicPr, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// PicCNvPicPr represents the non-visual properties of a picture.
type PicCNvPicPr struct {
XMLName xml.Name `xml:"pic:cNvPicPr,omitempty"`
Locks *APicLocks
}
// UnmarshalXML ...
func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
// Loop through XML tokens
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
// Switch based on token type
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "picLocks":
var value APicLocks
v := getAtt(tt.Attr, "noChangeAspect")
if v == "" {
continue
}
value.NoChangeAspect, err = GetInt(v)
if err != nil {
return err
}
p.Locks = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// APicLocks represents the locks applied to a picture.
type APicLocks struct {
XMLName xml.Name `xml:"a:picLocks,omitempty"`
NoChangeAspect int `xml:"noChangeAspect,attr"`
}
// PICBlipFill represents the blip fill of a picture in a Word document.
type PICBlipFill struct {
XMLName xml.Name `xml:"pic:blipFill,omitempty"`
Blip ABlip
Stretch AStretch
}
// UnmarshalXML ...
func (p *PICBlipFill) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "blip":
err = d.DecodeElement(&p.Blip, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
case "stretch":
err = d.DecodeElement(&p.Stretch, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// ABlip represents the blip of a picture in a Word document.
type ABlip struct {
XMLName xml.Name `xml:"a:blip,omitempty"`
Embed string `xml:"r:embed,attr"`
Cstate string `xml:"cstate,attr,omitempty"`
AlphaModFix *AAlphaModFix
}
// UnmarshalXML ...
func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "embed":
a.Embed = attr.Value
case "cstate":
a.Cstate = attr.Value
default:
// ignore other attributes
}
}
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "alphaModFix":
var value AAlphaModFix
v := getAtt(tt.Attr, "amt")
if v == "" {
continue
}
value.Amount, err = GetInt(v)
if err != nil {
return err
}
a.AlphaModFix = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// AAlphaModFix ...
type AAlphaModFix struct {
XMLName xml.Name `xml:"a:alphaModFix,omitempty"`
Amount int `xml:"amt,attr"`
}
// AStretch ...
type AStretch struct {
XMLName xml.Name `xml:"a:stretch,omitempty"`
FillRect *AFillRect
}
// UnmarshalXML ...
func (s *AStretch) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "fillRect":
var value AFillRect
/*err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}*/
s.FillRect = &value
err = d.Skip() // skip unparsed innerxml
if err != nil {
return err
}
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// AFillRect ...
type AFillRect struct {
XMLName xml.Name `xml:"a:fillRect,omitempty"`
}
// PICSpPr is a struct representing the <pic:spPr> element in OpenXML,
// which describes the shape properties for a picture.
type PICSpPr struct {
XMLName xml.Name `xml:"pic:spPr,omitempty"`
Xfrm AXfrm
PrstGeom *APrstGeom
}
// UnmarshalXML ...
func (p *PICSpPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "xfrm":
err = d.DecodeElement(&p.Xfrm, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
case "prstGeom":
var value APrstGeom
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.PrstGeom = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// AXfrm is a struct representing the <a:xfrm> element in OpenXML,
// which describes the position and size of a shape.
type AXfrm struct {
XMLName xml.Name `xml:"a:xfrm,omitempty"`
Rot int64 `xml:"rot,attr,omitempty"`
FlipH int `xml:"flipH,attr,omitempty"`
FlipV int `xml:"flipV,attr,omitempty"`
Off AOff `xml:"a:off,omitempty"`
Ext AExt `xml:"a:ext,omitempty"`
ChOff *AOff `xml:"a:chOff,omitempty"`
ChExt *AExt `xml:"a:chExt,omitempty"`
}
// UnmarshalXML ...
func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "rot":
a.Rot, err = GetInt64(attr.Value)
case "flipH":
a.FlipH, err = GetInt(attr.Value)
case "flipV":
a.FlipV, err = GetInt(attr.Value)
default:
// ignore other attributes
}
if err != nil {
return err
}
}
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "off":
for _, v := range tt.Attr {
switch v.Name.Local {
case "x":