hr.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package gopdf
  2. import (
  3. "github.com/tiechui1994/gopdf/core"
  4. )
  5. type HLine struct {
  6. pdf *core.Report
  7. color float64
  8. width float64
  9. margin core.Scope
  10. }
  11. func NewHLine(pdf *core.Report) *HLine {
  12. unit := pdf.GetUnit()
  13. return &HLine{
  14. pdf: pdf,
  15. color: 0,
  16. width: 0.1,
  17. margin: core.Scope{
  18. Left: 0,
  19. Right: 0,
  20. Top: 0.1 * unit,
  21. Bottom: 0.1 * unit,
  22. },
  23. }
  24. }
  25. func (h *HLine) SetColor(color float64) *HLine {
  26. if color < 0 || color > 1.0 {
  27. color = 0
  28. }
  29. h.color = color
  30. return h
  31. }
  32. func (h *HLine) SetMargin(margin core.Scope) *HLine {
  33. margin.ReplaceMarign()
  34. h.margin = margin
  35. return h
  36. }
  37. func (h *HLine) SetWidth(width float64) *HLine {
  38. h.width = width
  39. return h
  40. }
  41. func (h *HLine) GenerateAtomicCell() {
  42. var (
  43. sx, sy = h.pdf.GetXY()
  44. )
  45. x := sx + h.margin.Left
  46. y := sy + h.margin.Top
  47. endY := h.pdf.GetPageEndY()
  48. if (sy >= endY || sy < endY) && sy+h.width > endY {
  49. h.pdf.AddNewPage(false)
  50. h.pdf.SetXY(h.pdf.GetPageStartXY())
  51. h.GenerateAtomicCell()
  52. return
  53. }
  54. cw, _ := h.pdf.GetContentWidthAndHeight()
  55. h.pdf.GrayColor(x, y, cw, h.width, h.color)
  56. x, _ = h.pdf.GetPageStartXY()
  57. h.pdf.SetXY(x, y+h.margin.Bottom+h.width)
  58. }