mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-11 19:29:40 +08:00
Fix border corners and BasicLine Draw
This commit is contained in:
parent
c024c237d2
commit
38157c3a51
@ -6,7 +6,6 @@ import (
|
||||
pdfcontent "github.com/unidoc/unidoc/pdf/contentstream"
|
||||
pdfcore "github.com/unidoc/unidoc/pdf/core"
|
||||
pdf "github.com/unidoc/unidoc/pdf/model"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Circle struct {
|
||||
@ -377,44 +376,29 @@ type BasicLine struct {
|
||||
LineStyle LineStyle
|
||||
}
|
||||
|
||||
// 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.
|
||||
// Draw draws the basic line to 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) {
|
||||
w := line.LineWidth
|
||||
|
||||
line.X1 = line.X1 - (line.LineWidth / 2)
|
||||
line.Y1 = line.Y1 - (line.LineWidth / 2)
|
||||
|
||||
fmt.Println("---------------")
|
||||
fmt.Println("X1 : ", line.X1)
|
||||
fmt.Println("Y1 : ", line.Y1)
|
||||
fmt.Println("X2 : ", line.X2)
|
||||
fmt.Println("Y2 : ", line.Y2)
|
||||
|
||||
path := NewPath()
|
||||
path = path.AppendPoint(NewPoint(line.X1, line.Y1))
|
||||
path = path.AppendPoint(NewPoint(line.X2, line.Y2))
|
||||
|
||||
creator := pdfcontent.NewContentCreator()
|
||||
cc := pdfcontent.NewContentCreator()
|
||||
|
||||
pathBbox := path.GetBoundingBox()
|
||||
|
||||
DrawPathWithCreator(path, creator)
|
||||
DrawPathWithCreator(path, cc)
|
||||
|
||||
if line.LineStyle == LineStyleDashed {
|
||||
creator.
|
||||
Add_d([]int64{1, 1}, 0).
|
||||
Add_RG(line.LineColor.R(), line.LineColor.G(), line.LineColor.B()).
|
||||
Add_w(w).
|
||||
Add_S().
|
||||
Add_Q()
|
||||
} else {
|
||||
creator.
|
||||
Add_RG(line.LineColor.R(), line.LineColor.G(), line.LineColor.B()).
|
||||
Add_w(w).
|
||||
Add_S().
|
||||
Add_Q()
|
||||
cc.Add_d([]int64{1, 1}, 0)
|
||||
}
|
||||
cc.Add_RG(line.LineColor.R(), line.LineColor.G(), line.LineColor.B()).
|
||||
Add_w(w).
|
||||
Add_S().
|
||||
Add_Q()
|
||||
|
||||
// Bounding box - global coordinate system.
|
||||
bbox := &pdf.PdfRectangle{}
|
||||
@ -423,5 +407,5 @@ func (line BasicLine) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error) {
|
||||
bbox.Urx = pathBbox.X + pathBbox.Width
|
||||
bbox.Ury = pathBbox.Y + pathBbox.Height
|
||||
|
||||
return creator.Bytes(), bbox, nil
|
||||
return cc.Bytes(), bbox, nil
|
||||
}
|
||||
|
@ -107,6 +107,7 @@ func (border *border) SetFillColor(col Color) {
|
||||
// GeneratePageBlocks creates drawable
|
||||
func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
|
||||
block := NewBlock(ctx.PageWidth, ctx.PageHeight)
|
||||
// Start points is in upper left corner.
|
||||
startX := border.x
|
||||
startY := ctx.PageHeight - border.y
|
||||
|
||||
@ -161,9 +162,9 @@ func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext
|
||||
LineWidth: border.borderWidthTop,
|
||||
Opacity: 1.0,
|
||||
LineColor: border.borderColorTop,
|
||||
X1: startX,
|
||||
X1: startX - border.borderWidthTop/2,
|
||||
Y1: startY,
|
||||
X2: startX + border.width,
|
||||
X2: startX + border.width + border.borderWidthTop/2,
|
||||
Y2: startY,
|
||||
LineStyle: border.LineStyle,
|
||||
}
|
||||
@ -209,9 +210,9 @@ func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext
|
||||
LineWidth: border.borderWidthBottom,
|
||||
Opacity: 1.0,
|
||||
LineColor: border.borderColorBottom,
|
||||
X1: x,
|
||||
X1: x - border.borderWidthBottom/2,
|
||||
Y1: y,
|
||||
X2: x + border.width,
|
||||
X2: x + border.width + border.borderWidthBottom/2,
|
||||
Y2: y,
|
||||
LineStyle: border.LineStyle,
|
||||
}
|
||||
@ -257,9 +258,9 @@ func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext
|
||||
Opacity: 1.0,
|
||||
LineColor: border.borderColorLeft,
|
||||
X1: x,
|
||||
Y1: y,
|
||||
Y1: y + border.borderWidthLeft/2,
|
||||
X2: x,
|
||||
Y2: y - border.height,
|
||||
Y2: y - border.height - border.borderWidthLeft/2,
|
||||
LineStyle: border.LineStyle,
|
||||
}
|
||||
contentsLeft, _, err := lineLeft.Draw("")
|
||||
@ -304,9 +305,9 @@ func (border *border) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext
|
||||
Opacity: 1.0,
|
||||
LineColor: border.borderColorRight,
|
||||
X1: x,
|
||||
Y1: y,
|
||||
Y1: y + border.borderWidthRight/2,
|
||||
X2: x,
|
||||
Y2: y - border.height,
|
||||
Y2: y - border.height - border.borderWidthRight/2,
|
||||
LineStyle: border.LineStyle,
|
||||
}
|
||||
contentsRight, _, err := lineRight.Draw("")
|
||||
|
@ -20,11 +20,11 @@ import (
|
||||
"github.com/boombuler/barcode/qr"
|
||||
|
||||
"github.com/unidoc/unidoc/common"
|
||||
"github.com/unidoc/unidoc/pdf/contentstream/draw"
|
||||
"github.com/unidoc/unidoc/pdf/core"
|
||||
"github.com/unidoc/unidoc/pdf/model"
|
||||
"github.com/unidoc/unidoc/pdf/model/fonts"
|
||||
"github.com/unidoc/unidoc/pdf/model/textencoding"
|
||||
"github.com/unidoc/unidoc/pdf/contentstream/draw"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -1699,8 +1699,8 @@ func TestRectangle(t *testing.T) {
|
||||
func TestSingleBorder(t *testing.T) {
|
||||
border := newBorder(100, 100, 100, 100)
|
||||
//border.SetFillColor(ColorRed)
|
||||
border.SetColorBottom(ColorRed)
|
||||
border.SetColorTop(ColorRed)
|
||||
border.SetColorBottom(ColorGreen)
|
||||
border.SetColorTop(ColorGreen)
|
||||
border.SetColorLeft(ColorRed)
|
||||
border.SetColorRight(ColorRed)
|
||||
|
||||
@ -1713,7 +1713,8 @@ func TestSingleBorder(t *testing.T) {
|
||||
c.Draw(border)
|
||||
|
||||
//err := c.WriteToFile("/tmp/table_border_req1_test.pdf")
|
||||
err := c.WriteToFile("../../testfiles/table.pdf")
|
||||
//err := c.WriteToFile("../../testfiles/table.pdf")
|
||||
err := c.WriteToFile("/tmp/border_single.pdf")
|
||||
if err != nil {
|
||||
t.Errorf("Fail: %v\n", err)
|
||||
return
|
||||
|
Loading…
x
Reference in New Issue
Block a user