2023-06-24 18:56:47 +02:00
// Copyright 2023 The Ebitengine 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 (
"bytes"
_ "embed"
2023-11-23 15:13:27 +01:00
"image/color"
2023-06-24 18:56:47 +02:00
"log"
2023-11-12 09:47:31 +01:00
"golang.org/x/text/language"
2023-06-24 18:56:47 +02:00
"github.com/hajimehoshi/ebiten/v2"
2024-08-22 16:36:20 +02:00
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
2023-06-24 18:56:47 +02:00
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
2024-08-22 16:36:20 +02:00
"github.com/hajimehoshi/ebiten/v2/inpututil"
2023-11-12 09:47:31 +01:00
"github.com/hajimehoshi/ebiten/v2/text/v2"
2023-11-23 15:13:27 +01:00
"github.com/hajimehoshi/ebiten/v2/vector"
2023-06-24 18:56:47 +02:00
)
//go:embed NotoSansArabic-Regular.ttf
var arabicTTF [ ] byte
2023-11-12 09:47:31 +01:00
var arabicFaceSource * text . GoTextFaceSource
2023-06-24 18:56:47 +02:00
func init ( ) {
2023-11-12 09:47:31 +01:00
s , err := text . NewGoTextFaceSource ( bytes . NewReader ( arabicTTF ) )
2023-06-24 18:56:47 +02:00
if err != nil {
log . Fatal ( err )
}
2023-11-12 09:47:31 +01:00
arabicFaceSource = s
2023-06-24 18:56:47 +02:00
}
//go:embed NotoSansDevanagari-Regular.ttf
var devanagariTTF [ ] byte
2023-11-12 09:47:31 +01:00
var devanagariFaceSource * text . GoTextFaceSource
2023-06-24 18:56:47 +02:00
func init ( ) {
2023-11-12 09:47:31 +01:00
s , err := text . NewGoTextFaceSource ( bytes . NewReader ( devanagariTTF ) )
2023-06-24 18:56:47 +02:00
if err != nil {
log . Fatal ( err )
}
2023-11-12 09:47:31 +01:00
devanagariFaceSource = s
2023-06-24 18:56:47 +02:00
}
//go:embed NotoSansThai-Regular.ttf
var thaiTTF [ ] byte
2023-11-12 09:47:31 +01:00
var thaiFaceSource * text . GoTextFaceSource
2023-06-24 18:56:47 +02:00
func init ( ) {
2023-11-12 09:47:31 +01:00
s , err := text . NewGoTextFaceSource ( bytes . NewReader ( thaiTTF ) )
2023-06-24 18:56:47 +02:00
if err != nil {
log . Fatal ( err )
}
2023-11-12 09:47:31 +01:00
thaiFaceSource = s
2023-06-24 18:56:47 +02:00
}
2023-12-20 04:29:34 +01:00
//go:embed NotoSansMyanmar-Regular.ttf
var myanmarTTF [ ] byte
var myanmarFaceSource * text . GoTextFaceSource
func init ( ) {
s , err := text . NewGoTextFaceSource ( bytes . NewReader ( myanmarTTF ) )
if err != nil {
log . Fatal ( err )
}
myanmarFaceSource = s
}
2023-12-09 09:30:08 +01:00
//go:embed NotoSansMongolian-Regular.ttf
var mongolianTTF [ ] byte
var mongolianFaceSource * text . GoTextFaceSource
func init ( ) {
s , err := text . NewGoTextFaceSource ( bytes . NewReader ( mongolianTTF ) )
if err != nil {
log . Fatal ( err )
}
mongolianFaceSource = s
}
2023-11-12 09:47:31 +01:00
var japaneseFaceSource * text . GoTextFaceSource
2023-06-24 18:56:47 +02:00
func init ( ) {
2023-11-12 09:47:31 +01:00
s , err := text . NewGoTextFaceSource ( bytes . NewReader ( fonts . MPlus1pRegular_ttf ) )
2023-06-24 18:56:47 +02:00
if err != nil {
log . Fatal ( err )
}
2023-11-12 09:47:31 +01:00
japaneseFaceSource = s
2023-06-24 18:56:47 +02:00
}
const (
screenWidth = 640
2023-12-20 04:29:34 +01:00
screenHeight = 640
2023-06-24 18:56:47 +02:00
)
type Game struct {
2024-08-22 16:36:20 +02:00
showOrigins bool
2023-06-24 18:56:47 +02:00
}
func ( g * Game ) Update ( ) error {
2024-08-22 16:36:20 +02:00
if inpututil . IsKeyJustPressed ( ebiten . KeyO ) {
g . showOrigins = ! g . showOrigins
}
2023-06-24 18:56:47 +02:00
return nil
}
func ( g * Game ) Draw ( screen * ebiten . Image ) {
2024-08-22 20:54:11 +02:00
ebitenutil . DebugPrint ( screen , "Press O to show/hide origins.\nRed points are the original origin positions.\nThe green points are the origin positions after applying the offset." )
2024-08-22 16:36:20 +02:00
2023-11-23 15:13:27 +01:00
gray := color . RGBA { 0x80 , 0x80 , 0x80 , 0xff }
{
const arabicText = "لمّا كان الاعتراف بالكرامة المتأصلة في جميع"
f := & text . GoTextFace {
Source : arabicFaceSource ,
Direction : text . DirectionRightToLeft ,
Size : 24 ,
Language : language . Arabic ,
}
2024-08-22 20:54:11 +02:00
x , y := screenWidth - 20 , 50
2023-11-23 15:13:27 +01:00
w , h := text . Measure ( arabicText , f , 0 )
// The left upper point is not x but x-w, since the text runs in the rigth-to-left direction.
vector . DrawFilledRect ( screen , float32 ( x ) - float32 ( w ) , float32 ( y ) , float32 ( w ) , float32 ( h ) , gray , false )
op := & text . DrawOptions { }
op . GeoM . Translate ( float64 ( x ) , float64 ( y ) )
text . Draw ( screen , arabicText , f , op )
2024-08-22 16:36:20 +02:00
if g . showOrigins {
op := & text . LayoutOptions { }
for _ , g := range text . AppendGlyphs ( nil , arabicText , f , op ) {
2024-08-22 20:54:11 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX + g . OriginOffsetX ) , float32 ( y ) + float32 ( g . OriginY + g . OriginOffsetY ) , 2 , color . RGBA { 0 , 0xff , 0 , 0xff } , true )
2024-08-22 16:36:20 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX ) , float32 ( y ) + float32 ( g . OriginY ) , 2 , color . RGBA { 0xff , 0 , 0 , 0xff } , true )
}
}
2023-11-23 15:13:27 +01:00
}
{
const hindiText = "चूंकि मानव परिवार के सभी सदस्यों के जन्मजात गौरव और समान"
f := & text . GoTextFace {
Source : devanagariFaceSource ,
Size : 24 ,
Language : language . Hindi ,
}
2024-08-22 20:54:11 +02:00
x , y := 20 , 110
2023-11-23 15:13:27 +01:00
w , h := text . Measure ( hindiText , f , 0 )
vector . DrawFilledRect ( screen , float32 ( x ) , float32 ( y ) , float32 ( w ) , float32 ( h ) , gray , false )
op := & text . DrawOptions { }
op . GeoM . Translate ( float64 ( x ) , float64 ( y ) )
text . Draw ( screen , hindiText , f , op )
2024-08-22 16:36:20 +02:00
if g . showOrigins {
op := & text . LayoutOptions { }
for _ , g := range text . AppendGlyphs ( nil , hindiText , f , op ) {
2024-08-22 20:54:11 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX + g . OriginOffsetX ) , float32 ( y ) + float32 ( g . OriginY + g . OriginOffsetY ) , 2 , color . RGBA { 0 , 0xff , 0 , 0xff } , true )
2024-08-22 16:36:20 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX ) , float32 ( y ) + float32 ( g . OriginY ) , 2 , color . RGBA { 0xff , 0 , 0 , 0xff } , true )
}
}
2023-11-23 15:13:27 +01:00
}
{
2023-12-20 05:38:07 +01:00
const myanmarText = "လူခပ်သိမ်း၏ မျိုးရိုးဂုဏ်သိက္ခာနှင့်တကွ"
2023-11-23 15:13:27 +01:00
f := & text . GoTextFace {
2023-12-20 05:38:07 +01:00
Source : myanmarFaceSource ,
2023-11-23 15:13:27 +01:00
Size : 24 ,
2023-12-20 05:38:07 +01:00
Language : language . Burmese ,
2023-11-23 15:13:27 +01:00
}
2024-08-22 20:54:11 +02:00
x , y := 20 , 170
2023-12-20 05:38:07 +01:00
w , h := text . Measure ( myanmarText , f , 0 )
2023-11-23 15:13:27 +01:00
vector . DrawFilledRect ( screen , float32 ( x ) , float32 ( y ) , float32 ( w ) , float32 ( h ) , gray , false )
op := & text . DrawOptions { }
op . GeoM . Translate ( float64 ( x ) , float64 ( y ) )
2023-12-20 05:38:07 +01:00
text . Draw ( screen , myanmarText , f , op )
2024-08-22 16:36:20 +02:00
if g . showOrigins {
op := & text . LayoutOptions { }
for _ , g := range text . AppendGlyphs ( nil , myanmarText , f , op ) {
2024-08-22 20:54:11 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX + g . OriginOffsetX ) , float32 ( y ) + float32 ( g . OriginY + g . OriginOffsetY ) , 2 , color . RGBA { 0 , 0xff , 0 , 0xff } , true )
2024-08-22 16:36:20 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX ) , float32 ( y ) + float32 ( g . OriginY ) , 2 , color . RGBA { 0xff , 0 , 0 , 0xff } , true )
}
}
2023-11-23 15:13:27 +01:00
}
2023-12-20 04:29:34 +01:00
{
2023-12-20 05:38:07 +01:00
const thaiText = "โดยที่การยอมรับนับถือเกียรติศักดิ์ประจำตัว"
2023-12-20 04:29:34 +01:00
f := & text . GoTextFace {
2023-12-20 05:38:07 +01:00
Source : thaiFaceSource ,
2023-12-20 04:29:34 +01:00
Size : 24 ,
2023-12-20 05:38:07 +01:00
Language : language . Thai ,
2023-12-20 04:29:34 +01:00
}
2024-08-22 20:54:11 +02:00
x , y := 20 , 230
2023-12-20 05:38:07 +01:00
w , h := text . Measure ( thaiText , f , 0 )
2023-12-20 04:29:34 +01:00
vector . DrawFilledRect ( screen , float32 ( x ) , float32 ( y ) , float32 ( w ) , float32 ( h ) , gray , false )
op := & text . DrawOptions { }
op . GeoM . Translate ( float64 ( x ) , float64 ( y ) )
2023-12-20 05:38:07 +01:00
text . Draw ( screen , thaiText , f , op )
2024-08-22 16:36:20 +02:00
if g . showOrigins {
op := & text . LayoutOptions { }
for _ , g := range text . AppendGlyphs ( nil , thaiText , f , op ) {
2024-08-22 20:54:11 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX + g . OriginOffsetX ) , float32 ( y ) + float32 ( g . OriginY + g . OriginOffsetY ) , 2 , color . RGBA { 0 , 0xff , 0 , 0xff } , true )
2024-08-22 16:36:20 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX ) , float32 ( y ) + float32 ( g . OriginY ) , 2 , color . RGBA { 0xff , 0 , 0 , 0xff } , true )
}
}
2023-12-20 04:29:34 +01:00
}
2023-11-23 15:13:27 +01:00
{
2023-12-09 09:30:08 +01:00
const mongolianText = "ᠬᠦᠮᠦᠨ ᠪᠦᠷ ᠲᠥᠷᠥᠵᠦ ᠮᠡᠨᠳᠡᠯᠡᠬᠦ\nᠡᠷᠬᠡ ᠴᠢᠯᠥᠭᠡ ᠲᠡᠢ᠂ ᠠᠳᠠᠯᠢᠬᠠᠨ"
f := & text . GoTextFace {
Source : mongolianFaceSource ,
Direction : text . DirectionTopToBottomAndLeftToRight ,
Size : 24 ,
Language : language . Mongolian ,
// language.Mongolian.Script() returns "Cyrl" (Cyrillic), but we want Mongolian script here.
Script : language . MustParseScript ( "Mong" ) ,
}
const lineSpacing = 48
2024-08-22 20:54:11 +02:00
x , y := 20 , 290
2023-12-09 09:30:08 +01:00
w , h := text . Measure ( mongolianText , f , lineSpacing )
vector . DrawFilledRect ( screen , float32 ( x ) , float32 ( y ) , float32 ( w ) , float32 ( h ) , gray , false )
op := & text . DrawOptions { }
op . GeoM . Translate ( float64 ( x ) , float64 ( y ) )
2023-12-24 07:06:16 +01:00
op . LineSpacing = lineSpacing
2023-12-09 09:30:08 +01:00
text . Draw ( screen , mongolianText , f , op )
2024-08-22 16:36:20 +02:00
if g . showOrigins {
op := & text . LayoutOptions { }
op . LineSpacing = lineSpacing
for _ , g := range text . AppendGlyphs ( nil , mongolianText , f , op ) {
2024-08-22 20:54:11 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX + g . OriginOffsetX ) , float32 ( y ) + float32 ( g . OriginY + g . OriginOffsetY ) , 2 , color . RGBA { 0 , 0xff , 0 , 0xff } , true )
2024-08-22 16:36:20 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX ) , float32 ( y ) + float32 ( g . OriginY ) , 2 , color . RGBA { 0xff , 0 , 0 , 0xff } , true )
}
}
2023-12-09 09:30:08 +01:00
}
{
const japaneseText = "あのイーハトーヴォの\nすきとおった風、\n夏でも底に冷たさを\nもつ青いそら…\nあHello World.あ"
2023-11-23 15:13:27 +01:00
f := & text . GoTextFace {
Source : japaneseFaceSource ,
Direction : text . DirectionTopToBottomAndRightToLeft ,
Size : 24 ,
Language : language . Japanese ,
}
const lineSpacing = 48
2024-08-22 20:54:11 +02:00
x , y := screenWidth - 20 , 290
2023-11-23 15:13:27 +01:00
w , h := text . Measure ( japaneseText , f , lineSpacing )
// The left upper point is not x but x-w, since the text runs in the rigth-to-left direction as the secondary direction.
vector . DrawFilledRect ( screen , float32 ( x ) - float32 ( w ) , float32 ( y ) , float32 ( w ) , float32 ( h ) , gray , false )
op := & text . DrawOptions { }
op . GeoM . Translate ( float64 ( x ) , float64 ( y ) )
2023-12-24 07:06:16 +01:00
op . LineSpacing = lineSpacing
2023-11-23 15:13:27 +01:00
text . Draw ( screen , japaneseText , f , op )
2024-08-22 16:36:20 +02:00
if g . showOrigins {
op := & text . LayoutOptions { }
op . LineSpacing = lineSpacing
for _ , g := range text . AppendGlyphs ( nil , japaneseText , f , op ) {
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX ) , float32 ( y ) + float32 ( g . OriginY ) , 2 , color . RGBA { 0xff , 0 , 0 , 0xff } , true )
2024-08-22 20:54:11 +02:00
vector . DrawFilledCircle ( screen , float32 ( x ) + float32 ( g . OriginX + g . OriginOffsetX ) , float32 ( y ) + float32 ( g . OriginY + g . OriginOffsetY ) , 2 , color . RGBA { 0 , 0xff , 0 , 0xff } , true )
2024-08-22 16:36:20 +02:00
}
}
2023-11-23 15:13:27 +01:00
}
2023-06-24 18:56:47 +02:00
}
func ( g * Game ) Layout ( outsideWidth , outsideHeight int ) ( int , int ) {
return screenWidth , screenHeight
}
func main ( ) {
ebiten . SetWindowSize ( screenWidth , screenHeight )
ebiten . SetWindowTitle ( "Text I18N (Ebitengine Demo)" )
if err := ebiten . RunGame ( & Game { } ) ; err != nil {
log . Fatal ( err )
}
}