Python API reference

Meta

cairocffi.Error

An alias for CairoError, for compatibility with pycairo.

Surfaces

ImageSurface

PDFSurface

PSSurface

SVGSurface

RecordingSurface

Win32PrintingSurface

Context

Matrix

Patterns

SolidPattern

SurfacePattern

Gradient

LinearGradient

RadialGradient

Fonts & text

A font is (in simple terms) a collection of shapes used to draw text. A glyph is one of these shapes. There can be multiple glyphs for the same character (alternates to be used in different contexts, for example), or a glyph can be a ligature of multiple characters. Converting text to positioned glyphs is shaping.

Cairo itself provides a “toy” text API that only does simple shaping: no ligature or kerning; one glyph per character, positioned by moving the cursor by the X and Y advance of each glyph.

It is expected that most applications will need to use Pango or a similar library in conjunction with cairo for more comprehensive font handling and text layout.

Font faces

Note

At the moment cairocffi only supports cairo’s “toy” font selection API. FontFace objects of other types could be obtained eg. from Context.get_font_face(), but they can not be instantiated directly.

ToyFontFace

ScaledFont

FontOptions

Enumerated values

Some parameters or return values in the cairo API only have a fixed, finite set of valid values. These are represented as enumerated types in C, and as integers in CFFI. Users are encouraged to use the constants defined here in the cairocffi module rather than literal integers. For example:

surface = cairocffi.ImageSurface(cairocffi.FORMAT_ARGB32, 300, 400)

Content

Used to describe the content that a Surface will contain, whether color information, alpha information (translucence vs. opacity), or both.

cairocffi.CONTENT_COLOR

The surface will hold color content only.

cairocffi.CONTENT_ALPHA

The surface will hold alpha content only.

cairocffi.CONTENT_COLOR_ALPHA

The surface will hold color and alpha content.

Pixel format

Used to identify the memory format of image data.

cairocffi.FORMAT_ARGB32

Each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green, then blue. The 32-bit quantities are stored native-endian. Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000, not 0x80ff0000.)

cairocffi.FORMAT_RGB24

Each pixel is a 32-bit quantity, with the upper 8 bits unused. Red, Green, and Blue are stored in the remaining 24 bits in that order.

cairocffi.FORMAT_A8

Each pixel is a 8-bit quantity holding an alpha value.

cairocffi.FORMAT_A1

Each pixel is a 1-bit quantity holding an alpha value. Pixels are packed together into 32-bit quantities. The ordering of the bits matches the endianess of the platform. On a big-endian machine, the first pixel is in the uppermost bit, on a little-endian machine the first pixel is in the least-significant bit.

cairocffi.FORMAT_RGB16_565

Each pixel is a 16-bit quantity with red in the upper 5 bits, then green in the middle 6 bits, and blue in the lower 5 bits.

cairocffi.FORMAT_RGB30

Like FORMAT_RGB24, but with the upper 2 bits unused and 10 bits per components.

Compositiong operator

Used to set the compositing operator for all cairo drawing operations.

The default operator is OPERATOR_OVER.

The operators marked as unbounded modify their destination even outside of the mask layer (that is, their effect is not bound by the mask layer). However, their effect can still be limited by way of clipping.

To keep things simple, the operator descriptions here document the behavior for when both source and destination are either fully transparent or fully opaque. The actual implementation works for translucent layers too. For a more detailed explanation of the effects of each operator, including the mathematical definitions, see http://cairographics.org/operators/.

cairocffi.OPERATOR_CLEAR

Clear destination layer. (bounded)

cairocffi.OPERATOR_SOURCE

Replace destination layer. (bounded)

cairocffi.OPERATOR_OVER

Draw source layer on top of destination layer. (bounded)

cairocffi.OPERATOR_IN

Draw source where there was destination content. (unbounded)

cairocffi.OPERATOR_OUT

Draw source where there was no destination content. (unbounded)

cairocffi.OPERATOR_ATOP

Draw source on top of destination content and only there.

cairocffi.OPERATOR_DEST

Ignore the source.

cairocffi.OPERATOR_DEST_OVER

Draw destination on top of source.

cairocffi.OPERATOR_DEST_IN

Leave destination only where there was source content. (unbounded)

cairocffi.OPERATOR_DEST_OUT

Leave destination only where there was no source content.

cairocffi.OPERATOR_DEST_ATOP

Leave destination on top of source content and only there. (unbounded)

cairocffi.OPERATOR_XOR

