font/basicfont: new package for a basic font face.

The key feature is that its data is entirely self-contained and does not
require loading from separate files.

Change-Id: I4ef72b52cde93597be89dcfd55659f418c6cab23
Reviewed-on: https://go-review.googlesource.com/14486
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Nigel Tao 2015-09-11 13:43:18 +10:00
parent 2dc9880a10
commit baddd3465a
3 changed files with 1684 additions and 0 deletions

113
font/basicfont/basicfont.go Normal file
View File

@ -0,0 +1,113 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run gen.go
// Package basicfont provides fixed-size font faces.
package basicfont // import "golang.org/x/image/font/basicfont"
import (
"image"
"golang.org/x/image/math/fixed"
)
// Range maps a contiguous range of runes to vertically adjacent sub-images of
// a Face's Mask image. The rune range is inclusive on the low end and
// exclusive on the high end.
//
// If Low <= r && r < High, then the rune r is mapped to the sub-image of
// Face.Mask whose bounds are image.Rect(0, y, Face.Width, y+Face.Height),
// where y equals (int(r-Low) + Offset) * Face.Height.
type Range struct {
Low, High rune
Offset int
}
// Face7x13 is a Face derived from the public domain X11 misc-fixed font files.
//
// At the moment, it holds the printable characters in ASCII starting with
// space, and the Unicode replacement character U+FFFD.
//
// Its data is entirely self-contained and does not require loading from
// separate files.
var Face7x13 = &Face{
Advance: 7,
Width: 6,
Height: 13,
Ascent: 11,
Mask: mask7x13,
Ranges: []Range{
{'\u0020', '\u007f', 0},
{'\ufffd', '\ufffe', 95},
},
}
// Face is a basic font face whose glyphs all have the same metrics.
//
// It is safe to use concurrently.
type Face struct {
// Advance is the glyph advance, in pixels.
Advance int
// Width is the glyph width, in pixels.
Width int
// Height is the glyph height, in pixels.
Height int
// Ascent is the glyph ascent, in pixels.
Ascent int
// TODO: do we also need Top and Left fields?
// Mask contains all of the glyph masks. Its width is typically the Face's
// Width, and its height a multiple of the Face's Height.
Mask image.Image
// Ranges map runes to sub-images of Mask. The rune ranges must not
// overlap, and must be in increasing rune order.
Ranges []Range
}
func (f *Face) Close() error { return nil }
func (f *Face) Kern(r0, r1 rune) fixed.Int26_6 { return 0 }
func (f *Face) Glyph(dot fixed.Point26_6, r rune) (
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
loop:
for _, rr := range [2]rune{r, '\ufffd'} {
for _, rng := range f.Ranges {
if rr < rng.Low || rng.High <= rr {
continue
}
maskp.Y = (int(rr-rng.Low) + rng.Offset) * f.Height
ok = true
break loop
}
}
if !ok {
return image.Rectangle{}, nil, image.Point{}, 0, false
}
minX := int(dot.X+32) >> 6
minY := int(dot.Y+32)>>6 - f.Ascent
dr = image.Rectangle{
Min: image.Point{
X: minX,
Y: minY,
},
Max: image.Point{
X: minX + f.Width,
Y: minY + f.Height,
},
}
return dr, f.Mask, maskp, fixed.I(f.Advance), true
}
func (f *Face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
return fixed.R(0, -f.Ascent, f.Width, -f.Ascent+f.Height), fixed.I(f.Advance), true
}
func (f *Face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
return fixed.I(f.Advance), true
}

1456
font/basicfont/data.go Normal file

File diff suppressed because it is too large Load Diff

115
font/basicfont/gen.go Normal file
View File

@ -0,0 +1,115 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// This program generates data.go.
package main
import (
"bytes"
"fmt"
"go/format"
"image"
"image/draw"
"io/ioutil"
"log"
"path"
"path/filepath"
"golang.org/x/image/font"
"golang.org/x/image/font/plan9font"
"golang.org/x/image/math/fixed"
)
func main() {
// nGlyphs is the number of glyphs to generate: 95 characters in the range
// [0x20, 0x7e], plus the replacement character.
const nGlyphs = 95 + 1
// The particular font (unicode.7x13.font) leaves the right-most column
// empty in its ASCII glyphs. We don't have to include that column in the
// generated glyphs, so we subtract one off the effective width.
const width, height, ascent = 7 - 1, 13, 11
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.Fatalf("readFile: %v", err)
}
face, err := plan9font.ParseFont(fontData, readFile)
if err != nil {
log.Fatalf("plan9font.ParseFont: %v", err)
}
dst := image.NewRGBA(image.Rect(0, 0, width, nGlyphs*height))
draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
d := &font.Drawer{
Dst: dst,
Src: image.White,
Face: face,
}
for i := 0; i < nGlyphs; i++ {
r := '\ufffd'
if i < nGlyphs-1 {
r = 0x20 + rune(i)
}
d.Dot = fixed.P(0, height*i+ascent)
d.DrawString(string(r))
}
w := bytes.NewBuffer(nil)
w.WriteString(preamble)
fmt.Fprintf(w, "// mask7x13 contains %d %d×%d glyphs in %d Pix bytes.\n", nGlyphs, width, height, nGlyphs*width*height)
fmt.Fprintf(w, "var mask7x13 = &image.Alpha{\n")
fmt.Fprintf(w, " Stride: %d,\n", width)
fmt.Fprintf(w, " Rect: image.Rectangle{Max: image.Point{%d, %d*%d}},\n", width, nGlyphs, height)
fmt.Fprintf(w, " Pix: []byte{\n")
b := dst.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
if y%height == 0 {
if y != 0 {
w.WriteByte('\n')
}
i := y / height
if i < nGlyphs-1 {
i += 0x20
fmt.Fprintf(w, "// %#2x %q\n", i, rune(i))
} else {
fmt.Fprintf(w, "// U+FFFD REPLACEMENT CHARACTER\n")
}
}
for x := b.Min.X; x < b.Max.X; x++ {
if dst.RGBAAt(x, y).R > 0 {
w.WriteString("0xff,")
} else {
w.WriteString("0x00,")
}
}
w.WriteByte('\n')
}
w.WriteString("},\n}\n")
fmted, err := format.Source(w.Bytes())
if err != nil {
log.Fatalf("format.Source: %v", err)
}
if err := ioutil.WriteFile("data.go", fmted, 0644); err != nil {
log.Fatalf("ioutil.WriteFile: %v", err)
}
}
const preamble = `// generated by go generate; DO NOT EDIT.
package basicfont
// This data is derived from files in the font/fixed directory of the Plan 9
// Port source code (https://github.com/9fans/plan9port) which were originally
// based on the public domain X11 misc-fixed font files.
import "image"
`