config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package core
  2. // 单位像素
  3. type Config struct {
  4. startX, startY float64 // PDF页的开始坐标定位, 必须指定
  5. endX, endY float64 // PDF页的结束坐标定位, 必须指定
  6. width, height float64 // PDF页的宽度和高度, 必须指定
  7. contentWidth, contentHeight float64 // PDF页内容的宽度和高度, 计算得到
  8. }
  9. func (config *Config) checkConfig() {
  10. if config.startX < 0 || config.startY < 0 {
  11. panic("the pdf page start position invilid")
  12. }
  13. if config.endX < 0 || config.endY < 0 || config.endX <= config.startX || config.endY <= config.startY {
  14. panic("the pdf page end position invilid")
  15. }
  16. if config.width <= config.endX || config.height <= config.endY {
  17. panic("the pdf page width or height invilid")
  18. }
  19. // 关系验证
  20. if config.endX+config.startX != config.width || config.endY+config.startY != config.height {
  21. panic("the paf page config invilid")
  22. }
  23. }
  24. var defaultConfigs map[string]*Config // page -> config
  25. /**************************************
  26. A0 ~ A5 纸张像素表示
  27. 'A0': [2383.94, 3370.39],
  28. 'A1': [1683.78, 2383.94],
  29. 'A2': [1190.55, 1683.78],
  30. 'A3': [841.89, 1190.55],
  31. 'A4': [595.28, 841.89],
  32. 'A5': [419.53, 595.28],
  33. ***************************************/
  34. func init() {
  35. defaultConfigs = make(map[string]*Config)
  36. defaultConfigs["A3"] = &Config{
  37. startX: 90.14,
  38. startY: 72.00,
  39. endX: 751.76,
  40. endY: 1118.55,
  41. width: 841.89,
  42. height: 1190.55,
  43. contentWidth: 661.62,
  44. contentHeight: 1046.55,
  45. }
  46. defaultConfigs["A4"] = &Config{
  47. startX: 90.14,
  48. startY: 72.00,
  49. endX: 505.14,
  50. endY: 769.89,
  51. width: 595.28,
  52. height: 841.89,
  53. contentWidth: 415,
  54. contentHeight: 697.89,
  55. }
  56. defaultConfigs["LTR"] = &Config{
  57. startX: 90.14,
  58. startY: 72.00,
  59. endX: 521.86,
  60. endY: 720,
  61. width: 612,
  62. height: 792,
  63. contentWidth: 431.72,
  64. contentHeight: 648,
  65. }
  66. }
  67. func Register(size string, config *Config) {
  68. if _, ok := defaultConfigs[size]; ok {
  69. return
  70. }
  71. config.checkConfig()
  72. config.contentWidth = config.endX - config.startX
  73. config.contentHeight = config.endY - config.startY
  74. defaultConfigs[size] = config
  75. }