Exemplo n.º 1
0
func cfgSurf(ps *C.cairo_surface_t, eps bool, header, setup Comments) {
	var ceps C.cairo_bool_t
	if eps {
		ceps = 1
	}
	C.cairo_ps_surface_set_eps(ps, ceps)

	for _, c := range header {
		s := C.CString(c.String())
		C.cairo_ps_surface_dsc_comment(ps, s)
		C.free(unsafe.Pointer(s))
	}

	if len(setup) > 0 {
		C.cairo_ps_surface_dsc_begin_setup(ps)

		for _, c := range setup {
			s := C.CString(c.String())
			C.cairo_ps_surface_dsc_comment(ps, s)
			C.free(unsafe.Pointer(s))
		}
	}

	//ensure calls to AddComments apply to page even if no drawing has been performed.
	C.cairo_ps_surface_dsc_begin_page_setup(ps)
}
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
}