Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
194ea4ace5
|
|||
73b80a66bc
|
|||
d8b2d01274
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.457"
|
const GoextVersion = "0.0.460"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-05-20T00:07:33+0200"
|
const GoextVersionTimestamp = "2024-05-20T00:38:04+0200"
|
||||||
|
@@ -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)
|
// 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))
|
ow := int(math.Round(bbw * facOut))
|
||||||
oh := int(math.Round(bbh * facOut))
|
oh := int(math.Round(bbh * facOut))
|
||||||
|
|
||||||
facScale := mathext.Min(float64(ow)/float64(iw), float64(oh)/float64(ih))
|
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))
|
dw := int(math.Round(float64(iw) * facScale))
|
||||||
dh := int(math.Round(float64(ih) * 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 {
|
if fit == ImageFitContainCenter {
|
||||||
destBounds = image.Rect((ow-dw)/2, (oh-dh)/2, (ow-dw)/2+dw, (oh-dh)/2+dh)
|
destBounds = image.Rect((ow-dw)/2, (oh-dh)/2, (ow-dw)/2+dw, (oh-dh)/2+dh)
|
||||||
} else if fit == ImageFitContainTopLeft {
|
} else if fit == ImageFitContainTopLeft {
|
||||||
destBounds = image.Rect(0, 0, iw, dh)
|
destBounds = image.Rect(0, 0, dw, dh)
|
||||||
} else if fit == ImageFitContainTopRight {
|
} else if fit == ImageFitContainTopRight {
|
||||||
destBounds = image.Rect(ow-iw, 0, ow, dh)
|
destBounds = image.Rect(ow-dw, 0, ow, dh)
|
||||||
} else if fit == ImageFitContainBottomLeft {
|
} else if fit == ImageFitContainBottomLeft {
|
||||||
destBounds = image.Rect(0, oh-dh, iw, oh)
|
destBounds = image.Rect(0, oh-dh, dw, oh)
|
||||||
} else if fit == ImageFitContainBottomRight {
|
} else if fit == ImageFitContainBottomRight {
|
||||||
destBounds = image.Rect(ow-dw, oh-dh, ow, oh)
|
destBounds = image.Rect(ow-dw, oh-dh, ow, oh)
|
||||||
}
|
}
|
||||||
|
9
wpdf/utils.go
Normal file
9
wpdf/utils.go
Normal 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}
|
||||||
|
}
|
@@ -17,6 +17,9 @@ type PDFCellOpt struct {
|
|||||||
extraLn *float64
|
extraLn *float64
|
||||||
x *float64
|
x *float64
|
||||||
autoWidth *bool
|
autoWidth *bool
|
||||||
|
textColor *PDFColor
|
||||||
|
borderColor *PDFColor
|
||||||
|
fillColor *PDFColor
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPDFCellOpt() *PDFCellOpt {
|
func NewPDFCellOpt() *PDFCellOpt {
|
||||||
@@ -110,6 +113,36 @@ func (opt *PDFCellOpt) AutoWidth() *PDFCellOpt {
|
|||||||
return opt
|
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) {
|
func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
|
||||||
|
|
||||||
txtTR := b.tr(txt)
|
txtTR := b.tr(txt)
|
||||||
@@ -128,6 +161,9 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
|
|||||||
extraLn := float64(0)
|
extraLn := float64(0)
|
||||||
var x *float64
|
var x *float64
|
||||||
autoWidth := false
|
autoWidth := false
|
||||||
|
var textColor *PDFColor
|
||||||
|
var borderColor *PDFColor
|
||||||
|
var fillColor *PDFColor
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
width = langext.Coalesce(opt.width, width)
|
width = langext.Coalesce(opt.width, width)
|
||||||
@@ -144,6 +180,9 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
|
|||||||
extraLn = langext.Coalesce(opt.extraLn, extraLn)
|
extraLn = langext.Coalesce(opt.extraLn, extraLn)
|
||||||
x = langext.CoalesceOpt(opt.x, x)
|
x = langext.CoalesceOpt(opt.x, x)
|
||||||
autoWidth = langext.Coalesce(opt.autoWidth, autoWidth)
|
autoWidth = langext.Coalesce(opt.autoWidth, autoWidth)
|
||||||
|
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 {
|
if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil {
|
||||||
@@ -157,6 +196,24 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
|
|||||||
defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }()
|
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 {
|
if x != nil {
|
||||||
b.b.SetX(*x)
|
b.b.SetX(*x)
|
||||||
}
|
}
|
||||||
|
@@ -7,9 +7,7 @@ import (
|
|||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/png"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PDFImageRef struct {
|
type PDFImageRef struct {
|
||||||
@@ -229,7 +227,7 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
|||||||
reEncodePixelPerMM := 15.0
|
reEncodePixelPerMM := 15.0
|
||||||
var imageFit *imageext.ImageFit = nil
|
var imageFit *imageext.ImageFit = nil
|
||||||
var fillColor color.Color = color.Transparent
|
var fillColor color.Color = color.Transparent
|
||||||
var compression *imageext.ImageCompresson = nil
|
compression := imageext.CompressionPNGSpeed
|
||||||
var crop *imageext.ImageCrop = nil
|
var crop *imageext.ImageCrop = nil
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -245,14 +243,14 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
|||||||
allowNegativePosition = langext.Coalesce(opt.allowNegativePosition, allowNegativePosition)
|
allowNegativePosition = langext.Coalesce(opt.allowNegativePosition, allowNegativePosition)
|
||||||
imageFit = langext.CoalesceOpt(opt.imageFit, imageFit)
|
imageFit = langext.CoalesceOpt(opt.imageFit, imageFit)
|
||||||
fillColor = langext.Coalesce(opt.fillColor, fillColor)
|
fillColor = langext.Coalesce(opt.fillColor, fillColor)
|
||||||
compression = langext.CoalesceOpt(opt.compression, compression)
|
compression = langext.Coalesce(opt.compression, compression)
|
||||||
reEncodePixelPerMM = langext.Coalesce(opt.reEncodePixelPerMM, reEncodePixelPerMM)
|
reEncodePixelPerMM = langext.Coalesce(opt.reEncodePixelPerMM, reEncodePixelPerMM)
|
||||||
crop = langext.CoalesceOpt(opt.crop, crop)
|
crop = langext.CoalesceOpt(opt.crop, crop)
|
||||||
}
|
}
|
||||||
|
|
||||||
regName := img.Name
|
regName := img.Name
|
||||||
|
|
||||||
if imageFit != nil || fillColor != nil || crop != nil || compression != nil {
|
if imageFit != nil || fillColor != nil || crop != nil {
|
||||||
|
|
||||||
var dataimg image.Image
|
var dataimg image.Image
|
||||||
if img.Image != nil {
|
if img.Image != nil {
|
||||||
@@ -263,16 +261,6 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
|||||||
b.b.SetError(err)
|
b.b.SetError(err)
|
||||||
return
|
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 {
|
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 {
|
if err != nil {
|
||||||
b.b.SetError(err)
|
b.b.SetError(err)
|
||||||
return
|
return
|
||||||
@@ -313,8 +301,6 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
|||||||
imageType = "GIF"
|
imageType = "GIF"
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = os.WriteFile("/tmp/c.png", bfr.Bytes(), 0755)
|
|
||||||
|
|
||||||
b.b.RegisterImageOptionsReader(regName, gofpdf.ImageOptions{ImageType: imageType}, &bfr)
|
b.b.RegisterImageOptionsReader(regName, gofpdf.ImageOptions{ImageType: imageType}, &bfr)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,9 @@ type PDFMultiCellOpt struct {
|
|||||||
fontSizeOverride *float64
|
fontSizeOverride *float64
|
||||||
extraLn *float64
|
extraLn *float64
|
||||||
x *float64
|
x *float64
|
||||||
|
textColor *PDFColor
|
||||||
|
borderColor *PDFColor
|
||||||
|
fillColor *PDFColor
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPDFMultiCellOpt() *PDFMultiCellOpt {
|
func NewPDFMultiCellOpt() *PDFMultiCellOpt {
|
||||||
@@ -86,6 +89,36 @@ func (opt *PDFMultiCellOpt) X(v float64) *PDFMultiCellOpt {
|
|||||||
return opt
|
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) {
|
func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
|
||||||
|
|
||||||
txtTR := b.tr(txt)
|
txtTR := b.tr(txt)
|
||||||
@@ -100,6 +133,9 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
|
|||||||
var fontSizeOverride *float64
|
var fontSizeOverride *float64
|
||||||
extraLn := float64(0)
|
extraLn := float64(0)
|
||||||
var x *float64
|
var x *float64
|
||||||
|
var textColor *PDFColor
|
||||||
|
var borderColor *PDFColor
|
||||||
|
var fillColor *PDFColor
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
width = langext.Coalesce(opt.width, width)
|
width = langext.Coalesce(opt.width, width)
|
||||||
@@ -112,6 +148,9 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
|
|||||||
fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride)
|
fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride)
|
||||||
extraLn = langext.Coalesce(opt.extraLn, extraLn)
|
extraLn = langext.Coalesce(opt.extraLn, extraLn)
|
||||||
x = langext.CoalesceOpt(opt.x, x)
|
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 {
|
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) }()
|
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 {
|
if x != nil {
|
||||||
b.b.SetX(*x)
|
b.b.SetX(*x)
|
||||||
}
|
}
|
||||||
|
@@ -34,22 +34,22 @@ func (opt *PDFRectOpt) LineWidth(v float64) *PDFRectOpt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (opt *PDFRectOpt) DrawColor(cr, cg, cb int) *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
|
return opt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt *PDFRectOpt) DrawColorHex(c uint32) *PDFRectOpt {
|
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
|
return opt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt *PDFRectOpt) FillColor(cr, cg, cb int) *PDFRectOpt {
|
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
|
return opt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt *PDFRectOpt) FillColorHex(c uint32) *PDFRectOpt {
|
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
|
return opt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user