row.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package xvdoc
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type Row struct {
  8. oy int
  9. x int
  10. y int
  11. offsetY bool
  12. offsetX bool
  13. cells []Cell
  14. style string
  15. styles []string
  16. height float64
  17. }
  18. func (s *Row) UnmarshalJSON(src []byte) (err error) {
  19. s.y = -1
  20. dec := json.NewDecoder(bytes.NewReader(src))
  21. var t json.Token
  22. for dec.More() && err == nil {
  23. if t, err = dec.Token(); err == nil {
  24. if _, check := t.(json.Delim); !check {
  25. switch t.(string) {
  26. case "ox":
  27. err = dec.Decode(&s.oy)
  28. s.offsetX = true
  29. case "oy":
  30. err = dec.Decode(&s.oy)
  31. s.offsetY = true
  32. case "x":
  33. err = dec.Decode(&s.x)
  34. case "y":
  35. err = dec.Decode(&s.y)
  36. case "cells":
  37. s.cells, err = parseCells(dec)
  38. case "height":
  39. err = dec.Decode(&s.height)
  40. case "style":
  41. err = dec.Decode(&s.style)
  42. case "styles":
  43. err = dec.Decode(&s.styles)
  44. default:
  45. dec.Decode(new(interface{}))
  46. }
  47. }
  48. }
  49. }
  50. return
  51. }
  52. func (s *Row) String() (res string) {
  53. res = fmt.Sprintf("{\n x: %v\n y: %v\n oy: %v\n offsetY: %v\n cells: %v\n}", s.x, s.y, s.oy, s.offsetY, s.cells)
  54. return
  55. }
  56. func (s *Row) IsCustomHeight() bool {
  57. return s.height > 0
  58. }
  59. func (s *Row) Height() float64 {
  60. return s.height
  61. }
  62. func (s *Row) Styles() (res []string) {
  63. if len(s.styles) > 0 {
  64. res = s.styles
  65. } else if len(s.style) > 0 {
  66. res = []string{s.style}
  67. }
  68. return
  69. }