Compare commits

...

5 Commits

Author SHA1 Message Date
62980e1489 v0.0.462
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m32s
2024-05-23 14:37:05 +02:00
59963adf74 v0.0.461
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m17s
2024-05-20 00:52:49 +02:00
194ea4ace5 v0.0.460
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m16s
2024-05-20 00:38:04 +02:00
73b80a66bc v0.0.459
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m16s
2024-05-20 00:20:31 +02:00
d8b2d01274 v0.0.458 revert 457 and fix ObjectFitImage
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m49s
2024-05-20 00:15:24 +02:00
11 changed files with 172 additions and 39 deletions

View File

@@ -72,20 +72,22 @@ func NewEngine(opt Options) *GinWrapper {
engine.Use(CorsMiddleware())
}
// do not debug-print routes
gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {}
if wrapper.ginDebug {
gin.SetMode(gin.DebugMode)
if !wrapper.ginDebug {
gin.SetMode(gin.ReleaseMode)
// do not debug-print routes
gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {}
if !wrapper.suppressGinLogs {
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
ginlogger(context)
})
engine.Use(func(context *gin.Context) { ginlogger(context) })
}
} else {
gin.SetMode(gin.DebugMode)
gin.SetMode(gin.ReleaseMode)
// do not debug-print routes
gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {}
}
return wrapper

6
go.mod
View File

@@ -7,7 +7,7 @@ require (
github.com/glebarez/go-sqlite v1.22.0 // only needed for tests -.-
github.com/jmoiron/sqlx v1.4.0
github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.32.0
github.com/rs/zerolog v1.33.0
go.mongodb.org/mongo-driver v1.15.0
golang.org/x/crypto v0.23.0
golang.org/x/sys v0.20.0
@@ -21,7 +21,7 @@ require (
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic v1.11.7 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
@@ -33,7 +33,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect

6
go.sum
View File

@@ -18,6 +18,8 @@ github.com/bytedance/sonic v1.11.5 h1:G00FYjjqll5iQ1PYXynbg/hyzqBqavH8Mo9/oTopd9
github.com/bytedance/sonic v1.11.5/go.mod h1:X2PC2giUdj/Cv2lliWFLk6c/DUQok5rViJSemeB0wDw=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic v1.11.7 h1:k/l9p1hZpNIMJSk37wL9ltkcpqLfIho1vYthi4xT2t4=
github.com/bytedance/sonic v1.11.7/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
@@ -79,6 +81,8 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
@@ -162,6 +166,8 @@ github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=

View File

@@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.457"
const GoextVersion = "0.0.462"
const GoextVersionTimestamp = "2024-05-20T00:07:33+0200"
const GoextVersionTimestamp = "2024-05-23T14:37:05+0200"

View File

@@ -234,11 +234,15 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
// we scale the bounding box by fac (both dimension the same amount, to keep the bounding-box ratio)
// [ow|oh] ==> size of output image (same ratio as bounding box [bbw|bbh])
ow := int(math.Round(bbw * facOut))
oh := int(math.Round(bbh * facOut))
facScale := mathext.Min(float64(ow)/float64(iw), float64(oh)/float64(ih))
// [dw|dh] ==> size of destination rect (where to draw source in output image) (same ratio as input image [iw|ih])
dw := int(math.Round(float64(iw) * facScale))
dh := int(math.Round(float64(ih) * facScale))
@@ -248,11 +252,11 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
if fit == ImageFitContainCenter {
destBounds = image.Rect((ow-dw)/2, (oh-dh)/2, (ow-dw)/2+dw, (oh-dh)/2+dh)
} else if fit == ImageFitContainTopLeft {
destBounds = image.Rect(0, 0, iw, dh)
destBounds = image.Rect(0, 0, dw, dh)
} else if fit == ImageFitContainTopRight {
destBounds = image.Rect(ow-iw, 0, ow, dh)
destBounds = image.Rect(ow-dw, 0, ow, dh)
} else if fit == ImageFitContainBottomLeft {
destBounds = image.Rect(0, oh-dh, iw, oh)
destBounds = image.Rect(0, oh-dh, dw, oh)
} else if fit == ImageFitContainBottomRight {
destBounds = image.Rect(ow-dw, oh-dh, ow, oh)
}

9
wpdf/utils.go Normal file
View File

@@ -0,0 +1,9 @@
package wpdf
func hexToColor(c uint32) PDFColor {
return PDFColor{R: int((c >> 16) & 0xFF), G: int((c >> 8) & 0xFF), B: int((c >> 0) & 0xFF)}
}
func rgbToColor(r, g, b int) PDFColor {
return PDFColor{R: r, G: g, B: b}
}

View File

@@ -126,6 +126,10 @@ func (b *WPDFBuilder) SetX(x float64) {
b.b.SetX(x)
}
func (b *WPDFBuilder) IncX(dx float64) {
b.b.SetX(b.b.GetX() + dx)
}
func (b *WPDFBuilder) GetX() float64 {
return b.b.GetX()
}

View File

@@ -17,6 +17,10 @@ type PDFCellOpt struct {
extraLn *float64
x *float64
autoWidth *bool
textColor *PDFColor
borderColor *PDFColor
fillColor *PDFColor
autoWidthPaddingX *float64
}
func NewPDFCellOpt() *PDFCellOpt {
@@ -110,6 +114,41 @@ func (opt *PDFCellOpt) AutoWidth() *PDFCellOpt {
return opt
}
func (opt *PDFCellOpt) AutoWidthPaddingX(v float64) *PDFCellOpt {
opt.autoWidthPaddingX = &v
return opt
}
func (opt *PDFCellOpt) TextColor(cr, cg, cb int) *PDFCellOpt {
opt.textColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFCellOpt) TextColorHex(c uint32) *PDFCellOpt {
opt.textColor = langext.Ptr(hexToColor(c))
return opt
}
func (opt *PDFCellOpt) BorderColor(cr, cg, cb int) *PDFCellOpt {
opt.borderColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFCellOpt) BorderColorHex(c uint32) *PDFCellOpt {
opt.borderColor = langext.Ptr(hexToColor(c))
return opt
}
func (opt *PDFCellOpt) FillColor(cr, cg, cb int) *PDFCellOpt {
opt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFCellOpt) FillColorHex(c uint32) *PDFCellOpt {
opt.fillColor = langext.Ptr(hexToColor(c))
return opt
}
func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
txtTR := b.tr(txt)
@@ -128,6 +167,10 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
extraLn := float64(0)
var x *float64
autoWidth := false
var textColor *PDFColor
var borderColor *PDFColor
var fillColor *PDFColor
autoWidthPaddingX := float64(0)
for _, opt := range opts {
width = langext.Coalesce(opt.width, width)
@@ -144,6 +187,10 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
extraLn = langext.Coalesce(opt.extraLn, extraLn)
x = langext.CoalesceOpt(opt.x, x)
autoWidth = langext.Coalesce(opt.autoWidth, autoWidth)
textColor = langext.CoalesceOpt(opt.textColor, textColor)
borderColor = langext.CoalesceOpt(opt.borderColor, borderColor)
fillColor = langext.CoalesceOpt(opt.fillColor, fillColor)
autoWidthPaddingX = langext.Coalesce(opt.autoWidthPaddingX, autoWidthPaddingX)
}
if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil {
@@ -157,12 +204,30 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }()
}
if textColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetTextColor()
b.SetTextColor(textColor.R, textColor.G, textColor.B)
defer func() { b.SetTextColor(oldColorR, oldColorG, oldColorB) }()
}
if borderColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetDrawColor()
b.SetDrawColor(borderColor.R, borderColor.G, borderColor.B)
defer func() { b.SetDrawColor(oldColorR, oldColorG, oldColorB) }()
}
if fillColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetFillColor()
b.SetFillColor(fillColor.R, fillColor.G, fillColor.B)
defer func() { b.SetFillColor(oldColorR, oldColorG, oldColorB) }()
}
if x != nil {
b.b.SetX(*x)
}
if autoWidth {
width = b.b.GetStringWidth(txtTR)
width = b.b.GetStringWidth(txtTR) + autoWidthPaddingX
}
b.b.CellFormat(width, height, txtTR, string(border), int(ln), string(align), fill, link, linkStr)

