2020-07-29 17:19:08 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"log"
|
2021-07-27 10:01:36 +02:00
|
|
|
"math"
|
2023-06-10 12:21:01 +02:00
|
|
|
"strings"
|
2020-07-29 17:19:08 +02:00
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
2020-10-03 16:12:39 +02:00
|
|
|
"golang.org/x/image/font/opentype"
|
2023-06-10 12:21:01 +02:00
|
|
|
"golang.org/x/image/math/fixed"
|
2020-07-29 17:19:08 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
2023-11-11 11:04:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
2022-10-21 08:23:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
2020-07-29 17:19:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
)
|
|
|
|
|
2022-04-03 17:18:35 +02:00
|
|
|
const sampleText = ` The quick brown fox jumps
|
2020-07-29 17:19:08 +02:00
|
|
|
over the lazy dog.`
|
|
|
|
|
|
|
|
var (
|
2023-11-11 11:04:13 +01:00
|
|
|
mplusNormalFace *text.StdFace
|
|
|
|
mplusBigFace *text.StdFace
|
2020-07-29 17:19:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-10-03 16:12:39 +02:00
|
|
|
tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
|
2020-07-29 17:19:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const dpi = 72
|
2023-11-11 11:04:13 +01:00
|
|
|
mplusNormalFont, err := opentype.NewFace(tt, &opentype.FaceOptions{
|
2020-07-29 17:19:08 +02:00
|
|
|
Size: 24,
|
|
|
|
DPI: dpi,
|
2022-11-23 10:13:54 +01:00
|
|
|
Hinting: font.HintingVertical,
|
2020-07-29 17:19:08 +02:00
|
|
|
})
|
2020-10-03 16:12:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-11-11 11:04:13 +01:00
|
|
|
mplusNormalFace = text.NewStdFace(mplusNormalFont)
|
|
|
|
|
|
|
|
mplusBigFont, err := opentype.NewFace(tt, &opentype.FaceOptions{
|
2020-07-29 17:19:08 +02:00
|
|
|
Size: 32,
|
|
|
|
DPI: dpi,
|
2022-11-23 10:13:54 +01:00
|
|
|
Hinting: font.HintingVertical,
|
2020-07-29 17:19:08 +02:00
|
|
|
})
|
2020-10-03 16:12:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-11-11 11:04:13 +01:00
|
|
|
mplusBigFace = text.NewStdFace(mplusBigFont)
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
counter int
|
|
|
|
kanjiText []rune
|
|
|
|
kanjiTextColor color.RGBA
|
2023-11-11 11:04:13 +01:00
|
|
|
glyphs [][]text.Glyph
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2021-11-11 16:54:15 +01:00
|
|
|
// Initialize the glyphs for special (colorful) rendering.
|
|
|
|
if len(g.glyphs) == 0 {
|
2023-11-11 11:04:13 +01:00
|
|
|
for _, line := range strings.Split(sampleText, "\n") {
|
|
|
|
g.glyphs = append(g.glyphs, text.AppendGlyphs(nil, line, mplusNormalFace, 0, 0))
|
2023-06-10 12:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-11 11:04:13 +01:00
|
|
|
return nil
|
2023-06-10 12:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func fixed26_6ToFloat32(x fixed.Int26_6) float32 {
|
|
|
|
return float32(x>>6) + float32(x&((1<<6)-1))/(1<<6)
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
gray := color.RGBA{0x80, 0x80, 0x80, 0xff}
|
|
|
|
|
|
|
|
{
|
|
|
|
const x, y = 20, 40
|
2023-11-11 11:04:13 +01:00
|
|
|
w, h := text.Measure(sampleText, mplusNormalFace, mplusNormalFace.Metrics().Height)
|
|
|
|
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
|
|
|
|
op := &text.DrawOptions{}
|
|
|
|
op.GeoM.Translate(x, y)
|
|
|
|
op.LineHeightInPixels = mplusNormalFace.Metrics().Height
|
|
|
|
text.Draw(screen, sampleText, mplusNormalFace, op)
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const x, y = 20, 140
|
2023-11-11 11:04:13 +01:00
|
|
|
w, h := text.Measure(sampleText, mplusBigFace, mplusBigFace.Metrics().Height)
|
|
|
|
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
|
|
|
|
op := &text.DrawOptions{}
|
|
|
|
op.GeoM.Translate(x, y)
|
|
|
|
op.LineHeightInPixels = mplusBigFace.Metrics().Height
|
|
|
|
text.Draw(screen, sampleText, mplusBigFace, op)
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
2021-07-27 10:01:36 +02:00
|
|
|
{
|
|
|
|
const x, y = 20, 240
|
2023-11-11 11:04:13 +01:00
|
|
|
op := &text.DrawOptions{}
|
2021-07-27 10:01:36 +02:00
|
|
|
op.GeoM.Rotate(math.Pi / 4)
|
|
|
|
op.GeoM.Translate(x, y)
|
|
|
|
op.Filter = ebiten.FilterLinear
|
2023-11-11 11:04:13 +01:00
|
|
|
op.LineHeightInPixels = mplusNormalFace.Metrics().Height
|
|
|
|
text.Draw(screen, sampleText, mplusNormalFace, op)
|
2021-07-27 10:01:36 +02:00
|
|
|
}
|
2021-09-12 15:53:41 +02:00
|
|
|
{
|
|
|
|
const x, y = 160, 240
|
|
|
|
const lineHeight = 80
|
2023-11-11 11:04:13 +01:00
|
|
|
w, h := text.Measure(sampleText, mplusBigFace, lineHeight)
|
|
|
|
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
|
|
|
|
op := &text.DrawOptions{}
|
|
|
|
op.GeoM.Translate(x, y)
|
|
|
|
op.LineHeightInPixels = lineHeight
|
|
|
|
text.Draw(screen, sampleText, mplusBigFace, op)
|
2021-09-12 15:53:41 +02:00
|
|
|
}
|
2021-11-11 16:54:15 +01:00
|
|
|
{
|
|
|
|
const x, y = 240, 400
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
// g.glyphs is initialized by text.AppendGlyphs.
|
|
|
|
// You can customize how to render each glyph.
|
|
|
|
// In this example, multiple colors are used to render glyphs.
|
2023-11-11 11:04:13 +01:00
|
|
|
for j, line := range g.glyphs {
|
|
|
|
for i, gl := range line {
|
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(x, y)
|
|
|
|
op.GeoM.Translate(0, float64(j)*mplusNormalFace.Metrics().Height)
|
|
|
|
op.GeoM.Translate(gl.X, gl.Y)
|
|
|
|
op.ColorScale.Reset()
|
|
|
|
r := float32(1)
|
|
|
|
if i%3 == 0 {
|
|
|
|
r = 0.5
|
|
|
|
}
|
|
|
|
g := float32(1)
|
|
|
|
if i%3 == 1 {
|
|
|
|
g = 0.5
|
|
|
|
}
|
|
|
|
b := float32(1)
|
|
|
|
if i%3 == 2 {
|
|
|
|
b = 0.5
|
|
|
|
}
|
|
|
|
op.ColorScale.Scale(r, g, b, 1)
|
|
|
|
screen.DrawImage(gl.Image, op)
|
2021-11-11 16:54:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Text (Ebitengine Demo)")
|
2020-07-29 17:19:08 +02:00
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|