Skip to content

API Reference / @gtkx/cairo / Context

Class: Context ​

Defined in: context.d.ts:14

A cairo drawing context (cairo_t): the object every drawing operation goes through, holding the current path, source, transformation, clip and font state for one target surface. GTK hands one to draw callbacks and snapshots, and new Context(surface) creates one for offscreen drawing.

Constructors ​

Constructor ​

new Context(target): Context

Defined in: context.d.ts:20

Creates a context drawing onto target, throwing when it cannot be created, such as on a finished surface.

Parameters ​

target ​

Surface

Returns ​

Context

Properties ​

__type__ ​

__type__: bigint

Defined in: context.d.ts:18

GType of CairoContext, the boxed type this class is registered under.

Methods ​

appendPath() ​

appendPath(data): void

Defined in: context.d.ts:220

Appends segments, as returned by copyPath, to the current path.

Parameters ​

data ​

PathData[]

Returns ​

void


arc() ​

arc(xc, yc, radius, angle1, angle2): void

Defined in: context.d.ts:34

Adds a circular arc centred on (xc, yc), drawn clockwise from angle1 to angle2.

Parameters ​

xc ​

number

yc ​

number

radius ​

number

angle1 ​

number

angle2 ​

number

Returns ​

void


arcNegative() ​

arcNegative(xc, yc, radius, angle1, angle2): void

Defined in: context.d.ts:36

Adds a circular arc centred on (xc, yc), drawn counter-clockwise from angle1 to angle2.

Parameters ​

xc ​

number

yc ​

number

radius ​

number

angle1 ​

number

angle2 ​

number

Returns ​

void


clip() ​

clip(): void

Defined in: context.d.ts:58

Intersects the clip with the current path and clears the path.

Returns ​

void


clipExtents() ​

clipExtents(): Extents

Defined in: context.d.ts:152

Returns the bounding box of the current clip.

Returns ​

Extents


clipPreserve() ​

clipPreserve(): void

Defined in: context.d.ts:60

Intersects the clip with the current path and keeps the path.

Returns ​

void


closePath() ​

closePath(): void

Defined in: context.d.ts:40

Closes the current sub-path with a line back to its start.

Returns ​

void


copyClipRectangleList() ​

copyClipRectangleList(): RectangleData[]

Defined in: context.d.ts:162

Returns the current clip as a list of rectangles in user space.

Returns ​

RectangleData[]


copyPage() ​

copyPage(): void

Defined in: context.d.ts:136

Emits the current page without clearing it, on surfaces that support pages.

Returns ​

void


copyPath() ​

copyPath(): PathData[]

Defined in: context.d.ts:216

Returns a copy of the current path as segments.

Returns ​

PathData[]


copyPathFlat() ​

copyPathFlat(): PathData[]

Defined in: context.d.ts:218

Returns a copy of the current path with curves flattened into line segments.

Returns ​

PathData[]


curveTo() ​

curveTo(x1, y1, x2, y2, x3, y3): void

Defined in: context.d.ts:32

Adds a cubic Bézier curve from the current point through two control points to (x3, y3).

Parameters ​

x1 ​

number

y1 ​

number

x2 ​

number

y2 ​

number

x3 ​

number

y3 ​

number

Returns ​

void


deviceToUser() ​

deviceToUser(x, y): Point

Defined in: context.d.ts:180

Maps a point from device space to user space.

Parameters ​

x ​

number

y ​

number

Returns ​

Point


deviceToUserDistance() ​

deviceToUserDistance(dx, dy): Distance

Defined in: context.d.ts:182

Maps a distance from device space to user space, ignoring translation.

Parameters ​

dx ​

number

dy ​

number

Returns ​

Distance


fill() ​

fill(): void

Defined in: context.d.ts:50

Fills the current path with the current fill rule and clears it.

Returns ​

void


fillExtents() ​

fillExtents(): Extents

Defined in: context.d.ts:150

Returns the bounding box a fill of the current path would cover.

Returns ​

Extents


fillPreserve() ​

fillPreserve(): void

Defined in: context.d.ts:52

Fills the current path and keeps it.

Returns ​

void


fontExtents() ​

fontExtents(): FontExtents

