Ejemplo n.º 1
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
}
Ejemplo n.º 2
0
func NewSurfaceFromPNG(fileName string) (*Surface, error) {

	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, ErrorStatus(status)
	}

	return &Surface{surfaceNative}, nil
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
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
}
Ejemplo n.º 5
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
}
Ejemplo n.º 6
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)}
}