View File

@@ -7,9 +7,7 @@ import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"image"
"image/color"
"image/png"
"net/http"
"os"
)
type PDFImageRef struct {
@@ -229,7 +227,7 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
reEncodePixelPerMM := 15.0
var imageFit *imageext.ImageFit = nil
var fillColor color.Color = color.Transparent
var compression *imageext.ImageCompresson = nil
compression := imageext.CompressionPNGSpeed
var crop *imageext.ImageCrop = nil
for _, opt := range opts {
@@ -245,14 +243,14 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
allowNegativePosition = langext.Coalesce(opt.allowNegativePosition, allowNegativePosition)
imageFit = langext.CoalesceOpt(opt.imageFit, imageFit)
fillColor = langext.Coalesce(opt.fillColor, fillColor)
compression = langext.CoalesceOpt(opt.compression, compression)
compression = langext.Coalesce(opt.compression, compression)
reEncodePixelPerMM = langext.Coalesce(opt.reEncodePixelPerMM, reEncodePixelPerMM)
crop = langext.CoalesceOpt(opt.crop, crop)
}
regName := img.Name
if imageFit != nil || fillColor != nil || crop != nil || compression != nil {
if imageFit != nil || fillColor != nil || crop != nil {
var dataimg image.Image
if img.Image != nil {
@@ -263,16 +261,6 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
b.b.SetError(err)
return
}
img.Image = langext.Ptr(dataimg)
}
_ = os.WriteFile("/tmp/a.png", img.Bin, 0755)
{
bfr := bytes.Buffer{}
enc := &png.Encoder{CompressionLevel: png.NoCompression}
_ = enc.Encode(&bfr, dataimg)
_ = os.WriteFile("/tmp/b.png", img.Bin, 0755)
}
if crop != nil {
@@ -296,7 +284,7 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
}
}
bfr, imgMime, err := imageext.EncodeImage(dataimg, langext.Coalesce(compression, imageext.CompressionPNGSpeed))
bfr, imgMime, err := imageext.EncodeImage(dataimg, compression)
if err != nil {
b.b.SetError(err)
return
@@ -313,8 +301,6 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
imageType = "GIF"
}
_ = os.WriteFile("/tmp/c.png", bfr.Bytes(), 0755)
b.b.RegisterImageOptionsReader(regName, gofpdf.ImageOptions{ImageType: imageType}, &bfr)
}

