Skip to content

Commit

Permalink
Merge pull request #24 from tiechui1994/markdown
Browse files Browse the repository at this point in the history
Markdown
  • Loading branch information
tiechui1994 authored Aug 18, 2020
2 parents 219079b + deed1c2 commit 573f6fc
Show file tree
Hide file tree
Showing 254 changed files with 166,132 additions and 117 deletions.
2 changes: 1 addition & 1 deletion cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (cell *TextCell) GenerateAtomicCell(maxheight float64) (int, int, error) {

// 字体颜色控制
if !util.IsEmpty(cell.fontColor) {
cell.pdf.TextColor(util.GetColorRGB(cell.fontColor))
cell.pdf.TextColor(util.RGB(cell.fontColor))
}

cell.pdf.Cell(x, y, cell.contents[i])
Expand Down
9 changes: 6 additions & 3 deletions core/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (convert *Converter) LineColor(line string, elements []string) {

// backgroup color
func (convert *Converter) BackgroundColor(line string, elements []string) {
checkLength(line, elements, 9)
checkLength(line, elements, 12)

// white line
convert.pdf.SetStrokeColor(255, 255, 255)
Expand All @@ -278,10 +278,13 @@ func (convert *Converter) BackgroundColor(line string, elements []string) {
parseFloatPanic(elements[4], line)*convert.unit, "F")

// recover origin backgroup color and line color
convert.pdf.SetFillColor(0, 0, 0)
convert.pdf.SetStrokeColor(0, 0, 0)
convert.pdf.SetFillColor(1, 1, 1)
convert.pdf.SetStrokeColor(uint8(parseIntPanic(elements[9], line)),
uint8(parseIntPanic(elements[10], line)),
uint8(parseIntPanic(elements[11], line)))

convert.pdf.SetLineType("solid")
convert.pdf.SetLineWidth(convert.linew * convert.unit)

x := parseFloatPanic(elements[1], line) * convert.unit
y := parseFloatPanic(elements[2], line) * convert.unit
Expand Down
33 changes: 20 additions & 13 deletions core/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,26 +470,33 @@ func (report *Report) LineColor(red int, green int, blue int) {
"|" + strconv.Itoa(blue))
}

// color: 背景颜色
// line: 是否需要边框线条, "0000"不需要, "1111"需要, "0110" 是需要 TOP,RIGHT 线条
func (report *Report) BackgroundColor(x, y, w, h float64, color string, line string) {
if !rline.MatchString(line) {
line = "0000"
// bgcolor: background color. style is "r,g,b", eg. "1,1,1" is black
// lines: whether border lines are needed. eg, "0000" is not needed, "1111" is needed, "0110" is
// required for TOP, RIGHT lines.
// lcolor: line color, style is "r,g,b", eg. "1,1,1" is black
func (report *Report) BackgroundColor(x, y, w, h float64, bgcolor string, lines string, lcolor ...string) {
if !rline.MatchString(lines) {
lines = "0000"
}
if len(line) == 1 {
line = strings.Repeat(line, 4)
if len(lines) == 1 {
lines = strings.Repeat(lines, 4)
}
if len(line) == 2 {
line = strings.Repeat(line, 2)
if len(lines) == 2 {
lines = strings.Repeat(lines, 2)
}
if len(line) == 3 {
line += "0"
if len(lines) == 3 {
lines += "0"
}

red, green, blue := util.GetColorRGB(color)
bgred, bggreen, bgblue := util.RGB(bgcolor)
var lred, lgreen, lblue = 1, 1, 1
if lcolor != nil {
lred, lgreen, lblue = util.RGB(lcolor[0])
}

report.addAtomicCell("BC|" + util.Ftoa(x) + "|" + util.Ftoa(y) + "|" + util.Ftoa(w) + "|" +
util.Ftoa(h) + "|" + strconv.Itoa(red) + "|" + strconv.Itoa(green) + "|" + strconv.Itoa(blue) + "|" + line)
util.Ftoa(h) + "|" + strconv.Itoa(bgred) + "|" + strconv.Itoa(bggreen) + "|" + strconv.Itoa(bgblue) + "|" + lines + "|" +
strconv.Itoa(lred) + "|" + strconv.Itoa(lgreen) + "|" + strconv.Itoa(lblue))
}

// 线条灰度
Expand Down
2 changes: 1 addition & 1 deletion div.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (div *Div) GenerateAtomicCell() error {
}

if !util.IsEmpty(div.fontColor) {
div.pdf.TextColor(util.GetColorRGB(div.fontColor))
div.pdf.TextColor(util.RGB(div.fontColor))
}
div.pdf.Font(div.font.Family, div.font.Size, div.font.Style) // 添加设置
div.pdf.Cell(x, y, div.contents[i])
Expand Down
Binary file added example/ttf/microsoft-bold.ttf
Binary file not shown.
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/phpdave11/gofpdi v1.0.8 h1:9HRg0Z0qDfWeMU7ska+YNQ13RHxTxqP5KTg/dBl4o7c=
github.com/phpdave11/gofpdi v1.0.8/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/signintech/gopdf v0.9.8 h1:tafWEjYhorfHMl9JT9II43fvf5XhUwrjUKVNwQqqyJQ=
github.com/signintech/gopdf v0.9.8/go.mod h1:MrARAC6LaOgbnV6vrC5885VuoWCXazhAqx8L8zmjYy4=
golang.org/x/image v0.0.0-20200801110659-972c09e46d76 h1:U7GPaoQyQmX+CBRWXKrvRzWTbd+slqeSh8uARsIyhAw=
golang.org/x/image v0.0.0-20200801110659-972c09e46d76/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
26 changes: 26 additions & 0 deletions hr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,29 @@ func ComplexHLineReportExecutor(report *core.Report) {
func TestComplexHLineReport(t *testing.T) {
ComplexHLineReport()
}

type A interface {
A()
}

type B interface {
B()
}

type T struct {
}

func (t *T) A() {

}
func (t *T) B() {

}

func TestAB(t *testing.T) {
var b B
b = &T{}
if _, ok := b.(A); ok {
t.Log("ok")
}
}
133 changes: 78 additions & 55 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,49 @@ package gopdf
import (
"os"
"path/filepath"
"strings"
"fmt"
"log"
"time"

"github.com/tiechui1994/gopdf/core"
)

type Image struct {
pdf *core.Report
autobreak bool
path string
width, height float64
margin core.Scope
tempFilePath string
temppath []string
}

func NewImage(path string, pdf *core.Report) *Image {
var temppath []string
if _, err := os.Stat(path); err != nil {
panic("the path error")
path = fmt.Sprintf("/tmp/%v.png", time.Now().Unix())
temppath = append(temppath, path)
DrawPNG(path)
}

var tempFilePath string
picturePath, _ := filepath.Abs(path)
imageType, _ := GetImageType(picturePath)
if imageType == "png" {
index := strings.LastIndex(picturePath, ".")
tempFilePath = picturePath[0:index] + ".jpeg"
err := ConvertPNG2JPEG(picturePath, tempFilePath)
if err != nil {
panic(err)
}
picturePath = tempFilePath
dstPath := fmt.Sprintf("/tmp/%v.jpeg", time.Now().UnixNano())
srcPath, _ := filepath.Abs(path)
err := Convert2JPEG(srcPath, dstPath)
if err != nil {
log.Println(err)
return nil
}

w, h := GetImageWidthAndHeight(picturePath)
temppath = append(temppath, dstPath)

w, h := GetImageWidthAndHeight(dstPath)
image := &Image{
pdf: pdf,
path: picturePath,
width: float64(w / 10),
height: float64(h / 10),
tempFilePath: tempFilePath,
pdf: pdf,
path: dstPath,
width: float64(w),
height: float64(h),
temppath: temppath,
}
if tempFilePath != "" {
if dstPath != "" {
pdf.AddCallBack(image.delTempImage)
}

Expand All @@ -58,39 +61,44 @@ func NewImageWithWidthAndHeight(path string, width, height float64, pdf *core.Re
height = contentHeight
}

var temppath []string
if _, err := os.Stat(path); err != nil {
panic("the path error")
path = fmt.Sprintf("/tmp/%v.png", time.Now().Unix())
temppath = append(temppath, path)
DrawPNG(path)
}

var tempFilePath string
picturePath, _ := filepath.Abs(path)
imageType, _ := GetImageType(picturePath)

if imageType == "png" {
index := strings.LastIndex(picturePath, ".")
tempFilePath = picturePath[0:index] + ".jpeg"
err := ConvertPNG2JPEG(picturePath, tempFilePath)
if err != nil {
panic(err.Error())
}
picturePath = tempFilePath
dstPath := fmt.Sprintf("/tmp/%v.jpeg", time.Now().UnixNano())
srcPath, _ := filepath.Abs(path)
err := Convert2JPEG(srcPath, dstPath)
if err != nil {
return nil
}

w, h := GetImageWidthAndHeight(picturePath)
if float64(h)*width/float64(w) > height {
width = float64(w) * height / float64(h)
} else {
w, h := GetImageWidthAndHeight(dstPath)
if width > 0 && height > 0 {
if float64(h)*width/float64(w) > height {
width = float64(w) * height / float64(h)
} else {
height = float64(h) * width / float64(w)
}
} else if width > 0 {
height = float64(h) * width / float64(w)
} else if height > 0 {
width = float64(w) * height / float64(h)
}

temppath = append(temppath, dstPath)

image := &Image{
pdf: pdf,
path: picturePath,
width: width,
height: height,
tempFilePath: tempFilePath,
pdf: pdf,
path: dstPath,
width: width,
height: height,
temppath: temppath,
}

if tempFilePath != "" {
if dstPath != "" {
pdf.AddCallBack(image.delTempImage)
}

Expand All @@ -110,32 +118,47 @@ func (image *Image) GetWidth() float64 {
return image.width
}

func (image *Image) SetAutoBreak() {
image.autobreak = true
}

func (image *Image) GenerateAtomicCell() error {
// 自动换行
func (image *Image) GenerateAtomicCell() (pagebreak, over bool, err error) {
var (
sx, sy = image.pdf.GetXY()
)

x, y := sx+image.margin.Left, sy+image.margin.Top
_, pageEndY := image.pdf.GetPageEndXY()
pageEndX, pageEndY := image.pdf.GetPageEndXY()
if y < pageEndY && y+float64(image.height) > pageEndY {
image.pdf.AddNewPage(false)
if image.autobreak {
image.pdf.AddNewPage(false)
goto draw
}

return true, false, nil
}

draw:
image.pdf.Image(image.path, x, y, x+float64(image.width), y+float64(image.height))
sx, _ = image.pdf.GetPageStartXY()
image.pdf.SetXY(sx, y+float64(image.height)+image.margin.Bottom)
return nil
if x+float64(image.width) >= pageEndX {
sx, _ = image.pdf.GetPageStartXY()
image.pdf.SetXY(sx, y+float64(image.height)+image.margin.Bottom)
} else {
image.pdf.SetXY(x+float64(image.width), y)
}

return false, true, nil
}

func (image *Image) delTempImage(report *core.Report) {
if image.tempFilePath == "" {
if image.temppath == nil {
return
}

if _, err := os.Stat(image.tempFilePath); err != nil {
return
for _, path := range image.temppath {
if _, err := os.Stat(path); err == nil || os.IsExist(err) {
os.Remove(path)
}
}

os.Remove(image.tempFilePath)
}
Loading

0 comments on commit 573f6fc

Please sign in to comment.