font/opentype: de-duplicate sfnt.loadGlyf calls

Prior to this commit, opentype.Face.Glyph started with:

    segments, err := f.f.LoadGlyph(&f.buf, x, etc)
    etc
    bounds, advance, err := f.f.GlyphBounds(&f.buf, x, etc)
    etc
    doStuffWith(segments)

One problem with this is that both f.f.FooBar calls ultimately lead to
sfnt.loadGlyf(etc, x, etc) calls and the second one is unnecessary
duplicate work for the same sfnt.GlyphIndex x.

A subtler problem is that the sfnt.Font.LoadGlyph doc comment says that
"the segments become invalid to use once [the buffer] is re-used" and we
were re-using &f.buf before walking the segments.

The fix to both problems is to downgrade the GlyphBounds call to a
cheaper GlyphAdvance call and to also move it above the LoadGlyph call.

This commit also defines and exports a new sfnt.Segments type (with a
Bounds method).

This commit also renames some s/segs/segments/ variables for
consistency, and tweaks some opentype.Face.Glyph comments.

Change-Id: I7d327db742dd701448dc097f30a87227b0fc61f6
Reviewed-on: https://go-review.googlesource.com/c/image/+/256957
Run-TryBot: Nigel Tao <nigeltao@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
Trust: Nigel Tao <nigeltao@golang.org>
3 files changed
tree: e62c75e314d9e75bd46c2dbb2ce2a8f835aac015
  1. .gitattributes
  2. .gitignore
  3. AUTHORS
  4. CONTRIBUTING.md
  5. CONTRIBUTORS
  6. LICENSE
  7. PATENTS
  8. README.md
  9. bmp/
  10. ccitt/
  11. cmd/
  12. codereview.cfg
  13. colornames/
  14. draw/
  15. example/
  16. font/
  17. go.mod
  18. go.sum
  19. math/
  20. riff/
  21. testdata/
  22. tiff/
  23. vector/
  24. vp8/
  25. vp8l/
  26. webp/
README.md

Go Images

This repository holds supplementary Go image libraries.

Download/Install

The easiest way to install is to run go get -u golang.org/x/image/.... You can also manually git clone the repository to $GOPATH/src/golang.org/x/image.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The main issue tracker for the image repository is located at https://github.com/golang/go/issues. Prefix your issue with “x/image:” in the subject line, so it is easy to find.