diff --git a/.gitignore b/.gitignore index 54405ac..fbda58d 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ assets libgofunc libgo cmd/out.png +cmd/*.png diff --git a/build_lib.sh b/build_lib.sh index b86a603..01aed47 100755 --- a/build_lib.sh +++ b/build_lib.sh @@ -1,7 +1,7 @@ #!/bin/bash APP_NAME="libgofunc" -VERSION="${1:-v0.1.8}" +VERSION="${1:-v0.1.9}" OUTPUT_DIR="../assets" BUILD_DIR="../build" diff --git a/cmd/main.go b/cmd/main.go index 02fec58..53781af 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -22,6 +22,7 @@ func main() { const temp = `

{{.station.name}}

+

စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ

Address: မင်္ဂလာပါ {{(index .sales 0).salePerson}}

Receipt: RCPT001

@@ -57,8 +58,48 @@ func main() {

စက်သုံးဆီ အရေအတွက် နှင့် အရည်အသွေးစျေးနှုန်းများ အားသံသယရှိပါက ph 09450539099, 09765421033, 09765421029 သို့တိုင်ကြား နိုင်ပါသည်။

+ + +

{{.station.name}}

+

စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ စက်သုံးဆီဆိုင်လိပ်စာ

+ +

Address: မင်္ဂလာပါ {{(index .sales 0).salePerson}}

+

Receipt: RCPT001

+

Phone: 0977777777

+

Date: {{formatDateFromSec (index .sales 0).timeSec "2006-01-02 15:04:05"}}

+

Car No.: 3J/3883

+

Casher: မနှင်းနှင်း

+

MOP: B2B

+

Tier: 1

+

Deal No.: RR

+
+ + + + + + + + + + +{{range .sales}} + + + + + + + + +{{end}} +
ItemPumpPriceLiterGallonAmount
{{.product}}{{.fpId}}{{.price}}{{.volume}}{{.volume}}{{.amount}}
+ +

+စက်သုံးဆီ အရေအတွက် နှင့် အရည်အသွေးစျေးနှုန်းများ အားသံသယရှိပါက ph 09450539099, 09765421033, 09765421029 သို့တိုင်ကြား နိုင်ပါသည်။

+ ` - result := libgofunc.GenImg(550, "./out.png", payload, temp, "./") + result := libgofunc.GenImg(550, "./", payload, temp, "./") fmt.Println("Result:", result) // PrintImg(C.CString("usb:/dev/usb/lp1"), C.CString("build/out.png")) // libgofunc.Print("int:/dev/bus/usb/001/046", "./out.png") diff --git a/go.mod b/go.mod index 5e70a30..c69bb31 100644 --- a/go.mod +++ b/go.mod @@ -12,5 +12,6 @@ require ( require ( github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + github.com/google/uuid v1.6.0 golang.org/x/text v0.35.0 // indirect ) diff --git a/go.sum b/go.sum index 2e20190..2e79f92 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/kenshaw/escpos v0.0.0-20221114190919-df06b682a8fc h1:4JwmN2Scz1vR+hfSxkdy2IE/DzxX2Cftm2lhWHyN0k0= github.com/kenshaw/escpos v0.0.0-20221114190919-df06b682a8fc/go.mod h1:M+GIBmg2MqaSWIJrXCZS+/wRFbr9fOguRz3SHn8DRPE= golang.org/x/image v0.38.0 h1:5l+q+Y9JDC7mBOMjo4/aPhMDcxEptsX+Tt3GgRQRPuE= diff --git a/img.go b/img.go index 93bf838..823e1e0 100644 --- a/img.go +++ b/img.go @@ -30,6 +30,7 @@ import ( "strconv" "time" + "github.com/google/uuid" "golang.org/x/text/language" "golang.org/x/text/message" ) @@ -48,9 +49,9 @@ const ( var fontFs embed.FS //export GenPNG -func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char, workingDirC *C.char) *C.char { +func GenPNG(width C.int, outDir *C.char, payload *C.char, tmpl *C.char, workingDirC *C.char) *C.char { canvasWidth := int(width) - goPath := C.GoString(outputPath) + goOutDir := C.GoString(outDir) goPayload := C.GoString(payload) goTmpl := C.GoString(tmpl) workingDir := C.GoString(workingDirC) @@ -67,14 +68,27 @@ func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char, work } htmlStr = uni2zg(htmlStr) + htmls := strings.Split(htmlStr, "") + result := make([]string, 0) + for _, html := range htmls { + out, err := GenPng(canvasWidth, goOutDir, html, workingDir) + if err != nil { + return NewErr(err) + } + result = append(result, out) + } + return NewOk(result) +} + +func GenPng(canvasWidth int, outDir string, htmlStr string, workingDir string) (string, error) { root, err := html.Parse(strings.NewReader(htmlStr)) if err != nil { - return NewErr(err) + return "", err } face, err := LoadFont(defalutFontSize) if err != nil { - return NewErr(err) + return "", err } // First pass: compute required height @@ -96,20 +110,19 @@ func GenPNG(width C.int, outputPath *C.char, payload *C.char, tmpl *C.char, work y := 0 err = renderNode(dc, canvasWidth, body, &y, *face, workingDir) if err != nil { - return NewErr(err) + return "", err } - err = dc.SavePNG(goPath) + outPath := path.Join(outDir, uuid.NewString()+".png") + err = dc.SavePNG(outPath) if err != nil { - return NewErr(err) + return "", err } - - // PrintReceipt(goPath) - return NewOk(nil) + return outPath, nil } -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)) +func GenImg(width int, outputDir, payload, tmpl, workingDir string) string { + result := GenPNG(C.int(width), C.CString(outputDir), C.CString(payload), C.CString(tmpl), C.CString(workingDir)) r := C.GoString(result) return r } @@ -162,7 +175,8 @@ func renderNode(dc *gg.Context, canvasWidth int, n *Node, y *int, face font.Face if n.Style.PaddingBottom > 0 { *y += int(n.Style.PaddingBottom) } - log.Printf("render %s y, y', height: %d, %d, %d\n", n.Tag, before, *y, *y-before) + _ = before + // 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, workingDir) @@ -453,7 +467,7 @@ func nodeHeight(n *html.Node, dc *gg.Context, canvasWidth int, xPadding, yPaddin h = implicit } y += h - log.Printf("nodeHeight %s height: %d, \n", n.Data, y) + // log.Printf("nodeHeight %s height: %d, \n", n.Data, y) } return y }