View File

@@ -13,6 +13,9 @@ type PDFMultiCellOpt struct {
fontSizeOverride *float64
extraLn *float64
x *float64
textColor *PDFColor
borderColor *PDFColor
fillColor *PDFColor
}
func NewPDFMultiCellOpt() *PDFMultiCellOpt {
@@ -86,6 +89,36 @@ func (opt *PDFMultiCellOpt) X(v float64) *PDFMultiCellOpt {
return opt
}
func (opt *PDFMultiCellOpt) TextColor(cr, cg, cb int) *PDFMultiCellOpt {
opt.textColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFMultiCellOpt) TextColorHex(c uint32) *PDFMultiCellOpt {
opt.textColor = langext.Ptr(hexToColor(c))
return opt
}
func (opt *PDFMultiCellOpt) BorderColor(cr, cg, cb int) *PDFMultiCellOpt {
opt.borderColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFMultiCellOpt) BorderColorHex(c uint32) *PDFMultiCellOpt {
opt.borderColor = langext.Ptr(hexToColor(c))
return opt
}
func (opt *PDFMultiCellOpt) FillColor(cr, cg, cb int) *PDFMultiCellOpt {
opt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFMultiCellOpt) FillColorHex(c uint32) *PDFMultiCellOpt {
opt.fillColor = langext.Ptr(hexToColor(c))
return opt
}
func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
txtTR := b.tr(txt)
@@ -100,6 +133,9 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
var fontSizeOverride *float64
extraLn := float64(0)
var x *float64
var textColor *PDFColor
var borderColor *PDFColor
var fillColor *PDFColor
for _, opt := range opts {
width = langext.Coalesce(opt.width, width)
@@ -112,6 +148,9 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride)
extraLn = langext.Coalesce(opt.extraLn, extraLn)
x = langext.CoalesceOpt(opt.x, x)
textColor = langext.CoalesceOpt(opt.textColor, textColor)
borderColor = langext.CoalesceOpt(opt.borderColor, borderColor)
fillColor = langext.CoalesceOpt(opt.fillColor, fillColor)
}
if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil {
@@ -125,6 +164,24 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }()
}
if textColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetTextColor()
b.SetTextColor(textColor.R, textColor.G, textColor.B)
defer func() { b.SetTextColor(oldColorR, oldColorG, oldColorB) }()
}
if borderColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetDrawColor()
b.SetDrawColor(borderColor.R, borderColor.G, borderColor.B)
defer func() { b.SetDrawColor(oldColorR, oldColorG, oldColorB) }()
}
if fillColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetFillColor()
b.SetFillColor(fillColor.R, fillColor.G, fillColor.B)
defer func() { b.SetFillColor(oldColorR, oldColorG, oldColorB) }()
}
if x != nil {
b.b.SetX(*x)
}

View File

@@ -34,22 +34,22 @@ func (opt *PDFRectOpt) LineWidth(v float64) *PDFRectOpt {
}
func (opt *PDFRectOpt) DrawColor(cr, cg, cb int) *PDFRectOpt {
opt.drawColor = &PDFColor{R: cr, G: cg, B: cb}
opt.drawColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFRectOpt) DrawColorHex(c uint32) *PDFRectOpt {
opt.drawColor = &PDFColor{R: int((c >> 16) & 0xFF), G: int((c >> 8) & 0xFF), B: int((c >> 0) & 0xFF)}
opt.drawColor = langext.Ptr(hexToColor(c))
return opt
}
func (opt *PDFRectOpt) FillColor(cr, cg, cb int) *PDFRectOpt {
opt.fillColor = &PDFColor{R: cr, G: cg, B: cb}
opt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFRectOpt) FillColorHex(c uint32) *PDFRectOpt {
opt.fillColor = &PDFColor{R: int((c >> 16) & 0xFF), G: int((c >> 8) & 0xFF), B: int((c >> 0) & 0xFF)}
opt.fillColor = langext.Ptr(hexToColor(c))
return opt
}