ingenten

package module
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 7 Imported by: 0

README

ingenten

Go Report Card GitHub

Ingenten logo

A dead-simple pixel font library in Go, written for compatibility with Ebitengine.

Features:

  • Simple pixel font format for ASCII characters, relying only on image data.
  • Pluggable layout function for your own animations / effects.
  • Line-wrapping for left-justified text.

Usage

  1. Draw your pixel font, following the infographic below carefully. This infographic is also a test image used by this library.

Example infographic which is also a valid pixel font.

  1. Import in your project:
go get github.com/Frabjous-Studios/ingenten
  1. Parse the font... (you probably want to embed it, right?)
type Game struct {
	font *ingenten.PixelFont
}

g.font, err := ingenten.LoadPixelFont("pixel_font.png", os.DirFS("."))
if err != nil {
	log.Fatal("no font for you today! :(", err)
}
  1. Profit!
func (g *Game) Draw(screen *screen) {
	pos := image.Pt(30, 30)
	text := "Hello world!"
	measured := g.font.Measure(text)
	log.Println("drawing text to rect:", measured.Add(pos))
	
	g.font.PrintTo(screen, pos, text)
}

Developing / Testing Fonts

To test, cd internal and execute go run main.go. The demo scene requires you to type a few things in order to test the font. You can instead modify initial_text.txt in with as much or as little text as you'd like, to cut down on typing.

Planned features:

  • Word-wrapping text so that it fits in a given image.Rectangle.
  • Centered text.
  • Right-justified text.
  • Better kerning logic (take the maximum of left and right glyph instead of adding).

Pull requests are welcome.

Why "Ingenten"?

Because I convinced myself that it's Japanese for 'green bean tempura' and also 'ingredient' and I thought that might be a clever pun. Is it? I don't really know Japanese.

Anyway, Ebitengine was originally called 'Ebiten' which is the romanification of 'shrimp tempura'. 'Ingen' is Japanese for 'haricots' or 'green beans' and '-ten' is a suffix sometimes added to tempura dishes. 'Ingenten' is also 'ingredient', which I think describes the intent for this library pretty well... it provides a small, but important part of the recipe for drawing Pixel Fonts.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PixelFont

type PixelFont struct {

	// Tightness is divided by the width of the lowercase 'm' to define the width of a space, which is
	// always at least one pixel.
	Tightness int
	// contains filtered or unexported fields
}

PixelFont represents a single pixel font.

func LoadPixelFont

func LoadPixelFont(file string, fs fs.FS) (*PixelFont, error)

LoadPixelFont loads and parses a PixelFont from the provided fs.FS. This func uses image.Decode, callers will need to include the correct anonymous import from the image package in order to use this func properly. For example:

import _ "image/png"

var pf = LoadPixelFont("my_font.png", os.DirFS("."))

The image loaded contains all of the information needed to parse and use the font. An example font is included in the README for this package.

func (*PixelFont) DoLayout

func (pf *PixelFont) DoLayout(text string, do func(pos image.Point, img *ebiten.Image))

DoLayout calls do for each rune in the provided text, passing the relative position of the letter from the origin, and a subimage containing the glyph to be rendered.

This func is intended to be used to create your own text effects or animations without worrying about layout.

func (*PixelFont) DoLayoutRect added in v0.2.0

func (pf *PixelFont) DoLayoutRect(text string, rect image.Rectangle, do func(pos image.Point, img *ebiten.Image))

DoLayoutRect lays out the provided text in the provided rectangle, left-justified, with word wrapping. In case of long words that exceed the bounds of rect, the word-wrapping provided by this function extends the boundaries of rect, rather than breaking the word onto separate lines.

func (*PixelFont) Measure

func (pf *PixelFont) Measure(text string, origin image.Point) image.Rectangle

Measure measures the rectangle the text would be rendered in if it were to be written to screen using Print.

func (*PixelFont) MeasureRect added in v0.2.0

func (pf *PixelFont) MeasureRect(text string, rect image.Rectangle) image.Rectangle

MeasureRect measures the rectangle the text would be rendered in if it were to be written to screen using PrintRect

func (*PixelFont) Print added in v0.2.0

func (pf *PixelFont) Print(screen *ebiten.Image, origin image.Point, text string)

Print lays out the runes for the provided text, left-justified, with the top-left corner of the text at the provided origin. No automatic wrapping is included.

func (*PixelFont) PrintOpts added in v0.3.0

func (pf *PixelFont) PrintOpts(screen *ebiten.Image, origin image.Point, text string, opts *ebiten.DrawImageOptions)

PrintOpts works like Print, using options from the provided opts. GeoM is reset prior to drawing.

func (*PixelFont) PrintRect added in v0.2.0

func (pf *PixelFont) PrintRect(screen *ebiten.Image, rect image.Rectangle, text string)

PrintRect prints the provided text, left-justified, with the top-left corner of the text at the provided origin. Text is line-wrapped so that all the characters lie within the provided rectangle. No word-wrapping is attempted.

func (*PixelFont) PrintRectOpts added in v0.3.0

func (pf *PixelFont) PrintRectOpts(screen *ebiten.Image, rect image.Rectangle, text string, opts *ebiten.DrawImageOptions)

PrintRectOpts works like PrintRect

func (*PixelFont) String

func (pf *PixelFont) String() string

String prints the letters associated with each string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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