mirror of
https://github.com/bloeys/nterm.git
synced 2025-12-29 14:38:19 +00:00
Get all glyphs from font
This commit is contained in:
10
go.mod
10
go.mod
@ -2,14 +2,16 @@ module github.com/bloeys/nterm
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/bloeys/nmage v0.11.12
|
||||
require (
|
||||
github.com/bloeys/nmage v0.11.12
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
||||
github.com/veandco/go-sdl2 v0.4.10
|
||||
golang.org/x/image v0.0.0-20220617043117-41969df76e82
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bloeys/assimp-go v0.4.2 // indirect
|
||||
github.com/bloeys/gglm v0.3.1 // indirect
|
||||
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/inkyblackness/imgui-go/v4 v4.3.0 // indirect
|
||||
github.com/veandco/go-sdl2 v0.4.10 // indirect
|
||||
golang.org/x/image v0.0.0-20220617043117-41969df76e82 // indirect
|
||||
)
|
||||
|
||||
120
main.go
120
main.go
@ -2,15 +2,40 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"os"
|
||||
"unicode"
|
||||
|
||||
"github.com/bloeys/nmage/engine"
|
||||
"github.com/bloeys/nmage/input"
|
||||
"github.com/bloeys/nmage/renderer/rend3dgl"
|
||||
nmageimgui "github.com/bloeys/nmage/ui/imgui"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
win, err := engine.CreateOpenGLWindowCentered("nTerm", 1280, 720, engine.WindowFlags_ALLOW_HIGHDPI|engine.WindowFlags_RESIZABLE, rend3dgl.NewRend3DGL())
|
||||
if err != nil {
|
||||
panic("Failed to create window. Err: " + err.Error())
|
||||
}
|
||||
|
||||
engine.SetVSync(true)
|
||||
|
||||
p := &program{
|
||||
shouldRun: true,
|
||||
win: win,
|
||||
imguiInfo: nmageimgui.NewImGUI(),
|
||||
}
|
||||
|
||||
engine.Run(p)
|
||||
}
|
||||
|
||||
var _ engine.Game = &program{}
|
||||
|
||||
type program struct {
|
||||
@ -31,8 +56,39 @@ func (p *program) Init() {
|
||||
panic("Failed to parse font. Err: " + err.Error())
|
||||
}
|
||||
|
||||
face := truetype.NewFace(f, &truetype.Options{Size: 12, DPI: 72})
|
||||
fmt.Println(face.Metrics())
|
||||
size := 40
|
||||
face := truetype.NewFace(f, &truetype.Options{Size: float64(size), DPI: 72})
|
||||
imgFromText("Hello there my friend", size, face, "./text.png")
|
||||
fmt.Println(string(getGlyphs(loadFontRanges(f))))
|
||||
}
|
||||
|
||||
func imgFromText(text string, textSize int, face font.Face, file string) {
|
||||
|
||||
//Create a white image
|
||||
rgbaDest := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
||||
draw.Draw(rgbaDest, rgbaDest.Bounds(), image.White, image.Point{}, draw.Src)
|
||||
|
||||
//Draw black text on image
|
||||
drawer := &font.Drawer{
|
||||
Dst: rgbaDest,
|
||||
Src: image.Black,
|
||||
Face: face,
|
||||
}
|
||||
|
||||
drawer.Dot = fixed.P(0, textSize)
|
||||
drawer.DrawString(text)
|
||||
|
||||
// Save that RGBA image to disk.
|
||||
outFile, err := os.Create(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
err = png.Encode(outFile, rgbaDest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *program) Start() {
|
||||
@ -45,7 +101,7 @@ func (p *program) FrameStart() {
|
||||
|
||||
func (p *program) Update() {
|
||||
|
||||
if input.IsQuitClicked() {
|
||||
if input.IsQuitClicked() || input.KeyClicked(sdl.K_ESCAPE) {
|
||||
p.shouldRun = false
|
||||
}
|
||||
}
|
||||
@ -74,20 +130,50 @@ func (p *program) Deinit() {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
win, err := engine.CreateOpenGLWindowCentered("nTerm", 1280, 720, engine.WindowFlags_ALLOW_HIGHDPI|engine.WindowFlags_RESIZABLE, rend3dgl.NewRend3DGL())
|
||||
if err != nil {
|
||||
panic("Failed to create window. Err: " + err.Error())
|
||||
//loadFontRanges returns a list of ranges, each range is: [i][0]<=range<[i][1]
|
||||
func loadFontRanges(f *truetype.Font) (ret [][2]rune) {
|
||||
rr := [2]rune{-1, -1}
|
||||
for r := rune(0); r <= unicode.MaxRune; r++ {
|
||||
if privateUseArea(r) {
|
||||
continue
|
||||
}
|
||||
if f.Index(r) == 0 {
|
||||
continue
|
||||
}
|
||||
if rr[1] == r {
|
||||
rr[1] = r + 1
|
||||
continue
|
||||
}
|
||||
if rr[0] != -1 {
|
||||
ret = append(ret, rr)
|
||||
}
|
||||
rr = [2]rune{r, r + 1}
|
||||
}
|
||||
|
||||
engine.SetVSync(true)
|
||||
|
||||
p := &program{
|
||||
shouldRun: true,
|
||||
win: win,
|
||||
imguiInfo: nmageimgui.NewImGUI(),
|
||||
if rr[0] != -1 {
|
||||
ret = append(ret, rr)
|
||||
}
|
||||
|
||||
engine.Run(p)
|
||||
return ret
|
||||
}
|
||||
|
||||
func privateUseArea(r rune) bool {
|
||||
return 0xe000 <= r && r <= 0xf8ff ||
|
||||
0xf0000 <= r && r <= 0xffffd ||
|
||||
0x100000 <= r && r <= 0x10fffd
|
||||
}
|
||||
|
||||
//getGlyphs takes ranges of runes and produces an array of all the runes in these ranges
|
||||
func getGlyphs(ranges [][2]rune) []rune {
|
||||
|
||||
out := make([]rune, 0)
|
||||
for _, rr := range ranges {
|
||||
|
||||
temp := make([]rune, 0, rr[1]-rr[0])
|
||||
for r := rr[0]; r < rr[1]; r++ {
|
||||
temp = append(temp, r)
|
||||
}
|
||||
|
||||
out = append(out, temp...)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user