add working dir
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
APP_NAME="libgofunc"
|
APP_NAME="libgofunc"
|
||||||
VERSION="${1:-v0.1.5}"
|
VERSION="${1:-v0.1.7}"
|
||||||
OUTPUT_DIR="../assets"
|
OUTPUT_DIR="../assets"
|
||||||
BUILD_DIR="../build"
|
BUILD_DIR="../build"
|
||||||
|
|
||||||
|
|||||||
BIN
cmd/logo.png
BIN
cmd/logo.png
Binary file not shown.
|
Before Width: | Height: | Size: 18 KiB |
File diff suppressed because one or more lines are too long
35
img.go
35
img.go
@@ -26,6 +26,7 @@ import (
|
|||||||
)
|
)
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
@@ -46,11 +47,12 @@ const (
|
|||||||
var fontFs embed.FS
|
var fontFs embed.FS
|
||||||
|
|
||||||
//export GenPNG
|
//export GenPNG
|
||||||
func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char) *C.char {
|
func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char, workingDirC *C.char) *C.char {
|
||||||
canvasWidth := int(width)
|
canvasWidth := int(width)
|
||||||
goPath := C.GoString(outputPath)
|
goPath := C.GoString(outputPath)
|
||||||
goPayload := C.GoString(payload)
|
goPayload := C.GoString(payload)
|
||||||
goTmpl := C.GoString(tmpl)
|
goTmpl := C.GoString(tmpl)
|
||||||
|
workingDir := C.GoString(workingDirC)
|
||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
err := json.Unmarshal([]byte(goPayload), &data)
|
err := json.Unmarshal([]byte(goPayload), &data)
|
||||||
@@ -91,7 +93,10 @@ func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char) *C.c
|
|||||||
dc.SetFontFace(*face)
|
dc.SetFontFace(*face)
|
||||||
|
|
||||||
y := 0
|
y := 0
|
||||||
renderNode(dc, canvasWidth, body, &y, *face)
|
err = renderNode(dc, canvasWidth, body, &y, *face, workingDir)
|
||||||
|
if err != nil {
|
||||||
|
return NewErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
err = dc.SavePNG(goPath)
|
err = dc.SavePNG(goPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -102,8 +107,8 @@ func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char) *C.c
|
|||||||
return NewOk(nil)
|
return NewOk(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenImg(width int, outputPath, payload, tmpl string) string {
|
func GenImg(width int, outputPath, payload, tmpl, workingDir string) string {
|
||||||
result := GenPNG(C.int(width), C.CString(outputPath), C.CString(payload), C.CString(tmpl))
|
result := GenPNG(C.int(width), C.CString(outputPath), C.CString(payload), C.CString(tmpl), C.CString(workingDir))
|
||||||
r := C.GoString(result)
|
r := C.GoString(result)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@@ -125,7 +130,7 @@ func renderTemplate(tmp string, data map[string]interface{}) (string, error) {
|
|||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderNode(dc *gg.Context, canvasWidth int, n *Node, y *int, face font.Face) {
|
func renderNode(dc *gg.Context, canvasWidth int, n *Node, y *int, face font.Face, workingDir string) error {
|
||||||
before := *y
|
before := *y
|
||||||
if n.Style.PaddingTop > 0 {
|
if n.Style.PaddingTop > 0 {
|
||||||
*y += int(n.Style.PaddingTop)
|
*y += int(n.Style.PaddingTop)
|
||||||
@@ -146,7 +151,9 @@ func renderNode(dc *gg.Context, canvasWidth int, n *Node, y *int, face font.Face
|
|||||||
case "hr":
|
case "hr":
|
||||||
renderLine(dc, n, canvasWidth, y)
|
renderLine(dc, n, canvasWidth, y)
|
||||||
case "img":
|
case "img":
|
||||||
drawImage(dc, n, y)
|
if err := drawImage(dc, n, y, workingDir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
case "table":
|
case "table":
|
||||||
renderTable(dc, canvasWidth, n, y, face)
|
renderTable(dc, canvasWidth, n, y, face)
|
||||||
}
|
}
|
||||||
@@ -156,8 +163,9 @@ func renderNode(dc *gg.Context, canvasWidth int, n *Node, y *int, face font.Face
|
|||||||
log.Printf("render %s y, y', height: %d, %d, %d\n", n.Tag, before, *y, *y-before)
|
log.Printf("render %s y, y', height: %d, %d, %d\n", n.Tag, before, *y, *y-before)
|
||||||
|
|
||||||
for _, c := range n.Children {
|
for _, c := range n.Children {
|
||||||
renderNode(dc, canvasWidth, c, y, face)
|
renderNode(dc, canvasWidth, c, y, face, workingDir)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTextBlock(dc *gg.Context, n *Node, canvasWidth int, size float64, y *int, face font.Face) {
|
func drawTextBlock(dc *gg.Context, n *Node, canvasWidth int, size float64, y *int, face font.Face) {
|
||||||
@@ -194,14 +202,18 @@ func wordWrap(dc *gg.Context, text string, maxWidth int) []string {
|
|||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawImage(dc *gg.Context, n *Node, y *int) {
|
func drawImage(dc *gg.Context, n *Node, y *int, workingDir string) error {
|
||||||
src := n.getSrc()
|
src := n.getSrc()
|
||||||
file, err := os.Open(src)
|
s := path.Join(workingDir, src)
|
||||||
|
file, err := os.Open(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return fmt.Errorf("open file src: '%s', working directory: '%s', error : %s", src, workingDir, err.Error())
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
img, _, _ := image.Decode(file)
|
img, _, err := image.Decode(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
padding := n.Style.PaddingLeft
|
padding := n.Style.PaddingLeft
|
||||||
h := n.Style.Height
|
h := n.Style.Height
|
||||||
if n.Style.Width > 0 {
|
if n.Style.Width > 0 {
|
||||||
@@ -224,6 +236,7 @@ func drawImage(dc *gg.Context, n *Node, y *int) {
|
|||||||
dc.DrawImage(img, int(padding), *y)
|
dc.DrawImage(img, int(padding), *y)
|
||||||
*y += img.Bounds().Dy()
|
*y += img.Bounds().Dy()
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTable(dc *gg.Context, canvasWidth int, table *Node, y *int, face font.Face) {
|
func renderTable(dc *gg.Context, canvasWidth int, table *Node, y *int, face font.Face) {
|
||||||
|
|||||||
Reference in New Issue
Block a user