Source and destination are shown where there is only one of them.

cairocffi.OPERATOR_ADD

Source and destination layers are accumulated.

cairocffi.OPERATOR_SATURATE

Like OPERATOR_OVER, but assuming source and destination are disjoint geometries.

cairocffi.OPERATOR_MULTIPLY

Source and destination layers are multiplied. This causes the result to be at least as dark as the darker inputs. (Since 1.10)

cairocffi.OPERATOR_SCREEN

Source and destination are complemented and multiplied. This causes the result to be at least as light as the lighter inputs. (Since cairo 1.10)

cairocffi.OPERATOR_OVERLAY

Multiplies or screens, depending on the lightness of the destination color. (Since cairo 1.10)

cairocffi.OPERATOR_DARKEN

Replaces the destination with the source if it is darker, otherwise keeps the source. (Since cairo 1.10)

cairocffi.OPERATOR_LIGHTEN

Replaces the destination with the source if it is lighter, otherwise keeps the source. (Since cairo 1.10)

cairocffi.OPERATOR_COLOR_DODGE

Brightens the destination color to reflect the source color. (Since cairo 1.10)

cairocffi.OPERATOR_COLOR_BURN

Darkens the destination color to reflect the source color. (Since cairo 1.10)

cairocffi.OPERATOR_HARD_LIGHT

Multiplies or screens, dependent on source color. (Since cairo 1.10)

cairocffi.OPERATOR_SOFT_LIGHT

Darkens or lightens, dependent on source color. (Since cairo 1.10)

cairocffi.OPERATOR_DIFFERENCE

Takes the difference of the source and destination color. (Since cairo 1.10)

cairocffi.OPERATOR_EXCLUSION

Produces an effect similar to difference, but with lower contrast. (Since cairo 1.10)

cairocffi.OPERATOR_HSL_HUE

Creates a color with the hue of the source and the saturation and luminosity of the target. (Since cairo 1.10)

cairocffi.OPERATOR_HSL_SATURATION

Creates a color with the saturation of the source and the hue and luminosity of the target. Painting with this mode onto a gray area produces no change. (Since cairo 1.10)

cairocffi.OPERATOR_HSL_COLOR

Creates a color with the hue and saturation of the source and the luminosity of the target. This preserves the gray levels of the target and is useful for coloring monochrome images or tinting color images. (Since cairo 1.10)

cairocffi.OPERATOR_HSL_LUMINOSITY

Creates a color with the luminosity of the source and the hue and saturation of the target. This produces an inverse effect to OPERATOR_HSL_COLOR. (Since cairo 1.10)

Antialiasing mode

Specifies the type of antialiasing to do when rendering text or shapes.

cairocffi.ANTIALIAS_DEFAULT

Use the default antialiasing for the subsystem and target device.

cairocffi.ANTIALIAS_NONE

Use a bilevel alpha mask.

cairocffi.ANTIALIAS_GRAY

Perform single-color antialiasing.

cairocffi.ANTIALIAS_SUBPIXEL

Perform antialiasing by taking advantage of the order of subpixel elements on devices such as LCD panels.

As it is not necessarily clear from the above what advantages a particular antialias method provides, since cairo 1.12, there is also a set of hints:

cairocffi.ANTIALIAS_FAST

Allow the backend to degrade raster quality for speed.

cairocffi.ANTIALIAS_GOOD

A balance between speed and quality.

cairocffi.ANTIALIAS_BEST

A high-fidelity, but potentially slow, raster mode.

These make no guarantee on how the backend will perform its rasterisation (if it even rasterises!), nor that they have any differing effect other than to enable some form of antialiasing. In the case of glyph rendering, ANTIALIAS_FAST and ANTIALIAS_GOOD will be mapped to ANTIALIAS_GRAY, with ANTIALIAS_BEST being equivalent to ANTIALIAS_SUBPIXEL.

The interpretation of ANTIALIAS_DEFAULT is left entirely up to the backend, typically this will be similar to ANTIALIAS_GOOD.

Fill rule

Used to select how paths are filled. For both fill rules, whether or not a point is included in the fill is determined by taking a ray from that point to infinity and looking at intersections with the path. The ray can be in any direction, as long as it doesn’t pass through the end point of a segment or have a tricky intersection such as intersecting tangent to the path. (Note that filling is not actually implemented in this way. This is just a description of the rule that is applied.)

The default fill rule is FILL_RULE_WINDING.

New entries may be added in future versions.