Defined in: context.d.ts:124

Returns the metrics of the current font.

Returns ​

FontExtents


getAntialias() ​

getAntialias(): Antialias

Defined in: context.d.ts:132

Returns the antialiasing mode.

Returns ​

Antialias


getCurrentPoint() ​

getCurrentPoint(): Point | null

Defined in: context.d.ts:144

Returns the current point of the path, or null when there is none.

Returns ​

Point | null


getDash() ​

getDash(): DashPattern

Defined in: context.d.ts:86

Returns the dash pattern and its offset.

Returns ​

DashPattern


getDashCount() ​

getDashCount(): number

Defined in: context.d.ts:84

Returns how many lengths the dash pattern has, zero for a solid line.

Returns ​

number


getFillRule() ​

getFillRule(): FillRule

Defined in: context.d.ts:98

Returns the fill rule.

Returns ​

FillRule


getFontFace() ​

getFontFace(): FontFace

Defined in: context.d.ts:200

Returns the current font face, wrapped as its concrete class.

Returns ​

FontFace


getFontMatrix() ​

getFontMatrix(): Matrix

Defined in: context.d.ts:204

Returns the matrix mapping font space to user space.

Returns ​

Matrix


getFontOptions() ​

getFontOptions(): FontOptions

Defined in: context.d.ts:128

Returns a copy of the font rendering options set on the context.

Returns ​

FontOptions


getGroupTarget() ​

getGroupTarget(): Surface

Defined in: context.d.ts:196

Returns the surface drawing currently goes to: the group surface inside a group, else the target.

Returns ​

Surface


getLineCap() ​

getLineCap(): LineCap

Defined in: context.d.ts:76

Returns how the ends of stroked lines are drawn.

Returns ​

LineCap


getLineJoin() ​

getLineJoin(): LineJoin

Defined in: context.d.ts:80

Returns how the corners of stroked lines are joined.

Returns ​

LineJoin


getLineWidth() ​

getLineWidth(): number

Defined in: context.d.ts:72

Returns the width of stroked lines.

Returns ​

number


getMatrix() ​

getMatrix(): Matrix

Defined in: context.d.ts:170

Returns the transformation from user space to device space.

Returns ​

Matrix


getMiterLimit() ​

getMiterLimit(): number

Defined in: context.d.ts:90

Returns the miter limit.

Returns ​

number


getOperator() ​

getOperator(): Operator

Defined in: context.d.ts:112

Returns the compositing operator.

Returns ​

Operator


getReferenceCount() ​

getReferenceCount(): number

Defined in: context.d.ts:186

Returns the reference count of the underlying cairo_t.

Returns ​

number


getScaledFont() ​

getScaledFont(): ScaledFont

Defined in: context.d.ts:208

Returns the scaled font text operations currently use.

Returns ​

ScaledFont


getSource() ​

getSource(): Pattern

Defined in: context.d.ts:146

Returns the current source pattern, wrapped as its concrete class.

Returns ​

Pattern


getTarget() ​

getTarget(): Surface

Defined in: context.d.ts:138

Returns the surface the context was created for, wrapped as its concrete class.

Returns ​

Surface


getTolerance() ​

getTolerance(): number

Defined in: context.d.ts:94

Returns the tolerance used when curves are flattened.

Returns ​

number


glyphExtents() ​

glyphExtents(glyphs): TextExtents

Defined in: context.d.ts:214

Measures positioned glyphs with the current font.

Parameters ​

glyphs ​

CairoGlyph[]

Returns ​

TextExtents


glyphPath() ​

glyphPath(glyphs): void

Defined in: context.d.ts:212

Adds the outlines of positioned glyphs to the current path.

Parameters ​

glyphs ​

CairoGlyph[]

Returns ​

void


hasCurrentPoint() ​

hasCurrentPoint(): boolean

Defined in: context.d.ts:142

Returns whether the path has a current point.

Returns ​

boolean


identityMatrix() ​

identityMatrix(): void

Defined in: context.d.ts:174

Resets the transformation to the identity.

Returns ​

void


inClip() ​

inClip(x, y): boolean

Defined in: context.d.ts:160

Returns whether (x, y) lies inside the current clip.

