2010-04-13 12:26:10 +00:00
|
|
|
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by your choice of either the
|
2010-08-03 01:07:23 +00:00
|
|
|
// FreeType License or the GNU General Public License version 2 (or
|
|
|
|
// any later version), both of which can be found in the LICENSE file.
|
2010-04-13 12:26:10 +00:00
|
|
|
|
2016-04-10 05:05:36 +00:00
|
|
|
// +build example
|
2015-08-12 04:41:46 +00:00
|
|
|
//
|
|
|
|
// This build tag means that "go install github.com/golang/freetype/..."
|
2016-04-10 05:05:36 +00:00
|
|
|
// doesn't install this example program. Use "go run main.go" to run it or "go
|
|
|
|
// install -tags=example" to install it.
|
2015-08-12 04:41:46 +00:00
|
|
|
|
2010-04-13 12:26:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
2011-06-16 02:25:21 +00:00
|
|
|
"image/draw"
|
2010-04-13 12:26:10 +00:00
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2015-08-12 04:30:01 +00:00
|
|
|
"github.com/golang/freetype/raster"
|
2015-08-17 00:26:09 +00:00
|
|
|
"golang.org/x/image/math/fixed"
|
2010-04-13 12:26:10 +00:00
|
|
|
)
|
|
|
|
|
2015-08-17 00:26:09 +00:00
|
|
|
func p(x, y int) fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{
|
|
|
|
X: fixed.Int26_6(x * 64),
|
|
|
|
Y: fixed.Int26_6(y * 64),
|
2012-05-07 12:52:01 +00:00
|
|
|
}
|
2010-04-13 12:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Draw a rounded corner that is one pixel wide.
|
2010-05-14 03:29:53 +00:00
|
|
|
r := raster.NewRasterizer(50, 50)
|
2010-04-13 12:26:10 +00:00
|
|
|
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
|
|
|
|
)
|
2011-09-14 12:02:41 +00:00
|
|
|
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
|
2011-06-16 02:25:21 +00:00
|
|
|
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)
|
2011-09-14 12:02:41 +00:00
|
|
|
mask := image.NewAlpha(image.Rect(0, 0, 50, 50))
|
2010-10-12 05:55:01 +00:00
|
|
|
painter := raster.NewAlphaSrcPainter(mask)
|
2011-01-21 01:27:50 +00:00
|
|
|
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}
|
2010-04-13 12:26:10 +00:00
|
|
|
for i, g := range gammas {
|
2011-07-10 05:05:40 +00:00
|
|
|
draw.Draw(mask, mask.Bounds(), image.Transparent, image.ZP, draw.Src)
|
2010-05-14 03:29:53 +00:00
|
|
|
r.Rasterize(raster.NewGammaCorrectionPainter(painter, g))
|
2010-04-13 12:26:10 +00:00
|
|
|
x, y := 50*i+25, 25
|
2010-08-27 00:47:48 +00:00
|
|
|
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.White, image.ZP, mask, image.ZP, draw.Over)
|
2010-04-13 12:26:10 +00:00
|
|
|
y += 100
|
2010-08-27 00:47:48 +00:00
|
|
|
draw.DrawMask(rgba, image.Rect(x, y, x+50, y+50), image.Black, image.ZP, mask, image.ZP, draw.Over)
|
2010-04-13 12:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save that RGBA image to disk.
|
2015-08-20 06:07:51 +00:00
|
|
|
outFile, err := os.Create("out.png")
|
2010-04-13 12:26:10 +00:00
|
|
|
if err != nil {
|
2010-10-15 03:02:11 +00:00
|
|
|
log.Println(err)
|
2010-04-13 12:26:10 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-08-20 06:07:51 +00:00
|
|
|
defer outFile.Close()
|
|
|
|
b := bufio.NewWriter(outFile)
|
2010-04-13 12:26:10 +00:00
|
|
|
err = png.Encode(b, rgba)
|
|
|
|
if err != nil {
|
2010-10-15 03:02:11 +00:00
|
|
|
log.Println(err)
|
2010-04-13 12:26:10 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = b.Flush()
|
|
|
|
if err != nil {
|
2010-10-15 03:02:11 +00:00
|
|
|
log.Println(err)
|
2010-04-13 12:26:10 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println("Wrote out.png OK.")
|
|
|
|
}
|