add working dir

This commit is contained in:
2026-06-04 00:10:32 +06:30
parent d8781c3981
commit 99189e4b51
4 changed files with 27 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
#!/bin/bash
APP_NAME="libgofunc"
VERSION="${1:-v0.1.5}"
VERSION="${1:-v0.1.7}"
OUTPUT_DIR="../assets"
BUILD_DIR="../build"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

File diff suppressed because one or more lines are too long

35
img.go
View File

@@ -26,6 +26,7 @@ import (
)
import (
"log"
"path"
"strconv"
"golang.org/x/text/language"
@@ -46,11 +47,12 @@ const (
var fontFs embed.FS
//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)
goPath := C.GoString(outputPath)
goPayload := C.GoString(payload)
goTmpl := C.GoString(tmpl)
workingDir := C.GoString(workingDirC)
data := make(map[string]interface{})
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)
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)
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)
}
func GenImg(width int, outputPath, payload, tmpl string) string {
result := GenPNG(C.int(width), C.CString(outputPath), C.CString(payload), C.CString(tmpl))
func GenImg(width int, outputPath, payload, tmpl, workingDir string) string {
result := GenPNG(C.int(width), C.CString(outputPath), C.CString(payload), C.CString(tmpl), C.CString(workingDir))
r := C.GoString(result)
return r
}
@@ -125,7 +130,7 @@ func renderTemplate(tmp string, data map[string]interface{}) (string, error) {
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
if n.Style.PaddingTop > 0 {
*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":
renderLine(dc, n, canvasWidth, y)
case "img":
drawImage(dc, n, y)
if err := drawImage(dc, n, y, workingDir); err != nil {
return err
}
case "table":
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)
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) {
@@ -194,14 +202,18 @@ func wordWrap(dc *gg.Context, text string, maxWidth int) []string {
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()
file, err := os.Open(src)
s := path.Join(workingDir, src)
file, err := os.Open(s)
if err != nil {
return
return fmt.Errorf("open file src: '%s', working directory: '%s', error : %s", src, workingDir, err.Error())
}
defer file.Close()
img, _, _ := image.Decode(file)
img, _, err := image.Decode(file)
if err != nil {
return err
}
padding := n.Style.PaddingLeft
h := n.Style.Height
if n.Style.Width > 0 {
@@ -224,6 +236,7 @@ func drawImage(dc *gg.Context, n *Node, y *int) {
dc.DrawImage(img, int(padding), *y)
*y += img.Bounds().Dy()
}
return nil
}
func renderTable(dc *gg.Context, canvasWidth int, table *Node, y *int, face font.Face) {