cairocffi.FILL_RULE_WINDING

If the path crosses the ray fromleft-to-right, counts +1. If the path crosses the rayfrom right to left, counts -1. (Left and right are determined from the perspective of looking along the ray from the starting point.) If the total count is non-zero, the point will be filled.

cairocffi.FILL_RULE_EVEN_ODD

Counts the total number of intersections, without regard to the orientation of the contour. If the total number of intersections is odd, the point will be filled.

Line cap style

Specifies how to render the endpoints of the path when stroking.

The default line cap style is LINE_CAP_BUTT.

cairocffi.LINE_CAP_BUTT

Start (stop) the line exactly at the start (end) point.

cairocffi.LINE_CAP_ROUND

Use a round ending, the center of the circle is the end point.

cairocffi.LINE_CAP_SQUARE

Use squared ending, the center of the square is the end point.

Line join style

Specifies how to render the junction of two lines when stroking.

The default line join style is LINE_JOIN_MITER.

cairocffi.LINE_JOIN_MITER

Use a sharp (angled) corner, see Context.set_miter_limit().

cairocffi.LINE_JOIN_ROUND

Use a rounded join, the center of the circle is the joint point.

cairocffi.LINE_JOIN_BEVEL

Use a cut-off join, the join is cut off at half the line width from the joint point.

Font slant

Specifies variants of a font face based on their slant.

cairocffi.FONT_SLANT_NORMAL

Upright font style.

cairocffi.FONT_SLANT_ITALIC

Italic font style.

cairocffi.FONT_SLANT_OBLIQUE

Oblique font style.

Font weight

Specifies variants of a font face based on their weight.

cairocffi.FONT_WEIGHT_NORMAL

Normal font weight.

cairocffi.FONT_WEIGHT_BOLD

Bold font weight.

Subpixel order

The subpixel order specifies the order of color elements within each pixel on the display device when rendering with an antialiasing mode of ANTIALIAS_SUBPIXEL.

cairocffi.SUBPIXEL_ORDER_DEFAULT

Use the default subpixel order for for the target device.

cairocffi.SUBPIXEL_ORDER_RGB

Subpixel elements are arranged horizontally with red at the left.

cairocffi.SUBPIXEL_ORDER_BGR

Subpixel elements are arranged horizontally with blue at the left.

cairocffi.SUBPIXEL_ORDER_VRGB

Subpixel elements are arranged vertically with red at the top.

cairocffi.SUBPIXEL_ORDER_VBGR

Subpixel elements are arranged vertically with blue at the top.

Hint style

Specifies the type of hinting to do on font outlines. Hinting is the process of fitting outlines to the pixel grid in order to improve the appearance of the result. Since hinting outlines involves distorting them, it also reduces the faithfulness to the original outline shapes. Not all of the outline hinting styles are supported by all font backends.

New entries may be added in future versions.

cairocffi.HINT_STYLE_DEFAULT

Use the default hint style for font backend and target device.

cairocffi.HINT_STYLE_NONE

Do not hint outlines.

cairocffi.HINT_STYLE_SLIGHT

Hint outlines slightly to improve contrast while retaining good fidelity to the original shapes.

cairocffi.HINT_STYLE_MEDIUM

Hint outlines with medium strength giving a compromise between fidelity to the original shapes and contrast.

cairocffi.HINT_STYLE_FULL

Hint outlines to maximize contrast.

Metrics hinting mode

Specifies whether to hint font metrics; hinting font metrics means quantizing them so that they are integer values in device space. Doing this improves the consistency of letter and line spacing, however it also means that text will be laid out differently at different zoom factors.

cairocffi.HINT_METRICS_DEFAULT

Hint metrics in the default manner for the font backend and target device.

cairocffi.HINT_METRICS_OFF

Do not hint font metrics.

cairocffi.HINT_METRICS_ON

Hint font metrics.

Path operation

Used to describe the type of one portion of a path when represented as a list. See Context.copy_path() for details.

cairocffi.PATH_MOVE_TO
cairocffi.PATH_LINE_TO
cairocffi.PATH_CURVE_TO
cairocffi.PATH_CLOSE_PATH

Pattern extend

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).

Mesh patterns are not affected by the extend mode.

The default extend mode is EXTEND_NONE for SurfacePattern and EXTEND_PAD for Gradient patterns.

New entries may be added in future versions.

cairocffi.EXTEND_NONE

Pixels outside of the source pattern are fully transparent.

