Skip to content

Commit

Permalink
Use pointers in properties
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher committed Sep 24, 2023
1 parent 1870233 commit e6c33bc
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 63 deletions.
Binary file modified docs/assets/pdf/v2.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions docs/assets/text/v2.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
generate -> avg: 18.78ms, executions: [18.78ms]
header -> avg: 458.00ns, executions: [458.00ns]
footer -> avg: 110.00ns, executions: [110.00ns]
add_row -> avg: 122.60ns, executions: [0.13μs, 0.16μs, 0.05μs, 0.08μs, 0.03μs, 0.02μs, 3.80μs, 0.07μs, 0.03μs, 0.07μs, 0.02μs, 0.01μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.02μs, 0.35μs, 0.05μs, 0.01μs, 0.06μs, 0.02μs, 0.02μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.02μs, 0.27μs, 0.05μs, 0.02μs, 0.06μs, 0.03μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.02μs, 0.21μs, 0.05μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.03μs, 0.02μs, 0.25μs, 0.06μs, 0.02μs, 0.06μs, 0.02μs]
generate -> avg: 17.96ms, executions: [17.96ms]
header -> avg: 456.00ns, executions: [456.00ns]
footer -> avg: 60.00ns, executions: [60.00ns]
add_row -> avg: 63.35ns, executions: [153.00ns, 154.00ns, 69.00ns, 83.00ns, 24.00ns, 19.00ns, 586.00ns, 60.00ns, 25.00ns, 62.00ns, 26.00ns, 16.00ns, 17.00ns, 51.00ns, 16.00ns, 17.00ns, 24.00ns, 296.00ns, 51.00ns, 17.00ns, 57.00ns, 19.00ns, 17.00ns, 19.00ns, 63.00ns, 17.00ns, 16.00ns, 16.00ns, 249.00ns, 97.00ns, 21.00ns, 77.00ns, 17.00ns, 19.00ns, 18.00ns, 48.00ns, 19.00ns, 18.00ns, 16.00ns, 212.00ns, 51.00ns, 16.00ns, 62.00ns, 19.00ns, 16.00ns, 15.00ns, 57.00ns, 18.00ns, 19.00ns, 17.00ns, 239.00ns, 54.00ns, 19.00ns, 64.00ns, 17.00ns]
file_size -> 260.96Kb
14 changes: 7 additions & 7 deletions internal/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

// Code is the abstraction which deals of how to add QrCodes or Barcode in a PDF.
type Code interface {
AddQr(code string, cell *core.Cell, prop props.Rect)
AddBar(code string, cell *core.Cell, prop props.Barcode) (err error)
AddDataMatrix(code string, cell *core.Cell, prop props.Rect)
AddQr(code string, cell *core.Cell, prop *props.Rect)
AddBar(code string, cell *core.Cell, prop *props.Barcode) (err error)
AddDataMatrix(code string, cell *core.Cell, prop *props.Rect)
}

