Skip to content

Commit

Permalink
Add line (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Sep 23, 2023
1 parent ccaf6de commit 1228e6a
Show file tree
Hide file tree
Showing 26 changed files with 305 additions and 108 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ examples: font
go run docs/assets/examples/footer/v2/main.go
go run docs/assets/examples/header/v2/main.go
go run docs/assets/examples/imagegrid/v2/main.go
go run docs/assets/examples/line/v2/main.go
go run docs/assets/examples/list/v2/main.go
go run docs/assets/examples/margins/v2/main.go
go run docs/assets/examples/maxgridsum/v2/main.go
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ always when a new page appear, in this case, a header may have many rows, lines
* With `go get`:

```bash
go get github.com/johnfercher/maroto/v2/[email protected].19
go get github.com/johnfercher/maroto/v2/[email protected].20
```


Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#template-engines) [![Branch](https://img.shields.io/badge/V2-Branch-pink)](https://github.com/johnfercher/maroto/tree/v2) [![Roadmap](https://img.shields.io/badge/V2-Roadmap-purple)](https://github.com/users/johnfercher/projects/1) [![Discussion](https://img.shields.io/badge/V2-Discussion-blue)](https://github.com/johnfercher/maroto/issues/257) [![Release Notes](https://img.shields.io/badge/Release-Notes-cyan)](https://github.com/johnfercher/maroto/releases) [![Visits Badge](https://badges.pufler.dev/visits/johnfercher/maroto)](https://badges.pufler.dev)

#### Maroto`v2.0.0-alpha.19`is here! Try out:
#### Maroto`v2.0.0-alpha.20`is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2/[email protected].19
go get github.com/johnfercher/maroto/v2/[email protected].20
```

The public API was completely redesigned with the aim of enhancing the
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [Footer](v2/features/footer.md?id=footer)
* [Header](v2/features/header.md?id=header)
* [Images](v2/features/image.md?id=image)
* [Line](v2/features/line.md?id=line)
* [List](v2/features/list.md?id=list)
* [Margins](v2/features/margins.md?id=custom-margins)
* [Max Grid Sum](v2/features/maxgridsum.md?id=max-grid-sum)
Expand Down
4 changes: 2 additions & 2 deletions docs/assets/examples/cellstyle/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"math/rand"

"github.com/johnfercher/maroto/v2/pkg/consts/line"
"github.com/johnfercher/maroto/v2/pkg/consts/linestyle"

"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/components/text"
Expand All @@ -26,7 +26,7 @@ func main() {
BackgroundColor: &props.Color{80, 80, 80},
BorderType: border.Full,
BorderColor: &props.Color{200, 0, 0},
LineStyle: line.Dashed,
LineStyle: linestyle.Dashed,
BorderThickness: 0.5,
}

Expand Down
73 changes: 73 additions & 0 deletions docs/assets/examples/line/v2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"log"

"github.com/johnfercher/maroto/v2/pkg/components/line"
"github.com/johnfercher/maroto/v2/pkg/consts/linestyle"
"github.com/johnfercher/maroto/v2/pkg/consts/orientation"
"github.com/johnfercher/maroto/v2/pkg/props"

"github.com/johnfercher/maroto/v2/pkg"
"github.com/johnfercher/maroto/v2/pkg/config"
)

var redColor = props.Color{
Red: 255,
Green: 0,
Blue: 0,
}

func main() {
cfg := config.NewBuilder().
WithDebug(true).
Build()

mrt := pkg.NewMaroto(cfg)
m := pkg.NewMetricsDecorator(mrt)

m.AddRow(40,
line.NewCol(2),
line.NewCol(4),
line.NewCol(6),
)

m.AddRow(40,
line.NewCol(6),
line.NewCol(4),
line.NewCol(2),
)

m.AddRow(40,
line.NewCol(2, props.Line{Thickness: 0.5}),
line.NewCol(4, props.Line{Color: redColor}),
line.NewCol(6, props.Line{Orientation: orientation.Vertical}),
)

m.AddRow(40,
line.NewCol(6, props.Line{OffsetPercent: 50}),
line.NewCol(4, props.Line{OffsetPercent: 50, Orientation: orientation.Vertical}),
line.NewCol(2, props.Line{SizePercent: 50}),
)

m.AddRow(40,
line.NewCol(2, props.Line{Style: linestyle.Dashed}),
line.NewCol(4, props.Line{Color: redColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Vertical, OffsetPercent: 70, SizePercent: 70}),
line.NewCol(6, props.Line{Color: redColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Horizontal, OffsetPercent: 40, SizePercent: 40}),
)

document, err := m.Generate()
if err != nil {
log.Fatal(err.Error())
}

err = document.Save("docs/assets/pdf/linegridv2.pdf")
if err != nil {
log.Fatal(err.Error())
}

err = document.GetReport().Save("docs/assets/text/linegridv2.txt")
if err != nil {
log.Fatal(err.Error())
}
}
2 changes: 1 addition & 1 deletion docs/assets/examples/orientation/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
cfg := config.NewBuilder().
WithOrientation(orientation.Landscape).
WithOrientation(orientation.Horizontal).
WithDebug(true).
Build()

Expand Down
Binary file added docs/assets/pdf/linegridv2.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions docs/assets/text/linegridv2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generate -> avg: 66.16μs, executions: [66.16μs]
add_cols -> avg: 184.60ns, executions: [498.00ns, 139.00ns, 118.00ns, 68.00ns, 100.00ns]
file_size -> 2.67Kb
11 changes: 11 additions & 0 deletions docs/v2/features/line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Line

## Code Example
[filename](../../assets/examples/line/v2/main.go ':include :type=code')

## PDF Generated
```pdf
assets/pdf/linegridv2.pdf
```
## Time Execution
[filename](../../assets/text/linegridv2.txt ':include :type=code')
85 changes: 85 additions & 0 deletions internal/line.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package internal

import (
"github.com/johnfercher/maroto/v2/internal/fpdf"
"github.com/johnfercher/maroto/v2/pkg/consts/linestyle"
"github.com/johnfercher/maroto/v2/pkg/consts/orientation"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/props"
)

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

type line struct {
pdf fpdf.Fpdf
defaultColor *props.Color
defaultThickness float64
}

func NewLine(pdf fpdf.Fpdf) *line {
return &line{
pdf: pdf,
defaultColor: props.NewBlack(),
defaultThickness: linestyle.DefaultLineThickness,
}
}

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) {
size := cell.Height * (prop.SizePercent / 100.0)
position := cell.Width * (prop.OffsetPercent / 100.0)

space := (cell.Height - size) / 2.0

left, top, _, _ := l.pdf.GetMargins()

l.pdf.SetDrawColor(prop.Color.Red, prop.Color.Green, prop.Color.Blue)
l.pdf.SetLineWidth(prop.Thickness)

if prop.Style != linestyle.Solid {
l.pdf.SetDashPattern([]float64{1, 1}, 0)
}

l.pdf.Line(left+cell.X+position, top+cell.Y+space, left+cell.X+position, top+cell.Y+cell.Height-space)

l.pdf.SetDrawColor(l.defaultColor.Red, l.defaultColor.Green, l.defaultColor.Blue)
l.pdf.SetLineWidth(l.defaultThickness)

if prop.Style != linestyle.Solid {
l.pdf.SetDashPattern([]float64{1, 0}, 0)
}
}

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

space := (cell.Width - size) / 2.0

left, top, _, _ := l.pdf.GetMargins()

l.pdf.SetDrawColor(prop.Color.Red, prop.Color.Green, prop.Color.Blue)
l.pdf.SetLineWidth(prop.Thickness)

if prop.Style != linestyle.Solid {
l.pdf.SetDashPattern([]float64{1, 1}, 0)
}

l.pdf.Line(left+cell.X+space, top+cell.Y+position, left+cell.X+cell.Width-space, top+cell.Y+position)

l.pdf.SetDrawColor(l.defaultColor.Red, l.defaultColor.Green, l.defaultColor.Blue)
l.pdf.SetLineWidth(l.defaultThickness)

if prop.Style != linestyle.Solid {
l.pdf.SetDashPattern([]float64{1, 0}, 0)
}
}
54 changes: 54 additions & 0 deletions pkg/components/line/line.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package line

import (
"github.com/johnfercher/go-tree/tree"
"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/config"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/props"
)

type line struct {
config *config.Config
prop props.Line
}

func New(ps ...props.Line) core.Component {
lineProp := props.Line{}
if len(ps) > 0 {
lineProp = ps[0]
}
lineProp.MakeValid()

return &line{
prop: lineProp,
}
}

func NewCol(size int, ps ...props.Line) core.Col {
r := New(ps...)
return col.New(size).Add(r)
}

func NewRow(height float64, ps ...props.Line) core.Row {
r := New(ps...)
c := col.New().Add(r)
return row.New(height).Add(c)
}

func (l *line) GetStructure() *tree.Node[core.Structure] {
str := core.Structure{
Type: "linestyle",
}

return tree.NewNode(str)
}

func (l *line) SetConfig(config *config.Config) {
l.config = config
}

func (l *line) Render(provider core.Provider, cell core.Cell) {
provider.AddLine(cell, l.prop)
}
7 changes: 0 additions & 7 deletions pkg/components/row/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package row
import (
"fmt"

"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/config"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/props"
Expand All @@ -24,12 +23,6 @@ func New(height float64) core.Row {
}
}

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

func (r *row) SetConfig(config *config.Config) {
r.config = config
for _, cols := range r.cols {
Expand Down
4 changes: 0 additions & 4 deletions pkg/components/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func (t *text) SetConfig(config *config.Config) {
t.config = config
}

func (t *text) GetValue() string {
return t.value
}

func (t *text) Render(provider core.Provider, cell core.Cell) {
t.prop.MakeValid(t.config.DefaultFont)
provider.AddText(t.value, cell, t.prop)
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (b *builder) getDimensions() *Dimensions {
Height: height,
}

if b.orientation == orientation.Landscape && height > width {
if b.orientation == orientation.Horizontal && height > width {
dimensions.Width, dimensions.Height = dimensions.Height, dimensions.Width
}

Expand Down
16 changes: 0 additions & 16 deletions pkg/consts/line/line.go

This file was deleted.

16 changes: 16 additions & 0 deletions pkg/consts/linestyle/linestyle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package linestyle

const (
// DefaultLineThickness represents the default linestyle width in gofpdf.
DefaultLineThickness float64 = 0.2
)

// Type is a representation of a linestyle style.
type Type string

const (
// Solid represents a solid style.
Solid Type = "solid"
// Dashed represents a dashed style.
Dashed Type = "dashed"
)
8 changes: 4 additions & 4 deletions pkg/consts/orientation/orientation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package orientation
type Type string

const (
// Portrait represents the portrait orientation.
Portrait Type = "P"
// Landscape represents the landscape orientation.
Landscape Type = "L"
// Vertical represents the portrait orientation.
Vertical Type = "P"
// Horizontal represents the landscape orientation.
Horizontal Type = "L"
)
1 change: 1 addition & 0 deletions pkg/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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)
Expand Down
Loading

0 comments on commit 1228e6a

Please sign in to comment.