本文整理汇总了Golang中code/google/com/p/freetype-go/freetype/raster.NewRasterizer函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRasterizer函数的具体用法?Golang NewRasterizer怎么用?Golang NewRasterizer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRasterizer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewGraphicContext
/**
* Create a new Graphic context from an image
*/
func NewGraphicContext(img draw.Image) *ImageGraphicContext {
var painter Painter
switch selectImage := img.(type) {
case *image.RGBA:
painter = raster.NewRGBAPainter(selectImage)
//case *image.NRGBA:
// painter = NewNRGBAPainter(selectImage)
default:
panic("Image type not supported")
}
width, height := img.Bounds().Dx(), img.Bounds().Dy()
dpi := 92
ftContext := freetype.NewContext()
ftContext.SetDPI(dpi)
ftContext.SetClip(img.Bounds())
ftContext.SetDst(img)
gc := &ImageGraphicContext{
NewStackGraphicContext(),
img,
painter,
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
ftContext,
dpi,
}
return gc
}
示例2: NewGraphicContext
// NewGraphicContext creates a new Graphic context from an image.
func NewGraphicContext(width, height int) *GraphicContext {
gc := &GraphicContext{
draw2d.NewStackGraphicContext(),
NewPainter(),
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
}
return gc
}
示例3: NewGraphicContextWithPainter
// NewGraphicContextWithPainter creates a new Graphic context from an image and a Painter (see Freetype-go)
func NewGraphicContextWithPainter(img draw.Image, painter Painter) *ImageGraphicContext {
width, height := img.Bounds().Dx(), img.Bounds().Dy()
dpi := 92
gc := &ImageGraphicContext{
NewStackGraphicContext(),
img,
painter,
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
truetype.NewGlyphBuf(),
dpi,
}
return gc
}
示例4: TestFreetypeNonZeroWinding
func TestFreetypeNonZeroWinding(t *testing.T) {
var p Path
p.LineTo(10, 190)
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
c.Segment(&p, flatteningThreshold)
poly := Polygon(p.points)
color := color.RGBA{0, 0, 0, 0xff}
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
rasterizer := raster.NewRasterizer(200, 200)
rasterizer.UseNonZeroWinding = true
rasterizer.Start(raster.Point{
X: raster.Fix32(10 * 256),
Y: raster.Fix32(190 * 256)})
for j := 0; j < len(poly); j = j + 2 {
rasterizer.Add1(raster.Point{
X: raster.Fix32(poly[j] * 256),
Y: raster.Fix32(poly[j+1] * 256)})
}
painter := raster.NewRGBAPainter(img)
painter.SetColor(color)
rasterizer.Rasterize(painter)
savepng("../output/raster/TestFreetypeNonZeroWinding.png", img)
}
示例5: BenchmarkFreetype
func BenchmarkFreetype(b *testing.B) {
var p Path
p.LineTo(10, 190)
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
c.Segment(&p, flatteningThreshold)
poly := Polygon(p.points)
color := color.RGBA{0, 0, 0, 0xff}
for i := 0; i < b.N; i++ {
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
rasterizer := raster.NewRasterizer(200, 200)
rasterizer.UseNonZeroWinding = false
rasterizer.Start(raster.Point{
X: raster.Fix32(10 * 256),
Y: raster.Fix32(190 * 256)})
for j := 0; j < len(poly); j = j + 2 {
rasterizer.Add1(raster.Point{
X: raster.Fix32(poly[j] * 256),
Y: raster.Fix32(poly[j+1] * 256)})
}
painter := raster.NewRGBAPainter(img)
painter.SetColor(color)
rasterizer.Rasterize(painter)
}
}
示例6: TestFreetypeRasterizerNonZeroWinding
func TestFreetypeRasterizerNonZeroWinding(t *testing.T) {
var p Path
p.LineTo(10, 190)
draw2dbase.TraceCubic(&p, []float64{10, 190, 10, 10, 190, 10, 190, 190}, 0.5)
poly := Polygon(p.points)
color := color.RGBA{0, 0, 0, 0xff}
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
rasterizer := raster.NewRasterizer(200, 200)
rasterizer.UseNonZeroWinding = true
rasterizer.Start(raster.Point{
X: raster.Fix32(10 * 256),
Y: raster.Fix32(190 * 256)})
for j := 0; j < len(poly); j = j + 2 {
rasterizer.Add1(raster.Point{
X: raster.Fix32(poly[j] * 256),
Y: raster.Fix32(poly[j+1] * 256)})
}
painter := raster.NewRGBAPainter(img)
painter.SetColor(color)
rasterizer.Rasterize(painter)
err := draw2dimg.SaveToPngFile("output/TestFreetypeRasterizerNonZeroWinding.png", img)
if err != nil {
fmt.Println(err)
}
}
示例7: BenchmarkFreetypeNonZeroWinding
func BenchmarkFreetypeNonZeroWinding(b *testing.B) {
var p Path
p.LineTo(10, 190)
draw2dbase.TraceCubic(&p, []float64{10, 190, 10, 10, 190, 10, 190, 190}, 0.5)
poly := Polygon(p.points)
color := color.RGBA{0, 0, 0, 0xff}
for i := 0; i < b.N; i++ {
img := image.NewRGBA(image.Rect(0, 0, 200, 200))
rasterizer := raster.NewRasterizer(200, 200)
rasterizer.UseNonZeroWinding = true
rasterizer.Start(raster.Point{
X: raster.Fix32(10 * 256),
Y: raster.Fix32(190 * 256)})
for j := 0; j < len(poly); j = j + 2 {
rasterizer.Add1(raster.Point{
X: raster.Fix32(poly[j] * 256),
Y: raster.Fix32(poly[j+1] * 256)})
}
painter := raster.NewRGBAPainter(img)
painter.SetColor(color)
rasterizer.Rasterize(painter)
}
}
示例8: NewGraphicContextWithPainter
// Create a new Graphic context from an image and a Painter (see Freetype-go)
func NewGraphicContextWithPainter(img draw.Image, painter Painter) *ImageGraphicContext {
width, height := img.Bounds().Dx(), img.Bounds().Dy()
dpi := 92
ftContext := freetype.NewContext()
ftContext.SetDPI(float64(dpi))
ftContext.SetClip(img.Bounds())
ftContext.SetDst(img)
gc := &ImageGraphicContext{
NewStackGraphicContext(),
img,
painter,
raster.NewRasterizer(width, height),
raster.NewRasterizer(width, height),
ftContext,
dpi,
}
return gc
}
示例9: NewContext
// NewContext creates a new Context.
func NewContext() *Context {
return &Context{
r: raster.NewRasterizer(0, 0),
glyphBuf: truetype.NewGlyphBuf(),
fontSize: 12,
dpi: 72,
scale: 12 << 6,
}
}
示例10: main
func main() {
// Draw a rounded corner that is one pixel wide.
r := raster.NewRasterizer(50, 50)
r.Start(p(5, 5))
r.Add1(p(5, 25))
r.Add2(p(5, 45), p(25, 45))
r.Add1(p(45, 45))
r.Add1(p(45, 44))
r.Add1(p(26, 44))
r.Add2(p(6, 44), p(6, 24))
r.Add1(p(6, 5))
r.Add1(p(5, 5))
// Rasterize that curve multiple times at different gammas.
const (
w = 600
h = 200
)
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(rgba, image.Rect(0, 0, w, h/2), image.Black, image.ZP, draw.Src)
draw.Draw(rgba, image.Rect(0, h/2, w, h), image.White, image.ZP, draw.Src)
mask := image.NewAlpha(image.Rect(0, 0, 50, 50))
painter := raster.NewAlphaSrcPainter(mask)
gammas := []float64{1.0 / 10.0, 1.0 / 3.0, 1.0 / 2.0, 2.0 / 3.0, 4.0 / 5.0, 1.0, 5.0 / 4.0, 3.0 / 2.0, 2.0, 3.0, 10.0}
for i, g := range gammas {
draw.Draw(mask, mask.Bounds(), image.Transparent, image.ZP, draw.Src)
r.Rasterize(raster.NewGammaCorrectionPainter(painter, g))
x, y := 50*i+25, 25
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.White, image.ZP, mask, image.ZP, draw.Over)
y += 100
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.Black, image.ZP, mask, image.ZP, draw.Over)
}
// Save that RGBA image to disk.
f, err := os.Create("out.png")
if err != nil {
log.Println(err)
os.Exit(1)
}
defer f.Close()
b := bufio.NewWriter(f)
err = png.Encode(b, rgba)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = b.Flush()
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Println("Wrote out.png OK.")
}
示例11: NewCanvas
// Make a new canvas of size w x h.
func NewCanvas(w, h int) *Canvas {
c := new(Canvas)
c.RGBA = image.NewRGBA(image.Rect(0, 0, w, h))
c.Clear(color.White)
c.painter = raster.NewRGBAPainter(c.RGBA)
c.rasterizer = raster.NewRasterizer(w, h)
c.rasterizer.UseNonZeroWinding = true
c.SetColor(color.Black)
c.path = make(raster.Path, 0, 100)
c.resetPath()
c.SetStroke(1, Round)
return c
}
示例12: createGlyphPage
func createGlyphPage(resolution resolution, glyphMaxSizePixels math.Size) *glyphPage {
// Handle exceptionally large glyphs.
size := math.Size{W: glyphPageWidth, H: glyphPageHeight}.Max(glyphMaxSizePixels)
size.W = align(size.W, glyphSizeAlignment)
size.H = align(size.H, glyphSizeAlignment)
return &glyphPage{
resolution: resolution,
glyphMaxSizePixels: glyphMaxSizePixels,
size: size,
image: image.NewAlpha(image.Rect(0, 0, size.W, size.H)),
offsets: make(map[rune]math.Point),
rowHeight: 0,
rast: raster.NewRasterizer(glyphMaxSizePixels.W, glyphMaxSizePixels.H),
}
}
示例13: moustache
// moustache draws a moustache of the specified size and droop
// onto the image m and returns the result. It may overwrite the
// original.
func moustache(m image.Image, x, y, size, droopFactor int) image.Image {
mrgba := rgba(m)
p := raster.NewRGBAPainter(mrgba)
p.SetColor(color.RGBA{0, 0, 0, 255})
w, h := m.Bounds().Dx(), m.Bounds().Dy()
r := raster.NewRasterizer(w, h)
var (
mag = raster.Fix32((10 + size) << 8)
width = pt(20, 0).Mul(mag)
mid = pt(x, y)
droop = pt(0, droopFactor).Mul(mag)
left = mid.Sub(width).Add(droop)
right = mid.Add(width).Add(droop)
bow = pt(0, 5).Mul(mag).Sub(droop)
curlx = pt(10, 0).Mul(mag)
curly = pt(0, 2).Mul(mag)
risex = pt(2, 0).Mul(mag)
risey = pt(0, 5).Mul(mag)
)
r.Start(left)
r.Add3(
mid.Sub(curlx).Add(curly),
mid.Sub(risex).Sub(risey),
mid,
)
r.Add3(
mid.Add(risex).Sub(risey),
mid.Add(curlx).Add(curly),
right,
)
r.Add2(
mid.Add(bow),
left,
)
r.Rasterize(p)
return mrgba
}
示例14: TestFreetype
func TestFreetype(t *testing.T) {
var p Path
p.LineTo(10, 190)
c := curve.CubicCurveFloat64{10, 190, 10, 10, 190, 10, 190, 190}
c.Segment(&p, flattening_threshold)
poly := Polygon(p.points)
rgba := color.RGBA{0, 0, 0, 0xff}
bounds := image.Rect(0, 0, 200, 200)
mask := image.NewAlpha(bounds)
img := image.NewRGBA(bounds)
rasterizer := raster.NewRasterizer(200, 200)
rasterizer.UseNonZeroWinding = false
rasterizer.Start(raster.Point{raster.Fix32(10 * 256), raster.Fix32(190 * 256)})
for j := 0; j < len(poly); j = j + 2 {
rasterizer.Add1(raster.Point{raster.Fix32(poly[j] * 256), raster.Fix32(poly[j+1] * 256)})
}
painter := raster.NewAlphaSrcPainter(mask)
rasterizer.Rasterize(painter)
DrawSolidRGBA(img, mask, rgba)
savepng("_testFreetype.png", img)
}
示例15: worldImage
// the returned zoneOfColor always has A == 256.
func worldImage(t *testing.T) (im *image.RGBA, zoneOfColor map[color.RGBA]string) {
scale := *flagScale
width := int(scale * 360)
height := int(scale * 180)
im = image.NewRGBA(image.Rect(0, 0, width, height))
zoneOfColor = map[color.RGBA]string{}
tab := crc32.MakeTable(crc32.IEEE + 1)
drawPoly := func(col color.RGBA, xys ...int) {
painter := raster.NewRGBAPainter(im)
painter.SetColor(col)
r := raster.NewRasterizer(width, height)
r.Start(raster.Point{X: raster.Fix32(xys[0]) << 8, Y: raster.Fix32(xys[1]) << 8})
for i := 2; i < len(xys); i += 2 {
r.Add1(raster.Point{X: raster.Fix32(xys[i]) << 8, Y: raster.Fix32(xys[i+1]) << 8})
}
r.Add1(raster.Point{X: raster.Fix32(xys[0]) << 8, Y: raster.Fix32(xys[1]) << 8})
r.Rasterize(raster.NewMonochromePainter(painter))
}
sr, err := shp.Open("world/tz_world.shp")
if err != nil {
t.Fatalf("Error opening world/tz_world.shp: %v; unzip it from http://efele.net/maps/tz/world/tz_world.zip", err)
}
defer sr.Close()
for sr.Next() {
i, s := sr.Shape()
p, ok := s.(*shp.Polygon)
if !ok {
t.Fatalf("Unknown shape %T", p)
}
zoneName := sr.ReadAttribute(i, 0)
if zoneName == "uninhabited" {
continue
}
if _, err := time.LoadLocation(zoneName); err != nil {
t.Fatalf("Failed to load: %v (%v)", zoneName, err)
}
hash := crc32.Checksum([]byte(zoneName), tab)
col := color.RGBA{uint8(hash >> 24), uint8(hash >> 16), uint8(hash >> 8), 255}
if name, ok := zoneOfColor[col]; ok {
if name != zoneName {
log.Fatalf("Color %+v dup: %s and %s", col, name, zoneName)
}
} else {
zoneOfColor[col] = zoneName
}
var xys []int
for _, pt := range p.Points {
xys = append(xys, int((pt.X+180)*scale), int((90-pt.Y)*scale))
}
drawPoly(col, xys...)
}
// adjust point from scale 32 to whatever the user is using.
ap := func(x int) int { return x * int(scale) / 32 }
// Fix some rendering glitches:
// {186 205 234 255} = Europe/Rome
drawPoly(color.RGBA{186, 205, 234, 255},
ap(6156), ap(1468),
ap(6293), ap(1596),
ap(6293), ap(1598),
ap(6156), ap(1540))
// {136 136 180 255} = America/Boise
drawPoly(color.RGBA{136, 136, 180, 255},
ap(2145), ap(1468),
ap(2189), ap(1468),
ap(2189), ap(1536),
ap(2145), ap(1536))
// {120 247 14 255} = America/Denver
drawPoly(color.RGBA{120, 247, 14, 255},
ap(2167), ap(1536),
ap(2171), ap(1536),
ap(2217), ap(1714),
ap(2204), ap(1724),
ap(2160), ap(1537))
return
}