I’m using Cairo in a GTK application. It works fine drawing boxes etc. but when I try to “draw” as below I get AttributeError: 'gi.repository.cairo' object has no attribute 'FONT_SLANT_NORMAL'
not sure what you mean with “proper”, but cairo does have introspection
support:
In that specific case, the font slant is inside the FontSlant
enumeration (i.e. with Lua bindings I can access that value with cairo.FontSlant.NORMAL), so maybe it is just a syntax issue.
The cairo-1.0.gir file is only there to hold the GType for signals and properties; it does not cover the entirety of the enumerations, and it does not cover the entirety of the API. This is because GTK depends on some of the Cairo types, and thus we need a basic Cairo introspection file in order to generate GTK’s own introspection data.
Cairo simply cannot be used through introspection, because Cairo does not use GObject and the GType type system do describe the inheritance of its types. For instance, you cannot use the cairo_surface_t API with a cairo_image_surface_t via introspection.
Sorry but I need to clarify that. In another project I even proposed to
adopt the cairo approach [1], i.e. leverage gobject-introspection on a
library not based on GObject, and I need to know if there is a big
blocker I do not see right now.
I’m aware cairo is not GObject based and, AFAIK, this is the reason
cairo-gobject [2] has been written. If any enumeration is missing, this
is a bug.
I’m also aware cairo needs special care on the bindings side to recreate
the missing hierarchy (see e.g. the LGI overrides for cairo [3]) and
AFAIK this is the biggest issue. But, if implemented, I find cairo
perfectly usable with bindings. In my main public project [4] I use it
quite extensively directly in Lua on an OpenResty webserver.
After a second read, that statement sounds quite misleading: it is true that the ADG library uses cairo extensively, but much more on the C side. In the bindings side it is not used extensively: mainly matrix manipulation and enumeration types.
The main blocker is that you cannot handle type hierarchies in gobject-introspection unless you use instance types, like GObject; at most you can use GTypeInstance, but you should really only do that as a last resort.
What cairo-gobject does is exposing its own types as boxed types, not to have bindings or introspection data, but because GTK uses Cairo types in signals and properties, which require registration into the GObject type system, and the obvious way to do that is to use GBoxedType. Boxed types, though, cannot have derived types. For instance: a cairo_image_surface_t is, according to Cairo, a derived type of cairo_surface_t—i.e. you can call any function that operates on a cairo_surface_t* with a pointer to a cairo_image_surface_t; but according to GObject (and GObject-introspection), a cairo_image_surface_t and a cairo_surface_t are completely unrelated types, and they cannot be used interchangeably.
The whole point of cairo-1.0.gir is to merely provide enough types for generating the introspection data for GTK (and, of course, any other introspection data that depends on Cairo types); it is not meant to be used as the way to use the Cairo API. Any attempt at doing so is automatically and irreparably broken.
If you want to use the Cairo API you will need ad hoc native bindings.
Now, that does not mean you cannot use GObject-introspection with libraries that only expose boxed types and enumeration types; for instance, you can use introspection with Graphene. The main difference between Graphene and Cairo is that the former has a completely flat type hierarchy: every single type is independent from the others. This allows mapping plain C types to GBoxedType, and thus to provide an API that can be introspected.