Parameters ​

x ​

number

y ​

number

Returns ​

boolean


inFill() ​

inFill(x, y): boolean

Defined in: context.d.ts:158

Returns whether (x, y) lies inside the area a fill of the current path would cover.

Parameters ​

x ​

number

y ​

number

Returns ​

boolean


inStroke() ​

inStroke(x, y): boolean

Defined in: context.d.ts:156

Returns whether (x, y) lies inside the area a stroke of the current path would cover.

Parameters ​

x ​

number

y ​

number

Returns ​

boolean


lineTo() ​

lineTo(x, y): void

Defined in: context.d.ts:24

Adds a straight line from the current point to (x, y).

Parameters ​

x ​

number

y ​

number

Returns ​

void


mask() ​

mask(pattern): void

Defined in: context.d.ts:164

Paints the current source using the alpha channel of pattern as a mask.

Parameters ​

pattern ​

Pattern

Returns ​

void


maskSurface() ​

maskSurface(surface, x, y): void

Defined in: context.d.ts:166

Paints the current source using the alpha channel of surface, placed at (x, y), as a mask.

Parameters ​

surface ​

Surface

x ​

number

y ​

number

Returns ​

void


moveTo() ​

moveTo(x, y): void

Defined in: context.d.ts:22

Begins a new sub-path at (x, y), which becomes the current point.

Parameters ​

x ​

number

y ​

number

Returns ​

void


newPath() ​

newPath(): void

Defined in: context.d.ts:42

Clears the current path.

Returns ​

void


newSubPath() ​

newSubPath(): void

Defined in: context.d.ts:44

Begins a new sub-path without a current point, so the next arc needs no leading moveTo.

Returns ​

void


paint() ​

paint(): void

Defined in: context.d.ts:54

Paints the current source everywhere inside the clip.

Returns ​

void


paintWithAlpha() ​

paintWithAlpha(alpha): void

Defined in: context.d.ts:56

Paints the current source everywhere inside the clip, faded by alpha.

Parameters ​

alpha ​

number

Returns ​

void


pathExtents() ​

pathExtents(): Extents

Defined in: context.d.ts:154

Returns the bounding box of the current path.

Returns ​

Extents


popGroup() ​

popGroup(): Pattern

Defined in: context.d.ts:192

Ends the current group and returns what was drawn into it as a pattern.

Returns ​

Pattern


popGroupToSource() ​

popGroupToSource(): void

Defined in: context.d.ts:194

Ends the current group and installs what was drawn into it as the source.

Returns ​

void


pushGroup() ​

pushGroup(): void

Defined in: context.d.ts:188

Redirects drawing to an intermediate surface until popGroup or popGroupToSource.

Returns ​

void


pushGroupWithContent() ​

pushGroupWithContent(content): void

Defined in: context.d.ts:190

Redirects drawing to an intermediate surface of the given content.

Parameters ​

content ​

Content

Returns ​

void


rectangle() ​

rectangle(x, y, width, height): void

Defined in: context.d.ts:38

Adds a closed rectangular sub-path.

Parameters ​

x ​

number

y ​

number

width ​

number

height ​

number

Returns ​

void


relCurveTo() ​

relCurveTo(dx1, dy1, dx2, dy2, dx3, dy3): void

Defined in: context.d.ts:30

Adds a cubic Bézier curve whose control points and end point are offsets from the current point.

Parameters ​

dx1 ​

number

dy1 ​

number

dx2 ​

number

dy2 ​

number

dx3 ​

number

dy3 ​

number

Returns ​

void


relLineTo() ​

relLineTo(dx, dy): void

Defined in: context.d.ts:28

Adds a straight line from the current point to the point offset by (dx, dy).

Parameters ​

dx ​

number

dy ​

number

Returns ​

void


relMoveTo() ​

relMoveTo(dx, dy): void

Defined in: context.d.ts:26

Begins a new sub-path offset from the current point by (dx, dy).

Parameters ​

dx ​

number

dy ​

number

Returns ​

void


resetClip() ​

resetClip(): void

Defined in: context.d.ts:62

Removes the clip, so drawing reaches the whole surface again.

Returns ​

void


restore() ​

