plan9font

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package plan9font implements font faces for the Plan 9 font and subfont file formats. These formats are described at https://9p.io/magic/man2html/6/font

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseFont

func ParseFont(data []byte, readFile func(relFilename string) ([]byte, error)) (font.Face, error)

ParseFont parses a Plan 9 font file. data is the contents of that font file, which gives relative filenames for subfont files. readFile returns the contents of those subfont files. It is similar to io/ioutil's ReadFile function, except that it takes a relative filename instead of an absolute one.

Example
package main

import (
	"image"
	"image/draw"
	"io/ioutil"
	"log"
	"os"
	"path"
	"path/filepath"

	"github.com/hmmftg/image/font"
	"github.com/hmmftg/image/font/plan9font"
	"github.com/hmmftg/image/math/fixed"
)

func main() {
	readFile := func(name string) ([]byte, error) {
		return ioutil.ReadFile(filepath.FromSlash(path.Join("../testdata/fixed", name)))
	}
	fontData, err := readFile("unicode.7x13.font")
	if err != nil {
		log.Fatal(err)
	}
	face, err := plan9font.ParseFont(fontData, readFile)
	if err != nil {
		log.Fatal(err)
	}
	ascent := face.Metrics().Ascent.Ceil()

	dst := image.NewRGBA(image.Rect(0, 0, 4*7, 13))
	draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
	d := &font.Drawer{
		Dst:  dst,
		Src:  image.White,
		Face: face,
		Dot:  fixed.P(0, ascent),
	}
	// Draw:
	// 	- U+0053 LATIN CAPITAL LETTER S
	//	- U+03A3 GREEK CAPITAL LETTER SIGMA
	//	- U+222B INTEGRAL
	//	- U+3055 HIRAGANA LETTER SA
	// The testdata does not contain the CJK subfont files, so U+3055 HIRAGANA
	// LETTER SA (さ) should be rendered as U+FFFD REPLACEMENT CHARACTER (�).
	//
	// The missing subfont file will trigger an "open
	// ../testdata/shinonome/k12.3000: no such file or directory" log message.
	// This is expected and can be ignored.
	d.DrawString("SΣ∫さ")

	// Convert the dst image to ASCII art.
	var out []byte
	b := dst.Bounds()
	for y := b.Min.Y; y < b.Max.Y; y++ {
		out = append(out, '0'+byte(y%10), ' ')
		for x := b.Min.X; x < b.Max.X; x++ {
			if dst.RGBAAt(x, y).R > 0 {
				out = append(out, 'X')
			} else {
				out = append(out, '.')
			}
		}
		// Highlight the last row before the baseline. Glyphs like 'S' without
		// descenders should not affect any pixels whose Y coordinate is >= the
		// baseline.
		if y == ascent-1 {
			out = append(out, '_')
		}
		out = append(out, '\n')
	}
	os.Stdout.Write(out)

}
Output:

0 ..................X.........
1 .................X.X........
2 .XXXX..XXXXXX....X.....XXX..
3 X....X.X.........X....XX.XX.
4 X.......X........X....X.X.X.
5 X........X.......X....XXX.X.
6 .XXXX.....X......X....XX.XX.
7 .....X...X.......X....XX.XX.
8 .....X..X........X....XXXXX.
9 X....X.X.........X....XX.XX.
0 .XXXX..XXXXXX....X.....XXX.._
1 ...............X.X..........
2 ................X...........

func ParseSubfont

func ParseSubfont(data []byte, firstRune rune) (font.Face, error)

ParseSubfont parses a Plan 9 subfont file.

firstRune is the first rune in the subfont file. For example, the Phonetic.6.0 subfont, containing glyphs in the range U+0250 to U+02E9, would set firstRune to '\u0250'.

Types

This section is empty.

Jump to

Keyboard shortcuts

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