mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-12 19:58:36 +08:00
add: anchor drawing & some attrs
This commit is contained in:
117
apidrawing.go
117
apidrawing.go
@@ -15,9 +15,10 @@ func (p *Paragraph) AddInlineDrawing(pic []byte) (*Run, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := strconv.Itoa(int(atomic.AddUintptr(&p.file.imageId, 1)))
|
||||
idn := int(atomic.AddUintptr(&p.file.imageId, 1))
|
||||
id := strconv.Itoa(idn)
|
||||
rId := p.file.addImage(Media{Name: "image" + id + "." + format, Data: pic})
|
||||
w, h := sz.Width, sz.Height
|
||||
w, h := int64(sz.Width), int64(sz.Height)
|
||||
if float64(w)/float64(h) > 1.2 {
|
||||
h = A4_EMU_MAX_WIDTH * h / w
|
||||
w = A4_EMU_MAX_WIDTH
|
||||
@@ -36,7 +37,7 @@ func (p *Paragraph) AddInlineDrawing(pic []byte) (*Run, error) {
|
||||
},
|
||||
EffectExtent: &WPEffectExtent{},
|
||||
DocPr: &WPDocPr{
|
||||
ID: id,
|
||||
ID: idn,
|
||||
Name: "图片 " + id,
|
||||
},
|
||||
CNvGraphicFramePr: &WPCNvGraphicFramePr{
|
||||
@@ -96,7 +97,7 @@ func (p *Paragraph) AddInlineDrawingFrom(file string) (*Run, error) {
|
||||
}
|
||||
|
||||
// Size of the inline drawing by EMU
|
||||
func (in *WPInline) Size(w, h int) {
|
||||
func (in *WPInline) Size(w, h int64) {
|
||||
if in.Extent != nil {
|
||||
in.Extent.CX = w
|
||||
in.Extent.CY = h
|
||||
@@ -106,3 +107,111 @@ func (in *WPInline) Size(w, h int) {
|
||||
in.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
|
||||
}
|
||||
}
|
||||
|
||||
// AddAnchorDrawing adds inline drawing to paragraph
|
||||
func (p *Paragraph) AddAnchorDrawing(pic []byte) (*Run, error) {
|
||||
sz, format, err := imgsz.DecodeSize(bytes.NewReader(pic))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
idn := int(atomic.AddUintptr(&p.file.imageId, 1))
|
||||
id := strconv.Itoa(idn)
|
||||
rId := p.file.addImage(Media{Name: "image" + id + "." + format, Data: pic})
|
||||
w, h := int64(sz.Width), int64(sz.Height)
|
||||
if float64(w)/float64(h) > 1.2 {
|
||||
h = A4_EMU_MAX_WIDTH * h / w
|
||||
w = A4_EMU_MAX_WIDTH
|
||||
} else {
|
||||
h = A4_EMU_MAX_WIDTH * h / w / 2
|
||||
w = A4_EMU_MAX_WIDTH / 2
|
||||
}
|
||||
d := &Drawing{
|
||||
Anchor: &WPAnchor{
|
||||
LayoutInCell: 1,
|
||||
AllowOverlap: 1,
|
||||
|
||||
SimplePosXY: &WPSimplePos{},
|
||||
PositionH: &WPPositionH{
|
||||
RelativeFrom: "column",
|
||||
},
|
||||
PositionV: &WPPositionV{
|
||||
RelativeFrom: "paragraph",
|
||||
},
|
||||
|
||||
Extent: &WPExtent{
|
||||
CX: w,
|
||||
CY: h,
|
||||
},
|
||||
EffectExtent: &WPEffectExtent{},
|
||||
WrapNone: &struct{}{},
|
||||
DocPr: &WPDocPr{
|
||||
ID: idn,
|
||||
Name: "图片 " + id,
|
||||
},
|
||||
CNvGraphicFramePr: &WPCNvGraphicFramePr{
|
||||
Locks: &AGraphicFrameLocks{
|
||||
NoChangeAspect: 1,
|
||||
},
|
||||
},
|
||||
Graphic: &AGraphic{
|
||||
XMLA: XMLNS_DRAWINGML_MAIN,
|
||||
GraphicData: &AGraphicData{
|
||||
URI: XMLNS_PICTURE,
|
||||
Pic: &PICPic{
|
||||
XMLPIC: XMLNS_DRAWINGML_PICTURE,
|
||||
NonVisualPicProperties: &PICNonVisualPicProperties{
|
||||
NonVisualDrawingProperties: PICNonVisualDrawingProperties{
|
||||
ID: id,
|
||||
Name: "图片 " + id,
|
||||
},
|
||||
},
|
||||
BlipFill: &PICBlipFill{
|
||||
Blip: ABlip{
|
||||
Embed: rId,
|
||||
Cstate: "print",
|
||||
},
|
||||
},
|
||||
SpPr: &PICSpPr{
|
||||
Xfrm: AXfrm{
|
||||
Ext: AExt{
|
||||
CX: w,
|
||||
CY: h,
|
||||
},
|
||||
},
|
||||
PrstGeom: APrstGeom{
|
||||
Prst: "rect",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
run := &Run{
|
||||
Drawing: d,
|
||||
RunProperties: &RunProperties{},
|
||||
}
|
||||
p.Children = append(p.Children, ParagraphChild{Run: run})
|
||||
return run, nil
|
||||
}
|
||||
|
||||
// AddInlineDrawingFrom adds drawing from file to paragraph
|
||||
func (p *Paragraph) AddAnchorDrawingFrom(file string) (*Run, error) {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.AddAnchorDrawing(data)
|
||||
}
|
||||
|
||||
// Size of the anchor drawing by EMU
|
||||
func (a *WPAnchor) Size(w, h int64) {
|
||||
if a.Extent != nil {
|
||||
a.Extent.CX = w
|
||||
a.Extent.CY = h
|
||||
}
|
||||
if a.Graphic != nil && a.Graphic.GraphicData != nil && a.Graphic.GraphicData.Pic != nil && a.Graphic.GraphicData.Pic.SpPr != nil {
|
||||
a.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CX = w
|
||||
a.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user