plan9font: use image.Alpha for subface images.

image.Alpha is fast-pathed in draw.Draw, plan9Image is not.

Change-Id: I5ed8fc4d310bb5c6ec2cffdd0ba4295dae2274cf
Reviewed-on: https://go-review.googlesource.com/21453
Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
Ethan Burns 2016-04-02 12:19:39 -04:00 committed by Nigel Tao
parent 8b5ac8f8e5
commit 7a320489ae

View File

@ -7,16 +7,13 @@
// http://plan9.bell-labs.com/magic/man2html/6/font // http://plan9.bell-labs.com/magic/man2html/6/font
package plan9font // import "golang.org/x/image/font/plan9font" package plan9font // import "golang.org/x/image/font/plan9font"
// TODO: have a subface use an *image.Alpha instead of plan9Image implementing
// the image.Image interface? The image/draw code has a fast path for
// *image.Alpha masks.
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
"image/draw"
"log" "log"
"strconv" "strconv"
"strings" "strings"
@ -54,12 +51,12 @@ func parseFontchars(p []byte) []fontchar {
// subface implements font.Face for a Plan 9 subfont. // subface implements font.Face for a Plan 9 subfont.
type subface struct { type subface struct {
firstRune rune // First rune in the subfont. firstRune rune // First rune in the subfont.
n int // Number of characters in the subfont. n int // Number of characters in the subfont.
height int // Inter-line spacing. height int // Inter-line spacing.
ascent int // Height above the baseline. ascent int // Height above the baseline.
fontchars []fontchar // Character descriptions. fontchars []fontchar // Character descriptions.
img *plan9Image // Image holding the glyphs. img *image.Alpha // Image holding the glyphs.
} }
func (f *subface) Close() error { return nil } func (f *subface) Close() error { return nil }
@ -300,13 +297,15 @@ func ParseSubfont(data []byte, firstRune rune) (font.Face, error) {
if len(data) != 6*(n+1) { if len(data) != 6*(n+1) {
return nil, errors.New("plan9font: invalid subfont: data length mismatch") return nil, errors.New("plan9font: invalid subfont: data length mismatch")
} }
img := image.NewAlpha(m.Bounds())
draw.Draw(img, img.Bounds(), m, image.ZP, draw.Over)
return &subface{ return &subface{
firstRune: firstRune, firstRune: firstRune,
n: n, n: n,
height: height, height: height,
ascent: ascent, ascent: ascent,
fontchars: parseFontchars(data), fontchars: parseFontchars(data),
img: m, img: img,
}, nil }, nil
} }