efa48d599d
This CL implements Go bindings for a subset of the functions in the C API. It implements a Go version of the C demo program in experimental/c-api-example/skia-c-example.c and the output is identical. (Checked by hand). The main purpose is to establish a pattern of calling the Skia C API that is memory safe and provides a idiomatic Go interface to Skia. Follow up CLs will cover the entire C API, add documentation and establish a pattern to distribute the bindings more easily. BUG= Change-Id: I96ff7c3715164c533202ce300ab0312b1b07f884 Change-Id: I96ff7c3715164c533202ce300ab0312b1b07f884 Reviewed-on: https://skia-review.googlesource.com/10032 Reviewed-by: Mike Klein <mtklein@chromium.org> Commit-Queue: Stephan Altmueller <stephana@google.com>
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
/*
|
|
* Copyright 2015 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
package main
|
|
|
|
/*
|
|
This is a translation of the example code in
|
|
"experimental/c-api-example/skia-c-example.c" to test the
|
|
go-skia package.
|
|
*/
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
skia "skia.googlesource.com/skia/experimental/go-skia"
|
|
)
|
|
|
|
func main() {
|
|
imgInfo := &skia.ImageInfo{
|
|
Width: 640,
|
|
Height: 480,
|
|
ColorType: skia.GetDefaultColortype(),
|
|
AlphaType: skia.PREMUL_ALPHATYPE,
|
|
}
|
|
|
|
surface, err := skia.NewRasterSurface(imgInfo)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
canvas := surface.Canvas()
|
|
|
|
fillPaint := skia.NewPaint()
|
|
fillPaint.SetColor(0xFF0000FF)
|
|
canvas.DrawPaint(fillPaint)
|
|
fillPaint.SetColor(0xFF00FFFF)
|
|
|
|
rect := skia.NewRect(100, 100, 540, 380)
|
|
canvas.DrawRect(rect, fillPaint)
|
|
|
|
strokePaint := skia.NewPaint()
|
|
strokePaint.SetColor(0xFFFF0000)
|
|
strokePaint.SetAntiAlias(true)
|
|
strokePaint.SetStroke(true)
|
|
strokePaint.SetStrokeWidth(5.0)
|
|
|
|
path := skia.NewPath()
|
|
path.MoveTo(50, 50)
|
|
path.LineTo(590, 50)
|
|
path.CubicTo(-490, 50, 1130, 430, 50, 430)
|
|
path.LineTo(590, 430)
|
|
canvas.DrawPath(path, strokePaint)
|
|
|
|
fillPaint.SetColor(0x8000FF00)
|
|
canvas.DrawOval(skia.NewRect(120, 120, 520, 360), fillPaint)
|
|
|
|
// // Get a skia image from the surface.
|
|
skImg := surface.Image()
|
|
|
|
// Write new image to file if we have one.
|
|
if skImg != nil {
|
|
out, err := os.Create("testimage.png")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer out.Close()
|
|
|
|
if err := skImg.WritePNG(out); err != nil {
|
|
log.Fatalf("Unable to write png: %s", err)
|
|
}
|
|
}
|
|
}
|