restore(): void

Defined in: context.d.ts:102

Restores the drawing state saved by the matching save.

Returns ​

void


rotate() ​

rotate(angle): void

Defined in: context.d.ts:108

Rotates user space by angle radians.

Parameters ​

angle ​

number

Returns ​

void


save() ​

save(): void

Defined in: context.d.ts:100

Pushes the current drawing state (source, transformation, clip, line and font settings) onto a stack.

Returns ​

void


scale() ​

scale(sx, sy): void

Defined in: context.d.ts:106

Scales user space by sx and sy.

Parameters ​

sx ​

number

sy ​

number

Returns ​

void


selectFontFace() ​

selectFontFace(family, slant, weight): void

Defined in: context.d.ts:114

Selects a toy font face by family name, slant and weight.

Parameters ​

family ​

string

slant ​

FontSlant

weight ​

FontWeight

Returns ​

void


setAntialias() ​

setAntialias(antialias): void

Defined in: context.d.ts:130

Sets the antialiasing mode of the rasterizer.

Parameters ​

antialias ​

Antialias

Returns ​

void


setDash() ​

setDash(dashes, offset): void

Defined in: context.d.ts:82

Sets the dash pattern of strokes as alternating on and off lengths; an empty list draws solid lines.

Parameters ​

dashes ​

number[]

offset ​

number

Returns ​

void


setFillRule() ​

setFillRule(fillRule): void

Defined in: context.d.ts:96

Sets the rule deciding which areas a self-intersecting path fills.

Parameters ​

fillRule ​

FillRule

Returns ​

void


setFontFace() ​

setFontFace(fontFace): void

Defined in: context.d.ts:198

Sets the font face used by text operations.

Parameters ​

fontFace ​

FontFace

Returns ​

void


setFontMatrix() ​

setFontMatrix(matrix): void

Defined in: context.d.ts:202

Sets the matrix mapping font space to user space, which sizes and shapes the glyphs.

Parameters ​

matrix ​

Matrix

Returns ​

void


setFontOptions() ​

setFontOptions(options): void

Defined in: context.d.ts:126

Sets the font rendering options merged over the surface's defaults.

Parameters ​

options ​

FontOptions

Returns ​

void


setFontSize() ​

setFontSize(size): void

Defined in: context.d.ts:116

Sets the font size in user space, replacing the font matrix with a uniform scale.

Parameters ​

size ​

number

Returns ​

void


setLineCap() ​

setLineCap(lineCap): void

Defined in: context.d.ts:74

Sets how the ends of stroked lines are drawn.

Parameters ​

lineCap ​

LineCap

Returns ​

void


setLineJoin() ​

setLineJoin(lineJoin): void

Defined in: context.d.ts:78

Sets how the corners of stroked lines are joined.

Parameters ​

lineJoin ​

LineJoin

Returns ​

void


setLineWidth() ​

setLineWidth(width): void

Defined in: context.d.ts:70

Sets the width of stroked lines in user space.

Parameters ​

width ​

number

Returns ​

void


setMatrix() ​

setMatrix(matrix): void

Defined in: context.d.ts:168

Replaces the transformation from user space to device space.

Parameters ​

matrix ​

Matrix

Returns ​

void


setMiterLimit() ​

setMiterLimit(limit): void

Defined in: context.d.ts:88

Sets the miter limit above which a mitered join is drawn beveled.

Parameters ​

limit ​

number

Returns ​

void


setOperator() ​

setOperator(op): void

Defined in: context.d.ts:110

Sets the compositing operator used by drawing operations.

Parameters ​

op ​

Operator

Returns ​

void


setScaledFont() ​

setScaledFont(scaledFont): void

Defined in: context.d.ts:206

Replaces the font face, font matrix and font options with those of scaledFont.

Parameters ​

scaledFont ​

ScaledFont

Returns ​

void


setSource() ​

setSource(pattern): void

Defined in: context.d.ts:68

Sets the source pattern drawing operations paint with.

Parameters ​

pattern ​

Pattern

Returns ​

void


setSourceRgb() ​

setSourceRgb(red, green, blue): void

Defined in: context.d.ts:64

Sets the source to an opaque color with components in the 0 to 1 range.

