Do not import a default font

This import may prove unnecessary, adding several megabytes to an application's binary size.
This commit is contained in:
Trevor Slocum 2024-07-21 22:41:09 -07:00
parent 399fae94ad
commit 5a26cb4e80
10 changed files with 75 additions and 42 deletions

View file

@ -3,14 +3,20 @@
package main
import (
"log"
"code.rocket9labs.com/tslocum/etk"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
type game struct {
}
func newGame() *game {
etk.Style.TextFont = defaultFont()
g := &game{}
return g
}
@ -29,5 +35,12 @@ func (g *game) Draw(screen *ebiten.Image) {
if err != nil {
panic(err)
}
}
func defaultFont() *sfnt.Font {
f, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
return f
}

View file

@ -3,14 +3,20 @@
package main
import (
"log"
"code.rocket9labs.com/tslocum/etk"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
type game struct {
}
func newGame() *game {
etk.Style.TextFont = defaultFont()
g := &game{}
return g
}
@ -29,5 +35,12 @@ func (g *game) Draw(screen *ebiten.Image) {
if err != nil {
panic(err)
}
}
func defaultFont() *sfnt.Font {
f, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
return f
}

View file

@ -3,14 +3,20 @@
package main
import (
"log"
"code.rocket9labs.com/tslocum/etk"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
type game struct {
}
func newGame() *game {
etk.Style.TextFont = defaultFont()
g := &game{}
return g
}
@ -30,3 +36,11 @@ func (g *game) Draw(screen *ebiten.Image) {
panic(err)
}
}
func defaultFont() *sfnt.Font {
f, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
return f
}

8
go.mod
View file

@ -3,9 +3,9 @@ module code.rocket9labs.com/tslocum/etk
go 1.18
require (
github.com/hajimehoshi/ebiten/v2 v2.7.5
github.com/llgcode/draw2d v0.0.0-20240322162412-ee6987bd01dc
golang.org/x/image v0.17.0
github.com/hajimehoshi/ebiten/v2 v2.7.7
github.com/llgcode/draw2d v0.0.0-20240627062922-0ed1ff131195
golang.org/x/image v0.18.0
)
require (
@ -15,6 +15,6 @@ require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jezek/xgb v1.1.1 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
)

16
go.sum
View file

@ -7,18 +7,18 @@ github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2
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/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4=
github.com/hajimehoshi/ebiten/v2 v2.7.5 h1:jN6FnhCd9NGYCsm5GtrweuikrlyVGCSUpH5YgL+7UKA=
github.com/hajimehoshi/ebiten/v2 v2.7.5/go.mod h1:H2pHVgq29rfm5yeQ7jzWOM3VHsjo7/AyucODNLOhsVY=
github.com/hajimehoshi/ebiten/v2 v2.7.7 h1:FyiuIOZqKU4aefYVws/lBDhTZu2WY2m/eWI3PtXZaHs=
github.com/hajimehoshi/ebiten/v2 v2.7.7/go.mod h1:Ulbq5xDmdx47P24EJ+Mb31Zps7vQq+guieG9mghQUaA=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/llgcode/draw2d v0.0.0-20240322162412-ee6987bd01dc h1:lorg2FaIDdlahOHekjnQjItP2oCtkVlYc0QNekubCLk=
github.com/llgcode/draw2d v0.0.0-20240322162412-ee6987bd01dc/go.mod h1:muweRyJCZ1mZSMiCgYbAicfnwZFoeHpNr6A6QBu+rBg=
github.com/llgcode/draw2d v0.0.0-20240627062922-0ed1ff131195 h1:Vdz2cBh5Fw2MYHWi3ED2PraDQaWEUhNCr1XFHrP4N5A=
github.com/llgcode/draw2d v0.0.0-20240627062922-0ed1ff131195/go.mod h1:1Vk0LDW6jG5cGc2D9RQUxHaE0vYhTvIwSo9mOL6K4/U=
github.com/llgcode/ps v0.0.0-20210114104736-f4b0c5d1e02e h1:ZAvbj5hI/G/EbAYAcj4yCXUNiFKefEhH0qfImDDD0/8=
golang.org/x/image v0.17.0 h1:nTRVVdajgB8zCMZVsViyzhnMKPwYeroEERRC64JuLco=
golang.org/x/image v0.17.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=

View file

@ -17,7 +17,7 @@ type Keyboard struct {
// NewKeyboard returns a new Keyboard widget.
func NewKeyboard() *Keyboard {
k := kibodo.NewKeyboard()
k := kibodo.NewKeyboard(Style.TextFont)
k.Show()
return &Keyboard{
Box: NewBox(),

View file

@ -4,11 +4,15 @@ package game
import (
"fmt"
"log"
"code.rocket9labs.com/tslocum/etk/kibodo"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
type game struct {
@ -29,7 +33,7 @@ var spinner = []byte(`-\|/`)
// NewDemoGame returns a new kibodo demo game.
func NewDemoGame() *game {
k := kibodo.NewKeyboard()
k := kibodo.NewKeyboard(defaultFont())
k.SetPassThroughPhysicalInput(true)
k.SetKeys(kibodo.KeysQWERTY)
@ -116,3 +120,11 @@ func (g *game) Draw(screen *ebiten.Image) {
g.op.GeoM.Scale(2, 2)
screen.DrawImage(g.buffer, g.op)
}
func defaultFont() *sfnt.Font {
f, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
return f
}

View file

@ -7,11 +7,11 @@ import (
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
// Keyboard is an on-screen keyboard widget.
@ -62,8 +62,8 @@ type Keyboard struct {
}
// NewKeyboard returns a new Keyboard widget.
func NewKeyboard() *Keyboard {
fontFace, err := defaultFontFace(64)
func NewKeyboard(f *sfnt.Font) *Keyboard {
ff, err := fontFace(f, 64)
if err != nil {
log.Fatal(err)
}
@ -80,7 +80,7 @@ func NewKeyboard() *Keyboard {
backgroundColor: color.RGBA{0, 0, 0, 255},
holdTouchID: -1,
hideShortcuts: []ebiten.Key{ebiten.KeyEscape},
labelFont: fontFace,
labelFont: ff,
backspaceDelay: 500 * time.Millisecond,
backspaceRepeat: 75 * time.Millisecond,
}
@ -88,15 +88,7 @@ func NewKeyboard() *Keyboard {
return k
}
func defaultFont() (*opentype.Font, error) {
return opentype.Parse(fonts.MPlus1pRegular_ttf)
}
func defaultFontFace(size float64) (font.Face, error) {
f, err := defaultFont()
if err != nil {
return nil, err
}
func fontFace(f *sfnt.Font, size float64) (font.Face, error) {
const dpi = 72 // TODO
return opentype.NewFace(f, &opentype.FaceOptions{
Size: size,

View file

@ -5,6 +5,7 @@ import (
"testing"
"time"
"code.rocket9labs.com/tslocum/etk"
"github.com/hajimehoshi/ebiten/v2"
)
@ -58,7 +59,7 @@ func BenchmarkKeyboard_Press(b *testing.B) {
}
func newTestKeyboard() *Keyboard {
k := NewKeyboard()
k := NewKeyboard(etk.Style.TextFont)
k.SetRect(0, 0, 300, 100)
return k

View file

@ -2,23 +2,12 @@ package etk
import (
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)
var transparent = color.RGBA{0, 0, 0, 0}
func defaultFont() *sfnt.Font {
f, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
return f
}
// Attributes represents a default attribute configuration. Integer values will be scaled.
type Attributes struct {
TextFont *sfnt.Font
@ -55,7 +44,6 @@ type Attributes struct {
// Style is the current default attribute configuration. Integer values will be scaled.
var Style = &Attributes{
TextFont: defaultFont(),
TextSize: 32,
TextColorLight: color.RGBA{255, 255, 255, 255},