...

Source file src/github.com/golang/freetype/example/capjoin/main.go

Documentation: github.com/golang/freetype/example/capjoin

     1  // Copyright 2016 The Freetype-Go Authors. All rights reserved.
     2  // Use of this source code is governed by your choice of either the
     3  // FreeType License or the GNU General Public License version 2 (or
     4  // any later version), both of which can be found in the LICENSE file.
     5  
     6  // +build example
     7  //
     8  // This build tag means that "go install github.com/golang/freetype/..."
     9  // doesn't install this example program. Use "go run main.go" to run it or "go
    10  // install -tags=example" to install it.
    11  
    12  package main
    13  
    14  import (
    15  	"bufio"
    16  	"fmt"
    17  	"image"
    18  	"image/color"
    19  	"image/draw"
    20  	"image/png"
    21  	"log"
    22  	"os"
    23  
    24  	"github.com/golang/freetype/raster"
    25  	"golang.org/x/image/math/fixed"
    26  )
    27  
    28  func main() {
    29  	const (
    30  		w = 400
    31  		h = 400
    32  	)
    33  	r := raster.NewRasterizer(w, h)
    34  	r.UseNonZeroWinding = true
    35  
    36  	cjs := []struct {
    37  		c raster.Capper
    38  		j raster.Joiner
    39  	}{
    40  		{raster.RoundCapper, raster.RoundJoiner},
    41  		{raster.ButtCapper, raster.BevelJoiner},
    42  		{raster.SquareCapper, raster.BevelJoiner},
    43  	}
    44  
    45  	for i, cj := range cjs {
    46  		var path raster.Path
    47  		path.Start(fixed.P(30+100*i, 30+120*i))
    48  		path.Add1(fixed.P(180+100*i, 80+120*i))
    49  		path.Add1(fixed.P(50+100*i, 130+120*i))
    50  		raster.Stroke(r, path, fixed.I(20), cj.c, cj.j)
    51  	}
    52  
    53  	rgba := image.NewRGBA(image.Rect(0, 0, w, h))
    54  	draw.Draw(rgba, rgba.Bounds(), image.Black, image.Point{}, draw.Src)
    55  	p := raster.NewRGBAPainter(rgba)
    56  	p.SetColor(color.RGBA{0x7f, 0x7f, 0x7f, 0xff})
    57  	r.Rasterize(p)
    58  
    59  	white := color.RGBA{0xff, 0xff, 0xff, 0xff}
    60  	for i := range cjs {
    61  		rgba.SetRGBA(30+100*i, 30+120*i, white)
    62  		rgba.SetRGBA(180+100*i, 80+120*i, white)
    63  		rgba.SetRGBA(50+100*i, 130+120*i, white)
    64  	}
    65  
    66  	// Save that RGBA image to disk.
    67  	outFile, err := os.Create("out.png")
    68  	if err != nil {
    69  		log.Println(err)
    70  		os.Exit(1)
    71  	}
    72  	defer outFile.Close()
    73  	b := bufio.NewWriter(outFile)
    74  	err = png.Encode(b, rgba)
    75  	if err != nil {
    76  		log.Println(err)
    77  		os.Exit(1)
    78  	}
    79  	err = b.Flush()
    80  	if err != nil {
    81  		log.Println(err)
    82  		os.Exit(1)
    83  	}
    84  	fmt.Println("Wrote out.png OK.")
    85  }
    86  

View as plain text