package plan9font
import "golang.org/x/image/font/plan9font"
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 ¶
- func ParseFont(data []byte, readFile func(relFilename string) ([]byte, error)) (font.Face, error)
- func ParseSubfont(data []byte, firstRune rune) (font.Face, error)
Examples ¶
Functions ¶
func ParseFont ¶
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.
Code:play
Output:Example¶
package main
import (
"image"
"image/draw"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"golang.org/x/image/font"
"golang.org/x/image/font/plan9font"
"golang.org/x/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)
}
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 ¶
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'.
Source Files ¶
plan9font.go
- Version
- v0.24.0 (latest)
- Published
- Feb 4, 2025
- Platform
- linux/amd64
- Imports
- 10 packages
- Last checked
- 5 hours ago –
Tools for package owners.