2017-07-11 13:16:35 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package creator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2019-05-16 23:44:51 +03:00
|
|
|
"github.com/unidoc/unipdf/v3/contentstream/draw"
|
|
|
|
"github.com/unidoc/unipdf/v3/model"
|
2017-07-11 13:16:35 +00:00
|
|
|
)
|
|
|
|
|
2017-07-31 15:25:28 +00:00
|
|
|
// Line defines a line between point 1 (X1,Y1) and point 2 (X2,Y2). The line ending styles can be none (regular line),
|
2017-07-11 13:16:35 +00:00
|
|
|
// or arrows at either end. The line also has a specified width, color and opacity.
|
|
|
|
// Implements the Drawable interface and can be drawn on PDF using the Creator.
|
2017-07-31 15:25:28 +00:00
|
|
|
type Line struct {
|
2017-07-11 13:16:35 +00:00
|
|
|
x1 float64
|
|
|
|
y1 float64
|
|
|
|
x2 float64
|
|
|
|
y2 float64
|
|
|
|
lineColor *model.PdfColorDeviceRGB
|
|
|
|
lineWidth float64
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:00:02 +03:00
|
|
|
// newLine creates a new Line with default parameters between (x1,y1) to (x2,y2).
|
|
|
|
func newLine(x1, y1, x2, y2 float64) *Line {
|
2017-07-31 15:25:28 +00:00
|
|
|
l := &Line{}
|
2017-07-11 13:16:35 +00:00
|
|
|
|
|
|
|
l.x1 = x1
|
|
|
|
l.y1 = y1
|
|
|
|
l.x2 = x2
|
|
|
|
l.y2 = y2
|
|
|
|
|
|
|
|
l.lineColor = model.NewPdfColorDeviceRGB(0, 0, 0)
|
|
|
|
l.lineWidth = 1.0
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:25:28 +00:00
|
|
|
// GetCoords returns the (x1, y1), (x2, y2) points defining the Line.
|
|
|
|
func (l *Line) GetCoords() (float64, float64, float64, float64) {
|
2017-07-11 13:16:35 +00:00
|
|
|
return l.x1, l.y1, l.x2, l.y2
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:25:28 +00:00
|
|
|
// SetLineWidth sets the line width.
|
|
|
|
func (l *Line) SetLineWidth(lw float64) {
|
2017-07-11 13:16:35 +00:00
|
|
|
l.lineWidth = lw
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:25:28 +00:00
|
|
|
// SetColor sets the line color.
|
2017-07-14 21:58:16 +00:00
|
|
|
// Use ColorRGBFromHex, ColorRGBFrom8bit or ColorRGBFromArithmetic to make the color object.
|
2017-07-31 15:25:28 +00:00
|
|
|
func (l *Line) SetColor(col Color) {
|
2017-07-17 11:36:02 +00:00
|
|
|
l.lineColor = model.NewPdfColorDeviceRGB(col.ToRGB())
|
2017-07-11 13:16:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 15:25:28 +00:00
|
|
|
// Length calculates and returns the line length.
|
|
|
|
func (l *Line) Length() float64 {
|
2017-07-11 13:16:35 +00:00
|
|
|
return math.Sqrt(math.Pow(l.x2-l.x1, 2.0) + math.Pow(l.y2-l.y1, 2.0))
|
|
|
|
}
|
|
|
|
|
2017-07-31 15:25:28 +00:00
|
|
|
// GeneratePageBlocks draws the line on a new block representing the page. Implements the Drawable interface.
|
|
|
|
func (l *Line) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
|
2017-07-11 13:16:35 +00:00
|
|
|
block := NewBlock(ctx.PageWidth, ctx.PageHeight)
|
|
|
|
|
|
|
|
drawline := draw.Line{
|
|
|
|
LineWidth: l.lineWidth,
|
|
|
|
Opacity: 1.0,
|
|
|
|
LineColor: l.lineColor,
|
|
|
|
LineEndingStyle1: draw.LineEndingStyleNone,
|
|
|
|
LineEndingStyle2: draw.LineEndingStyleNone,
|
|
|
|
X1: l.x1,
|
|
|
|
Y1: ctx.PageHeight - l.y1,
|
|
|
|
X2: l.x2,
|
|
|
|
Y2: ctx.PageHeight - l.y2,
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, _, err := drawline.Draw("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, ctx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = block.addContentsByString(string(contents))
|
|
|
|
if err != nil {
|
|
|
|
return nil, ctx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*Block{block}, ctx, nil
|
|
|
|
}
|