type code struct {
Expand All @@ -31,7 +31,7 @@ func NewCode(pdf fpdf.Fpdf, math Math) *code {
}

// AddDataMatrix creates a DataMatrix code inside a cell.
func (s *code) AddDataMatrix(code string, cell *core.Cell, prop props.Rect) {
func (s *code) AddDataMatrix(code string, cell *core.Cell, prop *props.Rect) {
key := barcode.RegisterDataMatrix(s.pdf, code)

var x, y, w, h float64
Expand All @@ -45,7 +45,7 @@ func (s *code) AddDataMatrix(code string, cell *core.Cell, prop props.Rect) {
}

// AddQr create a QrCode inside a cell.
func (s *code) AddQr(code string, cell *core.Cell, prop props.Rect) {
func (s *code) AddQr(code string, cell *core.Cell, prop *props.Rect) {
key := barcode.RegisterQR(s.pdf, code, qr.H, qr.Unicode)

var x, y, w, h float64
Expand All @@ -60,7 +60,7 @@ func (s *code) AddQr(code string, cell *core.Cell, prop props.Rect) {
}

// AddBar create a Barcode inside a cell.
func (s *code) AddBar(code string, cell *core.Cell, prop props.Barcode) (err error) {
func (s *code) AddBar(code string, cell *core.Cell, prop *props.Barcode) (err error) {
bcode, err := code128.Encode(code)
if err != nil {
return
Expand All @@ -72,7 +72,7 @@ func (s *code) AddBar(code string, cell *core.Cell, prop props.Barcode) (err err
dimensions := &config.Dimensions{cell.Width, cell.Width * heightPercentFromWidth}
x, y, w, h = s.math.GetRectCenterColProperties(dimensions, cell, prop.Percent)
} else {
rectProps := props.Rect{Left: prop.Left, Top: prop.Top, Center: prop.Center, Percent: prop.Percent}
rectProps := &props.Rect{Left: prop.Left, Top: prop.Top, Center: prop.Center, Percent: prop.Percent}
x, y, w, h = s.math.GetRectNonCenterColProperties(cell.Width, cell.Width*heightPercentFromWidth, cell.Width,
cell.Height, cell.X, rectProps)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

// Image is the abstraction which deals of how to add images in a PDF.
type Image interface {
AddFromFile(path string, cell *core.Cell, prop props.Rect) (err error)
AddFromBase64(stringBase64 string, cell *core.Cell, prop props.Rect, extension extension.Type) (err error)
AddFromFile(path string, cell *core.Cell, prop *props.Rect) (err error)
AddFromBase64(stringBase64 string, cell *core.Cell, prop *props.Rect, extension extension.Type) (err error)
}

type image struct {
Expand All @@ -35,7 +35,7 @@ func NewImage(pdf fpdf.Fpdf, math Math) *image {
}

// AddFromFile open an image from disk and add to PDF.
func (s *image) AddFromFile(path string, cell *core.Cell, prop props.Rect) error {
func (s *image) AddFromFile(path string, cell *core.Cell, prop *props.Rect) error {
info := s.pdf.RegisterImageOptions(path, gofpdf.ImageOptions{
ReadDpi: false,
ImageType: "",
Expand All @@ -50,7 +50,7 @@ func (s *image) AddFromFile(path string, cell *core.Cell, prop props.Rect) error
}

// AddFromBase64 use a base64 string to add to PDF.
func (s *image) AddFromBase64(stringBase64 string, cell *core.Cell, prop props.Rect, extension extension.Type) error {
func (s *image) AddFromBase64(stringBase64 string, cell *core.Cell, prop *props.Rect, extension extension.Type) error {
imageID, _ := uuid.NewRandom()

ss, _ := base64.StdEncoding.DecodeString(stringBase64)
Expand All @@ -72,7 +72,7 @@ func (s *image) AddFromBase64(stringBase64 string, cell *core.Cell, prop props.R
return nil
}

func (s *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, cell *core.Cell, prop props.Rect) {
func (s *image) addImageToPdf(imageLabel string, info *gofpdf.ImageInfoType, cell *core.Cell, prop *props.Rect) {
var x, y, w, h float64
if prop.Center {
dimensions := &config.Dimensions{Width: info.Width(), Height: info.Height()}
Expand Down
8 changes: 4 additions & 4 deletions internal/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Line interface {
Add(cell *core.Cell, prop props.Line)
Add(cell *core.Cell, prop *props.Line)
}

type line struct {
Expand All @@ -26,15 +26,15 @@ func NewLine(pdf fpdf.Fpdf) *line {
}
}

func (l *line) Add(cell *core.Cell, prop props.Line) {
func (l *line) Add(cell *core.Cell, prop *props.Line) {
if prop.Orientation == orientation.Vertical {
l.renderVertical(cell, prop)
} else {
l.renderHorizontal(cell, prop)
}
}

func (l *line) renderVertical(cell *core.Cell, prop props.Line) {
func (l *line) renderVertical(cell *core.Cell, prop *props.Line) {
size := cell.Height * (prop.SizePercent / 100.0)
position := cell.Width * (prop.OffsetPercent / 100.0)

Expand All @@ -59,7 +59,7 @@ func (l *line) renderVertical(cell *core.Cell, prop props.Line) {
}
}

func (l *line) renderHorizontal(cell *core.Cell, prop props.Line) {
func (l *line) renderHorizontal(cell *core.Cell, prop *props.Line) {
size := cell.Width * (prop.SizePercent / 100.0)
position := cell.Height * (prop.OffsetPercent / 100.0)

Expand Down
4 changes: 2 additions & 2 deletions internal/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
type Math interface {
GetRectCenterColProperties(dimensions *config.Dimensions, cell *core.Cell, percent float64) (x float64, y float64, w float64, h float64)
GetRectNonCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64,
xColOffset float64, prop props.Rect) (x float64, y float64, w float64, h float64)
xColOffset float64, prop *props.Rect) (x float64, y float64, w float64, h float64)
GetCenterCorrection(outerSize, innerSize float64) float64
}

Expand Down Expand Up @@ -67,7 +67,7 @@ func (s *math) GetRectCenterColProperties(dimensions *config.Dimensions, cell *c

// GetRectNonCenterColProperties define Width, Height to and rectangle (QrCode, Barcode, Image) inside a cell.
func (s *math) GetRectNonCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64,
xColOffset float64, prop props.Rect,
xColOffset float64, prop *props.Rect,
) (x float64, y float64, w float64, h float64) {
percent := prop.Percent / maxPercent
left, top, _, _ := s.pdf.GetMargins()
Expand Down
4 changes: 2 additions & 2 deletions internal/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Signature is the abstraction which deals of how to add a signature space inside PDF.
type Signature interface {
AddSpaceFor(label string, cell *core.Cell, textProp props.Text)
AddSpaceFor(label string, cell *core.Cell, textProp *props.Text)
}

type signature struct {
Expand All @@ -27,7 +27,7 @@ func NewSignature(pdf fpdf.Fpdf, math Math, text Text) *signature {
}

// AddSpaceFor create a space for a signature inside a cell.
func (s *signature) AddSpaceFor(label string, cell *core.Cell, textProp props.Text) {
func (s *signature) AddSpaceFor(label string, cell *core.Cell, textProp *props.Text) {
lineSpaceProportion := 1.33
left, top, _, _ := s.pdf.GetMargins()
space := 4.0
Expand Down
8 changes: 4 additions & 4 deletions internal/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// Text is the abstraction which deals of how to add text inside PDF.
type Text interface {
Add(text string, cell *core.Cell, textProp props.Text)
Add(text string, cell *core.Cell, textProp *props.Text)
GetLinesQuantity(text string, fontFamily props.Text, colWidth float64) int
}

Expand All @@ -37,7 +37,7 @@ func NewText(pdf fpdf.Fpdf, math Math, font Font) *text {
}

// Add a text inside a cell.
func (s *text) Add(text string, cell *core.Cell, textProp props.Text) {
func (s *text) Add(text string, cell *core.Cell, textProp *props.Text) {
s.font.SetFont(textProp.Family, textProp.Style, textProp.Size)

if textProp.Top > cell.Height {
Expand Down Expand Up @@ -169,7 +169,7 @@ func (s *text) getLinesBreakingLineWithDash(words string, colWidth float64) []st
return lines
}

func (s *text) addLine(textProp props.Text, xColOffset, colWidth, yColOffset, textWidth float64, text string) {
func (s *text) addLine(textProp *props.Text, xColOffset, colWidth, yColOffset, textWidth float64, text string) {
left, top, _, _ := s.pdf.GetMargins()

if textProp.Align == align.Left {
Expand All @@ -188,7 +188,7 @@ func (s *text) addLine(textProp props.Text, xColOffset, colWidth, yColOffset, te
s.pdf.Text(dx+xColOffset+left, yColOffset+top, text)
}

func (s *text) textToUnicode(txt string, props props.Text) string {
func (s *text) textToUnicode(txt string, props *props.Text) string {
if props.Family == fontfamily.Arial ||
props.Family == fontfamily.Helvetica ||
props.Family == fontfamily.Symbol ||
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/code/barcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewBarRow(height float64, code string, ps ...props.Barcode) core.Row {
}

func (b *barcode) Render(provider core.Provider, cell *core.Cell) {
provider.AddBarCode(b.code, cell, b.prop)
provider.AddBarCode(b.code, cell, &b.prop)
}

func (b *barcode) GetStructure() *tree.Node[core.Structure] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/code/matrixcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewMatrixRow(height float64, code string, ps ...props.Rect) core.Row {
}

func (m *matrixCode) Render(provider core.Provider, cell *core.Cell) {
provider.AddMatrixCode(m.code, cell, m.prop)
provider.AddMatrixCode(m.code, cell, &m.prop)
}

func (m *matrixCode) GetStructure() *tree.Node[core.Structure] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/code/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewQrRow(height float64, code string, ps ...props.Rect) core.Row {
}

func (q *qrCode) Render(provider core.Provider, cell *core.Cell) {
provider.AddQrCode(q.code, cell, q.prop)
provider.AddQrCode(q.code, cell, &q.prop)
}

func (q *qrCode) GetStructure() *tree.Node[core.Structure] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/image/base64image.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewFromBase64Row(height float64, path string, extension extension.Type, ps
}

func (b *base64Image) Render(provider core.Provider, cell *core.Cell) {
provider.AddImage(b.base64, cell, b.prop, b.extension)
provider.AddImage(b.base64, cell, &b.prop, b.extension)
}

func (b *base64Image) GetStructure() *tree.Node[core.Structure] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/image/fileimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewFromFileRow(height float64, path string, ps ...props.Rect) core.Row {

func (f *fileImage) Render(provider core.Provider, cell *core.Cell) {
extensionStr := strings.Split(f.path, ".")[1]
provider.AddImage(f.path, cell, f.prop, extension.Type(extensionStr))
provider.AddImage(f.path, cell, &f.prop, extension.Type(extensionStr))
}

func (f *fileImage) GetStructure() *tree.Node[core.Structure] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/components/line/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ func (l *line) SetConfig(config *config.Config) {
}

func (l *line) Render(provider core.Provider, cell *core.Cell) {
provider.AddLine(cell, l.prop)
provider.AddLine(cell, &l.prop)
}
2 changes: 1 addition & 1 deletion pkg/components/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ func (t *text) SetConfig(config *config.Config) {

func (t *text) Render(provider core.Provider, cell *core.Cell) {
t.prop.MakeValid(t.config.DefaultFont)
provider.AddText(t.value, cell, t.prop)
provider.AddText(t.value, cell, &t.prop)
}
14 changes: 7 additions & 7 deletions pkg/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Provider interface {
CreateCol(width, height float64, config *config.Config, prop *props.Cell)

// Features
AddLine(cell *Cell, prop props.Line)
AddText(text string, cell *Cell, prop props.Text)
AddSignature(text string, cell *Cell, prop props.Text)
AddMatrixCode(code string, cell *Cell, prop props.Rect)
AddQrCode(code string, cell *Cell, rect props.Rect)
AddBarCode(code string, cell *Cell, prop props.Barcode)
AddImage(value string, cell *Cell, prop props.Rect, extension extension.Type)
AddLine(cell *Cell, prop *props.Line)
AddText(text string, cell *Cell, prop *props.Text)
AddSignature(text string, cell *Cell, prop *props.Text)
AddMatrixCode(code string, cell *Cell, prop *props.Rect)
AddQrCode(code string, cell *Cell, rect *props.Rect)
AddBarCode(code string, cell *Cell, prop *props.Barcode)
AddImage(value string, cell *Cell, prop *props.Rect, extension extension.Type)

// General
GetDimensions() (width float64, height float64)
Expand Down
4 changes: 2 additions & 2 deletions pkg/props/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (s *Font) MakeValid(defaultFamily string) {
}

// ToTextProp from Font return a Text based on Font.
func (s *Font) ToTextProp(align align.Type, top float64, verticalPadding float64) Text {
textProp := Text{
func (s *Font) ToTextProp(align align.Type, top float64, verticalPadding float64) *Text {
textProp := &Text{
Family: s.Family,
Style: s.Style,
Size: s.Size,
Expand Down
4 changes: 2 additions & 2 deletions pkg/props/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type Page struct {
Color *Color
}

func (p *Page) GetNumberTextProp(height float64) Text {
text := Text{
func (p *Page) GetNumberTextProp(height float64) *Text {
text := &Text{
Family: p.Family,
Style: p.Style,
Size: p.Size,
Expand Down
20 changes: 10 additions & 10 deletions pkg/providers/gofpdf/gofpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,40 +95,40 @@ func (g *gofpdfProvider) GetMargins() (left float64, top float64, right float64,
return g.fpdf.GetMargins()
}

func (g *gofpdfProvider) AddText(text string, cell *core.Cell, prop props.Text) {
func (g *gofpdfProvider) AddText(text string, cell *core.Cell, prop *props.Text) {
g.text.Add(text, cell, prop)
}

func (g *gofpdfProvider) AddLine(cell *core.Cell, prop props.Line) {
func (g *gofpdfProvider) AddLine(cell *core.Cell, prop *props.Line) {
g.line.Add(cell, prop)
}

func (g *gofpdfProvider) AddSignature(text string, cell *core.Cell, prop props.Text) {
func (g *gofpdfProvider) AddSignature(text string, cell *core.Cell, prop *props.Text) {
g.signature.AddSpaceFor(text, cell, prop)
}

func (g *gofpdfProvider) AddMatrixCode(code string, cell *core.Cell, prop props.Rect) {
func (g *gofpdfProvider) AddMatrixCode(code string, cell *core.Cell, prop *props.Rect) {
g.code.AddDataMatrix(code, cell, prop)
}

func (g *gofpdfProvider) AddQrCode(code string, cell *core.Cell, rect props.Rect) {
func (g *gofpdfProvider) AddQrCode(code string, cell *core.Cell, rect *props.Rect) {
g.code.AddQr(code, cell, rect)
}

func (g *gofpdfProvider) AddBarCode(code string, cell *core.Cell, prop props.Barcode) {
func (g *gofpdfProvider) AddBarCode(code string, cell *core.Cell, prop *props.Barcode) {
err := g.code.AddBar(code, cell, prop)
if err != nil {
textProp := props.Text{}
textProp := &props.Text{}
textProp.MakeValid(defaultErrorColor)
g.fpdf.ClearError()
g.AddText("Failed to render code", cell, textProp)
}
}

func (g *gofpdfProvider) AddImage(file string, cell *core.Cell, prop props.Rect, extension extension.Type) {
func (g *gofpdfProvider) AddImage(file string, cell *core.Cell, prop *props.Rect, extension extension.Type) {
img, err := g.imageCache.Load(file, extension)
if err != nil {
textProp := props.Text{}
textProp := &props.Text{}
textProp.MakeValid(defaultErrorColor)
g.fpdf.ClearError()
g.AddText("Failed to load image from file", cell, textProp)
Expand All @@ -137,7 +137,7 @@ func (g *gofpdfProvider) AddImage(file string, cell *core.Cell, prop props.Rect,

err = g.image.AddFromBase64(img.Value, cell, prop, img.Extension)
if err != nil {
textProp := props.Text{}
textProp := &props.Text{}
textProp.MakeValid(defaultErrorColor)
g.fpdf.ClearError()
g.AddText("Failed to load image from file", cell, textProp)
Expand Down
Loading

0 comments on commit e6c33bc

Please sign in to comment.