image: golang.org/x/image/font/plan9font Index | Examples | Files

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 http://plan9.bell-labs.com/magic/man2html/6/font

Index

Examples

Package Files

plan9font.go

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.

Code:

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)
}
// TODO: derive the ascent from the face's metrics.
const ascent = 11

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'.

Package plan9font imports 10 packages (graph) and is imported by 1 packages. Updated 6 days ago. Refresh now. Tools for package owners.