Parameters ​

red ​

number

green ​

number

blue ​

number

Returns ​

void


setSourceRgba() ​

setSourceRgba(red, green, blue, alpha): void

Defined in: context.d.ts:66

Sets the source to a translucent color with components in the 0 to 1 range.

Parameters ​

red ​

number

green ​

number

blue ​

number

alpha ​

number

Returns ​

void


setSourceSurface() ​

setSourceSurface(surface, x, y): void

Defined in: context.d.ts:140

Sets the source to surface placed with its origin at (x, y) in user space.

Parameters ​

surface ​

Surface

x ​

number

y ​

number

Returns ​

void


setTolerance() ​

setTolerance(tolerance): void

Defined in: context.d.ts:92

Sets the tolerance used when curves are flattened into line segments.

Parameters ​

tolerance ​

number

Returns ​

void


showGlyphs() ​

showGlyphs(glyphs): void

Defined in: context.d.ts:210

Draws positioned glyphs with the current font.

Parameters ​

glyphs ​

CairoGlyph[]

Returns ​

void


showPage() ​

showPage(): void

Defined in: context.d.ts:134

Emits the current page and clears it, on surfaces that support pages.

Returns ​

void


showText() ​

showText(text): void

Defined in: context.d.ts:118

Draws text with the current font at the current point.

Parameters ​

text ​

string

Returns ​

void


showTextGlyphs() ​

showTextGlyphs(text, glyphs, clusters, clusterFlags): void

Defined in: context.d.ts:226

Draws glyphs together with the text and clusters they came from, so the backend can keep the text.

Parameters ​

text ​

string

glyphs ​

CairoGlyph[]

clusters ​

CairoTextCluster[]

clusterFlags ​

1

Returns ​

void


status() ​

status(): Status

Defined in: context.d.ts:184

Returns the error status of the context, Status.SUCCESS while every operation so far succeeded.

Returns ​

Status


stroke() ​

stroke(): void

Defined in: context.d.ts:46

Strokes the current path with the current line settings and clears it.

Returns ​

void


strokeExtents() ​

strokeExtents(): Extents

Defined in: context.d.ts:148

Returns the bounding box a stroke of the current path would cover.

Returns ​

Extents


strokePreserve() ​

strokePreserve(): void

Defined in: context.d.ts:48

Strokes the current path and keeps it.

Returns ​

void


tagBegin() ​

tagBegin(tagName, attributes): void

Defined in: context.d.ts:222

Opens a tagged structure, such as a link or a destination, on surfaces that record tags.

Parameters ​

tagName ​

string

attributes ​

string

Returns ​

void


tagEnd() ​

tagEnd(tagName): void

Defined in: context.d.ts:224

Closes the tagged structure opened by the matching tagBegin.

Parameters ​

tagName ​

string

Returns ​

void


textExtents() ​

textExtents(text): TextExtents

Defined in: context.d.ts:122

Measures text with the current font.

Parameters ​

text ​

string

Returns ​

TextExtents


textPath() ​

textPath(text): void

Defined in: context.d.ts:120

Adds the outlines of text to the current path.

Parameters ​

text ​

string

Returns ​

void


transform() ​

transform(matrix): void

Defined in: context.d.ts:172

Applies matrix before the existing transformation.

Parameters ​

matrix ​

Matrix

Returns ​

void


translate() ​

translate(tx, ty): void

Defined in: context.d.ts:104

Moves the user-space origin by (tx, ty).

Parameters ​

tx ​

number

ty ​

number

Returns ​

void


userToDevice() ​

userToDevice(x, y): Point

Defined in: context.d.ts:176

Maps a point from user space to device space.

Parameters ​

x ​

number

y ​

number

Returns ​

Point


userToDeviceDistance() ​

userToDeviceDistance(dx, dy): Distance

Defined in: context.d.ts:178

Maps a distance from user space to device space, ignoring translation.

Parameters ​

dx ​

number

dy ​

number

Returns ​

Distance


create() ​

static create(target): Context

Defined in: context.d.ts:16

Creates a context drawing onto target, the same as new Context(target).

Parameters ​

target ​

Surface

Returns ​

Context