diff --git a/draw.go b/draw.go index 3b29a85..af9706b 100644 --- a/draw.go +++ b/draw.go @@ -6,6 +6,7 @@ import "image/draw" import "image/color" import "golang.org/x/image/font" import "golang.org/x/image/math/fixed" +import "git.tebibyte.media/tomo/typeset/internal" // Draw draws the contents of a TypeSetter to an image at the given offset. It // returns a rectangle containing all pixels in the image that were updated. @@ -63,11 +64,11 @@ func DrawBounds (destination draw.Image, setter *TypeSetter, offset fixed.Point2 layoutBounds := setter.LayoutBounds() minimum := setter.MinimumSize() - minimumRect := roundRect(fixed.Rectangle26_6 { Max: minimum }.Add(offset).Add(layoutBounds.Min)) - drawRectangleOutline(destination, minimumRect, green) + minimumRect := internal.RoundRect(fixed.Rectangle26_6 { Max: minimum }.Add(offset).Add(layoutBounds.Min)) + internal.DrawRectangleOutline(destination, minimumRect, green) - drawRectangleOutline(destination, roundRect(layoutBoundsSpace.Add(offset)), blue) - drawRectangleOutline(destination, roundRect(layoutBounds.Add(offset)), red) + internal.DrawRectangleOutline(destination, internal.RoundRect(layoutBoundsSpace.Add(offset)), blue) + internal.DrawRectangleOutline(destination, internal.RoundRect(layoutBounds.Add(offset)), red) } func drawTofu ( @@ -78,29 +79,8 @@ func drawTofu ( col color.Color, ) { bounds, _ := tofuBounds(face) - rectBounds := roundRect(bounds).Add(image.Pt ( + rectBounds := internal.RoundRect(bounds).Add(image.Pt ( position.X.Round(), position.Y.Round())) - drawRectangleOutline(destination, rectBounds, col) -} - -func drawRectangleOutline (destination draw.Image, bounds image.Rectangle, col color.Color) { - for x := bounds.Min.X; x < bounds.Max.X; x ++ { - destination.Set(x, bounds.Min.Y, col) - } - for y := bounds.Min.Y; y < bounds.Max.Y; y ++ { - destination.Set(bounds.Min.X, y, col) - destination.Set(bounds.Max.X - 1, y, col) - } - for x := bounds.Min.X; x < bounds.Max.X; x ++ { - destination.Set(x, bounds.Max.Y - 1, col) - } -} - -func roundRect (rectangle fixed.Rectangle26_6) image.Rectangle { - return image.Rect ( - rectangle.Min.X.Round(), - rectangle.Min.Y.Round(), - rectangle.Max.X.Round(), - rectangle.Max.Y.Round()) + internal.DrawRectangleOutline(destination, rectBounds, col) } diff --git a/internal/draw.go b/internal/draw.go new file mode 100644 index 0000000..9bf9502 --- /dev/null +++ b/internal/draw.go @@ -0,0 +1,27 @@ +package internal + +import "image" +import "image/draw" +import "image/color" +import "golang.org/x/image/math/fixed" + +func DrawRectangleOutline (destination draw.Image, bounds image.Rectangle, col color.Color) { + for x := bounds.Min.X; x < bounds.Max.X; x ++ { + destination.Set(x, bounds.Min.Y, col) + } + for y := bounds.Min.Y; y < bounds.Max.Y; y ++ { + destination.Set(bounds.Min.X, y, col) + destination.Set(bounds.Max.X - 1, y, col) + } + for x := bounds.Min.X; x < bounds.Max.X; x ++ { + destination.Set(x, bounds.Max.Y - 1, col) + } +} + +func RoundRect (rectangle fixed.Rectangle26_6) image.Rectangle { + return image.Rect ( + rectangle.Min.X.Round(), + rectangle.Min.Y.Round(), + rectangle.Max.X.Round(), + rectangle.Max.Y.Round()) +}