state.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright ©2021 The star-tex Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE-STAR-TEX file.
  4. package dvi
  5. // regs is the set of DVI registers.
  6. type regs struct {
  7. h int32 // h is the current horizontal position on the page.
  8. v int32 // v is the current vertical position on the page.
  9. w int32 // horizontal spacing
  10. x int32 // horizontal spacing
  11. y int32 // vertical spacing
  12. z int32 // vertical spacing
  13. hh int32 // hh is the current horizontal position on the page, in pixels.
  14. vv int32 // vv is the current vertical position on the page, in pixels.
  15. }
  16. type fntdef struct {
  17. ID int
  18. Checksum uint32
  19. Size int32
  20. Design int32
  21. Area string
  22. Name string
  23. mag int32
  24. font *Font
  25. }
  26. type state struct {
  27. fonts map[int]fntdef
  28. f int // current font
  29. stack []regs
  30. }
  31. func newState() state {
  32. return state{
  33. fonts: make(map[int]fntdef),
  34. stack: make([]regs, 1),
  35. }
  36. }
  37. func (st *state) reset(xoff, yoff int32, pixels func(v int32) int32) {
  38. st.stack = st.stack[:1]
  39. st.stack[0] = regs{
  40. h: xoff,
  41. v: yoff,
  42. hh: pixels(xoff),
  43. vv: pixels(yoff),
  44. }
  45. st.f = -1
  46. }
  47. func (st *state) cur() *regs {
  48. return &st.stack[len(st.stack)-1]
  49. }
  50. func (st *state) push() {
  51. stk := st.cur()
  52. st.stack = append(st.stack, *stk)
  53. }
  54. func (st *state) pop() {
  55. if len(st.stack) == 0 {
  56. panic("dvi: unbalanced push/pop")
  57. }
  58. st.stack = st.stack[:len(st.stack)-1]
  59. }