fopix

package module
v0.0.0-...-ed880dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2021 License: GPL-3.0 Imports: 8 Imported by: 2

README

fopix

Simple monospaced pixel font package for golang

Partly idea taken from: pixfont

Installation

go get github.com/toelsiba/fopix

Fonts

Font files are available in the directory fonts. Fonts is saved in JSON format.

Digits 3x3

digits-3x3

Digits 3x4

digits-3x4

Digits 3x5

digits-3x5

3x3 Font for Nerds

font-3x3-ascii

font-3x3-multiline

Victor

victor-ascii

victor-multiline

Miniwi

miniwi-ascii

miniwi-multiline

Tom Thumb

tom-thumb-ascii

tom-thumb-multiline

Tom Thumb New

tom-thumb-new-ascii

tom-thumb-new-multiline

Pixefon

pixefon-ascii

pixefon-multiline

VGA CP437

cp437-table

cp437-text


Examples

using existing font
package main

import (
	"image"
	"log"

	"github.com/toelsiba/fopix"
	"github.com/toelsiba/fopix/imutil"
)

func checkError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {

	filename := "../../fonts/tom-thumb-new.json"

	var fi fopix.FontInfo
	err := fopix.ReadFileJSON(filename, &fi)
	checkError(err)

	d, err := fopix.NewDrawer(fi)
	checkError(err)

	d.SetScale(5)

	text := "Hello, World!"

	m := image.NewRGBA(d.TextBounds(text))

	d.DrawText(m, image.ZP, text)

	err = imutil.ImageSaveToPNG("hello-world.png", m)
	checkError(err)
}
Result image

hello-world

using custom font
package main

import (
	"image"
	"image/color"
	"log"

	"github.com/toelsiba/fopix"
	"github.com/toelsiba/fopix/imutil"
)

// custom font
var gopherFont = fopix.FontInfo{
	Name:        "Go font",
	Author:      "Gopher",
	Description: "something ...",
	Size:        fopix.Point{X: 6, Y: 7},
	AnchorPos:   fopix.Point{X: 0, Y: 0},
	TargetChar:  '0',
	CharSet: []fopix.RuneInfo{
		fopix.RuneInfo{
			Character: 'G',
			Bitmap: []string{
				"-000-",
				"0---0",
				"0----",
				"0-000",
				"0---0",
				"-000-",
			},
		},
		fopix.RuneInfo{
			Character: 'o',
			Bitmap: []string{
				"-----",
				"-----",
				"-000-",
				"0---0",
				"0---0",
				"-000-",
			},
		},
	},
}

func checkError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {

	d, err := fopix.NewDrawer(gopherFont)
	checkError(err)

	d.SetScale(10)
	d.SetColor(color.RGBA{0, 0, 0xFF, 0xFF})

	text := "Go"

	m := image.NewRGBA(d.TextBounds(text))

	imutil.ImageSolidFill(m, color.White)

	d.DrawText(m, image.ZP, text)

	err = imutil.ImageSaveToPNG("go-font.png", m)
	checkError(err)
}
Result image

go-font

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadFileJSON

func ReadFileJSON(filename string, v interface{}) error

func WriteFileJSON

func WriteFileJSON(filename string, v interface{}) error

Types

type ByCharacter

type ByCharacter []RuneInfo

func (ByCharacter) Len

func (s ByCharacter) Len() int

func (ByCharacter) Less

func (s ByCharacter) Less(i, j int) bool

func (ByCharacter) Swap

func (s ByCharacter) Swap(i, j int)

type Character

type Character rune

func (Character) MarshalJSON

func (c Character) MarshalJSON() ([]byte, error)

func (*Character) UnmarshalJSON

func (c *Character) UnmarshalJSON(data []byte) error

type ColorSetter

type ColorSetter interface {
	Set(x, y int, c color.Color)
}

type Drawer

type Drawer struct {
	// contains filtered or unexported fields
}

func NewDrawer

func NewDrawer(fi FontInfo) (*Drawer, error)

func (*Drawer) Color

func (d *Drawer) Color() color.Color

func (*Drawer) DrawRune

func (d *Drawer) DrawRune(cs ColorSetter, pos image.Point, r rune)

func (*Drawer) DrawText

func (d *Drawer) DrawText(cs ColorSetter, pos image.Point, text string)

func (*Drawer) RuneBounds

func (d *Drawer) RuneBounds() image.Rectangle

func (*Drawer) Scale

func (d *Drawer) Scale() int

func (*Drawer) SetColor

func (d *Drawer) SetColor(c color.Color)

func (*Drawer) SetScale

func (d *Drawer) SetScale(scale int)

func (*Drawer) TextBounds

func (d *Drawer) TextBounds(text string) image.Rectangle

type FontInfo

type FontInfo struct {
	Name        string     `json:"name"`
	Author      string     `json:"author"`
	Description string     `json:"description"`
	Size        Point      `json:"size"`
	AnchorPos   Point      `json:"anchor-pos"`
	TargetChar  Character  `json:"target-char"`
	CharSet     []RuneInfo `json:"char-set"`
}

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

type RuneInfo

type RuneInfo struct {
	Character Character `json:"character"`
	Bitmap    []string  `json:"bitmap"`
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL