mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-02 22:17:06 +08:00
- Added : Code cleanup
This commit is contained in:
parent
a3518f5d5e
commit
f823d3b37e
@ -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.
|
// 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.
|
// 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) {
|
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
|
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 := NewPath()
|
||||||
path = path.AppendPoint(NewPoint(V1X, V1Y))
|
path = path.AppendPoint(NewPoint(line.X1, line.Y1))
|
||||||
//path = path.AppendPoint(NewPoint(V2X, V2Y))
|
path = path.AppendPoint(NewPoint(line.X2, line.X2))
|
||||||
//path = path.AppendPoint(NewPoint(V3X, V3Y))
|
|
||||||
path = path.AppendPoint(NewPoint(V4X, V4Y))
|
|
||||||
|
|
||||||
creator := pdfcontent.NewContentCreator()
|
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)
|
path = path.Offset(line.X1, line.Y1)
|
||||||
|
|
||||||
|
@ -17,11 +17,12 @@ type BasicLine struct {
|
|||||||
y2 float64
|
y2 float64
|
||||||
lineColor *model.PdfColorDeviceRGB
|
lineColor *model.PdfColorDeviceRGB
|
||||||
lineWidth float64
|
lineWidth float64
|
||||||
|
LineStyle draw.LineStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBasicLine creates a new Line with default parameters between (x1,y1) to (x2,y2).
|
// NewBasicLine creates a new Line with default parameters between (x1,y1) to (x2,y2).
|
||||||
func NewBasicLine(x1, y1, x2, y2 float64) *Line {
|
func NewBasicLine(x1, y1, x2, y2 float64) *BasicLine {
|
||||||
l := &Line{}
|
l := &BasicLine{}
|
||||||
|
|
||||||
l.x1 = x1
|
l.x1 = x1
|
||||||
l.y1 = y1
|
l.y1 = y1
|
||||||
@ -50,6 +51,10 @@ func (l *BasicLine) SetColor(col Color) {
|
|||||||
l.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB())
|
l.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *BasicLine) SetLineStyle(style draw.LineStyle) {
|
||||||
|
l.LineStyle = style
|
||||||
|
}
|
||||||
|
|
||||||
// Length calculates and returns the line length.
|
// Length calculates and returns the line length.
|
||||||
func (l *BasicLine) Length() float64 {
|
func (l *BasicLine) Length() float64 {
|
||||||
return math.Sqrt(math.Pow(l.x2-l.x1, 2.0) + math.Pow(l.y2-l.y1, 2.0))
|
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,
|
Y1: ctx.PageHeight - l.y1,
|
||||||
X2: l.x2,
|
X2: l.x2,
|
||||||
Y2: ctx.PageHeight - l.y2,
|
Y2: ctx.PageHeight - l.y2,
|
||||||
|
LineStyle: l.LineStyle,
|
||||||
}
|
}
|
||||||
|
|
||||||
contents, _, err := drawline.Draw("")
|
contents, _, err := drawline.Draw("")
|
||||||
|
@ -1637,7 +1637,7 @@ func TestCreatorTableBorderReq1(t *testing.T) {
|
|||||||
|
|
||||||
func TestBasicLine(t *testing.T) {
|
func TestBasicLine(t *testing.T) {
|
||||||
line := NewBasicLine(100, 100, 200, 100)
|
line := NewBasicLine(100, 100, 200, 100)
|
||||||
line.SetLineWidth(2)
|
line.SetLineWidth(20)
|
||||||
line.SetColor(ColorRed)
|
line.SetColor(ColorRed)
|
||||||
|
|
||||||
c := New()
|
c := New()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user