Skip to content

Commit 64b3464

Browse files
committed
优化完善 table canvas
1 parent fa6f03a commit 64b3464

17 files changed

+806
-156
lines changed

apidrawing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (p *Paragraph) AddInlineDrawing(pic []byte) (*Run, error) {
6969
XMLA: XMLNS_DRAWINGML_MAIN,
7070
GraphicData: &AGraphicData{
7171
URI: XMLNS_PICTURE,
72-
Pic: &PICPic{
72+
Pic: &Picture{
7373
XMLPIC: XMLNS_DRAWINGML_PICTURE,
7474
NonVisualPicProperties: &PICNonVisualPicProperties{
7575
NonVisualDrawingProperties: PICNonVisualDrawingProperties{
@@ -179,7 +179,7 @@ func (p *Paragraph) AddAnchorDrawing(pic []byte) (*Run, error) {
179179
XMLA: XMLNS_DRAWINGML_MAIN,
180180
GraphicData: &AGraphicData{
181181
URI: XMLNS_PICTURE,
182-
Pic: &PICPic{
182+
Pic: &Picture{
183183
XMLPIC: XMLNS_DRAWINGML_PICTURE,
184184
NonVisualPicProperties: &PICNonVisualPicProperties{
185185
NonVisualDrawingProperties: PICNonVisualDrawingProperties{

apishape.go

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,87 @@
1+
/*
2+
Copyright (c) 2020 gingfrederik
3+
Copyright (c) 2021 Gonzalo Fernandez-Victorio
4+
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
5+
Copyright (c) 2023 Fumiama Minamoto (源文雨)
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License as published
9+
by the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Affero General Public License for more details.
16+
17+
You should have received a copy of the GNU Affero General Public License
18+
along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
121
package docx
222

323
import (
424
"strconv"
525
"sync/atomic"
626
)
727

8-
// AddShape adds wsp named drawing to paragraph
9-
func (p *Paragraph) AddShape(w, h int64, name, bwMode, prst string, elems []interface{}) (*Run, error) {
28+
// AddInlineShape adds wsp named drawing to paragraph
29+
func (p *Paragraph) AddInlineShape(w, h int64, name, bwMode, prst string, ln *ALine) (*Run, error) {
30+
idn := int(atomic.AddUintptr(&p.file.docID, 1))
31+
id := strconv.Itoa(int(p.file.IncreaseID(name)))
32+
d := &Drawing{
33+
Inline: &WPInline{
34+
Extent: &WPExtent{
35+
CX: w,
36+
CY: h,
37+
},
38+
EffectExtent: &WPEffectExtent{},
39+
DocPr: &WPDocPr{
40+
ID: idn,
41+
Name: name + " " + id,
42+
},
43+
CNvGraphicFramePr: &WPCNvGraphicFramePr{},
44+
Graphic: &AGraphic{
45+
XMLA: XMLNS_DRAWINGML_MAIN,
46+
GraphicData: &AGraphicData{
47+
URI: XMLNS_WPS,
48+
Shape: &WordprocessingShape{
49+
CNvCnPr: &WPSCNvCnPr{
50+
ConnShapeLocks: &struct{}{},
51+
},
52+
SpPr: &WPSSpPr{
53+
BWMode: bwMode,
54+
55+
Xfrm: AXfrm{
56+
Ext: AExt{
57+
CX: w,
58+
CY: h,
59+
},
60+
},
61+
PrstGeom: APrstGeom{
62+
Prst: prst,
63+
},
64+
NoFill: &struct{}{},
65+
Line: ln,
66+
},
67+
BodyPr: &WPSBodyPr{},
68+
},
69+
},
70+
},
71+
},
72+
}
73+
c := make([]interface{}, 1, 64)
74+
c[0] = d
75+
run := &Run{
76+
RunProperties: &RunProperties{},
77+
Children: c,
78+
}
79+
p.Children = append(p.Children, run)
80+
return run, nil
81+
}
82+
83+
// AddAnchorShape adds wsp named drawing to paragraph
84+
func (p *Paragraph) AddAnchorShape(w, h int64, name, bwMode, prst string, ln *ALine) (*Run, error) {
1085
idn := int(atomic.AddUintptr(&p.file.docID, 1))
1186
id := strconv.Itoa(int(p.file.IncreaseID(name)))
1287
d := &Drawing{
@@ -37,7 +112,7 @@ func (p *Paragraph) AddShape(w, h int64, name, bwMode, prst string, elems []inte
37112
XMLA: XMLNS_DRAWINGML_MAIN,
38113
GraphicData: &AGraphicData{
39114
URI: XMLNS_WPS,
40-
Shape: &WPSWordprocessingShape{
115+
Shape: &WordprocessingShape{
41116
CNvCnPr: &WPSCNvCnPr{
42117
ConnShapeLocks: &struct{}{},
43118
},
@@ -54,7 +129,7 @@ func (p *Paragraph) AddShape(w, h int64, name, bwMode, prst string, elems []inte
54129
Prst: prst,
55130
},
56131
NoFill: &struct{}{},
57-
Elems: elems,
132+
Line: ln,
58133
},
59134
BodyPr: &WPSBodyPr{},
60135
},

cmd/main/main.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,24 @@ func main() {
125125

126126
p := w.AddParagraph().Justification("center")
127127
p.AddText("测试 AutoShape w:ln").Size("44")
128-
p.AddShape(808355, 238760, "AutoShape", "auto", "straightConnector1", []interface{}{
128+
p.AddAnchorShape(808355, 238760, "AutoShape", "auto", "straightConnector1",
129129
&docx.ALine{
130130
W: 9525,
131131
SolidFill: &docx.ASolidFill{SrgbClr: &docx.ASrgbClr{Val: "000000"}},
132132
Round: &struct{}{},
133133
HeadEnd: &docx.AHeadEnd{},
134134
TailEnd: &docx.ATailEnd{},
135135
},
136-
})
136+
)
137+
p.AddInlineShape(808355, 238760, "AutoShape", "auto", "straightConnector1",
138+
&docx.ALine{
139+
W: 9525,
140+
SolidFill: &docx.ASolidFill{SrgbClr: &docx.ASrgbClr{Val: "000000"}},
141+
Round: &struct{}{},
142+
HeadEnd: &docx.AHeadEnd{},
143+
TailEnd: &docx.ATailEnd{},
144+
},
145+
)
137146

138147
f, err := os.Create(*fileLocation)
139148
if err != nil {
@@ -193,7 +202,7 @@ func main() {
193202
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 {
194203
fmt.Printf("<%d> %v\t", y, &c.Paragraphs[0])
195204
} else {
196-
fmt.Print("\t")
205+
fmt.Printf("<%d> \t\t", y)
197206
}
198207
}
199208
fmt.Print("\n")

empty.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,11 @@ func newEmptyA4File() *Docx {
6060
Type: `http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable`,
6161
Target: "fontTable.xml",
6262
},
63-
{
64-
ID: "rId4",
65-
Type: `http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings`,
66-
Target: "settings.xml",
67-
},
68-
{
69-
ID: "rId5",
70-
Type: `http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings`,
71-
Target: "webSettings.xml",
72-
},
7363
},
7464
},
7565
media: make([]Media, 0, 64),
7666
mediaNameIdx: make(map[string]int, 64),
77-
rID: 5,
67+
rID: 3,
7868
slowIDs: make(map[string]uintptr, 64),
7969
template: "a4",
8070
tmpfslst: []string{
@@ -83,9 +73,7 @@ func newEmptyA4File() *Docx {
8373
"docProps/core.xml",
8474
"word/theme/theme1.xml",
8575
"word/fontTable.xml",
86-
"word/settings.xml",
8776
"word/styles.xml",
88-
"word/webSettings.xml",
8977
"[Content_Types].xml",
9078
},
9179
buf: bytes.NewBuffer(make([]byte, 0, 1024*1024)),

id.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
Copyright (c) 2020 gingfrederik
3+
Copyright (c) 2021 Gonzalo Fernandez-Victorio
4+
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
5+
Copyright (c) 2023 Fumiama Minamoto (源文雨)
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License as published
9+
by the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Affero General Public License for more details.
16+
17+
You should have received a copy of the GNU Affero General Public License
18+
along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
121
package docx
222

323
func (f *Docx) IncreaseID(name string) (n uintptr) {

structcanvas.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
Copyright (c) 2020 gingfrederik
3+
Copyright (c) 2021 Gonzalo Fernandez-Victorio
4+
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
5+
Copyright (c) 2023 Fumiama Minamoto (源文雨)
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License as published
9+
by the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Affero General Public License for more details.
16+
17+
You should have received a copy of the GNU Affero General Public License
18+
along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
121
package docx
222

323
import (
@@ -12,6 +32,8 @@ type WordprocessingCanvas struct {
1232
Whole *WPCWhole
1333

1434
Items []interface{}
35+
36+
file *Docx
1537
}
1638

1739
// UnmarshalXML ...
@@ -40,7 +62,8 @@ func (c *WordprocessingCanvas) UnmarshalXML(d *xml.Decoder, start xml.StartEleme
4062
return err
4163
}
4264
case "wsp":
43-
var value WPSWordprocessingShape
65+
var value WordprocessingShape
66+
value.file = c.file
4467
err = d.DecodeElement(&value, &tt)
4568
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
4669
return err

0 commit comments

Comments
 (0)