From f823d3b37eb175ae02faa6557a8fa367a5829e32 Mon Sep 17 00:00:00 2001 From: Sakib Sami Date: Sat, 4 Aug 2018 00:51:37 +0600 Subject: [PATCH] - Added : Code cleanup --- pdf/contentstream/draw/shapes.go | 52 ++------------------------------ pdf/creator/basic_line.go | 10 ++++-- pdf/creator/creator_test.go | 2 +- 3 files changed, 11 insertions(+), 53 deletions(-) diff --git a/pdf/contentstream/draw/shapes.go b/pdf/contentstream/draw/shapes.go index 749b62f4..c4773ca2 100644 --- a/pdf/contentstream/draw/shapes.go +++ b/pdf/contentstream/draw/shapes.go @@ -379,61 +379,13 @@ type BasicLine struct { // Draw a basic line in PDF. Generates the content stream which can be used in page contents or appearance stream of annotation. // Returns the stream content, XForm bounding box (local), bounding box and an error if one occurred. func (line BasicLine) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error) { - x1, x2 := line.X1, line.X2 - y1, y2 := line.Y1, line.Y2 - - dy := y2 - y1 - dx := x2 - x1 - theta := math.Atan2(dy, dx) - - //L := math.Sqrt(math.Pow(dx, 2.0) + math.Pow(dy, 2.0)) w := line.LineWidth - pi := math.Pi - - mul := 1.0 - if dx < 0 { - mul *= -1.0 - } - if dy < 0 { - mul *= -1.0 - } - - // Vs. - VsX := mul * (-w / 2 * math.Cos(theta+pi/2)) - VsY := mul * (-w/2*math.Sin(theta+pi/2) + w*math.Sin(theta+pi/2)) - - // V1. - V1X := VsX + w/2*math.Cos(theta+pi/2) - V1Y := VsY + w/2*math.Sin(theta+pi/2) - // - //// P2. - //V2X := VsX + w/2*math.Cos(theta+pi/2) + L*math.Cos(theta) - //V2Y := VsY + w/2*math.Sin(theta+pi/2) + L*math.Sin(theta) - // - //// P3. - //V3X := VsX + w/2*math.Cos(theta+pi/2) + L*math.Cos(theta) + w*math.Cos(theta-pi/2) - //V3Y := VsY + w/2*math.Sin(theta+pi/2) + L*math.Sin(theta) + w*math.Sin(theta-pi/2) - - // P4. - V4X := VsX + w/2*math.Cos(theta-pi/2) - V4Y := VsY + w/2*math.Sin(theta-pi/2) - path := NewPath() - path = path.AppendPoint(NewPoint(V1X, V1Y)) - //path = path.AppendPoint(NewPoint(V2X, V2Y)) - //path = path.AppendPoint(NewPoint(V3X, V3Y)) - path = path.AppendPoint(NewPoint(V4X, V4Y)) + path = path.AppendPoint(NewPoint(line.X1, line.Y1)) + path = path.AppendPoint(NewPoint(line.X2, line.X2)) creator := pdfcontent.NewContentCreator() - //// Draw line with arrow - //creator. - // Add_q(). - // Add_rg(line.LineColor.R(), line.LineColor.G(), line.LineColor.B()) - //if len(gsName) > 1 { - // // If a graphics state is provided, use it. (Used for transparency settings here). - // creator.Add_gs(pdfcore.PdfObjectName(gsName)) - //} path = path.Offset(line.X1, line.Y1) diff --git a/pdf/creator/basic_line.go b/pdf/creator/basic_line.go index f6537172..75d32a88 100644 --- a/pdf/creator/basic_line.go +++ b/pdf/creator/basic_line.go @@ -17,11 +17,12 @@ type BasicLine struct { y2 float64 lineColor *model.PdfColorDeviceRGB lineWidth float64 + LineStyle draw.LineStyle } // NewBasicLine creates a new Line with default parameters between (x1,y1) to (x2,y2). -func NewBasicLine(x1, y1, x2, y2 float64) *Line { - l := &Line{} +func NewBasicLine(x1, y1, x2, y2 float64) *BasicLine { + l := &BasicLine{} l.x1 = x1 l.y1 = y1 @@ -50,6 +51,10 @@ func (l *BasicLine) SetColor(col Color) { l.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB()) } +func (l *BasicLine) SetLineStyle(style draw.LineStyle) { + l.LineStyle = style +} + // Length calculates and returns the line length. func (l *BasicLine) Length() float64 { return math.Sqrt(math.Pow(l.x2-l.x1, 2.0) + math.Pow(l.y2-l.y1, 2.0)) @@ -67,6 +72,7 @@ func (l *BasicLine) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, Y1: ctx.PageHeight - l.y1, X2: l.x2, Y2: ctx.PageHeight - l.y2, + LineStyle: l.LineStyle, } contents, _, err := drawline.Draw("") diff --git a/pdf/creator/creator_test.go b/pdf/creator/creator_test.go index a34ea0a8..71d9bdc9 100644 --- a/pdf/creator/creator_test.go +++ b/pdf/creator/creator_test.go @@ -1637,7 +1637,7 @@ func TestCreatorTableBorderReq1(t *testing.T) { func TestBasicLine(t *testing.T) { line := NewBasicLine(100, 100, 200, 100) - line.SetLineWidth(2) + line.SetLineWidth(20) line.SetColor(ColorRed) c := New()