Fix : Dashed Line && Control on Border

This commit is contained in:
Sakib Sami 2018-06-25 00:30:32 +06:00
parent a7ee5dcb78
commit a0c3f57e4a
4 changed files with 99 additions and 70 deletions

View File

@ -381,7 +381,7 @@ func (line DashedLine) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error) {
theta := math.Atan2(dy, dx) theta := math.Atan2(dy, dx)
L := math.Sqrt(math.Pow(dx, 2.0) + math.Pow(dy, 2.0)) L := math.Sqrt(math.Pow(dx, 2.0) + math.Pow(dy, 2.0))
w := line.LineWidth w := line.LineWidth - 1
pi := math.Pi pi := math.Pi
@ -512,13 +512,17 @@ func (line DashedLine) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error) {
pathBbox := path.GetBoundingBox() pathBbox := path.GetBoundingBox()
DrawDashedPathWithCreator(path, creator) DrawPathWithCreator(path, creator)
creator. creator.
Add_d([]int64{3}, 0). Add_d([]int64{3}, -1).
Add_s() Add_S().
//Add_q() Add_Q()
//Add_f()
/*
// Offsets (needed for placement of annotations bbox).
offX := x1 - VsX
offY := y1 - VsY
*/
// Bounding box - global coordinate system. // Bounding box - global coordinate system.
bbox := &pdf.PdfRectangle{} bbox := &pdf.PdfRectangle{}

View File

@ -3,7 +3,6 @@ package creator
import ( import (
"github.com/unidoc/unidoc/pdf/contentstream/draw" "github.com/unidoc/unidoc/pdf/contentstream/draw"
"github.com/unidoc/unidoc/pdf/model" "github.com/unidoc/unidoc/pdf/model"
"fmt"
) )
type Border struct { type Border struct {
@ -81,20 +80,9 @@ func (border *Border) SetFillColor(col Color) {
func (border *Border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) { func (border *Border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
block := NewBlock(ctx.PageWidth, ctx.PageHeight) block := NewBlock(ctx.PageWidth, ctx.PageHeight)
fmt.Println("WT = ", border.borderWidthTop)
fmt.Println("WL = ", border.borderWidthLeft)
fmt.Println("WR = ", border.borderWidthRight)
fmt.Println("WB = ", border.borderWidthBottom)
startX := border.x startX := border.x
startY := ctx.PageHeight - border.y startY := ctx.PageHeight - border.y
//fmt.Printf("x = %f, y = %f, h = %f, w = %f\n", ctx.X, ctx.Y,
// ctx.Width, ctx.Height)
//fmt.Printf("x1 = %f, y1 = %f, x2 = %f, y2 = %f, h = %f, w = %f\n", startX, startY,
// border.x, border.y, border.height, border.width)
if border.fillColor != nil { if border.fillColor != nil {
drawrect := draw.Rectangle{ drawrect := draw.Rectangle{
Opacity: 1.0, Opacity: 1.0,

View File

@ -950,68 +950,70 @@ func TestTable(t *testing.T) {
} }
// Test creating and drawing a table. // Test creating and drawing a table.
func TestBorderedTable(t *testing.T) { func TestBorderedTable1(t *testing.T) {
table := NewTable(4) // Mx4 table table := NewTable(4) // Mx4 table
// Default, equal column sizes (4x0.25)... // Default, equal column sizes (4x0.25)...
table.SetColumnWidths(0.5, 0.2, 0.2, 0.1) table.SetColumnWidths(0.5, 0.2, 0.2, 0.1)
cell := table.NewCell() cell1 := table.NewCell()
p := NewParagraph("1,1") p := NewParagraph("1,1")
cell.SetContent(p) cell1.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell1.SetBorder(CellBorderStyleBoxTop, 1) // Border will be on top
cell1.SetBorder(CellBorderStyleBoxLeft, 1) // Border will be on left
cell = table.NewCell() cell2 := table.NewCell()
p = NewParagraph("1,2") p = NewParagraph("1,2")
cell.SetContent(p) cell2.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell2.SetBorder(CellBorderStyleBox, 1) // Border will be around
cell = table.NewCell() cell3 := table.NewCell()
p = NewParagraph("1,3") p = NewParagraph("1,3")
cell.SetContent(p) cell3.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell3.SetBorder(CellBorderStyleBoxBottom, 1) // Border will be on bottom
cell = table.NewCell() cell4 := table.NewCell()
p = NewParagraph("1,4") p = NewParagraph("1,4")
cell.SetContent(p) cell4.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell4.SetBorder(CellBorderStyleBox, 1) // Border will be around
cell = table.NewCell() cell5 := table.NewCell()
p = NewParagraph("2,1") p = NewParagraph("2,1")
cell.SetContent(p) cell5.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell5.SetBorder(CellBorderStyleBox, 1) // Border will be around
cell = table.NewCell() cell6 := table.NewCell()
p = NewParagraph("2,2") p = NewParagraph("2,2")
cell.SetContent(p) cell6.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell6.SetBorder(CellBorderStyleBoxLeft, 1) // Border will be on left
table.SkipCells(1) // Skip over 2,3. table.SkipCells(1) // Skip over 2,3.
cell = table.NewCell() cell7 := table.NewCell()
p = NewParagraph("2,4") p = NewParagraph("2,4")
cell.SetContent(p) cell7.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell7.SetBorder(CellBorderStyleBox, 1) // Border will be around
// Skip over two rows. // Skip over two rows.
table.SkipRows(2) table.SkipRows(2)
cell = table.NewCell() cell8 := table.NewCell()
p = NewParagraph("4,4") p = NewParagraph("4,4")
cell.SetContent(p) cell8.SetContent(p)
cell.SetBorder(CellBorderStyleBox, 1) cell8.SetBorder(CellBorderStyleBoxRight, 1) // Border will be on right
// Move down 3 rows, 2 to the left. // Move down 3 rows, 2 to the left.
table.SkipOver(3, -2) table.SkipOver(3, -2)
cell = table.NewCell() cell9 := table.NewCell()
p = NewParagraph("7,2") p = NewParagraph("7,2")
cell.SetContent(p) cell9.SetContent(p)
cell.SetBackgroundColor(ColorRGBFrom8bit(255, 0, 0)) cell9.SetBackgroundColor(ColorRGBFrom8bit(255, 0, 0))
cell.SetBorder(CellBorderStyleBox, 1) cell9.SetBorder(CellBorderStyleBoxBottom, 1) // Border will be on bottom
cell9.SetBorder(CellBorderStyleBoxTop, 1) // Border will be on top
c := New() c := New()
c.Draw(table) c.Draw(table)
//err := c.WriteToFile("/tmp/4_table_bordered.pdf") err := c.WriteToFile("/tmp/4_table_bordered.pdf")
err := c.WriteToFile(testPdfWithTable) //err := c.WriteToFile(testPdfWithTable)
if err != nil { if err != nil {
t.Errorf("Fail: %v\n", err) t.Errorf("Fail: %v\n", err)
return return
@ -1024,37 +1026,74 @@ func TestBorderedTable2(t *testing.T) {
// Default, equal column sizes (4x0.25)... // Default, equal column sizes (4x0.25)...
table.SetColumnWidths(0.5, 0.2, 0.2, 0.1) table.SetColumnWidths(0.5, 0.2, 0.2, 0.1)
cell := table.NewCell() cell1 := table.NewCell()
p := NewParagraph("Hello") p := NewParagraph("A")
cell.SetContent(p) cell1.SetContent(p)
cell.SetBorderColorAndWidthByStyle(CellBorderStyleBoxTop, ColorBlue, 1) cell1.SetBorder(CellBorderStyleBox, 1) // Border will be on top
cell.SetBorderLineStyle(CellBorderLineStyleDashed) cell1.SetBorder(CellBorderStyleBoxLeft, 1) // Border will be on left
cell1.SetBorderLineStyle(CellBorderLineStyleDashed)
cell2 := table.NewCell() cell2 := table.NewCell()
p2 := NewParagraph("World") p = NewParagraph("B")
cell2.SetContent(p2) cell2.SetContent(p)
cell2.SetBorderColorAndWidthByStyle(CellBorderStyleBoxBottom, ColorRed, 1) cell2.SetBorder(CellBorderStyleBox, 1) // Border will be around
cell2.SetBorderLineStyle(CellBorderLineStyleDashed) cell2.SetBorderLineStyle(CellBorderLineStyleDashed)
cell3 := table.NewCell() cell3 := table.NewCell()
p3 := NewParagraph("Welcome to") p = NewParagraph("C")
cell3.SetContent(p3) cell3.SetContent(p)
cell3.SetBorderColorAndWidthByStyle(CellBorderStyleBoxLeft, ColorBlue, 1) cell3.SetBorder(CellBorderStyleBoxBottom, 1) // Border will be on bottom
cell3.SetBorderLineStyle(CellBorderLineStyleDashed) cell3.SetBorderLineStyle(CellBorderLineStyleDashed)
table.SkipCells(1)
cell4 := table.NewCell() cell4 := table.NewCell()
p4 := NewParagraph("Unidoc") p = NewParagraph("D")
cell4.SetContent(p4) cell4.SetContent(p)
cell4.SetBorderColorAndWidthByStyle(CellBorderStyleBoxRight, ColorGreen, 1) cell4.SetBorder(CellBorderStyleBox, 1) // Border will be around
//cell4.SetBorderColorAndWidthByStyle(CellBorderStyleBoxBottom, ColorGreen, 1)
cell4.SetBorderLineStyle(CellBorderLineStyleDashed) cell4.SetBorderLineStyle(CellBorderLineStyleDashed)
cell5 := table.NewCell()
p = NewParagraph("E")
cell5.SetContent(p)
cell5.SetBorder(CellBorderStyleBox, 1) // Border will be around
cell5.SetBorderLineStyle(CellBorderLineStyleDashed)
cell6 := table.NewCell()
p = NewParagraph("F")
cell6.SetContent(p)
cell6.SetBorder(CellBorderStyleBoxLeft, 1) // Border will be on left
cell6.SetBorderLineStyle(CellBorderLineStyleDashed)
table.SkipCells(1) // Skip over 2,3.
cell7 := table.NewCell()
p = NewParagraph("G")
cell7.SetContent(p)
cell7.SetBorder(CellBorderStyleBox, 1) // Border will be around
cell7.SetBorderLineStyle(CellBorderLineStyleDashed)
// Skip over two rows.
table.SkipRows(2)
cell8 := table.NewCell()
p = NewParagraph("H")
cell8.SetContent(p)
cell8.SetBorder(CellBorderStyleBoxRight, 1) // Border will be on right
cell8.SetBorderLineStyle(CellBorderLineStyleDashed)
// Move down 3 rows, 2 to the left.
table.SkipOver(3, -2)
cell9 := table.NewCell()
p = NewParagraph("I")
cell9.SetContent(p)
cell9.SetBackgroundColor(ColorRGBFrom8bit(255, 0, 0))
cell9.SetBorder(CellBorderStyleBoxBottom, 1) // Border will be on bottom
cell9.SetBorder(CellBorderStyleBoxTop, 1) // Border will be on top
cell9.SetBorderLineStyle(CellBorderLineStyleDashed)
c := New() c := New()
c.Draw(table) c.Draw(table)
//err := c.WriteToFile("/tmp/4_table_bordered.pdf") err := c.WriteToFile("/tmp/4_table_bordered.pdf")
err := c.WriteToFile(testPdfWithTable) //err := c.WriteToFile(testPdfWithTable)
if err != nil { if err != nil {
t.Errorf("Fail: %v\n", err) t.Errorf("Fail: %v\n", err)
return return

View File

@ -10,7 +10,6 @@ import (
"github.com/unidoc/unidoc/common" "github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/model" "github.com/unidoc/unidoc/pdf/model"
"fmt"
) )
// Table allows organizing content in an rows X columns matrix, which can spawn across multiple pages. // Table allows organizing content in an rows X columns matrix, which can spawn across multiple pages.
@ -250,7 +249,6 @@ func (table *Table) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext,
common.Log.Debug("Error: %v\n", err) common.Log.Debug("Error: %v\n", err)
} }
} else if cell.borderStyle != CellBorderStyleNone { } else if cell.borderStyle != CellBorderStyleNone {
fmt.Println("NoneFilled")
//Draw border (no fill). //Draw border (no fill).
border := NewBorder(ctx.X, ctx.Y, w, h, cell.borderLineStyle) border := NewBorder(ctx.X, ctx.Y, w, h, cell.borderLineStyle)
if cell.borderColorLeft != nil { if cell.borderColorLeft != nil {