fontpic

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

fontpic

Goal of the project - allow to easily generate images with text using widely available CGA/EGA/VGA ROM fonts. This can be useful for generating captcha images for website or telegram chats.

It comes with 8x8, 8x14 and 8x16 fonts in DOS 866 (Cyrillic) encoding in fnt directory. Font files 08x08.fnt, 08x14.fnt, 08x16.fnt were distributed with the freeware KeyRus program, and are used in this library by default as a tribute to Dmitry Gurtyak (1971-1998), author of KeyRus.

Currenly the project supports only raw font files, it's easy to tell if it's a raw FNT by looking at the file size:

  • 8x8 - 2048 bytes
  • 8x14 - 3584 bytes
  • 8x16 - 4096 bytes

Where to get more fonts

  1. There is a great project that contains a lot of fonts extracted from different ROMs: romfont
  2. Extract fonts from a BIOS of the old PC. Read the romfont repository README.
  3. Unpack fonts from the Abandonware programs. I.e. DOS distribution includes '*.CPI' files that contain fonts. You can use psf2inc utility to extract them. If you went down that path, you'd probably know what to do with the output.
  4. Convert BDF fonts.

Licensing

BSD 3-clause. See LICENSE.

Included fonts are freeware (c) Dmitry Gurtyak.

  • fnt/microfnt.fnt - GPL2+ font by mibi88 (generated from source)

Documentation

Overview

Package fontpic is a package for rendering fonts to images.

Index

Constants

View Source
const (
	DefaultWidth  = 720
	DefaultHeight = 540
)
View Source
const (
	// CharsetSz is the number of characters in a font file.  It is unlikely to
	// ever change.
	CharsetSz = 256
)

Variables

View Source
var (
	// FontDefault is the default font.
	Font8x8     = Must(ToFontCharset(kr8x8, "866"))
	Font8x14    = Must(ToFontCharset(kr8x14, "866"))
	Font8x16    = Must(ToFontCharset(kr8x16, "866"))
	FontDefault = Font8x16
)

Functions

func RenderCharAt

func RenderCharAt(img draw.Image, at image.Point, width, height int, bits []byte, hi color.Color, lo color.Color)

RenderCharAt is a low level function that renders a character, defined in bits, at the given position on the image. It uses width and height to know how to render the character in bits.

Types

type Canvas

type Canvas struct {
	Width      int
	Height     int
	Background color.Color
	Foreground color.Color // Color to use for the font
	Font       *Font       // Font to use
	Spacing    image.Point // Spacing between characters.
	Scale      image.Point // scaling factor (not used yet)
	// contains filtered or unexported fields
}

Canvas is a canvas that can be rendered to.

The member function naming convention is the following:

  • Render* - renders text or lines of text to the canvas
  • With* - sets a property

Render functions, that have Text in the name do the minimum amount of transformations to the text to make it renderable. For example, tabs are replaced with spaces, and newlines are treated as line separators.

Render functions without Text in the name, render the provided lines of text verbatim, so any \n or \t characters will be rendered as a characters from the supplied font.

Render functions, that have At in the name, render the text at the specified location.

Zero canvas value is usable. It will use the default font, and will render the text in Grey (0xa8) on Black background, just like the good old days.

func NewCanvas

func NewCanvas(defavlt *Font) *Canvas

NewCanvas creates the new canvas with the default font.

func (*Canvas) CalcSize

func (c *Canvas) CalcSize(lines [][]byte) *Canvas

CalcSize calculates the size of the canvas based on the provided lines of text.

func (*Canvas) Image

func (c *Canvas) Image() draw.Image

func (*Canvas) Render

func (c *Canvas) Render(lines [][]byte) *Canvas

Render renders the lines of text to the canvas.

func (*Canvas) RenderAt

func (c *Canvas) RenderAt(lines [][]byte, at image.Point) *Canvas

RenderAt renders the lines of text at the specified location.

func (*Canvas) RenderText

func (c *Canvas) RenderText(text []byte) *Canvas

RenderText renders the text to the canvas.

func (*Canvas) RenderTextAt

func (c *Canvas) RenderTextAt(text []byte, at image.Point) *Canvas

RenderTextAt renders the text at the specified location.

func (*Canvas) WithBackground

func (c *Canvas) WithBackground(bg color.Color) *Canvas

func (*Canvas) WithFont

func (c *Canvas) WithFont(font *Font) *Canvas

func (*Canvas) WithForeground

func (c *Canvas) WithForeground(fg color.Color) *Canvas

func (*Canvas) WithImage

func (c *Canvas) WithImage(img draw.Image) *Canvas

func (*Canvas) WithSize

func (c *Canvas) WithSize(w, h int) *Canvas

func (*Canvas) WithSpacing

func (c *Canvas) WithSpacing(x, y int) *Canvas

type Font

type Font struct {
	Width   int
	Height  int
	Charset string
	Chars   [CharsetSz][]byte
}

func LoadFont added in v0.0.3

func LoadFont(filename string, width int) (*Font, error)

func Must

func Must(font *Font, err error) *Font

func ToFont

func ToFont(b []byte, width int) (*Font, error)

ToFont8 converts byte data to a Font structure. It detects the font height based on the slice size.

func ToFont8

func ToFont8(b []byte) (*Font, error)

ToFont8 is a shortcut for calling ToFont(b, 8).

Usual slice sizes for 8-bit wide fonts:

  1. 8x8 font: 2048 bytes (1x8bytes x 256)
  2. 8x14 font: 3584 bytes (1x14bytes x 256)
  3. 8x16 font: 4096 bytes (1x16bytes x 256)

func ToFontCharset

func ToFontCharset(b []byte, charset string) (*Font, error)

func (*Font) Bytes

func (f *Font) Bytes() []byte

func (*Font) Sample

func (f *Font) Sample(perLine int) image.Image

Sample renders a sample of the font. The font is rendered in a grid of perLine characters. The sx and sy parameters are space between characters in pixels

func (*Font) SampleColor

func (f *Font) SampleColor(perLine int, fg, bg color.Color) image.Image

func (*Font) TextAt

func (f *Font) TextAt(img draw.Image, x, y int, text []byte, fg, bg color.Color)

func (*Font) WriteTo

func (f *Font) WriteTo(w io.Writer) (n int64, err error)

Directories

Path Synopsis
cmd
fntgen
Command fntgen is just a sandbox to fuck around.
Command fntgen is just a sandbox to fuck around.

Jump to

Keyboard shortcuts

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