update to latest

This commit is contained in:
2026-06-02 10:08:24 +06:30
parent ecb03c1fb7
commit d7ffc17d71
9 changed files with 98 additions and 296 deletions

38
img.go
View File

@@ -103,7 +103,10 @@ func GenImg(width int, outputPath, payload, tmpl string) string {
}
func renderTemplate(tmp string, data map[string]interface{}) (string, error) {
tmpl := template.Must(template.New("mytemplate").Parse(tmp))
tmpl, err := template.New("mytemplate").Parse(tmp)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return "", err
@@ -124,7 +127,11 @@ func renderNode(dc *gg.Context, canvasWidth int, n *Node, y *int, face font.Face
case "h3":
drawTextBlock(dc, n, canvasWidth, eleH3Size, y, face)
case "p":
drawTextBlock(dc, n, canvasWidth, elePSize, y, face)
size := float64(elePSize)
if n.Style.FontSize > 0 {
size = n.Style.FontSize
}
drawTextBlock(dc, n, canvasWidth, size, y, face)
case "hr":
renderLine(dc, n, canvasWidth, y)
case "img":
@@ -209,7 +216,7 @@ func drawImage(dc *gg.Context, n *Node, y *int) {
}
func renderTable(dc *gg.Context, canvasWidth int, table *Node, y *int, face font.Face) {
rows := extractRows(table)
headers, rows := extractRows(table)
if len(rows) == 0 {
return
}
@@ -223,9 +230,11 @@ func renderTable(dc *gg.Context, canvasWidth int, table *Node, y *int, face font
colCount := len(rows[0])
cellWidth := (canvasWidth - padding) / colCount
border := table.Style.Border
for _, row := range rows {
x := padding
for _, cell := range row {
for i, cell := range row {
header := headers[i]
if border > 0 {
dc.SetLineWidth(border)
dc.DrawRectangle(float64(x), float64(*y), float64(cellWidth), fontSize+defalutTableBorder)
@@ -233,7 +242,11 @@ func renderTable(dc *gg.Context, canvasWidth int, table *Node, y *int, face font
dc.Stroke()
dc.SetRGB(0, 0, 0)
dc.DrawStringAnchored(cell, float64(x+8), float64(*y+20), 0, 0)
x += cellWidth
if w := header.Style.Width; w > 0 {
x += int(w)
} else {
x += cellWidth
}
}
*y += int(fontSize) + defalutTableBorder
}
@@ -247,8 +260,9 @@ func renderLine(dc *gg.Context, line *Node, canvasWidth int, y *int) {
*y += int(height)
}
func extractRows(table *Node) [][]string {
func extractRows(table *Node) ([]*Node, [][]string) {
var rows [][]string
var headers []*Node
var traverse func(*Node)
traverse = func(n *Node) {
if n.Tag == "tr" {
@@ -257,6 +271,9 @@ func extractRows(table *Node) [][]string {
if td.Tag == "td" || td.Tag == "th" {
row = append(row, td.Text)
}
if td.Tag == "th" {
headers = append(headers, td)
}
}
if len(row) > 0 {
rows = append(rows, row)
@@ -267,7 +284,7 @@ func extractRows(table *Node) [][]string {
}
}
traverse(table)
return rows
return headers, rows
}
func extractNodeRows(table *html.Node) [][]string {
@@ -332,9 +349,14 @@ func printImg(prt *escpos.Escpos, imgPath string) error {
fmt.Println(err)
return err
}
data := []byte{0x1D, 0x4C, 0x00, 0x00}
_, err = prt.WriteRaw(data)
if err != nil {
fmt.Println("error 0x1D, 0x4C:", err.Error())
}
gray := toMonochrome(img)
data := escposRaster(gray)
data = escposRaster(gray)
_, err = prt.WriteRaw(data)
return err