diff --git a/pdf/creator/border.go b/pdf/creator/border.go index bac9dcfe..c8892235 100644 --- a/pdf/creator/border.go +++ b/pdf/creator/border.go @@ -2,7 +2,6 @@ package creator import ( "github.com/unidoc/unidoc/pdf/contentstream/draw" - "github.com/unidoc/unidoc/pdf/model" ) const ( @@ -15,14 +14,14 @@ type border struct { y float64 width float64 height float64 - fillColor *model.PdfColorDeviceRGB - borderColorLeft *model.PdfColorDeviceRGB + fillColor *Color + borderColorLeft *Color borderWidthLeft float64 - borderColorBottom *model.PdfColorDeviceRGB + borderColorBottom *Color borderWidthBottom float64 - borderColorRight *model.PdfColorDeviceRGB + borderColorRight *Color borderWidthRight float64 - borderColorTop *model.PdfColorDeviceRGB + borderColorTop *Color borderWidthTop float64 LineStyle draw.LineStyle StyleLeft CellBorderStyle @@ -40,10 +39,10 @@ func newBorder(x, y, width, height float64) *border { border.width = width border.height = height - border.borderColorTop = model.NewPdfColorDeviceRGB(0, 0, 0) - border.borderColorBottom = model.NewPdfColorDeviceRGB(0, 0, 0) - border.borderColorLeft = model.NewPdfColorDeviceRGB(0, 0, 0) - border.borderColorRight = model.NewPdfColorDeviceRGB(0, 0, 0) + border.borderColorTop = &ColorBlack + border.borderColorBottom = &ColorBlack + border.borderColorLeft = &ColorBlack + border.borderColorRight = &ColorBlack border.borderWidthTop = 0 border.borderWidthBottom = 0 @@ -66,7 +65,7 @@ func (border *border) SetWidthLeft(bw float64) { // SetColorLeft sets border color for left func (border *border) SetColorLeft(col Color) { - border.borderColorLeft = model.NewPdfColorDeviceRGB(col.ToRGB()) + border.borderColorLeft = &col } // SetWidthBottom sets border width for bottom @@ -76,7 +75,7 @@ func (border *border) SetWidthBottom(bw float64) { // SetColorBottom sets border color for bottom func (border *border) SetColorBottom(col Color) { - border.borderColorBottom = model.NewPdfColorDeviceRGB(col.ToRGB()) + border.borderColorBottom = &col } // SetWidthRight sets border width for right @@ -86,7 +85,7 @@ func (border *border) SetWidthRight(bw float64) { // SetColorRight sets border color for right func (border *border) SetColorRight(col Color) { - border.borderColorRight = model.NewPdfColorDeviceRGB(col.ToRGB()) + border.borderColorRight = &col } // SetWidthTop sets border width for top @@ -96,12 +95,12 @@ func (border *border) SetWidthTop(bw float64) { // SetColorTop sets border color for top func (border *border) SetColorTop(col Color) { - border.borderColorTop = model.NewPdfColorDeviceRGB(col.ToRGB()) + border.borderColorTop = &col } // SetFillColor sets background color for border func (border *border) SetFillColor(col Color) { - border.fillColor = model.NewPdfColorDeviceRGB(col.ToRGB()) + border.fillColor = &col } // GeneratePageBlocks creates drawable @@ -111,18 +110,12 @@ func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext startY := ctx.PageHeight - border.y if border.fillColor != nil { - drawrect := draw.Rectangle{ - Opacity: 1.0, - X: border.x + (border.borderWidthLeft * 3) - 0.5, - Y: (ctx.PageHeight - border.y - border.height) + (border.borderWidthLeft * 2), - Height: border.height - (border.borderWidthBottom * 3), - Width: border.width - (border.borderWidthRight * 3), - } - drawrect.FillEnabled = true - drawrect.FillColor = border.fillColor - drawrect.BorderEnabled = false + drawrect := NewRectangle(border.x+(border.borderWidthLeft*3)-0.5, + (ctx.PageHeight-border.y-border.height)+(border.borderWidthLeft*2), border.height-(border.borderWidthBottom*3), + border.width-(border.borderWidthRight*3)) + drawrect.SetFillColor(border.fillColor) - contents, _, err := drawrect.Draw("") + contents, _, err := drawrect.GeneratePageBlocks(ctx) if err != nil { return nil, ctx, err } @@ -138,16 +131,12 @@ func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext x := startX y := startY + (border.borderWidthTop * gapBetweenDoubleBorder) - lineTop := draw.BasicLine{ - LineWidth: border.borderWidthTop, - Opacity: 1.0, - LineColor: border.borderColorTop, - X1: x - border.borderWidthLeft*gapBetweenDoubleBorder, - Y1: y, - X2: x + border.width + (border.borderWidthRight * gapBetweenDoubleBorder) + border.borderWidthRight, - Y2: y, - LineStyle: border.LineStyle, - } + lineTop := NewBasicLine(x-border.borderWidthLeft*gapBetweenDoubleBorder, y, + x+border.width+(border.borderWidthRight*gapBetweenDoubleBorder)+border.borderWidthRight, y) + lineTop.SetLineStyle(border.LineStyle) + lineTop.SetLineWidth(border.borderWidthTop) + lineTop.SetColor(border.borderColorTop) + contentsTop, _, err := lineTop.Draw("") if err != nil { return nil, ctx, err diff --git a/pdf/creator/creator_test.go b/pdf/creator/creator_test.go index 33510fcb..1c3b5853 100644 --- a/pdf/creator/creator_test.go +++ b/pdf/creator/creator_test.go @@ -1651,6 +1651,51 @@ func TestBasicLine(t *testing.T) { } } +func TestBasicLineWithDash(t *testing.T) { + line := NewBasicLine(100, 100, 100, 150) + line.SetLineWidth(2) + line.SetColor(ColorRed) + line.SetLineStyle(draw.LineStyleDashed) + + c := New() + c.Draw(line) + + //err := c.WriteToFile("/tmp/table_border_req1_test.pdf") + err := c.WriteToFile("../../testfiles/table.pdf") + if err != nil { + t.Errorf("Fail: %v\n", err) + return + } +} + +func newBasicLine(x1, y1, x2, y2 float64, w float64, style draw.LineStyle) *BasicLine { + line := NewBasicLine(x1, y1, x2, y2) + line.SetLineWidth(w) + line.SetColor(ColorRed) + line.SetLineStyle(style) + return line +} + +func TestRectangle(t *testing.T) { + line1 := newBasicLine(100, 100, 200, 100, 2, draw.LineStyleSolid) + line2 := newBasicLine(100, 100, 100, 200, 2, draw.LineStyleSolid) + line3 := newBasicLine(100, 200, 200, 200, 2, draw.LineStyleSolid) + line4 := newBasicLine(200, 200, 200, 100, 2, draw.LineStyleSolid) + + c := New() + c.Draw(line1) + c.Draw(line2) + c.Draw(line3) + c.Draw(line4) + + //err := c.WriteToFile("/tmp/table_border_req1_test.pdf") + err := c.WriteToFile("../../testfiles/table.pdf") + if err != nil { + t.Errorf("Fail: %v\n", err) + return + } +} + func TestTableInSubchapter(t *testing.T) { c := New()