unipdf/pdf/creator/border.go

342 lines
9.9 KiB
Go
Raw Normal View History

2018-06-09 06:03:40 +06:00
package creator
import (
"github.com/unidoc/unidoc/pdf/contentstream/draw"
"github.com/unidoc/unidoc/pdf/model"
)
2018-07-06 21:11:34 +06:00
const (
gapBetweenDoubleBorder = 2
2018-07-06 21:11:34 +06:00
)
// border represents cell border
type border struct {
2018-06-12 03:41:33 +06:00
x float64 // Upper left corner
y float64
width float64
height float64
fillColor *model.PdfColorDeviceRGB
borderColorLeft *model.PdfColorDeviceRGB
borderWidthLeft float64
borderColorBottom *model.PdfColorDeviceRGB
borderWidthBottom float64
borderColorRight *model.PdfColorDeviceRGB
borderWidthRight float64
borderColorTop *model.PdfColorDeviceRGB
borderWidthTop float64
2018-06-29 23:55:23 +06:00
LineStyle draw.LineStyle
2018-07-06 21:11:34 +06:00
StyleLeft CellBorderStyle
StyleRight CellBorderStyle
StyleTop CellBorderStyle
StyleBottom CellBorderStyle
2018-06-09 06:03:40 +06:00
}
2018-07-06 22:12:20 +06:00
// newBorder returns and instance of border
func newBorder(x, y, width, height float64) *border {
2018-07-06 21:11:34 +06:00
border := &border{}
2018-06-09 06:03:40 +06:00
border.x = x
border.y = y
border.width = width
border.height = height
2018-06-12 03:41:33 +06:00
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)
2018-06-29 23:55:23 +06:00
border.borderWidthTop = 0
border.borderWidthBottom = 0
border.borderWidthLeft = 0
border.borderWidthRight = 0
2018-06-29 23:55:23 +06:00
2018-07-06 21:11:34 +06:00
border.LineStyle = draw.LineStyleSolid
2018-06-09 06:03:40 +06:00
return border
}
2018-06-29 23:55:23 +06:00
// GetCoords returns coordinates of border
2018-07-06 21:11:34 +06:00
func (border *border) GetCoords() (float64, float64) {
2018-06-09 06:03:40 +06:00
return border.x, border.y
}
2018-06-29 23:55:23 +06:00
// SetWidthLeft sets border width for left
2018-07-06 21:11:34 +06:00
func (border *border) SetWidthLeft(bw float64) {
2018-06-09 06:03:40 +06:00
border.borderWidthLeft = bw
}
2018-06-29 23:55:23 +06:00
// SetColorLeft sets border color for left
2018-07-06 21:11:34 +06:00
func (border *border) SetColorLeft(col Color) {
2018-06-09 06:03:40 +06:00
border.borderColorLeft = model.NewPdfColorDeviceRGB(col.ToRGB())
}
2018-06-29 23:55:23 +06:00
// SetWidthBottom sets border width for bottom
2018-07-06 21:11:34 +06:00
func (border *border) SetWidthBottom(bw float64) {
2018-06-09 06:03:40 +06:00
border.borderWidthBottom = bw
}
2018-06-29 23:55:23 +06:00
// SetColorBottom sets border color for bottom
2018-07-06 21:11:34 +06:00
func (border *border) SetColorBottom(col Color) {
2018-06-09 06:03:40 +06:00
border.borderColorBottom = model.NewPdfColorDeviceRGB(col.ToRGB())
}
2018-06-29 23:55:23 +06:00
// SetWidthRight sets border width for right
2018-07-06 21:11:34 +06:00
func (border *border) SetWidthRight(bw float64) {
2018-06-09 06:03:40 +06:00
border.borderWidthRight = bw
}
2018-06-29 23:55:23 +06:00
// SetColorRight sets border color for right
2018-07-06 21:11:34 +06:00
func (border *border) SetColorRight(col Color) {
2018-06-09 06:03:40 +06:00
border.borderColorRight = model.NewPdfColorDeviceRGB(col.ToRGB())
}
2018-06-29 23:55:23 +06:00
// SetWidthTop sets border width for top
2018-07-06 21:11:34 +06:00
func (border *border) SetWidthTop(bw float64) {
2018-06-09 06:03:40 +06:00
border.borderWidthTop = bw
}
2018-06-29 23:55:23 +06:00
// SetColorTop sets border color for top
2018-07-06 21:11:34 +06:00
func (border *border) SetColorTop(col Color) {
2018-06-09 06:03:40 +06:00
border.borderColorTop = model.NewPdfColorDeviceRGB(col.ToRGB())
}
2018-06-29 23:55:23 +06:00
// SetFillColor sets background color for border
2018-07-06 21:11:34 +06:00
func (border *border) SetFillColor(col Color) {
2018-06-09 06:03:40 +06:00
border.fillColor = model.NewPdfColorDeviceRGB(col.ToRGB())
}
2018-06-29 23:55:23 +06:00
// GeneratePageBlocks creates drawable
2018-07-06 21:11:34 +06:00
func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
2018-06-09 06:03:40 +06:00
block := NewBlock(ctx.PageWidth, ctx.PageHeight)
startX := border.x
startY := ctx.PageHeight - border.y
2018-07-06 21:11:34 +06:00
2018-06-09 06:03:40 +06:00
if border.fillColor != nil {
drawrect := draw.Rectangle{
Opacity: 1.0,
2018-07-06 21:11:34 +06:00
X: border.x + (border.borderWidthLeft * 3) - 0.5,
2018-07-10 00:51:45 +06:00
Y: (ctx.PageHeight - border.y - border.height) + (border.borderWidthLeft * 2),
2018-07-06 21:11:34 +06:00
Height: border.height - (border.borderWidthBottom * 3),
Width: border.width - (border.borderWidthRight * 3),
2018-06-09 06:03:40 +06:00
}
drawrect.FillEnabled = true
drawrect.FillColor = border.fillColor
drawrect.BorderEnabled = false
contents, _, err := drawrect.Draw("")
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contents))
if err != nil {
return nil, ctx, err
}
}
2018-06-30 00:06:50 +06:00
if border.borderWidthTop != 0 {
2018-07-06 21:11:34 +06:00
if border.StyleTop == CellBorderStyleDoubleTop {
x := startX
y := startY + (border.borderWidthTop * gapBetweenDoubleBorder)
2018-07-10 00:51:45 +06:00
2018-07-06 21:11:34 +06:00
lineTop := draw.Line{
LineWidth: border.borderWidthTop,
Opacity: 1.0,
LineColor: border.borderColorTop,
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: x - border.borderWidthLeft*gapBetweenDoubleBorder,
Y1: y,
X2: x + border.width + (border.borderWidthRight * gapBetweenDoubleBorder) + border.borderWidthRight,
Y2: y,
2018-07-06 21:11:34 +06:00
LineStyle: border.LineStyle,
}
contentsTop, _, err := lineTop.Draw("")
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsTop))
if err != nil {
return nil, ctx, err
}
}
2018-06-30 00:06:50 +06:00
// Line Top
lineTop := draw.Line{
LineWidth: border.borderWidthTop,
Opacity: 1.0,
LineColor: border.borderColorTop,
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: startX,
Y1: startY,
X2: startX + border.width + border.borderWidthRight,
Y2: startY,
2018-06-30 00:06:50 +06:00
LineStyle: border.LineStyle,
}
contentsTop, _, err := lineTop.Draw("")
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsTop))
if err != nil {
return nil, ctx, err
}
2018-06-09 06:03:40 +06:00
}
if border.borderWidthBottom != 0 {
x := startX
y := startY - border.height
2018-07-10 00:51:45 +06:00
if border.StyleBottom == CellBorderStyleDoubleBottom {
dx := x
dy := y - (border.borderWidthBottom * gapBetweenDoubleBorder)
2018-07-06 21:11:34 +06:00
lineBottom := draw.Line{
LineWidth: border.borderWidthBottom,
2018-07-06 21:11:34 +06:00
Opacity: 1.0,
LineColor: border.borderColorBottom,
2018-07-06 21:11:34 +06:00
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: dx - border.borderWidthLeft*gapBetweenDoubleBorder,
Y1: dy,
X2: dx + border.width + (border.borderWidthRight * gapBetweenDoubleBorder) + border.borderWidthRight,
Y2: dy,
2018-07-06 21:11:34 +06:00
LineStyle: border.LineStyle,
}
contentsBottom, _, err := lineBottom.Draw("")
2018-07-06 21:11:34 +06:00
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsBottom))
2018-07-06 21:11:34 +06:00
if err != nil {
return nil, ctx, err
}
}
lineBottom := draw.Line{
LineWidth: border.borderWidthBottom,
2018-06-30 00:06:50 +06:00
Opacity: 1.0,
LineColor: border.borderColorBottom,
2018-06-30 00:06:50 +06:00
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: x,
Y1: y,
X2: x + border.width + border.borderWidthRight,
Y2: y,
2018-06-30 00:06:50 +06:00
LineStyle: border.LineStyle,
}
contentsBottom, _, err := lineBottom.Draw("")
2018-06-30 00:06:50 +06:00
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsBottom))
2018-06-30 00:06:50 +06:00
if err != nil {
return nil, ctx, err
}
2018-06-09 06:03:40 +06:00
}
if border.borderWidthLeft != 0 {
x := startX
y := startY
2018-07-06 21:11:34 +06:00
if border.StyleLeft == CellBorderStyleDoubleLeft {
// Line Left
lineLeft := draw.Line{
LineWidth: border.borderWidthLeft,
2018-07-06 21:11:34 +06:00
Opacity: 1.0,
LineColor: border.borderColorLeft,
2018-07-06 21:11:34 +06:00
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: x - border.borderWidthLeft*gapBetweenDoubleBorder,
Y1: y + border.borderWidthTop + (border.borderWidthTop * 2),
X2: x - border.borderWidthLeft*gapBetweenDoubleBorder,
Y2: y - border.height - (border.borderWidthTop * 2),
2018-07-06 21:11:34 +06:00
LineStyle: border.LineStyle,
}
contentsLeft, _, err := lineLeft.Draw("")
2018-07-06 21:11:34 +06:00
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsLeft))
2018-07-06 21:11:34 +06:00
if err != nil {
return nil, ctx, err
}
}
// Line Left
lineLeft := draw.Line{
LineWidth: border.borderWidthLeft,
2018-06-30 00:06:50 +06:00
Opacity: 1.0,
LineColor: border.borderColorLeft,
2018-06-30 00:06:50 +06:00
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: x,
Y1: y + border.borderWidthTop,
X2: x,
Y2: y - border.height,
2018-06-30 00:06:50 +06:00
LineStyle: border.LineStyle,
}
contentsLeft, _, err := lineLeft.Draw("")
2018-06-30 00:06:50 +06:00
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsLeft))
2018-06-30 00:06:50 +06:00
if err != nil {
return nil, ctx, err
}
2018-06-09 06:03:40 +06:00
}
if border.borderWidthRight != 0 {
x := startX + border.width
y := startY
2018-07-10 00:51:45 +06:00
if border.StyleRight == CellBorderStyleDoubleRight {
// Line Right
lineRight := draw.Line{
LineWidth: border.borderWidthRight,
2018-07-06 21:11:34 +06:00
Opacity: 1.0,
LineColor: border.borderColorRight,
2018-07-06 21:11:34 +06:00
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: x + border.borderWidthRight*gapBetweenDoubleBorder,
Y1: y + border.borderWidthTop + (border.borderWidthTop * gapBetweenDoubleBorder),
X2: x + border.borderWidthRight*gapBetweenDoubleBorder,
Y2: y - border.height - (border.borderWidthTop * gapBetweenDoubleBorder),
2018-07-06 21:11:34 +06:00
LineStyle: border.LineStyle,
}
contentsRight, _, err := lineRight.Draw("")
2018-07-06 21:11:34 +06:00
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsRight))
2018-07-06 21:11:34 +06:00
if err != nil {
return nil, ctx, err
}
}
// Line Right
lineRight := draw.Line{
LineWidth: border.borderWidthRight,
2018-06-30 00:06:50 +06:00
Opacity: 1.0,
LineColor: border.borderColorRight,
2018-06-30 00:06:50 +06:00
LineEndingStyle1: draw.LineEndingStyleNone,
LineEndingStyle2: draw.LineEndingStyleNone,
X1: x,
Y1: y + border.borderWidthTop,
X2: x,
Y2: y - border.height,
2018-06-30 00:06:50 +06:00
LineStyle: border.LineStyle,
}
contentsRight, _, err := lineRight.Draw("")
2018-06-30 00:06:50 +06:00
if err != nil {
return nil, ctx, err
}
err = block.addContentsByString(string(contentsRight))
2018-06-30 00:06:50 +06:00
if err != nil {
return nil, ctx, err
}
2018-06-09 06:03:40 +06:00
}
2018-06-30 00:06:50 +06:00
2018-06-09 06:03:40 +06:00
return []*Block{block}, ctx, nil
}