cairocffi.EXTEND_REPEAT

The pattern is tiled by repeating.

cairocffi.EXTEND_REFLECT

The pattern is tiled by reflecting at the edges.

cairocffi.EXTEND_PAD

Pixels outside of the pattern copy the closest pixel from the source.

Pixel filter

Used to indicate what filtering should be applied when reading pixel values from patterns. See Pattern.set_filter() for indicating the desired filter to be used with a particular pattern.

cairocffi.FILTER_FAST

A high-performance filter, with quality similar to FILTER_NEAREST.

cairocffi.FILTER_GOOD

A reasonable-performance filter, with quality similar to FILTER_BILINEAR.

cairocffi.FILTER_BEST

The highest-quality available, performance may not be suitable for interactive use.

cairocffi.FILTER_NEAREST

Nearest-neighbor filtering.

cairocffi.FILTER_BILINEAR

Linear interpolation in two dimensions.

cairocffi.FILTER_GAUSSIAN

This filter value is currently unimplemented, and should not be used in current code.

PDF version

Used to describe the version number of the PDF specification that a generated PDF file will conform to.

cairocffi.PDF_VERSION_1_4

The version 1.4 of the PDF specification.

cairocffi.PDF_VERSION_1_5

The version 1.5 of the PDF specification.

PDF outline

Used to specify the attributes of an outline item. These flags may be bitwise-or’d to produce any combination of flags.

New in cairo 1.16.

New in cairocffi 0.9.

cairocffi.PDF_OUTLINE_FLAG_OPEN

The outline item defaults to open in the PDF viewer.

cairocffi.PDF_OUTLINE_FLAG_BOLD

The outline item is displayed by the viewer in bold text.

cairocffi.PDF_OUTLINE_FLAG_ITALIC

The outline item is displayed by the viewer in italic text.

There’s also a constant used to specify the root level for outlines.

cairocffi.PDF_OUTLINE_ROOT

Root level for outlines.

PDF metadata

Used to specify the metadata to set.

New in cairo 1.16.

New in cairocffi 0.9.

cairocffi.PDF_METADATA_TITLE

The document title.

cairocffi.PDF_METADATA_AUTHOR

The document author.

cairocffi.PDF_METADATA_SUBJECT

The document subject.

cairocffi.PDF_METADATA_KEYWORDS

The document keywords.

cairocffi.PDF_METADATA_CREATOR

The document creator.

cairocffi.PDF_METADATA_CREATE_DATE

The document creation date.

cairocffi.PDF_METADATA_MOD_DATE

The document modification date.

PostScript level

Used to describe the language level of the PostScript Language Reference that a generated PostScript file will conform to.

cairocffi.PS_LEVEL_2

The language level 2 of the PostScript specification.

cairocffi.PS_LEVEL_3

The language level 3 of the PostScript specification.

SVG version

Used to describe the version number of the SVG specification that a generated SVG file will conform to.

cairocffi.SVG_VERSION_1_1

The version 1.1 of the SVG specification.

cairocffi.SVG_VERSION_1_2

The version 1.2 of the SVG specification.

SVG unit

Used to describe the units valid for coordinates and lengths in the SVG specification.

See also:

New in cairo 1.16.

New in cairocffi 0.9.

cairocffi.SVG_UNIT_USER

User unit, a value in the current coordinate system. If used in the root element for the initial coordinate systems it corresponds to pixels.

cairocffi.SVG_UNIT_EM

The size of the element’s font.

cairocffi.SVG_UNIT_EX

The x-height of the element’s font.

cairocffi.SVG_UNIT_PX

Pixels (1px = 1/96th of 1in).

cairocffi.SVG_UNIT_IN

Inches (1in = 2.54cm = 96px).

cairocffi.SVG_UNIT_CM

Centimeters (1cm = 96px/2.54).

cairocffi.SVG_UNIT_MM

Millimeters (1mm = 1/10th of 1cm).

cairocffi.SVG_UNIT_PT

Points (1pt = 1/72th of 1in).

cairocffi.SVG_UNIT_PC

Picas (1pc = 1/6th of 1in).

cairocffi.SVG_UNIT_PERCENT

Percent, a value that is some fraction of another reference value.

Cluster flags

Specifies properties of a text cluster mapping. Flags are integer values representing a bit field.

cairocffi.TEXT_CLUSTER_FLAG_BACKWARD = 0x00000001

The clusters in the cluster array map to glyphs in the glyph array from end to start. (Since 1.8)