option.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import (
  6. "io"
  7. "modernc.org/knuth/font/fixed"
  8. "modernc.org/knuth/kpath"
  9. )
  10. type Option func(cfg *config) error
  11. type config struct {
  12. ctx kpath.Context
  13. rdr Renderer
  14. out io.Writer
  15. xoff int32 // width offset
  16. yoff int32 // height offset
  17. handlers []Handler
  18. }
  19. func newConfig() *config {
  20. return &config{
  21. ctx: kpath.New(),
  22. rdr: nopRenderer{},
  23. out: io.Discard,
  24. }
  25. }
  26. func WithContext(ctx kpath.Context) Option {
  27. return func(cfg *config) error {
  28. cfg.ctx = ctx
  29. return nil
  30. }
  31. }
  32. func WithRenderer(rdr Renderer) Option {
  33. return func(cfg *config) error {
  34. cfg.rdr = rdr
  35. return nil
  36. }
  37. }
  38. func WithLogOutput(w io.Writer) Option {
  39. return func(cfg *config) error {
  40. cfg.out = w
  41. return nil
  42. }
  43. }
  44. func WithOffsetX(v fixed.Int12_20) Option {
  45. return func(cfg *config) error {
  46. cfg.xoff = int32(v)
  47. return nil
  48. }
  49. }
  50. func WithOffsetY(v fixed.Int12_20) Option {
  51. return func(cfg *config) error {
  52. cfg.yoff = int32(v)
  53. return nil
  54. }
  55. }
  56. // WithHandlers specifies a list of Handlers a DVI Machine will use to
  57. // handle the XXXn special commands.
  58. func WithHandlers(hs ...Handler) Option {
  59. return func(cfg *config) error {
  60. cfg.handlers = hs
  61. return nil
  62. }
  63. }