Context is the main object used when drawing with cairo. To draw with cairo,
you create a Context, set the target surface, and drawing options for the
Context, create shapes with functions like moveTo(double, double)
and lineTo(double, double)
, and then draw shapes with
stroke()
or fill()
.
Context's can be pushed to a stack via save()
. They may then safely
be changed, without losing the current state. Use restore()
to
restore to the saved state.
The functions with text in their name form cairo's toy text API. The toy API takes UTF-8 encoded text and is limited in its functionality to rendering simple left-to-right text with no advanced features. That means for example that most complex scripts like Hebrew, Arabic, and Indic scripts are out of question. No kerning or correct positioning of diacritical marks either. The font selection is pretty limited too and doesn't handle the case that the selected font does not cover the characters in the text. This set of functions are really that, a toy text API, for testing and demonstration purposes. Any serious application should avoid them.
The functions with glyphs in their name form cairo's low-level text API. The low-level API relies on the user to convert text to a set of glyph indexes and positions. This is a very hard problem and is best handled by external libraries, like the pangocairo that is part of the Pango text layout and rendering library.
The tag functions (tagBegin(String, String)
and
tagEnd(String)
) provide the ability to specify hyperlinks and
document logical structure on supported backends. The following tags are
supported:
- Link - Create a hyperlink
- Destinations - Create a hyperlink destination
- Document Structure Tags - Create PDF Document Structure
- Since:
- 1.0
-
Field Summary
-
Constructor Summary
ConstructorDescriptionContext
(MemorySegment address) Constructor used internally to instantiate a java Context object for a nativecairo_t
instance -
Method Summary
Modifier and TypeMethodDescriptionappendPath
(Path path) Append the path onto the current path.arc
(double xc, double yc, double radius, double angle1, double angle2) Adds a circular arc of the given radius to the current path.arcNegative
(double xc, double yc, double radius, double angle1, double angle2) Adds a circular arc of the given radius to the current path.clip()
Establishes a new clip region by intersecting the current clip region with the current path as it would be filled byfill()
and according to the current fill rule (seesetFillRule(FillRule)
).Computes a bounding box in user coordinates covering the area inside the current clip.Establishes a new clip region by intersecting the current clip region with the current path as it would be filled byfill()
and according to the current fill rule (seesetFillRule(FillRule)
).Adds a line segment to the path from the current point to the beginning of the current sub-path, (the most recent point passed tomoveTo(double, double)
), and closes this sub-path.Gets the current clip region as a list of rectangles in user coordinates.copyPage()
Emits the current page for backends that support multiple pages, but doesn't clear it, so, the contents of the current page will be retained for the next page too.copyPath()
Creates a copy of the current path and returns it to the user as aPath
.Gets a flattened copy of the current path and returns it to the user as aPath
.static Context
Creates a new Context with all graphics state parameters set to default values and with target as a target surface.curveTo
(double x1, double y1, double x2, double y2, double x3, double y3) Adds a cubic Bézier spline to the path from the current point to position(x3, y3)
in user-space coordinates, using(x1 , y1)
and(x2, y2)
as the control points.void
destroy()
Invokes the cleanup action that is normally invoked during garbage collection.deviceToUser
(Point point) Transform a coordinate from device space to user space by multiplying the given point by the inverse of the current transformation matrix (CTM).deviceToUserDistance
(Point point) Transform a distance vector from device space to user space.fill()
A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled).Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by afill()
operation given the current path and fill parameters.A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled).void
fontExtents
(FontExtents extents) Gets the font extents for the currently selected font.Gets the current shape antialiasing mode, as set bysetAntialias(Antialias)
.Gets the current point of the current path, which is conceptually the final point reached by the path so far.double[]
getDash()
Gets the current dash array.int
This function returns the length of the dash array in the context (0 if dashing is not currently in effect).double
Gets the current offset into the dash array.Gets the current fill rule, as set bysetFillRule(FillRule)
.Gets the current font face for aContext
.void
getFontMatrix
(Matrix matrix) Stores the current font matrix intomatrix
.Retrieves font rendering options set viasetFontOptions(FontOptions)
.Gets the current destination surface for the context.boolean
Returns whether or not hairline mode is set, as set bysetHairLine(boolean)
.Gets the current line cap style, as set bysetLineCap(LineCap)
.Gets the current line join style, as set bysetLineJoin(LineJoin)
.double
This function returns the current line width value exactly as set bysetLineWidth(double)
.void
Stores the current transformation matrix (CTM) intomatrix
.double
Gets the current miter limit, as set bysetMiterLimit(double)
.Gets the current compositing operator for a cairo context.Gets the current scaled fontGets the current source pattern.Gets the target surface for the cairo context as passed tocreate(Surface)
.double
Gets the current tolerance value, as set bysetTolerance(double)
.static org.gnome.glib.Type
getType()
Get the CairoContext GTypegetUserData
(UserDataKey key) Return user data previously attached to the context using the specified key.void
glyphExtents
(Glyphs glyphs, TextExtents extents) Gets the extents for an array of glyphs.Adds closed paths for the glyphs to the current path.boolean
Returns whether a current point is defined on the current path.Resets the current transformation matrix (CTM) by setting it equal to the identity matrix.boolean
inClip
(double x, double y) Tests whether the given point is inside the area that would be visible through the current clip, i.e.boolean
inFill
(double x, double y) Tests whether the given point is inside the area that would be affected by afill()
operation given the current path and filling parameters.boolean
inStroke
(double x, double y) Tests whether the given point is inside the area that would be affected by astroke()
operation given the current path and stroking parameters.lineTo
(double x, double y) Adds a line to the path from the current point to position(x, y)
in user-space coordinates.A drawing operator that paints the current source using the alpha channel of pattern as a mask.A drawing operator that paints the current source using the alpha channel of surface as a mask.moveTo
(double x, double y) Begin a new sub-path.newPath()
Clears the current path.Begin a new sub-path.paint()
A drawing operator that paints the current source everywhere within the current clip region.paintWithAlpha
(double alpha) A drawing operator that paints the current source everywhere within the current clip region using a mask of constant alpha valuealpha
.Computes a bounding box in user-space coordinates covering the points on the current path.popGroup()
Terminates the redirection begun by a call topushGroup()
orpushGroupWithContent(Content)
and returns a new pattern containing the results of all drawing operations performed to the group.Terminates the redirection begun by a call topushGroup()
orpushGroupWithContent(Content)
and installs the resulting pattern as the source pattern in the given cairo context.Temporarily redirects drawing to an intermediate surface known as a group.pushGroupWithContent
(Content content) Temporarily redirects drawing to an intermediate surface known as a group.rectangle
(double x, double y, double width, double height) Adds a closed sub-path rectangle of the given size to the current path at position (x
,y
) in user-space coordinates.relCurveTo
(double dx1, double dy1, double dx2, double dy2, double dx3, double dy3) Relative-coordinate version ofcurveTo(double, double, double, double, double, double)
.relLineTo
(double dx, double dy) Relative-coordinate version oflineTo(double, double)
.relMoveTo
(double dx, double dy) Begin a new sub-path.Reset the current clip region to its original, unrestricted state.restore()
Restores the context to the state saved by a preceding call tosave()
and removes that state from the stack of saved states.rotate
(double angle) Modifies the current transformation matrix (CTM) by rotating the user-space axes byangle
radians.save()
Makes a copy of the current state of the context and saves it on an internal stack of saved states for the context.scale
(double sx, double sy) Modifies the current transformation matrix (CTM) by scaling the X and Y user-space axes bysx
andsy
respectively.selectFontFace
(String family, FontSlant slant, FontWeight weight) Note: TheselectFontFace()
method is part of what the cairo designers call the "toy" text API.setAntialias
(Antialias antialias) Set the antialiasing mode of the rasterizer used for drawing shapes.setDash
(double[] dash, double offset) Sets the dash pattern to be used bystroke()
.setFillRule
(FillRule fillRule) Set the current fill rule within the cairo context.setFontFace
(FontFace fontFace) Replaces the current FontFace object in the Context withfontFace
.setFontMatrix
(Matrix matrix) Sets the current font matrix tomatrix
.setFontOptions
(FontOptions options) Sets a set of custom font rendering options for the Context.setFontSize
(double size) Sets the current font matrix to a scale by a factor ofsize
, replacing any font matrix previously set withsetFontSize()
orsetFontMatrix(Matrix)
.setHairLine
(boolean setHairline) Sets lines within the cairo context to be hairlines.setLineCap
(LineCap lineCap) Sets the current line cap style within the cairo context.setLineJoin
(LineJoin lineJoin) Sets the current line join style within the cairo context.setLineWidth
(double width) Sets the current line width within the cairo context.Modifies the current transformation matrix (CTM) by setting it equal tomatrix
.setMiterLimit
(double limit) Sets the current miter limit within the cairo context.setOperator
(Operator op) Sets the compositing operator to be used for all drawing operations.setScaledFont
(ScaledFont scaledFont) Replaces the current font face, font matrix, and font options in theContext
with those of theScaledFont
.Sets the source pattern within the context to source.This is a convenience function for creating a pattern from surface and setting it as the source withsetSource(Pattern)
.setSourceRGB
(double red, double green, double blue) Sets the source pattern within the context to an opaque color.setSourceRGBA
(double red, double green, double blue, double alpha) Sets the source pattern within the context to a translucent color.setTolerance
(double tolerance) Sets the tolerance used when converting paths into trapezoids.setUserData
(UserDataKey key, MemorySegment userData) Attach user data to the context.showGlyphs
(Glyphs glyphs) A drawing operator that generates the shape from an array of glyphs, rendered according to the current fontFace, fontSize (fontMatrix), and fontOptions.showPage()
Emits and clears the current page for backends that support multiple pages.A drawing operator that generates the shape from a string of characters, rendered according to the current fontFace, fontSize (fontMatrix), and fontOptions.showTextGlyphs
(String string, Glyphs glyphs) This operation has rendering effects similar toshowGlyphs(org.freedesktop.cairo.Glyphs)
but, if the target surface supports it, uses the provided text and cluster mapping to embed the text for the glyphs shown in the output.status()
Checks whether an error has previously occurred for this context.stroke()
A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings.Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by astroke()
operation given the current path and stroke parameters.A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings.Marks the beginning of the tag_name structure.Marks the end of the tag_name structure.void
textExtents
(String string, TextExtents extents) Gets the extents for a string of text.Adds closed paths for text to the current path.Modifies the current transformation matrix (CTM) by applyingmatrix
as an additional transformation.translate
(double tx, double ty) Modifies the current transformation matrix (CTM) by translating the user-space origin by(tx, ty)
.userToDevice
(Point point) Transform a coordinate from user space to device space by multiplying the given point by the current transformation matrix (CTM).userToDeviceDistance
(Point point) Transform a distance vector from user space to device space.
-
Field Details
-
TAG_DEST
Create a destination for a hyperlink. Destination tag attributes are detailed at Destinations.- Since:
- 1.16
- See Also:
-
TAG_LINK
-
TAG_CONTENT
-
TAG_CONTENT_REF
-
-
Constructor Details
-
Context
Constructor used internally to instantiate a java Context object for a nativecairo_t
instance- Parameters:
address
- the memory address of the nativecairo_t
instance
-
-
Method Details
-
destroy
public void destroy()Invokes the cleanup action that is normally invoked during garbage collection. If the instance is "owned" by the user, thedestroy()
function is run to dispose the native instance. -
create
Creates a new Context with all graphics state parameters set to default values and with target as a target surface. The target surface should be constructed with a backend-specific function such asImageSurface.create(Format, int, int)
(or any other backend surfacecreate()
variant).- Parameters:
target
- target surface for the context- Returns:
- a newly allocated
Context
- Throws:
IOException
- attempt to target a surface that does not support writing- Since:
- 1.0
-
status
-
save
Makes a copy of the current state of the context and saves it on an internal stack of saved states for the context. Whenrestore()
is called, the context will be restored to the saved state. Multiple calls tosave()
andrestore()
can be nested; each call torestore()
restores the state from the matching pairedsave()
.It isn't necessary to clear all saved states before a context is freed. If the reference count of a context drops to zero in response to a call to
destroy()
, any saved states will be freed along with the context.- Returns:
- the context
- Since:
- 1.0
-
restore
-
getTarget
Gets the target surface for the cairo context as passed tocreate(Surface)
.- Returns:
- the target surface.
- Since:
- 1.0
-
pushGroup
Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call topopGroup()
orpopGroupToSource()
. These calls provide the result of any drawing to the group as a pattern, (either as an explicit object, or set as the source pattern).This group functionality can be convenient for performing intermediate compositing. One common use of a group is to render objects as opaque within the group, (so that they occlude each other), and then blend the result with translucence onto the destination.
Groups can be nested arbitrarily deep by making balanced calls to
pushGroup()
/popGroup()
. Each call pushes/pops the new target group onto/from a stack.The
pushGroup()
function callssave()
so that any changes to the graphics state will not be visible outside the group, (the pop_group functions callrestore()
).By default the intermediate group will have a content type of
Content.COLOR_ALPHA
. Other content types can be chosen for the group by usingpushGroupWithContent(Content)
instead.As an example, here is how one might fill and stroke a path with translucence, but without any portion of the fill being visible under the stroke:
cr.pushGroup() .setSource(fillPattern) .fillPreserve() .setSource(strokePattern) .stroke() .popGroupToSource() .paintWithAlpha(alpha);
- Returns:
- the context
- Since:
- 1.2
-
pushGroupWithContent
Temporarily redirects drawing to an intermediate surface known as a group. The redirection lasts until the group is completed by a call topopGroup()
orpopGroupToSource()
. These calls provide the result of any drawing to the group as a pattern, (either as an explicit object, or set as the source pattern).The group will have a content type of content . The ability to control this content type is the only distinction between this function and
pushGroup()
which you should see for a more detailed description of group rendering.- Parameters:
content
- aContent
indicating the type of group that will be created- Returns:
- the context
- Since:
- 1.2
-
popGroup
Terminates the redirection begun by a call topushGroup()
orpushGroupWithContent(Content)
and returns a new pattern containing the results of all drawing operations performed to the group.The
popGroup()
function callsrestore()
, (balancing a call tosave()
by the pushGroup function), so that any changes to the graphics state will not be visible outside the group.- Returns:
- a newly created (surface) pattern containing the results of all drawing operations performed to the group.
- Since:
- 1.2
-
popGroupToSource
Terminates the redirection begun by a call topushGroup()
orpushGroupWithContent(Content)
and installs the resulting pattern as the source pattern in the given cairo context.The behavior of this function is equivalent to the sequence of operations:
SurfacePattern group = cr.popGroup(); cr.setSource(group);
but is more convenient as there is no need for a variable to store the short-lived pointer to the pattern.The
popGroup()
function callsrestore()
, (balancing a call tosave()
by the pushGroup function), so that any changes to the graphics state will not be visible outside the group.- Returns:
- the context
- Since:
- 1.2
-
getGroupTarget
Gets the current destination surface for the context. This is either the original target surface as passed tocreate(Surface)
or the target surface for the current group as started by the most recent call topushGroup()
orpushGroupWithContent(Content)
.- Returns:
- the target surface
- Since:
- 1.2
-
setSourceRGB
Sets the source pattern within the context to an opaque color. This opaque color will then be used for any subsequent drawing operation until a new source pattern is set.The color components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, they will be clamped.
The default source pattern is opaque black, (that is, it is equivalent to
setSourceRGB(0.0, 0.0, 0.0)
).- Parameters:
red
- red component of colorgreen
- green component of colorblue
- blue component of color- Returns:
- the context
- Since:
- 1.0
-
setSourceRGBA
Sets the source pattern within the context to a translucent color. This color will then be used for any subsequent drawing operation until a new source pattern is set.The color and alpha components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, they will be clamped.
Note that the color and alpha values are not premultiplied.
The default source pattern is opaque black, (that is, it is equivalent to
setSourceRGBA(0.0, 0.0, 0.0, 1.0)
).- Parameters:
red
- red component of colorgreen
- green component of colorblue
- blue component of coloralpha
- alpha component of color- Returns:
- the context
- Since:
- 1.0
-
setSource
Sets the source pattern within the context to source. This pattern will then be used for any subsequent drawing operation until a new source pattern is set.Note: The pattern's transformation matrix will be locked to the user space in effect at the time of
setSource()
. This means that further modifications of the current transformation matrix will not affect the source pattern. SeePattern.setMatrix(Matrix)
.The default source pattern is a solid pattern that is opaque black, (that is, it is equivalent to
setSourceRGB(0.0, 0.0, 0.0)
).- Parameters:
source
- a Pattern to be used as the source for subsequent drawing operations.- Returns:
- the context
- Since:
- 1.0
-
setSource
This is a convenience function for creating a pattern from surface and setting it as the source withsetSource(Pattern)
.The x and y parameters give the user-space coordinate at which the surface origin should appear. (The surface origin is its upper-left corner before any transformation has been applied.) The x and y parameters are negated and then set as translation values in the pattern matrix.
Other than the initial translation pattern matrix, as described above, all other pattern attributes, (such as its extend mode), are set to the default values as in
SurfacePattern.create(Surface)
. The resulting pattern can be queried withgetSource()
so that these attributes can be modified if desired, (eg. to create a repeating pattern withPattern.setExtend(Extend)
).- Parameters:
surface
- a surface to be used to set the source patternx
- User-space X coordinate for surface originy
- User-space Y coordinate for surface origin- Returns:
- the context
- Since:
- 1.0
-
getSource
Gets the current source pattern.- Returns:
- the current source pattern.
- Since:
- 1.0
-
setAntialias
Set the antialiasing mode of the rasterizer used for drawing shapes. This value is a hint, and a particular backend may or may not support a particular value. At the current time, no backend supportsAntialias.SUBPIXEL
when drawing shapes.Note that this option does not affect text rendering, instead see cairo_font_options_set_antialias().
- Parameters:
antialias
- the new antialiasing mode- Returns:
- the context
- Since:
- 1.0
-
getAntialias
Gets the current shape antialiasing mode, as set bysetAntialias(Antialias)
.- Returns:
- the current shape antialiasing mode.
- Since:
- 1.0
-
setDash
Sets the dash pattern to be used bystroke()
. A dash pattern is specified by dashes, an array of positive values. Each value provides the length of alternate "on" and "off" portions of the stroke. The offset specifies an offset into the pattern at which the stroke begins.Each "on" segment will have caps applied as if the segment were a separate sub-path. In particular, it is valid to use an "on" length of 0.0 with
LineCap.ROUND
orLineCap.SQUARE
in order to distributed dots or squares along a path.Note: The length values are in user-space units as evaluated at the time of stroking. This is not necessarily the same as the user space at the time of
setDash()
.If
dashes.length
is 0 dashing is disabled.If
dashes.length
is 1 a symmetric pattern is assumed with alternating on and off portions of the size specified by the single value in dashes.- Parameters:
dash
- an array specifying alternate lengths of on and off stroke portionsoffset
- an offset into the dash pattern at which the stroke should start- Returns:
- the context
- Throws:
IllegalArgumentException
- if any value in dashes is negative, or if all values are 0- Since:
- 1.0
-
getDashCount
public int getDashCount()This function returns the length of the dash array in the context (0 if dashing is not currently in effect).See also
setDash(double[], double)
andgetDash()
.- Returns:
- the length of the dash array, or 0 if no dash array set.
- Since:
- 1.4
-
getDash
public double[] getDash()Gets the current dash array.See also
getDashOffset()
.- Returns:
- the dash array
- Since:
- 1.4
-
getDashOffset
public double getDashOffset()Gets the current offset into the dash array.See also
getDash()
- Returns:
- the offset
- Since:
- 1.4
-
setFillRule
Set the current fill rule within the cairo context. The fill rule is used to determine which regions are inside or outside a complex (potentially self-intersecting) path. The current fill rule affects bothfill()
andclip()
. SeeFillRule
for details on the semantics of each available fill rule.The default fill rule is
FillRule.WINDING
.- Parameters:
fillRule
- a fill rule, specified as aFillRule
- Returns:
- the context
- Since:
- 1.0
-
getFillRule
Gets the current fill rule, as set bysetFillRule(FillRule)
.- Returns:
- the current fill rule.
- Since:
- 1.0
-
setLineCap
Sets the current line cap style within the cairo context. SeeLineCap
for details about how the available line cap styles are drawn.As with the other stroke parameters, the current line cap style is examined by
stroke()
andstrokeExtents()
, but does not have any effect during path construction.The default line cap style is
LineCap.BUTT
.- Parameters:
lineCap
- a line cap style- Returns:
- the context
- Since:
- 1.0
-
getLineCap
Gets the current line cap style, as set bysetLineCap(LineCap)
.- Returns:
- the current line cap style.
- Since:
- 1.0
-
setLineJoin
Sets the current line join style within the cairo context. SeeLineJoin
for details about how the available line join styles are drawn.As with the other stroke parameters, the current line join style is examined by
stroke()
andstrokeExtents()
, but does not have any effect during path construction.The default line join style is
LineJoin.MITER
.- Parameters:
lineJoin
- a line join style- Returns:
- the context
- Since:
- 1.0
-
getLineJoin
Gets the current line join style, as set bysetLineJoin(LineJoin)
.- Returns:
- the current line join style.
- Since:
- 1.0
-
setLineWidth
Sets the current line width within the cairo context. The line width value specifies the diameter of a pen that is circular in user space, (though device-space pen may be an ellipse in general due to scaling/shear/rotation of the CTM).Note: When the description above refers to user space and CTM it refers to the user space and CTM in effect at the time of the stroking operation, not the user space and CTM in effect at the time of the call to
setLineWidth()
. The simplest usage makes both of these spaces identical. That is, if there is no change to the CTM between a call tosetLineWidth()
and the stroking operation, then one can just pass user-space values tosetLineWidth()
and ignore this note.As with the other stroke parameters, the current line width is examined by {
stroke()
andstrokeExtents()
} but does not have any effect during path construction.The default line width value is 2.0.
- Parameters:
width
- a line width- Returns:
- the context
- Since:
- 1.0
-
getLineWidth
public double getLineWidth()This function returns the current line width value exactly as set bysetLineWidth(double)
. Note that the value is unchanged even if the CTM has changed between the calls tosetLineWidth(double)
andgetLineWidth()
.- Returns:
- the current line width.
- Since:
- 1.0
-
setMiterLimit
Sets the current miter limit within the cairo context.If the current line join style is set to
LineJoin.MITER
(seesetLineJoin(LineJoin)
), the miter limit is used to determine whether the lines should be joined with a bevel instead of a miter. Cairo divides the length of the miter by the line width. If the result is greater than the miter limit, the style is converted to a bevel.As with the other stroke parameters, the current line miter limit is examined by
stroke()
andstrokeExtents()
, but does not have any effect during path construction.The default miter limit value is 10.0, which will convert joins with interior angles less than 11 degrees to bevels instead of miters. For reference, a miter limit of 2.0 makes the miter cutoff at 60 degrees, and a miter limit of 1.414 makes the cutoff at 90 degrees.
A miter limit for a desired angle can be computed as:
miter limit = 1/sin(angle/2)
- Parameters:
limit
- miter limit to set- Returns:
- the context
- Since:
- 1.0
-
getMiterLimit
public double getMiterLimit()Gets the current miter limit, as set bysetMiterLimit(double)
.- Returns:
- the current miter limit.
- Since:
- 1.0
-
setOperator
Sets the compositing operator to be used for all drawing operations. SeeOperator
for details on the semantics of each available compositing operator.The default operator is
Operator.OVER
.- Parameters:
op
- a compositing operator, specified as anOperator
- Returns:
- the context
- Since:
- 1.0
-
getOperator
Gets the current compositing operator for a cairo context.- Returns:
- the current compositing operator.
- Since:
- 1.0
-
setTolerance
Sets the tolerance used when converting paths into trapezoids. Curved segments of the path will be subdivided until the maximum deviation between the original path and the polygonal approximation is less than tolerance . The default value is 0.1. A larger value will give better performance, a smaller value, better appearance. (Reducing the value from the default value of 0.1 is unlikely to improve appearance significantly.) The accuracy of paths within Cairo is limited by the precision of its internal arithmetic, and the prescribed tolerance is restricted to the smallest representable internal value.- Parameters:
tolerance
- the tolerance, in device units (typically pixels)- Returns:
- the context
- Since:
- 1.0
-
getTolerance
public double getTolerance()Gets the current tolerance value, as set bysetTolerance(double)
.- Returns:
- the current tolerance value.
- Since:
- 1.0
-
clip
Establishes a new clip region by intersecting the current clip region with the current path as it would be filled byfill()
and according to the current fill rule (seesetFillRule(FillRule)
).After
clip()
, the current path will be cleared from the cairo context.The current clip region affects all drawing operations by effectively masking out any changes to the surface that are outside the current clip region.
Calling
clip()
can only make the clip region smaller, never larger. But the current clip is part of the graphics state, so a temporary restriction of the clip region can be achieved by callingclip()
within asave()
/restore()
pair. The only other means of increasing the size of the clip region isresetClip()
.- Returns:
- the context
- Since:
- 1.0
-
clipPreserve
Establishes a new clip region by intersecting the current clip region with the current path as it would be filled byfill()
and according to the current fill rule (seesetFillRule(FillRule)
).Unlike
clip()
,clipPreserve()
preserves the path within the cairo context.The current clip region affects all drawing operations by effectively masking out any changes to the surface that are outside the current clip region.
Calling
clipPreserve()
can only make the clip region smaller, never larger. But the current clip is part of the graphics state, so a temporary restriction of the clip region can be achieved by callingclipPreserve()
within asave()
/restore()
pair. The only other means of increasing the size of the clip region isresetClip()
.- Returns:
- the context
- Since:
- 1.0
-
clipExtents
Computes a bounding box in user coordinates covering the area inside the current clip.- Returns:
- the resulting extents
- Since:
- 1.4
-
inClip
public boolean inClip(double x, double y) Tests whether the given point is inside the area that would be visible through the current clip, i.e. the area that would be filled by apaint()
operation.- Parameters:
x
- X coordinate of the point to testy
- Y coordinate of the point to test- Returns:
- True if the point is inside, false if outside.
- Since:
- 1.10
- See Also:
-
resetClip
Reset the current clip region to its original, unrestricted state. That is, set the clip region to an infinitely large shape containing the target surface. Equivalently, if infinity is too hard to grasp, one can imagine the clip region being reset to the exact bounds of the target surface.Note that code meant to be reusable should not call
resetClip()
as it will cause results unexpected by higher-level code whichclip()
. Consider usingsave()
andrestore()
aroundclip()
as a more robust means of temporarily restricting the clip region.- Returns:
- the context
- Since:
- 1.0
-
copyClipRectangleList
Gets the current clip region as a list of rectangles in user coordinates. Never returnsnull
.- Returns:
- the current clip region as a list of rectangles in user coordinates
- Throws:
IllegalStateException
- if the clip region cannot be represented as a list of user-space rectangles. The exception may also indicate other errors.- Since:
- 1.4
-
fill
A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before being filled). Afterfill()
, the current path will be cleared from the cairo context. SeesetFillRule(FillRule)
andfillPreserve()
.- Returns:
- the context
- Since:
- 1.0
-
fillPreserve
-
fillExtents
Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by afill()
operation given the current path and fill parameters. If the current path is empty, returns an empty rectangle ((0,0), (0,0)
). Surface dimensions and clipping are not taken into account.Contrast with
pathExtents()
, which is similar, but returns non-zero extents for some paths with no inked area, (such as a simple line segment).Note that
fillExtents()
must necessarily do more work to compute the precise inked areas in light of the fill rule, sopathExtents()
may be more desirable for sake of performance if the non-inked path extents are desired.- Returns:
- The resulting extents
- Since:
- 1.0
- See Also:
-
inFill
public boolean inFill(double x, double y) Tests whether the given point is inside the area that would be affected by afill()
operation given the current path and filling parameters. Surface dimensions and clipping are not taken into account.- Parameters:
x
- X coordinate of the point to testy
- Y coordinate of the point to test- Returns:
- true if the point is inside, or false if outside.
- Since:
- 1.0
- See Also:
-
mask
-
mask
A drawing operator that paints the current source using the alpha channel of surface as a mask. (Opaque areas of surface are painted with the source, transparent areas are not painted.)- Parameters:
surface
- aSurface
surfaceX
- X coordinate at which to place the origin of surfacesurfaceY
- Y coordinate at which to place the origin of surface- Returns:
- the context
- Since:
- 1.0
-
paint
A drawing operator that paints the current source everywhere within the current clip region.- Returns:
- the context
- Since:
- 1.0
-
paintWithAlpha
A drawing operator that paints the current source everywhere within the current clip region using a mask of constant alpha valuealpha
. The effect is similar topaint()
, but the drawing is faded out using the alpha value.- Parameters:
alpha
- alpha value, between 0 (transparent) and 1 (opaque)- Returns:
- the context
- Since:
- 1.0
-
stroke
A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. Afterstroke()
, the current path will be cleared from the cairo context.Note: Degenerate segments and sub-paths are treated specially and provide a useful result. These can result in two different situations:
- Zero-length "on" segments set in
setDash(double[], double)
. If the cap style isLineCap.ROUND
orLineCap.SQUARE
then these segments will be drawn as circular dots or squares respectively. In the case ofLineCap.SQUARE
, the orientation of the squares is determined by the direction of the underlying path. - A sub-path created by
moveTo(double, double)
followed by either aclosePath()
or one or more calls tolineTo(double, double)
to the same coordinate as themoveTo(double, double)
. If the cap style isLineCap.ROUND
then these sub-paths will be drawn as circular dots. Note that in the case ofLineCap.SQUARE
a degenerate sub-path will not be drawn at all, (since the correct orientation is indeterminate).
LineCap.BUTT
cause anything to be drawn in the case of either degenerate segments or sub-paths.- Returns:
- the context
- Since:
- 1.0
- See Also:
- Zero-length "on" segments set in
-
strokePreserve
-
strokeExtents
Computes a bounding box in user coordinates covering the area that would be affected, (the "inked" area), by astroke()
operation given the current path and stroke parameters. If the current path is empty, returns an empty rectangle ((0,0), (0,0)
). Surface dimensions and clipping are not taken into account.Note that if the line width is set to exactly zero, then
strokeExtents()
will return an empty rectangle. Contrast withpathExtents()
which can be used to compute the non-empty bounds as the line width approaches zero.Note that
strokeExtents()
must necessarily do more work to compute the precise inked areas in light of the stroke parameters, sopathExtents()
may be more desirable for sake of performance if non-inked path extents are desired.- Returns:
- The resulting extents
- Since:
- 1.0
- See Also:
-
inStroke
public boolean inStroke(double x, double y) Tests whether the given point is inside the area that would be affected by astroke()
operation given the current path and stroking parameters. Surface dimensions and clipping are not taken into account.- Parameters:
x
- X coordinate of the point to testy
- Y coordinate of the point to test- Returns:
- true if the point is inside, or false if outside.
- Since:
- 1.0
- See Also:
-
copyPage
Emits the current page for backends that support multiple pages, but doesn't clear it, so, the contents of the current page will be retained for the next page too. UseshowPage()
if you want to get an empty page after the emission.This is a convenience function that simply calls
Surface.copyPage()
on this Context's target.- Returns:
- the context
- Since:
- 1.0
-
showPage
Emits and clears the current page for backends that support multiple pages. UsecopyPage()
if you don't want to clear the page.This is a convenience function that simply calls
Surface.showPage()
on this Context's target.- Returns:
- the context
- Since:
- 1.0
-
setHairLine
Sets lines within the cairo context to be hairlines. Hairlines are logically zero-width lines that are drawn at the thinnest renderable width possible in the current context.On surfaces with native hairline support, the native hairline functionality will be used. Surfaces that support hairlines include:
- pdf/ps: Encoded as 0-width line.
- win32_printing: Rendered with PS_COSMETIC pen.
- svg: Encoded as 1px non-scaling-stroke.
- script: Encoded with set-hairline function.
stroke()
. See https://cairographics.org/cookbook/ellipses/ for an example.- Parameters:
setHairline
- whether or not to set hairline mode- Returns:
- the context
- Since:
- 1.18
-
getHairLine
public boolean getHairLine()Returns whether or not hairline mode is set, as set bysetHairLine(boolean)
.- Returns:
- whether hairline mode is set.
- Since:
- 1.18
-
copyPath
Creates a copy of the current path and returns it to the user as aPath
. SeePathData
for hints on how to iterate over the returned data structure.- Returns:
- the copy of the current path, or
null
when the Context is in an error state, or there is insufficient memory to copy the path. - Since:
- 1.0
-
copyPathFlat
Gets a flattened copy of the current path and returns it to the user as aPath
. SeePathData
for hints on how to iterate over the returned data structure.This function is like
copyPath()
except that any curves in the path will be approximated with piecewise-linear approximations, (accurate to within the current tolerance value). That is, the result is guaranteed to not have any elements of typePathDataType.CURVE_TO
which will instead be replaced by a series ofPathDataType.LINE_TO
elements.- Returns:
- the copy of the current path, or
null
when the Context is in an error state, or there is insufficient memory to copy the path. - Since:
- 1.0
-
appendPath
Append the path onto the current path. The path may be either the return value from one ofcopyPath()
orcopyPathFlat()
or it may be constructed manually. SeePath
for details on how the path data structure should be initialized, and note thatpath.status
must be initialized toStatus.SUCCESS
.- Parameters:
path
- path to be appended- Returns:
- the context
- Since:
- 1.0
-
hasCurrentPoint
public boolean hasCurrentPoint()Returns whether a current point is defined on the current path. SeegetCurrentPoint()
for details on the current point.- Returns:
- whether a current point is defined.
- Since:
- 1.6
-
getCurrentPoint
Gets the current point of the current path, which is conceptually the final point reached by the path so far.The current point is returned in the user-space coordinate system. If there is no defined current point or if the Context is in an error status, x and y will both be set to 0.0. It is possible to check this in advance with
hasCurrentPoint()
.Most path construction functions alter the current point. See the following for details on how they affect the current point:
newPath()
,newSubPath()
,appendPath(Path)
,closePath()
,moveTo(double, double)
,lineTo(double, double)
,curveTo(double, double, double, double, double, double)
,relMoveTo(double, double)
,relLineTo(double, double)
,relCurveTo(double, double, double, double, double, double)
,arc(double, double, double, double, double)
,arcNegative(double, double, double, double, double)
,rectangle(double, double, double, double)
,textPath(String)
,glyphPath(org.freedesktop.cairo.Glyphs)
.Some functions use and alter the current point but do not otherwise change current path: cairo_show_text().
Some functions unset the current path and as a result, current point:
fill()
,stroke()
.- Returns:
- X and Y coordinates of the current point
- Since:
- 1.0
-
newPath
Clears the current path. After this call there will be no path and no current point.- Returns:
- the context
- Since:
- 1.0
-
newSubPath
Begin a new sub-path. Note that the existing path is not affected. After this call there will be no current point.In many cases, this call is not needed since new sub-paths are frequently started with
moveTo(double, double)
.A call to
newSubPath()
is particularly useful when beginning a new sub-path with one of thearc()
calls. This makes things easier as it is no longer necessary to manually compute the arc's initial coordinates for a call tomoveTo(double, double)
.- Returns:
- the context
- Since:
- 1.2
-
closePath
Adds a line segment to the path from the current point to the beginning of the current sub-path, (the most recent point passed tomoveTo(double, double)
), and closes this sub-path. After this call the current point will be at the joined endpoint of the sub-path.The behavior of
closePath()
is distinct from simply callinglineTo(double, double)
with the equivalent coordinate in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead, there is a line join connecting the final and initial segments of the sub-path.If there is no current point before the call to
closePath()
, this function will have no effect.Note: As of cairo version 1.2.4 any call to
closePath()
will place an explicitMOVE_TO
element into the path immediately after theCLOSE_PATH
element, (which can be seen incopyPath()
for example). This can simplify path processing in some cases as it may not be necessary to save the "last move_to point" during processing as the MOVE_TO immediately after the CLOSE_PATH will provide that point.- Returns:
- the context
- Since:
- 1.0
-
arc
Adds a circular arc of the given radius to the current path. The arc is centered at (xc
,yc
), begins atangle1
and proceeds in the direction of increasing angles to end atangle2
. Ifangle2
is less thanangle1
it will be progressively increased by 2*M_PI until it is greater thanangle2
.If there is a current point, an initial line segment will be added to the path to connect the current point to the beginning of the arc. If this initial line is undesired, it can be avoided by calling
newSubPath()
before callingarc()
.Angles are measured in radians. An angle of 0.0 is in the direction of the positive X axis (in user space). An angle of M_PI/2.0 radians (90 degrees) is in the direction of the positive Y axis (in user space). Angles increase in the direction from the positive X axis toward the positive Y axis. So with the default transformation matrix, angles increase in a clockwise direction.
(To convert from degrees to radians, use degrees * (M_PI / 180.).)
This function gives the arc in the direction of increasing angles; see
arcNegative(double, double, double, double, double)
to get the arc in the direction of decreasing angles.The arc is circular in user space. To achieve an elliptical arc, you can scale the current transformation matrix by different amounts in the X and Y directions. For example, to draw an ellipse in the box given by
x, y, width, height
:cr.save() .translate(x + width / 2., y + height / 2.) .scale(width / 2., height / 2.) .arc(0., 0., 1., 0., 2 * Math.PI) .restore();
- Parameters:
xc
- X position of the center of the arcyc
- Y position of the center of the arcradius
- the radius of the arcangle1
- the start angle, in radiansangle2
- the end angle, in radians- Returns:
- the context
- Since:
- 1.0
-
arcNegative
Adds a circular arc of the given radius to the current path. The arc is centered at (xc
,yc
), begins atangle1
and proceeds in the direction of decreasing angles to end atangle2
. Ifangle2
is greater thanangle1
it will be progressively decreased by 2*M_PI until it is less thanangle1
.See
arc(double, double, double, double, double)
for more details. This function differs only in the direction of the arc between the two angles.- Parameters:
xc
- X position of the center of the arcyc
- Y position of the center of the arcradius
- the radius of the arcangle1
- the start angle, in radiansangle2
- the end angle, in radians- Returns:
- the context
- Since:
- 1.0
-
curveTo
Adds a cubic Bézier spline to the path from the current point to position(x3, y3)
in user-space coordinates, using(x1 , y1)
and(x2, y2)
as the control points. After this call the current point will be(x3, y3)
.If there is no current point before the call to
curveTo()
this function will behave as if preceded by a call tomoveTo(x1, y1)
.- Parameters:
x1
- the X coordinate of the first control pointy1
- the Y coordinate of the first control pointx2
- the X coordinate of the second control pointy2
- the Y coordinate of the second control pointx3
- the X coordinate of the end of the curvey3
- the Y coordinate of the end of the curve- Returns:
- the context
- Since:
- 1.0
-
lineTo
Adds a line to the path from the current point to position(x, y)
in user-space coordinates. After this call the current point will be(x, y)
.If there is no current point before the call to
lineTo()
this function will behave asmoveTo(x, y)
.- Parameters:
x
- the X coordinate of the end of the new liney
- the Y coordinate of the end of the new line- Returns:
- the context
- Since:
- 1.0
-
moveTo
Begin a new sub-path. After this call the current point will be (x
,y
).- Parameters:
x
- the X coordinate of the new positiony
- the Y coordinate of the new position- Returns:
- the context
- Since:
- 1.0
-
rectangle
Adds a closed sub-path rectangle of the given size to the current path at position (x
,y
) in user-space coordinates.This function is logically equivalent to:
cr.moveTo(x, y) .relLineTo(width, 0) .relLineTo(0, height) .relLineTo(-width, 0) .closePath();
- Parameters:
x
- the X coordinate of the top left corner of the rectangley
- the Y coordinate of the top left corner of the rectanglewidth
- the width of the rectangleheight
- the height of the rectangle- Returns:
- the context
- Since:
- 1.0
-
glyphPath
Adds closed paths for the glyphs to the current path. The generated path if filled, achieves an effect similar to that ofshowGlyphs(org.freedesktop.cairo.Glyphs)
.- Parameters:
glyphs
- array of glyphs to show- Returns:
- the context
- Since:
- 1.0
-
textPath
Adds closed paths for text to the current path. The generated path if filled, achieves an effect similar to that ofshowText(String)
.Text conversion and positioning is done similar to
showText(String)
.Like
showText(String)
, After this call the current point is moved to the origin of where the next glyph would be placed in this same progression. That is, the current point will be at the origin of the final glyph offset by its advance values. This allows for chaining multiple calls to totextPath()
without having to set current point in between.Note: The
textPath()
function call is part of what the cairo designers call the "toy" text API. It is convenient for short demos and simple programs, but it is not expected to be adequate for serious text-using applications. SeeglyphPath(org.freedesktop.cairo.Glyphs)
for the "real" text path API in cairo.- Parameters:
string
- a string of text- Returns:
- the context
- Since:
- 1.0
-
relCurveTo
public Context relCurveTo(double dx1, double dy1, double dx2, double dy2, double dx3, double dy3) throws IllegalStateException Relative-coordinate version ofcurveTo(double, double, double, double, double, double)
. All offsets are relative to the current point. Adds a cubic Bézier spline to the path from the current point to a point offset from the current point by(dx3, dy3)
, using points offset by(dx1, dy1)
and(dx2, dy2)
as the control points. After this call the current point will be offset by(dx3, dy3)
.Given a current point of (
x
,y
),relCurveTo(dx1, dy1, dx2, dy2, dx3, dy3)
is logically equivalent tocurveTo(x+dx1, y+dy1, x+dx2, y+dy2, x+dx3, y+dy3)
.- Parameters:
dx1
- the X offset to the first control pointdy1
- the Y offset to the first control pointdx2
- the X offset to the second control pointdy2
- the Y offset to the second control pointdx3
- the X offset to the end of the curvedy3
- the Y offset to the end of the curve- Returns:
- the context
- Throws:
IllegalStateException
- when called with no current point- Since:
- 1.0
-
relLineTo
Relative-coordinate version oflineTo(double, double)
. Adds a line to the path from the current point to a point that is offset from the current point by(dx, dy)
in user space. After this call the current point will be offset by(dx, dy)
.Given a current point of (
x
,y
),relLineTo(dx, dy)
is logically equivalent tolineTo(x + dx, y + dy)
.- Parameters:
dx
- the X offset to the end of the new linedy
- the Y offset to the end of the new line- Returns:
- the context
- Throws:
IllegalStateException
- when called with no current point- Since:
- 1.0
-
relMoveTo
Begin a new sub-path. After this call the current point will offset by (x
,y
).Given a current point of (
x
,y
),relMoveTo(dx, dy)
is logically equivalent tomoveTo(x + dx, y + dy)
.- Parameters:
dx
- the X offsetdy
- the Y offset- Returns:
- the context
- Throws:
IllegalStateException
- when called with no current point- Since:
- 1.0
-
pathExtents
Computes a bounding box in user-space coordinates covering the points on the current path. If the current path is empty, returns an empty rectangle ((0,0), (0,0)). Stroke parameters, fill rule, surface dimensions and clipping are not taken into account.Contrast with
fillExtents()
andstrokeExtents()
which return the extents of only the area that would be "inked" by the corresponding drawing operations.The result of
pathExtents()
is defined as equivalent to the limit ofstrokeExtents()
withLineCap.ROUND
as the line width approaches 0.0, (but never reaching the empty-rectangle returned bystrokeExtents()
for a line width of 0.0).Specifically, this means that zero-area sub-paths such as
cr.moveTo().lineTo()
segments, (even degenerate cases where the coordinates to both calls are identical), will be considered as contributing to the extents. However, a lonemoveTo()
will not contribute to the results ofpathExtents()
.- Returns:
- a
Rectangle
with the left, top, right and bottom of the resulting extents - Since:
- 1.6
-
translate
Modifies the current transformation matrix (CTM) by translating the user-space origin by(tx, ty)
. This offset is interpreted as a user-space coordinate according to the CTM in place before the new call totranslate()
. In other words, the translation of the user-space origin takes place after any existing transformation.- Parameters:
tx
- amount to translate in the X directionty
- amount to translate in the Y direction- Returns:
- the context
- Since:
- 1.0
-
scale
Modifies the current transformation matrix (CTM) by scaling the X and Y user-space axes bysx
andsy
respectively. The scaling of the axes takes place after any existing transformation of user space.- Parameters:
sx
- scale factor for the X dimensionsy
- scale factor for the Y dimension- Returns:
- the context
- Since:
- 1.0
-
rotate
Modifies the current transformation matrix (CTM) by rotating the user-space axes byangle
radians. The rotation of the axes takes places after any existing transformation of user space. The rotation direction for positive angles is from the positive X axis toward the positive Y axis.- Parameters:
angle
- angle (in radians) by which the user-space axes will be rotated- Returns:
- the context
- Since:
- 1.0
-
transform
Modifies the current transformation matrix (CTM) by applyingmatrix
as an additional transformation. The new transformation of user space takes place after any existing transformation.- Parameters:
matrix
- a transformation to be applied to the user-space axes- Returns:
- the context
- Since:
- 1.0
-
setMatrix
Modifies the current transformation matrix (CTM) by setting it equal tomatrix
.The CTM is a two-dimensional affine transformation that maps all coordinates and other drawing instruments from the user space into the surface's canonical coordinate system, also known as the device space.
- Parameters:
matrix
- a transformation matrix from user space to device space- Returns:
- the context
- Since:
- 1.0
-
getMatrix
Stores the current transformation matrix (CTM) intomatrix
.The CTM is a two-dimensional affine transformation that maps all coordinates and other drawing instruments from the user space into the surface's canonical coordinate system, also known as the device space.
- Parameters:
matrix
- return value for the matrix- Since:
- 1.0
-
identityMatrix
Resets the current transformation matrix (CTM) by setting it equal to the identity matrix. That is, the user-space and device-space axes will be aligned and one user-space unit will transform to one device-space unit.- Returns:
- the context
- Since:
- 1.0
-
userToDevice
-
userToDeviceDistance
Transform a distance vector from user space to device space. This function is similar touserToDevice(Point)
except that the translation components of the CTM will be ignored when transforming(dx, dy)
.- Parameters:
point
- X and Y components of a distance vector- Returns:
- a Point with the transformed X and Y components
- Since:
- 1.0
-
deviceToUser
Transform a coordinate from device space to user space by multiplying the given point by the inverse of the current transformation matrix (CTM).- Parameters:
point
- X and Y values of coordinate- Returns:
- a Point with the transformed X and Y coordinates
- Since:
- 1.0
-
deviceToUserDistance
Transform a distance vector from device space to user space. This function is similar todeviceToUser(Point)
except that the translation components of the inverse CTM will be ignored when transforming(dx, dy)
.- Parameters:
point
- X and Y components of a distance vector- Returns:
- a Point with the transformed X and Y components
- Since:
- 1.0
-
selectFontFace
Note: TheselectFontFace()
method is part of what the cairo designers call the "toy" text API. It is convenient for short demos and simple programs, but it is not expected to be adequate for serious text-using applications.Selects a family and style of font from a simplified description as a family name, slant and weight. Cairo provides no operation to list available family names on the system (this is a "toy", remember), but the standard CSS2 generic family names, ("serif", "sans-serif", "cursive", "fantasy", "monospace"), are likely to work as expected.
If
family
starts with the string "cairo :
", or if no native font backends are compiled in, cairo will use an internal font family. The internal font family recognizes many modifiers in thefamily
string, most notably, it recognizes the string "monospace". That is, the family name "cairo :monospace
" will use the monospace version of the internal font family.For "real" font selection, see the font-backend-specific font_face_create functions for the font backend you are using. (For example, if you are using the freetype-based cairo-ft font backend, see cairo_ft_font_face_create_for_ft_face() or cairo_ft_font_face_create_for_pattern().) The resulting font face could then be used with
ScaledFont.create(FontFace, Matrix, Matrix, FontOptions)
andsetScaledFont(ScaledFont)
.Similarly, when using the "real" font support, you can call directly into the underlying font system, (such as fontconfig or freetype), for operations such as listing available fonts, etc.
It is expected that most applications will need to use a more comprehensive font handling and text layout library, (for example, pango), in conjunction with cairo.
If text is drawn without a call to
selectFontFace()
, (norsetFontFace(FontFace)
norsetScaledFont(ScaledFont)
), the default family is platform-specific, but is essentially "sans-serif". Default slant isFontSlant.NORMAL
, and default weight isFontWeight.NORMAL
.This function is equivalent to a call to
ToyFontFace.create()
followed bysetFontFace(FontFace)
.- Parameters:
family
- a font family nameslant
- the slant for the fontweight
- the weight for the font- Returns:
- the context
- Since:
- 1.0
-
setFontSize
Sets the current font matrix to a scale by a factor ofsize
, replacing any font matrix previously set withsetFontSize()
orsetFontMatrix(Matrix)
. This results in a font size ofsize
user space units. (More precisely, this matrix will result in the font's em-square being asize
bysize
square in user space.)If text is drawn without a call to
setFontSize()
, (norsetFontMatrix(Matrix)
norsetScaledFont(ScaledFont)
), the default font size is 10.0.- Parameters:
size
- the new font size, in user space units- Returns:
- the context
- Since:
- 1.0
-
setFontMatrix
Sets the current font matrix tomatrix
. The font matrix gives a transformation from the design space of the font (in this space, the em-square is 1 unit by 1 unit) to user space. Normally, a simple scale is used (seesetFontSize(double)
), but a more complex font matrix can be used to shear the font or stretch it unequally along the two axes- Parameters:
matrix
- a Matrix describing a transform to be applied to the current font.- Returns:
- the context
- Since:
- 1.0
-
getFontMatrix
Stores the current font matrix intomatrix
. SeesetFontMatrix(org.freedesktop.cairo.Matrix)
- Parameters:
matrix
- return value for the matrix- Since:
- 1.0
-
setFontOptions
Sets a set of custom font rendering options for the Context. Rendering options are derived by merging these options with the options derived from underlying surface; if the value inoptions
has a default value (likeAntialias.DEFAULT
), then the value from the surface is used.- Parameters:
options
- font options to use- Returns:
- this context
- Since:
- 1.0
-
getFontOptions
Retrieves font rendering options set viasetFontOptions(FontOptions)
. Note that the returned options do not include any options derived from the underlying surface; they are literally the options passed tosetFontOptions(FontOptions)
.- Returns:
- the retrieved options
- Since:
- 1.0
-
setFontFace
Replaces the current FontFace object in the Context withfontFace
. The replaced font face in the Context will be destroyed if there are no other references to it.- Parameters:
fontFace
- a FontFace, ornull
to restore to the default font- Returns:
- the context
- Since:
- 1.0
-
getFontFace
-
setScaledFont
Replaces the current font face, font matrix, and font options in theContext
with those of theScaledFont
. Except for some translation, the current CTM of the Context should be the same as that of the ScaledFont, which can be accessed usingScaledFont.getCTM(org.freedesktop.cairo.Matrix)
.- Parameters:
scaledFont
- a ScaledFont- Returns:
- the context
- Since:
- 1.2
-
getScaledFont
Gets the current scaled font- Returns:
- the current scaled font
- Since:
- 1.4
-
showText
A drawing operator that generates the shape from a string of characters, rendered according to the current fontFace, fontSize (fontMatrix), and fontOptions.This function first computes a set of glyphs for the string of text. The first glyph is placed so that its origin is at the current point. The origin of each subsequent glyph is offset from that of the previous glyph by the advance values of the previous glyph.
After this call the current point is moved to the origin of where the next glyph would be placed in this same progression. That is, the current point will be at the origin of the final glyph offset by its advance values. This allows for easy display of a single logical string with multiple calls to
showText()
.Note: The
showText()
function call is part of what the cairo designers call the "toy" text API. It is convenient for short demos and simple programs, but it is not expected to be adequate for serious text-using applications. SeeshowGlyphs(org.freedesktop.cairo.Glyphs)
for the "real" text display API in cairo.- Parameters:
string
- a string of text, ornull
- Returns:
- the context
- Since:
- 1.0
-
showGlyphs
-
showTextGlyphs
This operation has rendering effects similar toshowGlyphs(org.freedesktop.cairo.Glyphs)
but, if the target surface supports it, uses the provided text and cluster mapping to embed the text for the glyphs shown in the output. If the target does not support the extended attributes, this function acts like the basicshowGlyphs(org.freedesktop.cairo.Glyphs)
as if it had been passedglyphs
.The mapping between the string and glyphs array is provided by an array of clusters. Each cluster covers a number of text bytes and glyphs, and neighboring clusters cover neighboring areas of the text string and the glyphs array. The clusters should collectively cover
string
andglyphs
in entirety.The first cluster always covers bytes from the beginning of
string
. IfclusterFlags
do not have theTextClusterFlags.BACKWARD
set, the first cluster also covers the beginning ofglyphs
, otherwise it covers the end of the glyphs array and following clusters move backward.See
TextCluster
for constraints on valid clusters.- Parameters:
string
- a string of textglyphs
- array of glyphs to show- Returns:
- the context
- Since:
- 1.8
-
fontExtents
Gets the font extents for the currently selected font.- Parameters:
extents
- a FontExtents object into which the results will be stored.- Since:
- 1.0
-
textExtents
Gets the extents for a string of text. The extents describe a user-space rectangle that encloses the "inked" portion of the text, (as it would be drawn byshowText(String)
). Additionally, thexAdvance
andyAdvance
values indicate the amount by which the current point would be advanced byshowText(String)
.Note that whitespace characters do not directly contribute to the size of the rectangle (
extents.width
andextents.height
). They do contribute indirectly by changing the position of non-whitespace characters. In particular, trailing whitespace characters are likely to not affect the size of the rectangle, though they will affect thexAdvance
andyAdvance
values.- Parameters:
string
- a string of text, ornull
extents
- a TextExtents object into which the results will be stored- Since:
- 1.0
-
glyphExtents
Gets the extents for an array of glyphs. The extents describe a user-space rectangle that encloses the "inked" portion of the glyphs, (as they would be drawn byshowGlyphs(org.freedesktop.cairo.Glyphs)
). Additionally, thexAdvance
andyAdvance
values indicate the amount by which the current point would be advanced byshowGlyphs(org.freedesktop.cairo.Glyphs)
.Note that whitespace glyphs do not contribute to the size of the rectangle (
extents.width
andextents.height
).- Parameters:
glyphs
- an array of Glyph objectsextents
- a TextExtents object into which the results will be stored- Since:
- 1.0
-
tagBegin
Marks the beginning of the tag_name structure. CalltagEnd(String)
with the sametagName
to mark the end of the structure.The attributes string is of the form "key1=value2 key2=value2 ...". Values may be boolean (true/false or 1/0), integer, float, string, or an array.
String values are enclosed in single quotes ('). Single quotes and backslashes inside the string should be escaped with a backslash.
Boolean values may be set to true by only specifying the key. eg the attribute string "key" is the equivalent to "key=true".
Arrays are enclosed in '[]'. eg "rect=[1.2 4.3 2.0 3.0]".
If no attributes are required,
attributes
can be an empty string ornull
.See Tags and Links Description for the list of tags and attributes.
- Parameters:
tagName
- tag nameattributes
- tag attributes- Returns:
- the context
- Throws:
IllegalArgumentException
- invalid nesting of tags or invalid attributes, seeStatus.TAG_ERROR
- Since:
- 1.16
- See Also:
-
tagEnd
Marks the end of the tag_name structure.- Parameters:
tagName
- tag name- Returns:
- the context
- Throws:
IllegalArgumentException
- Invalid nesting of tags, seeStatus.TAG_ERROR
- Since:
- 1.16
- See Also:
-
setUserData
Attach user data to the context. To remove user data from a context, call this function with the key that was used to set it andnull
foruserData
.- Parameters:
key
- the key to attach the user data touserData
- the user data to attach to the context- Returns:
- the key
- Throws:
NullPointerException
- ifkey
isnull
- Since:
- 1.4
-
getUserData
Return user data previously attached to the context using the specified key. If no user data has been attached with the given key this function returnsnull
.The returned memory segment has zero length. It can be resized with
MemorySegment.reinterpret(long)
RESTRICTED.- Parameters:
key
- the UserDataKey the user data was attached to- Returns:
- the user data previously attached or
null
- Since:
- 1.4
-
getType
public static org.gnome.glib.Type getType()Get the CairoContext GType- Returns:
- the GType
-