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

multiple fixes: prevent crash from nil loca, fix Height metric, cache advances, more robust to bad kern issues #62

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
# 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]>
Nigel Tao <[email protected]>
Randall O'Reilly <[email protected]>
Rémy Oudompheng <[email protected]> <[email protected]>
Rob Pike <[email protected]>
Roger Peppe <[email protected]>
Expand Down
75 changes: 75 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Basic Go makefile

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get

DIRS=`go list ./...`

all: build

build:
@echo "GO111MODULE = $(value GO111MODULE)"
$(GOBUILD) -v $(DIRS)

test:
@echo "GO111MODULE = $(value GO111MODULE)"
$(GOTEST) -v $(DIRS)

clean:
@echo "GO111MODULE = $(value GO111MODULE)"
$(GOCLEAN) ./...

fmts:
gofmt -s -w .

vet:
@echo "GO111MODULE = $(value GO111MODULE)"
$(GOCMD) vet $(DIRS) | grep -v unkeyed

tidy: export GO111MODULE = on
tidy:
@echo "GO111MODULE = $(value GO111MODULE)"
go mod tidy

mod-update: export GO111MODULE = on
mod-update:
@echo "GO111MODULE = $(value GO111MODULE)"
go get -u ./...
go mod tidy

# gopath-update is for GOPATH to get most things updated.
# need to call it in a target executable directory
gopath-update: export GO111MODULE = off
gopath-update:
@echo "GO111MODULE = $(value GO111MODULE)"
go get -u ./...

# NOTE: MUST update version number here prior to running 'make release' and edit this file!
VERS=v1.0.1
PACKAGE=freetype
GIT_COMMIT=`git rev-parse --short HEAD`
VERS_DATE=`date -u +%Y-%m-%d\ %H:%M`
VERS_FILE=version.go

release:
/bin/rm -f $(VERS_FILE)
@echo "// WARNING: auto-generated by Makefile release target -- run 'make release' to update" > $(VERS_FILE)
@echo "" >> $(VERS_FILE)
@echo "package $(PACKAGE)" >> $(VERS_FILE)
@echo "" >> $(VERS_FILE)
@echo "const (" >> $(VERS_FILE)
@echo " Version = \"$(VERS)\"" >> $(VERS_FILE)
@echo " GitCommit = \"$(GIT_COMMIT)\" // the commit JUST BEFORE the release" >> $(VERS_FILE)
@echo " VersionDate = \"$(VERS_DATE)\" // UTC" >> $(VERS_FILE)
@echo ")" >> $(VERS_FILE)
@echo "" >> $(VERS_FILE)
goimports -w $(VERS_FILE)
/bin/cat $(VERS_FILE)
git commit -am "$(VERS) release"
git tag -a $(VERS) -m "$(VERS) release"
git push
git push origin --tags

2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The Freetype font rasterizer in the Go programming language.

To download and install from source:
$ go get github.com/golang/freetype
$ go get github.com/tdewolff/freetype

It is an incomplete port:
* It only supports TrueType fonts, and not Type 1 fonts nor bitmap fonts.
Expand Down
6 changes: 4 additions & 2 deletions example/capjoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -21,7 +23,7 @@ import (
"log"
"os"

"github.com/golang/freetype/raster"
"github.com/goki/freetype/raster"
"golang.org/x/image/math/fixed"
)

Expand Down
6 changes: 4 additions & 2 deletions example/drawer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -24,7 +26,7 @@ import (
"math"
"os"

"github.com/golang/freetype/truetype"
"github.com/goki/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
Expand Down
6 changes: 4 additions & 2 deletions example/freetype/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -23,7 +25,7 @@ import (
"log"
"os"

"github.com/golang/freetype"
"github.com/goki/freetype"
"golang.org/x/image/font"
)

Expand Down
6 changes: 4 additions & 2 deletions example/gamma/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -20,7 +22,7 @@ import (
"log"
"os"

"github.com/golang/freetype/raster"
"github.com/goki/freetype/raster"
"golang.org/x/image/math/fixed"
)

Expand Down
6 changes: 4 additions & 2 deletions example/genbasicfont/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -26,7 +28,7 @@ import (
"strings"
"unicode"

"github.com/golang/freetype/truetype"
"github.com/goki/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
Expand Down
6 changes: 4 additions & 2 deletions example/raster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -21,7 +23,7 @@ import (
"log"
"os"

"github.com/golang/freetype/raster"
"github.com/goki/freetype/raster"
"golang.org/x/image/math/fixed"
)

Expand Down
6 changes: 4 additions & 2 deletions example/round/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -27,7 +29,7 @@ import (
"math"
"os"

"github.com/golang/freetype/raster"
"github.com/goki/freetype/raster"
"golang.org/x/image/math/fixed"
)

Expand Down
6 changes: 4 additions & 2 deletions example/truetype/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

//go:build example
// +build example

//
// This build tag means that "go install github.com/golang/freetype/..."
// This build tag means that "go install ..."
// doesn't install this example program. Use "go run main.go" to run it or "go
// install -tags=example" to install it.

Expand All @@ -17,7 +19,7 @@ import (
"io/ioutil"
"log"

"github.com/golang/freetype/truetype"
"github.com/goki/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
Expand Down
14 changes: 8 additions & 6 deletions freetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
// The freetype package provides a convenient API to draw text onto an image.
// Use the freetype/raster and freetype/truetype packages for lower level
// control over rasterization and TrueType parsing.
package freetype // import "github.com/golang/freetype"
package freetype

import (
"errors"
"image"
"image/draw"

"github.com/golang/freetype/raster"
"github.com/golang/freetype/truetype"
"github.com/goki/freetype/raster"
"github.com/goki/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
Expand Down 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 Expand Up @@ -332,10 +332,12 @@ func (c *Context) SetClip(clip image.Rectangle) {

// NewContext creates a new Context.
func NewContext() *Context {
return &Context{
c := &Context{
r: raster.NewRasterizer(0, 0),
fontSize: 12,
dpi: 72,
scale: 12 << 6,
}
c.r.UseNonZeroWinding = true // needed for font rendering
return c
}
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)
}
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/goki/freetype

go 1.18

require golang.org/x/image v0.6.0
Loading