font.go 957 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. "modernc.org/knuth/font/fixed"
  7. "modernc.org/knuth/font/tfm"
  8. )
  9. // Font describes a DVI font, with TeX Font Metrics and its
  10. // associated font glyph data.
  11. type Font struct {
  12. name string
  13. font tfm.Font
  14. scale fixed.Int12_20
  15. }
  16. // Name returns the name of the DVI font.
  17. // ex: "cmr10", "cmmi10" or "cmti10".
  18. func (fnt *Font) Name() string {
  19. return fnt.name
  20. }
  21. // Size returns the DVI font size.
  22. func (fnt *Font) Size() fixed.Int12_20 {
  23. return fnt.scale
  24. }
  25. // Metrics returns the associated TeX Font Metrics.
  26. func (fnt *Font) Metrics() *tfm.Font {
  27. return &fnt.font
  28. }
  29. func (fnt *Font) advance(r rune) (fixed.Int12_20, bool) {
  30. w, _, _, ok := fnt.font.Box(r)
  31. if !ok {
  32. return 0, ok
  33. }
  34. return fixed.Int12_20((int64(w) * int64(fnt.scale)) >> 20), true
  35. }