Peta::FFI::Cairo#
Native reimplementation of the Perl Cairo module against libcairo.so.2.
The upstream Cairo CPAN distribution (v1.109) is a thin XS shim over the cairo 2d graphics C library. This module reproduces its observable Perl behavior (the ”phenotype“) by calling into libcairo.so.2 directly through dlopen/dlsym, without a build-time C binding.
Although the Rust source lives under Peta::FFI::Cairo, the Perl-visible packages are the real Cairo, Cairo::Context, Cairo::Surface, Cairo::ImageSurface, etc. - so ported cairo tests and existing Perl code see the same API they would from the XS module.
Architecture: per-package plug-in modules#
mod.rs is the FOUNDATION: the shared dlopen handle, the string<->int enum conversion tables, the blessing/refcount contract, the struct-as-hashref marshallers, the status-to-$@ croak helper, and the base Cairo:: version/capability subs. Each Perl-visible object package (Cairo::Context, Cairo::Surface, Cairo::Pattern, …) lives in its OWN sibling file Cairo/<Pkg>.rs and plugs into boot() through a NARROW, STABLE contract so that the seven package files can be authored in PARALLEL without touching shared state.
Crucially, each package resolves its OWN cairo symbols locally (via cairo_dlsym over the shared cairo_handle), so a package agent never edits a central symbol table. The foundation resolves only the two base-namespace symbols (cairo_version, cairo_version_string).
Refcount contract (census section 4)#
Every cairo object is a blessed scalar ref whose referent’s IV holds the raw cairo_*_t*. Constructors (create*) return an object with a reference already owned (+1), so we bless the pointer directly. Borrowed getters (get_source, get_target, pop_group) must take an extra reference (cairo_*_reference) before blessing. Every DESTROY drops exactly one reference via cairo_*_destroy.
Modules#
Peta::FFI::Cairo::Context—Cairo::Contextpackage plug-in.Peta::FFI::Cairo::Font—Cairo::Fontfamily plug-in: FontFace, ToyFontFace, ScaledFont,Peta::FFI::Cairo::Matrix—Cairo::Matrixpackage plug-in.Peta::FFI::Cairo::Path—Cairo::Pathpackage plug-in - the tied-array live view ontocairo_path_t*.Peta::FFI::Cairo::Pattern—Cairo::Patternfamily plug-in (CairoPattern.xs phenotype).Peta::FFI::Cairo::Region—Cairo::Regionpackage plug-in.Peta::FFI::Cairo::Surface—Cairo::Surfacefamily package plug-in.
Functions#
Other Functions#
lib_version#
Cairo::LIB_VERSION / Cairo::version / Cairo::lib_version -> the compile-time-encoded cairo version integer of the loaded library. Note: the Perl-level Cairo::VERSION dispatcher (2-arg -> version check, else -> LIB_VERSION) lives in the .pm; here we provide the underlying LIB_VERSION that it and the tests call.
version_string#
Cairo::version_string / Cairo::lib_version_string -> the cairo version as a string (e.g. ”1.18.4“).
version_encode#
Cairo::VERSION_ENCODE / Cairo::LIB_VERSION_ENCODE (major,minor,micro) -> encoded int, mirroring CAIRO_VERSION_ENCODE. Called as either a function Cairo::VERSION_ENCODE(1,2,0) or class method Cairo->VERSION_ENCODE(1,2,0); the class-method form prepends the invocant, so we read the LAST three numeric args.
has_png#
Cairo::HAS_PNG_FUNCTIONS -> whether the loaded libcairo exposes PNG output. Probed by dlsym presence (census runtime-env note), not a compile-time ifdef. The symbol itself lives in the Surface package, so we probe it directly rather than through a central table field.