forked from fumiama/go-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
635 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
docxlib | ||
.vscode/ | ||
*.docx | ||
/*.xml | ||
/*.xml | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,95 @@ | ||
package docxlib | ||
|
||
// AddDrawing adds drawing to paragraph | ||
func (p *Paragraph) AddDrawing(pic []byte) *Run { | ||
//TODO: finish add drawing | ||
return nil | ||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"strconv" | ||
"sync/atomic" | ||
|
||
"github.com/fumiama/imgsz" | ||
) | ||
|
||
// AddInlineDrawing adds inline drawing to paragraph | ||
func (p *Paragraph) AddInlineDrawing(pic []byte) (*Run, error) { | ||
sz, format, err := imgsz.DecodeSize(bytes.NewReader(pic)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
id := strconv.Itoa(int(atomic.AddUintptr(&p.file.imageId, 1))) | ||
rId := p.file.addImage(Media{Name: "image" + id + "." + format, Data: pic}) | ||
w, h := sz.Width, 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{ | ||
Inline: &WPInline{ | ||
AnchorID: fmt.Sprintf("%08X", rand.Uint32()), | ||
EditID: fmt.Sprintf("%08X", rand.Uint32()), | ||
|
||
Extent: &WPExtent{ | ||
CX: w, | ||
CY: h, | ||
}, | ||
EffectExtent: &WPEffectExtent{}, | ||
DocPr: &WPDocPr{ | ||
ID: id, | ||
Name: "图片 " + id, | ||
}, | ||
CNvGraphicFramePr: &WPCNvGraphicFramePr{ | ||
Locks: &AGraphicFrameLocks{ | ||
NoChangeAspect: 1, | ||
}, | ||
}, | ||
Graphic: &AGraphic{ | ||
GraphicData: &AGraphicData{ | ||
URI: XMLNS_PICTURE, | ||
Pic: &PICPic{ | ||
NonVisualPicProperties: &PICNonVisualPicProperties{ | ||
NonVisualDrawingProperties: PICNonVisualDrawingProperties{ | ||
ID: 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) AddInlineDrawingFrom(file string) (*Run, error) { | ||
data, err := os.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p.AddInlineDrawing(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/fumiama/docxlib | ||
|
||
go 1.16 | ||
|
||
require github.com/fumiama/imgsz v0.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/fumiama/imgsz v0.0.2 h1:fAkC0FnIscdKOXwAxlyw3EUba5NzxZdSxGaq3Uyfxak= | ||
github.com/fumiama/imgsz v0.0.2/go.mod h1:dR71mI3I2O5u6+PCpd47M9TZptzP+39tRBcbdIkoqM4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package docxlib | ||
|
||
// addImage add image to docx and return its rId | ||
func (f *Docx) addImage(m Media) string { | ||
f.addMedia(m) | ||
return f.addImageRelation(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package docxlib | ||
|
||
import ( | ||
"strconv" | ||
"sync/atomic" | ||
) | ||
|
||
// when adding an hyperlink we need to store a reference in the relationship field | ||
// | ||
// this func is not thread-safe | ||
func (f *Docx) addLinkRelation(link string) string { | ||
rel := &Relationship{ | ||
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rId, 1))), | ||
Type: REL_HYPERLINK, | ||
Target: link, | ||
TargetMode: REL_TARGETMODE, | ||
} | ||
|
||
f.DocRelation.Relationships = append(f.DocRelation.Relationships, rel) | ||
|
||
return rel.ID | ||
} | ||
|
||
// when adding an image we need to store a reference in the relationship field | ||
// | ||
// this func is not thread-safe | ||
func (f *Docx) addImageRelation(m Media) string { | ||
rel := &Relationship{ | ||
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rId, 1))), | ||
Type: REL_IMAGE, | ||
Target: "media/" + m.Name, | ||
} | ||
|
||
f.DocRelation.Relationships = append(f.DocRelation.Relationships, rel) | ||
|
||
return rel.ID | ||
} | ||
|
||
// ReferHref gets the url for a reference | ||
func (f *Docx) ReferHref(id string) (href string, err error) { | ||
f.DocRelation.mu.RLock() | ||
defer f.DocRelation.mu.RUnlock() | ||
for _, a := range f.DocRelation.Relationships { | ||
if a.ID == id { | ||
href = a.Target | ||
return | ||
} | ||
} | ||
err = ErrRefIDNotFound | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package docxlib | ||
|
||
const MEDIA_FOLDER = `word/media/` | ||
|
||
// Media is in word/media | ||
type Media struct { | ||
Name string // Name is for word/media/Name | ||
Data []byte // Data is data of this media | ||
} | ||
|
||
// String is the full path of the media | ||
func (m *Media) String() string { | ||
return MEDIA_FOLDER + m.Name | ||
} | ||
|
||
// Media get media struct pointer (or nil on notfound) by name | ||
func (f *Docx) Media(name string) *Media { | ||
i, ok := f.mediaNameIdx[name] | ||
if !ok { | ||
return nil | ||
} | ||
return &f.media[i] | ||
} | ||
|
||
// addMedia append the media to docx's media list | ||
func (f *Docx) addMedia(m Media) { | ||
f.mediaNameIdx[m.Name] = len(f.media) | ||
f.media = append(f.media, m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.