font/sfnt: fix Font.GlyphBounds with truncated hmtx table

Thanks to Joe Blubaugh for the suggestion.

Change-Id: I27cb776614b5a4a0d023755f01b4db06e3d9a1c5
Reviewed-on: https://go-review.googlesource.com/c/image/+/244997
Run-TryBot: Nigel Tao <nigeltao@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/font/sfnt/sfnt.go b/font/sfnt/sfnt.go
index f05b4cd..d882891 100644
--- a/font/sfnt/sfnt.go
+++ b/font/sfnt/sfnt.go
@@ -1518,11 +1518,12 @@
 	// optimization, the number of records can be less than the number of
 	// glyphs, in which case the advance width value of the last record applies
 	// to all remaining glyph IDs."
+	metricIndex := x
 	if n := GlyphIndex(f.cached.numHMetrics - 1); x > n {
-		x = n
+		metricIndex = n
 	}
 
-	buf, err := b.view(&f.src, int(f.hmtx.offset)+4*int(x), 2)
+	buf, err := b.view(&f.src, int(f.hmtx.offset)+4*int(metricIndex), 2)
 	if err != nil {
 		return fixed.Rectangle26_6{}, 0, err
 	}