Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d8ddbbb41d | |||
| fa8691c04e | |||
| 016dad446f | |||
| 06f38b707e | |||
| 5f4a427f3b | |||
| 7bb8778611 | |||
| 58c029b5f2 | |||
| 8a23c7037f | |||
| 29ec943d13 | |||
| 99189e4b51 | |||
| d8781c3981 | |||
| d7ffc17d71 | |||
| ecb03c1fb7 | |||
| 016cc5b6fe | |||
| 9a0d1d098c |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -26,3 +26,8 @@ go.work.sum
|
||||
.env
|
||||
|
||||
build
|
||||
assets
|
||||
libgofunc
|
||||
libgo
|
||||
cmd/out.png
|
||||
cmd/*.png
|
||||
|
||||
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Dev",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}/cmd",
|
||||
},
|
||||
]
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
37
build_lib.sh
37
build_lib.sh
@@ -1,9 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
APP_NAME="libgofunc"
|
||||
VERSION="${1:-v0.1.1}"
|
||||
OUTPUT_DIR="assets"
|
||||
BUILD_DIR="build"
|
||||
VERSION="${1:-v0.1.13}"
|
||||
OUTPUT_DIR="../assets"
|
||||
BUILD_DIR="../build"
|
||||
|
||||
# need Android NDK
|
||||
NDK_HOME="$HOME/Android/Sdk/ndk/28.2.13676358" # <--- CHECK YOUR VERSION
|
||||
@@ -11,7 +11,7 @@ API=21
|
||||
|
||||
TOOLCHAIN="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"
|
||||
OS="$(uname -s)"
|
||||
|
||||
cd cmd
|
||||
if [ "$OS" = "Darwin" ]; then
|
||||
export IOS_SDK=$(xcrun --sdk iphoneos --show-sdk-path)
|
||||
export IOS_SIM_SDK=$(xcrun --sdk iphonesimulator --show-sdk-path)
|
||||
@@ -27,7 +27,7 @@ if [ "$OS" = "Darwin" ]; then
|
||||
CC="$(xcrun --sdk iphonesimulator --find clang)" \
|
||||
CGO_CFLAGS="-isysroot $IOS_SIM_SDK -arch x86_64" \
|
||||
CGO_LDFLAGS="-isysroot $IOS_SIM_SDK -arch x86_64" \
|
||||
go build -buildmode=c-archive -o build/ios/sim/libgofunc_arm64_sim.a .
|
||||
go build ./cmd -buildmode=c-archive -o build/ios/sim/libgofunc_arm64_sim.a .
|
||||
|
||||
# xcodebuild -create-xcframework \
|
||||
# -library build/ios/device/libgofunc_arm64.a -headers build/ios/device/ \
|
||||
@@ -46,34 +46,35 @@ if [ "$OS" = "Darwin" ]; then
|
||||
elif [ "$OS" = "Linux" ]; then
|
||||
mkdir -p "${OUTPUT_DIR}/${VERSION}"
|
||||
|
||||
echo "Building for Android amd64..."
|
||||
ARCH="amd64"
|
||||
echo "Building for Android $ARCH..."
|
||||
CC="$TOOLCHAIN/x86_64-linux-android$API-clang" \
|
||||
CGO_ENABLED=1 GOOS=android GOARCH=amd64 \
|
||||
go build -buildmode=c-shared -o $BUILD_DIR/libgofunc_amd64.so .
|
||||
go build -buildmode=c-shared -o $BUILD_DIR/$VERSION/$ARCH/libgofunc.so .
|
||||
|
||||
ARCHIVE_NAME="${APP_NAME}-${VERSION}-amd64.tar.gz"
|
||||
tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" libgofunc_amd64.so
|
||||
tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" ./${VERSION}/${ARCH}
|
||||
|
||||
echo "Building for Android arm64..."
|
||||
ARCH="arm64"
|
||||
echo "Building for Android $ARCH..."
|
||||
CC="$TOOLCHAIN/aarch64-linux-android$API-clang" \
|
||||
CGO_ENABLED=1 GOOS=android GOARCH=arm64 \
|
||||
go build -buildmode=c-shared -o $BUILD_DIR/libgofunc_arm64.so .
|
||||
go build -buildmode=c-shared -o $BUILD_DIR/$VERSION/$ARCH/libgofunc.so .
|
||||
|
||||
ARCHIVE_NAME="${APP_NAME}-${VERSION}-arm64.tar.gz"
|
||||
tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" libgofunc_arm64.so
|
||||
ARCHIVE_NAME="${APP_NAME}-${VERSION}-${ARCH}.tar.gz"
|
||||
tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" ./${VERSION}/${ARCH}
|
||||
|
||||
ARCH="armv7a"
|
||||
echo "Building for Android armv7a..."
|
||||
CC="$TOOLCHAIN/armv7a-linux-androideabi$API-clang" \
|
||||
CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \
|
||||
go build -buildmode=c-shared -o $BUILD_DIR/libgofunc_armv7a.so .
|
||||
go build -buildmode=c-shared -o $BUILD_DIR/$VERSION/$ARCH/libgofunc.so .
|
||||
|
||||
ARCHIVE_NAME="${APP_NAME}-${VERSION}-armv7a.tar.gz"
|
||||
tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" libgofunc_armv7a.so
|
||||
|
||||
# cp ./assets/libgofunc_x64.so $HOME/ws/forward_pos/native/android/x86_64/libgofunc.so
|
||||
# cp ./assets/libgofunc_arm64.so $HOME/ws/forward_pos/native/android/arm64-v8a/libgofunc.so
|
||||
# cp ./assets/libgofunc_armv7a.so $HOME/ws/forward_pos/native/android/armeabi-v7a/libgofunc.so
|
||||
tar -czf "${OUTPUT_DIR}/${VERSION}/${ARCHIVE_NAME}" -C "${BUILD_DIR}" ./${VERSION}/${ARCH}
|
||||
|
||||
export HTTPS_PROXY="socks5://localhost:8080"
|
||||
rclone copy ../assets/${VERSION} s3:mokkon/libs/libgofunc/${VERSION}
|
||||
else
|
||||
echo "Unsupported OS: $OS"
|
||||
exit 1
|
||||
|
||||
141
canvas.go
Normal file
141
canvas.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package libgofunc
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/fogleman/gg"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// -------------------- STYLE --------------------
|
||||
type Style struct {
|
||||
PaddingLeft float64
|
||||
PaddingRight float64
|
||||
PaddingTop float64
|
||||
PaddingBottom float64
|
||||
FontSize float64
|
||||
Width float64
|
||||
Height float64
|
||||
TextAlign string
|
||||
VerticalAlign string
|
||||
Border float64
|
||||
}
|
||||
|
||||
func parseStyle(styleStr string) Style {
|
||||
style := Style{
|
||||
FontSize: 16,
|
||||
TextAlign: "left",
|
||||
VerticalAlign: "top",
|
||||
}
|
||||
|
||||
parts := strings.Split(styleStr, ";")
|
||||
for _, p := range parts {
|
||||
kv := strings.SplitN(strings.TrimSpace(p), ":", 2)
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(kv[0])
|
||||
val := strings.TrimSpace(kv[1])
|
||||
|
||||
switch key {
|
||||
case "padding":
|
||||
v := parsePx(val)
|
||||
style.PaddingLeft, style.PaddingRight, style.PaddingTop, style.PaddingBottom = v, v, v, v
|
||||
case "padding-left":
|
||||
v := parsePx(val)
|
||||
style.PaddingLeft = v
|
||||
case "padding-right":
|
||||
v := parsePx(val)
|
||||
style.PaddingRight = v
|
||||
case "padding-top":
|
||||
v := parsePx(val)
|
||||
style.PaddingTop = v
|
||||
case "padding-bottom":
|
||||
v := parsePx(val)
|
||||
style.PaddingBottom = v
|
||||
case "font-size":
|
||||
style.FontSize = parsePx(val)
|
||||
case "width":
|
||||
style.Width = parsePx(val)
|
||||
case "height":
|
||||
style.Height = parsePx(val)
|
||||
case "text-align":
|
||||
style.TextAlign = val
|
||||
case "vertical-align":
|
||||
style.VerticalAlign = val
|
||||
case "border":
|
||||
style.Border = parsePx(val)
|
||||
}
|
||||
}
|
||||
return style
|
||||
}
|
||||
|
||||
func parsePx(v string) float64 {
|
||||
v = strings.ReplaceAll(v, "px", "")
|
||||
f, _ := strconv.ParseFloat(v, 64)
|
||||
return f
|
||||
}
|
||||
|
||||
// -------------------- NODE --------------------
|
||||
type Node struct {
|
||||
Tag string
|
||||
Text string
|
||||
Attr map[string]string
|
||||
Style Style
|
||||
Children []*Node
|
||||
}
|
||||
|
||||
func (n Node) getSrc() string {
|
||||
for k, v := range n.Attr {
|
||||
if k == "src" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (n Node) getHeight() int {
|
||||
return int(n.Style.Height)
|
||||
}
|
||||
|
||||
func BuildTree(n *html.Node, canvasWidth int, dc *gg.Context, face *font.Face) (*Node, int) {
|
||||
if n.Type != html.ElementNode {
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
text := ""
|
||||
if n.Data == "h1" || n.Data == "h2" || n.Data == "h3" ||
|
||||
n.Data == "p" || n.Data == "td" || n.Data == "th" ||
|
||||
n.Type == html.TextNode {
|
||||
text = getText(n)
|
||||
}
|
||||
|
||||
node := &Node{
|
||||
Tag: n.Data,
|
||||
Text: text,
|
||||
Attr: map[string]string{},
|
||||
}
|
||||
|
||||
for _, a := range n.Attr {
|
||||
node.Attr[a.Key] = a.Val
|
||||
}
|
||||
|
||||
if s, ok := node.Attr["style"]; ok {
|
||||
node.Style = parseStyle(s)
|
||||
}
|
||||
xPadding := node.Style.PaddingLeft + node.Style.PaddingRight
|
||||
yPadding := node.Style.PaddingTop + node.Style.PaddingBottom
|
||||
fontSize := node.Style.FontSize
|
||||
h := nodeHeight(n, dc, canvasWidth, int(xPadding), int(yPadding),
|
||||
face, fontSize, node.getHeight())
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
child, ch := BuildTree(c, canvasWidth, dc, face)
|
||||
if child != nil {
|
||||
node.Children = append(node.Children, child)
|
||||
}
|
||||
h += ch
|
||||
}
|
||||
return node, h
|
||||
}
|
||||
143
cmd/main.go
Normal file
143
cmd/main.go
Normal file
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package libgofunc
|
||||
|
||||
import (
|
||||
"image"
|
||||
@@ -91,7 +91,8 @@ func toMonochrome(img image.Image) *image.Gray {
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
r, g, b, _ := img.At(x, y).RGBA()
|
||||
grayValue := uint8((r + g + b) / 3 >> 8)
|
||||
// grayValue := uint8((r + g + b) / 3 >> 8)
|
||||
grayValue := uint8((0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)) / 256.0)
|
||||
if grayValue > 128 {
|
||||
gray.Set(x, y, color.White)
|
||||
} else {
|
||||
@@ -138,7 +139,6 @@ func escposRaster(img *image.Gray) []byte {
|
||||
data = append(data, b)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
11
go.mod
11
go.mod
@@ -1,16 +1,17 @@
|
||||
module libgofunc
|
||||
module gt.mokkon.com/sainw/libgofunc
|
||||
|
||||
go 1.22.0
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.11.5
|
||||
github.com/fogleman/gg v1.3.0
|
||||
github.com/kenshaw/escpos v0.0.0-20221114190919-df06b682a8fc
|
||||
golang.org/x/image v0.24.0
|
||||
golang.org/x/net v0.34.0
|
||||
golang.org/x/image v0.38.0
|
||||
golang.org/x/net v0.52.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
golang.org/x/text v0.22.0 // indirect
|
||||
github.com/google/uuid v1.6.0
|
||||
golang.org/x/text v0.35.0 // indirect
|
||||
)
|
||||
|
||||
14
go.sum
14
go.sum
@@ -4,11 +4,13 @@ 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.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ=
|
||||
golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||
golang.org/x/image v0.38.0 h1:5l+q+Y9JDC7mBOMjo4/aPhMDcxEptsX+Tt3GgRQRPuE=
|
||||
| ||||