Fixes golang/go#45723

Change-Id: Id9131dd690a4496ed5ec8aae3bf4d54f44a53d79
Reviewed-on: https://go-review.googlesource.com/c/image/+/316069
Trust: Nigel Tao <nigeltao@golang.org>
Run-TryBot: Nigel Tao <nigeltao@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Nigel Tao 2021-05-02 13:27:31 +10:00
Родитель ac19c3e999
Коммит 7319ad40d3
2 изменённых файлов: 78 добавлений и 0 удалений

Просмотреть файл

@ -25,6 +25,7 @@ func Example_rasterizeGlyph() {
originY = 32
)
// Load the 'G' glyph from the Go Regular font.
f, err := sfnt.Parse(goregular.TTF)
if err != nil {
log.Fatalf("Parse: %v", err)
@ -42,6 +43,7 @@ func Example_rasterizeGlyph() {
log.Fatalf("LoadGlyph: %v", err)
}
// Translate and scale that glyph as we pass it to a vector.Rasterizer.
r := vector.NewRasterizer(width, height)
r.DrawOp = draw.Src
for _, seg := range segments {
@ -77,9 +79,12 @@ func Example_rasterizeGlyph() {
}
}
// Finish the rasterization: the conversion from vector graphics (shapes)
// to raster graphics (pixels).
dst := image.NewAlpha(image.Rect(0, 0, width, height))
r.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})
// Visualize the pixels.
const asciiArt = ".++8"
buf := make([]byte, 0, height*(width+1))
for y := 0; y < height; y++ {

73
vector/example_test.go Normal file
Просмотреть файл

@ -0,0 +1,73 @@
// Copyright 2021 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.
package vector_test
import (
"image"
"image/draw"
"os"
"golang.org/x/image/vector"
)
func Example_draw() {
const (
width = 30
height = 20
)
// Define a closed shape with three edges: two linear and one quadratic.
// One of its vertices is at the top-left corner of the (1, 2) pixel, which
// is also the bottom-right corner of the (0, 1) pixel.
//
// Co-ordinates can be floating point numbers, not just integers. They can
// also be outside the vector.Rasterizer's dimensions. The shapes will be
// clipped during rasterization.
r := vector.NewRasterizer(width, height)
r.DrawOp = draw.Src
r.MoveTo(1, 2)
r.LineTo(20, 2)
r.QuadTo(40.5, 15, 10, 20)
r.ClosePath()
// Finish the rasterization: the conversion from vector graphics (shapes)
// to raster graphics (pixels). Co-ordinates are now integers.
dst := image.NewAlpha(image.Rect(0, 0, width, height))
r.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})
// Visualize the pixels.
const asciiArt = ".++8"
buf := make([]byte, 0, height*(width+1))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
a := dst.AlphaAt(x, y).A
buf = append(buf, asciiArt[a>>6])
}
buf = append(buf, '\n')
}
os.Stdout.Write(buf)
// Output:
// ..............................
// ..............................
// .8888888888888888888+.........
// .+88888888888888888888+.......
// ..888888888888888888888+......
// ..+888888888888888888888+.....
// ...8888888888888888888888+....
// ...+8888888888888888888888+...
// ....88888888888888888888888+..
// ....+88888888888888888888888..
// .....88888888888888888888888..
// .....+8888888888888888888888..
// ......8888888888888888888888..
// ......+88888888888888888888+..
// .......8888888888888888888+...
// .......+88888888888888888.....
// ........888888888888888+......
// ........+88888888888+.........
// .........8888888++............
// .........+8+++................
}