Skip to content

Commit

Permalink
finish mutil table
Browse files Browse the repository at this point in the history
  • Loading branch information
tiechui1994 committed Apr 8, 2019
1 parent 28847d7 commit f7d98f8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
64 changes: 62 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (table *Table) drawPageLines(sx, sy float64) {
}

if y1 < pageEndY && y2 < pageEndY {
// 判断当前的cell的下面的实体是否写入, TODO: 还需要判断是否需要竖线, 当前只是解决了是否需要水平线
// 判断当前的cell的下面的实体是否写入
i, j := cell.row+cell.rowspan-table.cells[0][0].row, cell.col-table.cells[0][0].col
_, y3, _, y4 := table.getVLinePosition(sx, sy, j, i)
if y3 < pageEndY && y4 >= pageEndY {
Expand All @@ -584,8 +584,11 @@ func (table *Table) drawPageLines(sx, sy float64) {
table.pdf.LineH(x, y2, x1) // 需要判断底线否
}

// TODO: 需要先判断是否需要竖线
if y1 < pageEndY && y2 >= pageEndY {
table.pdf.LineV(x1, y1, pageEndY)
if table.checkNeedVline(row, col) {
table.pdf.LineV(x1, y1, pageEndY)
}
table.pdf.LineH(x, pageEndY, x1)
}
}
Expand Down Expand Up @@ -682,6 +685,60 @@ func (table *Table) checkNextCellWrite(row, col int) bool {
return cellwrited
}

func (table *Table) checkNeedVline(row, col int) bool {
var (
needvline bool
curwrited bool
cells = table.cells
origin = cells[0][0]
)

if cells[row][col].rowspan <= 0 {
return needvline
}

// 当前的cell
if cells[row][col].rowspan == 1 {
origin, remian := cells[row][col].element.GetLines()
if cells[row][col].element.GetHeight() == 0 || origin-remian > 0 {
curwrited = true
}
}
if cells[row][col].rowspan > 1 {
if cells[row][col].cellwrited > 0 {
curwrited = true
}
}

// 邻居cell
nextcol := cells[row][col].col + cells[row][col].colspan - cells[0][0].col
if nextcol == table.cols {
needvline = true
return needvline && curwrited
}

if cells[row][nextcol].rowspan <= 0 {
row, nextcol = -cells[row][nextcol].rowspan-origin.row, -cells[row][nextcol].colspan-origin.col
}

if cells[row][nextcol].rowspan == 1 {
origin, remian := cells[row][nextcol].element.GetLines()
if cells[row][nextcol].element.GetHeight() == 0 || origin-remian > 0 {
needvline = true
return needvline && curwrited
}
}

if cells[row][nextcol].rowspan > 1 {
if cells[row][nextcol].cellwrited > 0 {
needvline = true
return needvline && curwrited
}
}

return needvline && curwrited
}

// 校验table是否合法
func (table *Table) checkTableConstraint() {
if !table.tableCheck {
Expand Down Expand Up @@ -844,6 +901,9 @@ func (table *Table) resetTableCells() {

// cell重置
row := table.hasWrited + min
if row >= len(table.cells) { // TODO: 这里的判断是修复未知的错误
row = len(table.cells) - 1
}
for col := 0; col < table.cols; {
cell := table.cells[row][col]
if cell.rowspan <= 0 {
Expand Down
15 changes: 9 additions & 6 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/tiechui1994/gopdf/core"
"strings"
"math/rand"
"time"
)

const (
Expand Down Expand Up @@ -183,7 +184,7 @@ func ManyTableReportWithDataExecutor(report *core.Report) {
lineSpace := 0.01 * unit
lineHeight := 2 * unit

rows, cols := 3545, 5
rows, cols := 800, 5
table := NewTable(cols, rows, 80*unit, lineHeight, report)
table.SetMargin(core.Scope{0, 0, 0, 0})

Expand Down Expand Up @@ -274,14 +275,16 @@ func TestTableWithdata(t *testing.T) {
}

func TestTable(t *testing.T) {
//for i := 0; i < 10; i++ {
for i := 0; i < 500; i++ {
ManyTableReportWithData()
//}
}
}

func GetRandStr(l ...int) string {
seed := rand.New(rand.NewSource(time.Now().UnixNano()))
str := "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"
l = append(l, 5)
data := strings.Repeat(str, l[0]*8/36+1)
return data[:l[0]*8] + "---"
l = append(l, 8)
r := seed.Intn(l[0]*11) + 8
data := strings.Repeat(str, r/36+1)
return data[:r] + "---"
}

0 comments on commit f7d98f8

Please sign in to comment.