Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round scale to nearest 26.6 fixed point. #86

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# Please keep the list sorted.

Andrew Gerrand <[email protected]>
Dmitri Shuralyov <[email protected]>
Jeff R. Allen <[email protected]> <[email protected]>
Maksim Kochkin <[email protected]>
Michael Fogleman <[email protected]>
Expand Down
4 changes: 2 additions & 2 deletions freetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Context struct {
// PointToFixed converts the given number of points (as in "a 12 point font")
// into a 26.6 fixed point number of pixels.
func (c *Context) PointToFixed(x float64) fixed.Int26_6 {
return fixed.Int26_6(x * float64(c.dpi) * (64.0 / 72.0))
return fixed.Int26_6(0.5 + (x * c.dpi * 64 / 72))
}

// drawContour draws the given closed contour with the given offset.
Expand Down Expand Up @@ -260,7 +260,7 @@ func (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, erro
// recalc recalculates scale and bounds values from the font size, screen
// resolution and font metrics, and invalidates the glyph cache.
func (c *Context) recalc() {
c.scale = fixed.Int26_6(c.fontSize * c.dpi * (64.0 / 72.0))
c.scale = fixed.Int26_6(0.5 + (c.fontSize * c.dpi * 64 / 72))
if c.f == nil {
c.r.SetBounds(0, 0)
} else {
Expand Down
25 changes: 25 additions & 0 deletions freetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"runtime"
"strings"
"testing"

"golang.org/x/image/math/fixed"
)

func BenchmarkDrawString(b *testing.B) {
Expand Down Expand Up @@ -57,3 +59,26 @@ func BenchmarkDrawString(b *testing.B) {
mallocs = ms.Mallocs - mallocs
b.Logf("%d iterations, %d mallocs per iteration\n", b.N, int(mallocs)/b.N)
}

func TestScaling(t *testing.T) {
c := NewContext()
for _, tc := range [...]struct {
in float64
want fixed.Int26_6
}{
{in: 12, want: fixed.I(12)},
{in: 11.992, want: fixed.I(12) - 1},
{in: 11.993, want: fixed.I(12)},
{in: 12.007, want: fixed.I(12)},
{in: 12.008, want: fixed.I(12) + 1},
{in: 86.4, want: fixed.Int26_6(86<<6 + 26)}, // Issue https://github.com/golang/freetype/issues/85.
} {
c.SetFontSize(tc.in)
if got, want := c.scale, tc.want; got != want {
t.Errorf("scale after SetFontSize(%v) = %v, want %v", tc.in, got, want)
}
if got, want := c.PointToFixed(tc.in), tc.want; got != want {
t.Errorf("PointToFixed(%v) = %v, want %v", tc.in, got, want)
}
}
}