Skip to content

Commit

Permalink
Simplify col (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Sep 17, 2023
1 parent 8334552 commit c4f1c30
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/assets/examples/customsize/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
Size: 12,
Extrapolate: true,
}),
col.Empty(4),
col.New(4),
)

document, err := m.Generate()
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/examples/margins/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
Size: 12,
Extrapolate: true,
}),
col.Empty(4),
col.New(4),
)

document, err := m.Generate()
Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/code/barcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewBarCol(size int, code string, ps ...props.Barcode) domain.Col {

func NewBarRow(height float64, code string, ps ...props.Barcode) domain.Row {
bar := NewBar(code, ps...)
c := col.New(0).Add(bar)
c := col.New().Add(bar)
return row.New(height).Add(c)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/code/matrixcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewMatrixCol(size int, code string, ps ...props.Rect) domain.Col {

func NewMatrixRow(height float64, code string, ps ...props.Rect) domain.Row {
matrixCode := NewMatrix(code, ps...)
c := col.New(0).Add(matrixCode)
c := col.New().Add(matrixCode)
return row.New(height).Add(c)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/code/qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewQrCol(size int, code string, ps ...props.Rect) domain.Col {

func NewQrRow(height float64, code string, ps ...props.Rect) domain.Row {
qrCode := NewQr(code, ps...)
c := col.New(0).Add(qrCode)
c := col.New().Add(qrCode)
return row.New(height).Add(c)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Col interface {
Node
Add(components ...Component) Col
AddInner(rows ...Row) Col
GetSize() (int, bool)
GetSize() int
WithStyle(style *props.Style) Col
Render(provider Provider, cell internal.Cell, createCell bool)
}
Expand Down
22 changes: 10 additions & 12 deletions pkg/v2/grid/col/col.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ type col struct {
style *props.Style
}

func New(size int) domain.Col {
return &col{
size: size,
isMax: size == 0,
func New(size ...int) domain.Col {
if len(size) == 0 {
return &col{isMax: true}
}
}

func Empty(size int) domain.Col {
return &col{
size: size,
isMax: size == 0,
}
return &col{size: size[0]}
}

func (c *col) Add(components ...domain.Component) domain.Col {
Expand All @@ -43,8 +37,12 @@ func (c *col) AddInner(rows ...domain.Row) domain.Col {
return c
}

func (c *col) GetSize() (int, bool) {
return c.size, c.isMax
func (c *col) GetSize() int {
if c.isMax {
return c.config.MaxGridSize
}

return c.size
}

func (c *col) GetStructure() *tree.Node[domain.Structure] {
Expand Down
7 changes: 2 additions & 5 deletions pkg/v2/grid/row/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func New(height float64) domain.Row {

func Empty(height float64) domain.Row {
r := New(height)
r.Add(col.Empty(12))
r.Add(col.New())
return r
}

Expand Down Expand Up @@ -70,13 +70,10 @@ func (r *row) Render(provider domain.Provider, cell internal.Cell) {
}

for _, col := range r.cols {
size, isMax := col.GetSize()
size := col.GetSize()
parentWidth := cell.Width

percent := float64(size) / float64(r.config.MaxGridSize)
if isMax {
percent = 1
}

colDimension := parentWidth * percent
innerCell.Width = colDimension
Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/image/base64image.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewFromBase64Col(size int, path string, extension consts.Extension, ps ...p

func NewFromBase64Row(height float64, path string, extension consts.Extension, ps ...props.Rect) domain.Row {
image := NewFromBase64(path, extension, ps...)
c := col.New(0).Add(image)
c := col.New().Add(image)
return row.New(height).Add(c)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/image/fileimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewFromFileCol(size int, path string, ps ...props.Rect) domain.Col {

func NewFromFileRow(height float64, path string, ps ...props.Rect) domain.Row {
image := NewFromFile(path, ps...)
c := col.New(0).Add(image)
c := col.New().Add(image)
return row.New(height).Add(c)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewCol(size int, value string, ps ...props.Font) domain.Col {

func NewRow(height float64, value string, ps ...props.Font) domain.Row {
signature := New(value, ps...)
c := col.New(0).Add(signature)
c := col.New().Add(signature)
return row.New(height).Add(c)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/v2/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewCol(size int, value string, ps ...props.Text) domain.Col {

func NewRow(height float64, value string, ps ...props.Text) domain.Row {
r := New(value, ps...)
c := col.New(0).Add(r)
c := col.New().Add(r)
return row.New(height).Add(c)
}

Expand Down

0 comments on commit c4f1c30

Please sign in to comment.