Example #1
0
func NewPDFSurface(filename string, widthInPoints, heightInPoints float64, version PDFVersion) *Surface {
	cs := C.CString(filename)
	defer C.free(unsafe.Pointer(cs))
	s := C.cairo_pdf_surface_create(cs, C.double(widthInPoints), C.double(heightInPoints))
	C.cairo_pdf_surface_restrict_to_version(s, C.cairo_pdf_version_t(version))
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Example #2
0
func NewPSSurface(filename string, widthInPoints, heightInPoints float64, level PSLevel) *Surface {
	cs := C.CString(filename)
	defer C.free(unsafe.Pointer(cs))
	s := C.cairo_ps_surface_create(cs, C.double(widthInPoints), C.double(heightInPoints))
	C.cairo_ps_surface_restrict_to_level(s, C.cairo_ps_level_t(level))
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Example #3
0
func NewSurfaceFromPNG(filename string) *Surface {
	surface := new(Surface)
	p := C.CString(filename)
	surface.surface = C.cairo_image_surface_create_from_png(p)
	C.free(unsafe.Pointer(p))
	surface.context = C.cairo_create(surface.surface)
	return surface
}
Example #4
0
func CairoCreateContext(surface CairoSurface) (CairoContext, error) {
	context := C.cairo_create(surface.nativePointer())
	c := CairoContext(unsafe.Pointer(context))
	if err := c.status(); err != nil {
		return 0, err
	}
	return c, nil
}
Example #5
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
}
Example #6
0
//New creates a new drawing context that draws on the target surface.
//
//Originally cairo_create.
func New(target Surface) (*Context, error) {
	s := target.XtensionRaw()
	c := &Context{
		c: C.cairo_create(s),
		s: target,
	}
	runtime.SetFinalizer(c, (*Context).Close)
	return c, c.Err()
}
Example #7
0
File: icon.go Project: sqp/godock
func (o *dockIcon) SetIcon(str string) error {
	if o.Ptr.image.pSurface == nil {
		return errors.New("icon has no image.pSurface")
	}

	var cstr *C.gchar
	if str != "" {
		cstr = (*C.gchar)(C.CString(str))
		defer C.free(unsafe.Pointer((*C.char)(cstr)))
	}
	ctx := C.cairo_create(o.Ptr.image.pSurface)
	C.cairo_dock_set_image_on_icon(ctx, cstr, o.Ptr, o.GetContainer().Ptr) // returns gboolean
	C.cairo_destroy(ctx)

	return nil
}
Example #8
0
func NewSurfaceFromXCB(xcb_drawable xproto.Drawable, xcb_VI xproto.VisualInfo, width, height int) *Surface {

	var xcb_visualtype C.xcb_visualtype_t
	xcb_visualtype.visual_id = C.xcb_visualid_t(xcb_VI.VisualId)
	xcb_visualtype._class = C.uint8_t(xcb_VI.Class)
	xcb_visualtype.bits_per_rgb_value = C.uint8_t(xcb_VI.BitsPerRgbValue)
	xcb_visualtype.colormap_entries = C.uint16_t(xcb_VI.ColormapEntries)
	xcb_visualtype.red_mask = C.uint32_t(xcb_VI.RedMask)
	xcb_visualtype.green_mask = C.uint32_t(xcb_VI.GreenMask)
	xcb_visualtype.blue_mask = C.uint32_t(xcb_VI.BlueMask)

	var connect_xcb (*C.xcb_connection_t) = C.xcb_connect(nil, nil)

	s := C.cairo_xcb_surface_create(connect_xcb, C.xcb_drawable_t(xcb_drawable), &xcb_visualtype, C.int(width), C.int(height))
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Example #9
0
// Create an image of the given size
func Create(w, h int, mode ColorSpace) *Image {
	var img Image
	var format C.cairo_format_t
	switch mode {
	case MODE_RGB, MODE_G8:
		format = C.CAIRO_FORMAT_RGB24
	case MODE_RGBA:
		format = C.CAIRO_FORMAT_ARGB32
	default:
		format = C.CAIRO_FORMAT_A8
	}
	img.ColorSpace = mode
	img.Surf = C.cairo_image_surface_create(format, C.int(w), C.int(h))
	img.Ctx = C.cairo_create(img.Surf)
	img.W, img.H = w, h
	return &img
}
Example #10
0
func CreateXWindow(width, height int) (*XWindow, error) {
	C.XInitThreads()

	W := &XWindow{}

	W.Display = C.XOpenDisplay(nil)
	if W.Display == nil {
		return &XWindow{}, errors.New("Can't open display")
	}
	W.Window = C.XCreateSimpleWindow(W.Display, C.XDefaultRootWindow(W.Display), 0, 0, C.uint(width), C.uint(height), 0, 0, 0xFF151515)
	C.XSetWindowBackgroundPixmap(W.Display, W.Window, 0) // This avoids flickering on resize
	C.XMapWindow(W.Display, W.Window)
	C.XStoreName(W.Display, W.Window, C.CString("gowitt"))

	C.XSelectInput(W.Display, W.Window, C.ExposureMask|C.KeyPressMask|C.ButtonPressMask)
	C.XFlush(W.Display)

	// Cairo
	W.Surface = C.cairo_xlib_surface_create(W.Display, C.Drawable(W.Window), C.XDefaultVisual(W.Display, 0), C.int(width), C.int(height))
	C.cairo_xlib_surface_set_size(W.Surface, C.int(width), C.int(height))
	W.Cairo = C.cairo_create(W.Surface)

	// Pango
	InitLayoutsCache(W.Cairo)
	W.PangoContext = C.pango_cairo_create_context(W.Cairo)
	W.FontDesc = C.pango_font_description_from_string(C.CString("Sans 10"))

	W.AttrList = C.pango_attr_list_new()

	placeholderImage = C.cairo_image_surface_create_from_png(C.CString("test.png"))

	W.UserImages = NewImageCache(func() {
		var ev C.XEvent
		exev := (*C.XExposeEvent)(unsafe.Pointer(&ev))
		exev._type = C.Expose
		exev.count = 0
		exev.window = W.Window
		exev.send_event = 1
		exev.display = W.Display

		C.XSendEvent(W.Display, W.Window, 0, C.ExposureMask, &ev)
		C.XFlush(W.Display)
	})
	return W, nil
}
Example #11
0
// Load an image from a PNG file
func Load(path string) (*Image, error) {
	var img Image

	img.Surf = C.cairo_image_surface_create_from_png(C.CString(path))
	switch C.cairo_image_surface_get_format(img.Surf) {
	case C.CAIRO_FORMAT_A8:
		img.ColorSpace = MODE_A8
	case C.CAIRO_FORMAT_RGB24:
		img.ColorSpace = MODE_RGB
	case C.CAIRO_FORMAT_ARGB32:
		img.ColorSpace = MODE_RGBA
	default:
		fmt.Println("ERROR: Format not supported")
		return nil, error("Format not supported")
	}
	img.W = int(C.cairo_image_surface_get_width(img.Surf))
	img.H = int(C.cairo_image_surface_get_height(img.Surf))
	img.Ctx = C.cairo_create(img.Surf)
	return &img, nil
}
Example #12
0
func NewSurfaceFromPNG(filename string) (*Surface, Status) {

	cstr := C.CString(filename)
	defer C.free(unsafe.Pointer(cstr))

	surfaceNative := C.cairo_image_surface_create_from_png(cstr)
	status := Status(C.cairo_surface_status(surfaceNative))
	if status != STATUS_SUCCESS {
		return nil, status
	}

	contextNative := C.cairo_create(surfaceNative)
	status = Status(C.cairo_status(contextNative))
	if status != STATUS_SUCCESS {
		return nil, status
	}

	surface := &Surface{
		surface: surfaceNative,
		context: contextNative,
	}

	return surface, STATUS_SUCCESS
}
Example #13
0
func NewSurface(format Format, width, height int) *Surface {
	s := C.cairo_image_surface_create(C.cairo_format_t(format), C.int(width), C.int(height))
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Example #14
0
// Create is a wrapper around cairo_create().
func Create(target *Surface) *Context {
	c := C.cairo_create(target.native())
	ctx := wrapContext(c)
	runtime.SetFinalizer(ctx, (*Context).destroy)
	return ctx
}
Example #15
0
func NewSurface(format Format, width, height int) *Surface {
	surface := new(Surface)
	surface.surface = C.cairo_image_surface_create(C.cairo_format_t(format), C.int(width), C.int(height))
	surface.context = C.cairo_create(surface.surface)
	return surface
}
Example #16
0
func NewSurfaceFromPNG(filename string) *Surface {
	cs := C.CString(filename)
	defer C.free(unsafe.Pointer(cs))
	s := C.cairo_image_surface_create_from_png(cs)
	return &Surface{surface: s, context: C.cairo_create(s)}
}
Example #17
0
func MakePDFStreamTextObject(writer io.Writer, width, height float64) *PDFStreamTextObject {
	var t PDFStreamTextObject
	t.surface = C.gocairo_pdf_surface_create_for_stream(unsafe.Pointer(&writer), C.double(width), C.double(height))
	t.context = C.cairo_create(t.surface)
	return &t
}