font: fix rectangle-union for empty rectangles.

The bounding box of a string does not necessarily contain the origin.
Prior to this commit, BoundString(etc, "x") would call grow exactly
once, with the first argument being the (empty) zero rectangle.

Change-Id: Id7d4f6c250ac0749f6dae19d11f4e97f9c6f45bc
Reviewed-on: https://go-review.googlesource.com/34674
Reviewed-by: Dave Day <djd@golang.org>
diff --git a/font/font.go b/font/font.go
index d523baf..2556ebb 100644
--- a/font/font.go
+++ b/font/font.go
@@ -220,7 +220,9 @@
 			// TODO: set prevC = '\ufffd'?
 			continue
 		}
-		bounds = grow(bounds, b, advance)
+		b.Min.X += advance
+		b.Max.X += advance
+		bounds = grow(bounds, b)
 		advance += a
 		prevC = c
 	}
@@ -242,25 +244,36 @@
 			// TODO: set prevC = '\ufffd'?
 			continue
 		}
-		bounds = grow(bounds, b, advance)
+		b.Min.X += advance
+		b.Max.X += advance
+		bounds = grow(bounds, b)
 		advance += a
 		prevC = c
 	}
 	return
 }
 
-// grow returns the smallest rectangle containing both b and b2+shift.
-func grow(b, b2 fixed.Rectangle26_6, shift fixed.Int26_6) fixed.Rectangle26_6 {
-	x := b2.Min.X + shift
-	if b.Min.X > x {
-		b.Min.X = x
+func empty(r fixed.Rectangle26_6) bool {
+	return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
+}
+
+// grow returns the smallest rectangle containing both b and b2.
+func grow(b, b2 fixed.Rectangle26_6) fixed.Rectangle26_6 {
+	if empty(b) {
+		return b2
+	}
+	if empty(b2) {
+		return b
+	}
+
+	if b.Min.X > b2.Min.X {
+		b.Min.X = b2.Min.X
 	}
 	if b.Min.Y > b2.Min.Y {
 		b.Min.Y = b2.Min.Y
 	}
-	x = b2.Max.X + shift
-	if b.Max.X < x {
-		b.Max.X = x
+	if b.Max.X < b2.Max.X {
+		b.Max.X = b2.Max.X
 	}
 	if b.Max.Y < b2.Max.Y {
 		b.Max.Y = b2.Max.Y
diff --git a/font/font_test.go b/font/font_test.go
new file mode 100644
index 0000000..1f05524
--- /dev/null
+++ b/font/font_test.go
@@ -0,0 +1,65 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package font
+
+import (
+	"image"
+	"strings"
+	"testing"
+
+	"golang.org/x/image/math/fixed"
+)
+
+const toyAdvance = fixed.Int26_6(10 << 6)
+
+type toyFace struct{}
+
+func (toyFace) Close() error {
+	return nil
+}
+
+func (toyFace) Glyph(dot fixed.Point26_6, r rune) (image.Rectangle, image.Image, image.Point, fixed.Int26_6, bool) {
+	panic("unimplemented")
+}
+
+func (toyFace) GlyphBounds(r rune) (fixed.Rectangle26_6, fixed.Int26_6, bool) {
+	return fixed.Rectangle26_6{
+		Min: fixed.P(2, 0),
+		Max: fixed.P(6, 1),
+	}, toyAdvance, true
+}
+
+func (toyFace) GlyphAdvance(r rune) (fixed.Int26_6, bool) {
+	return toyAdvance, true
+}
+
+func (toyFace) Kern(r0, r1 rune) fixed.Int26_6 {
+	return 0
+}
+
+func (toyFace) Metrics() Metrics {
+	return Metrics{}
+}
+
+func TestBound(t *testing.T) {
+	wantBounds := []fixed.Rectangle26_6{
+		{Min: fixed.P(0, 0), Max: fixed.P(0, 0)},
+		{Min: fixed.P(2, 0), Max: fixed.P(6, 1)},
+		{Min: fixed.P(2, 0), Max: fixed.P(16, 1)},
+		{Min: fixed.P(2, 0), Max: fixed.P(26, 1)},
+	}
+
+	for i, wantBound := range wantBounds {
+		s := strings.Repeat("x", i)
+		gotBound, gotAdvance := BoundString(toyFace{}, s)
+		if gotBound != wantBound {
+			t.Errorf("i=%d: bound: got %v, want %v", i, gotBound, wantBound)
+		}
+		wantAdvance := toyAdvance * fixed.Int26_6(i)
+		if gotAdvance != wantAdvance {
+			t.Errorf("i=%d: advance: got %v, want %v", i, gotAdvance, wantAdvance)
+		}
+	}
+}