Exemplo n.º 1
0
func NewSVGSurface(filename string, widthInPoints, heightInPoints float64, version SVGVersion) *Surface {
	cs := C.CString(filename)
	defer C.free(unsafe.Pointer(cs))
	s := C.cairo_svg_surface_create(cs, C.double(widthInPoints), C.double(heightInPoints))
	C.cairo_svg_surface_restrict_to_version(s, C.cairo_svg_version_t(version))
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Exemplo n.º 2
0
// Format is determined from filename extension
// Supported formats: eps, jpeg, pdf, png, ps, svg
//
// Width and height are in pts for eps, pdf, ps, and svg; pixels for jpeg and png.
// Pixel measures will be truncated into integers
//
// Close the graphic to write the file
func Create(filename string, width float64, height float64) (*Graphic, error) {
	g := &Graphic{}

	var err error
	filename = filepath.Clean(filename)
	g.filename, err = filepath.Abs(filename)
	if err != nil {
		return nil, err
	}

	g.format = filepath.Ext(filename)[1:]

	// create the surface
	switch g.format {
	case "pdf":
		g.surface = C.cairo_pdf_surface_create(
			C.CString(filename),
			C.double(width),
			C.double(height),
		)
	case "png", "jpeg":
		g.surface = C.cairo_image_surface_create(
			C.CAIRO_FORMAT_ARGB32,
			C.int(width),
			C.int(height),
		)
	case "ps", "eps":
		g.surface = C.cairo_ps_surface_create(
			C.CString(filename),
			C.double(width),
			C.double(height),
		)
		if g.format == "eps" {
			C.cairo_ps_surface_set_eps(g.surface, 1)
		}
	case "svg":
		g.surface = C.cairo_svg_surface_create(
			C.CString(filename),
			C.double(width),
			C.double(height),
		)
	default:
		return nil, errors.New("cairo: unsupported format: " + g.format)
	}
	err = g.cairoSurfaceStatus()
	if err != nil {
		return nil, err
	}

	// create the cairo context
	g.cr = C.cairo_create(g.surface)
	err = g.cairoStatus()
	if err != nil {
		return nil, err
	}

	return g, nil
}