examples: Use golang.org/x/image/font/opentype

Fixes #484
This commit is contained in:
Hajime Hoshi 2020-10-03 23:12:39 +09:00
parent c08f47f445
commit eb38324021
7 changed files with 46 additions and 22 deletions

View File

@ -22,8 +22,8 @@ import (
"sort" "sort"
"strconv" "strconv"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts" "github.com/hajimehoshi/ebiten/examples/resources/fonts"
@ -37,27 +37,36 @@ var (
) )
func init() { func init() {
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf) tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
const dpi = 72 const dpi = 72
mplusSmallFont = truetype.NewFace(tt, &truetype.Options{ mplusSmallFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 24, Size: 24,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
mplusNormalFont = truetype.NewFace(tt, &truetype.Options{ if err != nil {
log.Fatal(err)
}
mplusNormalFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 32, Size: 32,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
mplusBigFont = truetype.NewFace(tt, &truetype.Options{ if err != nil {
log.Fatal(err)
}
mplusBigFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 48, Size: 48,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
if err != nil {
log.Fatal(err)
}
} }
// TileData represents a tile information like a value and a position. // TileData represents a tile information like a value and a position.

View File

@ -24,8 +24,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts" "github.com/hajimehoshi/ebiten/examples/resources/fonts"
@ -90,22 +90,28 @@ func init() {
} }
func init() { func init() {
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf) tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
const dpi = 72 const dpi = 72
mplusNormalFont = truetype.NewFace(tt, &truetype.Options{ mplusNormalFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 24, Size: 24,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
mplusBigFont = truetype.NewFace(tt, &truetype.Options{ if err != nil {
log.Fatal(err)
}
mplusBigFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 48, Size: 48,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
if err != nil {
log.Fatal(err)
}
} }
func init() { func init() {

View File

@ -26,8 +26,8 @@ import (
"log" "log"
"math" "math"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts" "github.com/hajimehoshi/ebiten/examples/resources/fonts"
@ -59,17 +59,20 @@ func init() {
} }
func initFont() { func initFont() {
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf) tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
const dpi = 72 const dpi = 72
mplusFont = truetype.NewFace(tt, &truetype.Options{ mplusFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 12 * ebiten.DeviceScaleFactor(), Size: 12 * ebiten.DeviceScaleFactor(),
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
if err != nil {
log.Fatal(err)
}
} }
type Game struct { type Game struct {

View File

@ -22,8 +22,8 @@ import (
"math/rand" "math/rand"
"time" "time"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
@ -45,22 +45,28 @@ var (
) )
func init() { func init() {
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf) tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
const dpi = 72 const dpi = 72
mplusNormalFont = truetype.NewFace(tt, &truetype.Options{ mplusNormalFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 24, Size: 24,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
mplusBigFont = truetype.NewFace(tt, &truetype.Options{ if err != nil {
log.Fatal(err)
}
mplusBigFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 32, Size: 32,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
if err != nil {
log.Fatal(err)
}
} }
func init() { func init() {

View File

@ -24,9 +24,9 @@ import (
"log" "log"
"strings" "strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular" "golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/images" "github.com/hajimehoshi/ebiten/examples/resources/images"
@ -60,15 +60,18 @@ func init() {
} }
uiImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) uiImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
tt, err := truetype.Parse(goregular.TTF) tt, err := opentype.Parse(goregular.TTF)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
uiFont = truetype.NewFace(tt, &truetype.Options{ uiFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: 12, Size: 12,
DPI: 72, DPI: 72,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
if err != nil {
log.Fatal(err)
}
b, _, _ := uiFont.GlyphBounds('M') b, _, _ := uiFont.GlyphBounds('M')
uiFontMHeight = (b.Max.Y - b.Min.Y).Ceil() uiFontMHeight = (b.Max.Y - b.Min.Y).Ceil()
} }

1
go.mod
View File

@ -5,7 +5,6 @@ go 1.12
require ( require (
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2
github.com/gofrs/flock v0.8.0 github.com/gofrs/flock v0.8.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/hajimehoshi/bitmapfont/v2 v2.1.0 github.com/hajimehoshi/bitmapfont/v2 v2.1.0
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e
github.com/hajimehoshi/go-mp3 v0.3.1 github.com/hajimehoshi/go-mp3 v0.3.1

2
go.sum
View File

@ -3,8 +3,6 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2 h1:Ac1OEHHkbA
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/gofrs/flock v0.8.0 h1:MSdYClljsF3PbENUUEx85nkWfJSGfzYI9yEBZOJz6CY= github.com/gofrs/flock v0.8.0 h1:MSdYClljsF3PbENUUEx85nkWfJSGfzYI9yEBZOJz6CY=
github.com/gofrs/flock v0.8.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/flock v0.8.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hajimehoshi/bitmapfont/v2 v2.1.0 h1:Kit9SsNcnrU5Q/39zni2adUMTEm128/lkNnYxnP+v3A= github.com/hajimehoshi/bitmapfont/v2 v2.1.0 h1:Kit9SsNcnrU5Q/39zni2adUMTEm128/lkNnYxnP+v3A=
github.com/hajimehoshi/bitmapfont/v2 v2.1.0/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs= github.com/hajimehoshi/bitmapfont/v2 v2.1.0/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e h1:4IP7CPObI35+mQShFOYg2JMHDJKciLTW5599inhFfkA= github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e h1:4IP7CPObI35+mQShFOYg2JMHDJKciLTW5599inhFfkA=