font_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2016 The Go 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 file.
  4. package font
  5. import (
  6. "image"
  7. "strings"
  8. "testing"
  9. "golang.org/x/image/math/fixed"
  10. )
  11. const toyAdvance = fixed.Int26_6(10 << 6)
  12. type toyFace struct{}
  13. func (toyFace) Close() error {
  14. return nil
  15. }
  16. func (toyFace) Glyph(dot fixed.Point26_6, r rune) (image.Rectangle, image.Image, image.Point, fixed.Int26_6, bool) {
  17. panic("unimplemented")
  18. }
  19. func (toyFace) GlyphBounds(r rune) (fixed.Rectangle26_6, fixed.Int26_6, bool) {
  20. return fixed.Rectangle26_6{
  21. Min: fixed.P(2, 0),
  22. Max: fixed.P(6, 1),
  23. }, toyAdvance, true
  24. }
  25. func (toyFace) GlyphAdvance(r rune) (fixed.Int26_6, bool) {
  26. return toyAdvance, true
  27. }
  28. func (toyFace) Kern(r0, r1 rune) fixed.Int26_6 {
  29. return 0
  30. }
  31. func (toyFace) Metrics() Metrics {
  32. return Metrics{}
  33. }
  34. func TestBound(t *testing.T) {
  35. wantBounds := []fixed.Rectangle26_6{
  36. {Min: fixed.P(0, 0), Max: fixed.P(0, 0)},
  37. {Min: fixed.P(2, 0), Max: fixed.P(6, 1)},
  38. {Min: fixed.P(2, 0), Max: fixed.P(16, 1)},
  39. {Min: fixed.P(2, 0), Max: fixed.P(26, 1)},
  40. }
  41. for i, wantBound := range wantBounds {
  42. s := strings.Repeat("x", i)
  43. gotBound, gotAdvance := BoundString(toyFace{}, s)
  44. if gotBound != wantBound {
  45. t.Errorf("i=%d: bound: got %v, want %v", i, gotBound, wantBound)
  46. }
  47. wantAdvance := toyAdvance * fixed.Int26_6(i)
  48. if gotAdvance != wantAdvance {
  49. t.Errorf("i=%d: advance: got %v, want %v", i, gotAdvance, wantAdvance)
  50. }
  51. }
  52. }