Module org.freedesktop.cairo
Package org.freedesktop.cairo
package org.freedesktop.cairo
This package contains Java language bindings for the cairo
graphics library using the JEP-454 Panama FFI. The bindings are based on cairo 1.18
and work with JDK 22 or later.
These language bindings were primarily created as a companion to the GObject-based language
bindings for Gtk and GStreamer generated with
Java-GI, but they can also be used independently.
The dependency on io.github.jwharm.javagi:glib
is optional. There are no other external
dependencies.
Overview
Java API
In general, the Java bindings match the cairo C API, but with a Java "coding style". C structs likecairo_t
, cairo_surface_t
and cairo_matrix_t
are modeled with Java Proxy
classes like Context
, Surface
and Matrix
, and all flags and enumerations are
available as Java enums. The proxy classes inherit when applicable: RadialGradient
extends
Gradient
, which extends Pattern
, and ImageSurface
extends Surface
. Types,
functions and parameters follow Java (camel case) naming practices, so
cairo_move_to(*cr, x, y)
becomes cr.moveTo(x, y)
. Out-parameters in the C API
are mapped to return values in Java. Multiple out parameters (like coordinates) are mapped to a
Point
or Rectangle
return type in Java.
Resource allocation and disposal
Resources are allocated and deallocated automatically, so there is no need to manually dispose cairo resources in Java. However, please be aware that the disposal of proxy objects (like Context, surfaces, matrices and patterns) is initiated by the Java garbage collector, which does not know about the native resources, and might wait an indefinite amount of time before the objects are effectively disposed. Therefore, manual calls todestroy()
are still possible in case the
normal cleanup during GC is not sufficient to prevent resource exhaustion.
Error handling
Cairo status codes are checked in the language binding, and throw exceptions (IllegalStateException, IllegalArgumentException or IOException) with the detailed status description (fromcairo_status_to_string()
). The exceptions are documented in the
Javadoc, except for the CAIRO_STATUS_NO_MEMORY
status, which is not documented and will
throw a RuntimeException if it occurs. If your application consumes a lot of memory, add try-catch
blocks for this situation where applicable.
Other notable features
Some other features that the language bindings offer:- In the
Context
,Surface
andPattern
classes (likeMesh
), methods that returnvoid
in the C API, returnthis
in Java, to allow method chaining. - The
Path
class is iterable, and path traversal is implemented withPathElement
objects. ThePathElement
type is a sealed interface implemented by a record type for every path operation. They can be iterated and processed with record patterns (JEP 440). See thePath
class javadoc for example code. - The
cairo_set_user_data()
andcairo_get_user_data()
functions (to attach custom data to a cairo struct) are available in Java, with a twist. You can callsetUserData()
to attach any Java object instance, andgetUserData()
to get it back. Objects that can be marshaled to a native memory segment (primitive types, memory segments, and otherProxy
objects) will be attached to the native cairo struct. Other types will only be attached to the Java object and will not be passed to cairo itself. - I/O operations in cairo that are designed to work with streams accept Java
InputStream
andOutputStream
parameters. - The
Surface
andDevice
classes implementAutoCloseable
and can be used in try-with-resources blocks. (Theclose()
method calls the Ccairo_..._finish()
function.) - The cairo Script surface has been split into a
Script
class that inherits fromDevice
, and aScriptSurface
class that inherits fromSurface
. - The functions for reading and comparing cairo version information are available in Java as static
methods in the
Cairo
class.
-
ClassDescriptionSpecifies the type of antialiasing to do when rendering text or shapes.This class contains global declarations that do not belong in a specific cairo class definition.A circle defined by the x and y coordinates of the center and the radius.Specifies if color fonts are to be rendered using the color glyphs or outline glyphs.Content is used to describe the content that a surface will contain, whether color information, alpha information (translucence vs.The cairo drawing context.DestroyFunc the type of function which is called when a data element is destroyed.Devices are the abstraction Cairo employs for the rendering system used by a
Surface
.DeviceType is used to describe the type of a given device.Dither is an intentionally applied form of noise used to randomize quantization error, preventing large-scale patterns such as color banding in images (e.g.Extend is used to describe how pattern color/alpha will be determined for areas "outside" the pattern's natural area, (for example, outside the surface bounds or outside the gradient geometry).cairo_fill_rule_t is used to select how paths are filled.Filter is used to indicate what filtering should be applied when reading pixel values from patterns.The FontExtents structure stores metric information for a font.The base class for font faces.How a font should be rendered.Specifies variants of a font face based on their slant.FontType is used to describe the type of a given font face or scaled font.Specifies variants of a font face based on their weight.Used to identify the memory format of image data.Provides a FontFace for FreeType.Provides a ScaledFont from the FreeType font backend.A set of synthesis options to control how FreeType renders the glyphs for a particular font face.The Glyph structure holds information about a single glyph when drawing or measuring text.TheGlyphs
class represents an array of glyphs.Base class for (linear or radial) gradient patterns.Specifies whether to hint font metrics; hinting font metrics means quantizing them so that they are integer values in device space.Specifies the type of hinting to do on font outlines.Image Surfaces — Rendering to memory buffers.A linear gradient pattern.Specifies how to render the endpoints of the path when stroking.Specifies how to render the junction of two lines when stroking.Generic matrix operations.A mesh pattern.MIME types defined as constants in Cairo.cairo_operator_t is used to set the compositing operator for all cairo drawing operations.A data structure for holding a path.Describes the type of one portion of a path when represented as aPath
.The PathElement interface is a sealed type that models Path elements.APathDataType.CLOSE_PATH
path elementAPathDataType.CURVE_TO
path elementAPathDataType.LINE_TO
path elementAPathDataType.MOVE_TO
path elementSources for drawing.PatternType is used to describe the type of a given pattern.PDFMetadata
is used by the cairo_pdf_surface_set_metadata() function specify the metadata to set.PDFOutlineFlags
is used by the cairo_pdf_surface_add_outline() function specify the attributes of an outline item.The PDF surface is used to render cairo graphics to Adobe PDF files and is a multi-page vector surface backend.PDFVersion
is used to describe the version number of the PDF specification that a generated PDF file will conform to.A Point defined by its x and y coordinates.PSLevel is used to describe the language level of the PostScript Language Reference that a generated PostScript file will conform to.The PostScript surface is used to render cairo graphics to Adobe PostScript files and is a multi-page vector surface backend.A radial gradient pattern.A user pattern providing raster data.RasterSourceAcquireFunc is the type of function which is called when a pattern is being rendered from.RasterSourceCopyFunc is the type of function which is called when the pattern gets copied as a normal part of rendering.RasterSourceFinishFunc is the type of function which is called when the pattern (or a copy thereof) is no longer required.RasterSourceReleaseFunc is the type of function which is called when the pixel data is no longer being access by the pattern for the rendering operation.RasterSourceSnapshotFunc is the type of function which is called when the pixel data needs to be preserved for later use during printing.ReadFunc is the type of function which is called when a backend needs to read data from an input stream.A recording surface records all drawing operations.A rectangle.A data structure for holding a rectangle.A data structure for holding a rectangle with integer coordinates.A data structure for holding a dynamically allocated array of rectangles.ARegion
represents a set of integer-aligned rectangles.Used as the return value for cairo_region_contains_rectangle().A color with red, green, blue and alpha componentsFont face at particular size and options.Output device for use with aScriptSurface
.A set of script output variants.The script surface provides the ability to render to a native script that matches the cairo drawing model.A pattern with a solid (uniform) color.Status is used to indicate errors that can occur when using Cairo.The subpixel order specifies the order of color elements within each pixel on the display device when rendering with an antialiasing mode ofAntialias.SUBPIXEL
.Base class for surfaces.A surface that exists solely to watch what another surface is doing.A generic callback function for surface operations.Whether operations should be recorded.A pattern based on a surface (an image).SurfaceType is used to describe the type of a given surface.The SVG surface is used to render cairo graphics to SVG files and is a multi-page vector surface backend.SVGUnit is used to describe the units valid for coordinates and lengths in the SVG specification.SVGVersion is used to describe the version number of the SVG specification that a generated SVG file will conform to.The "tee" surface supports redirecting all its input to multiple surfaces.The TextCluster structure holds information about a single text cluster.Specifies properties of a text cluster mapping.The TextExtents structure stores the extents of a single glyph or a string of glyphs in user-space coordinates.ToyFontFace is used in cairo's toy text API.UserDataKey is used for attaching user data to cairo data structures.Font support with font data provided by the user.Provides a ScaledFont from the User font backend.UserScaledFontInitFunc
is the type of function which is called when a scaled-font needs to be created for a user font-face.UserScaledFontRenderGlyphFunc is the type of function which is called when a user scaled-font needs to render a glyph.UserScaledFontTextToGlyphsFunc is the type of function which is called to convert input text to an array of glyphs.UserScaledFontUnicodeToGlyphFunc is the type of function which is called to convert an input Unicode character to a single glyph.WriteFunc is the type of function which is called when a backend needs to write data to an output stream.