- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
ImageSurface
,PDFSurface
,PSSurface
,RecordingSurface
,ScriptSurface
,SurfaceObserver
,SVGSurface
,TeeSurface
Surface is the abstract type representing all different drawing targets that cairo can render to. The actual drawings are performed using a cairo context.
A cairo surface is created by using backend-specific constructors, typically
of the form of a static create()
method.
Most surface types allow accessing the surface without using Cairo functions.
If you do this, keep in mind that it is mandatory that you call
flush()
before reading from or writing to the surface and
that you must use markDirty()
after modifying it.
Note that for surface types other than ImageSurface it might be necessary to
acquire the surface's device first. See Device.acquire()
for a
discussion of devices.
-
Constructor Summary
ConstructorDescriptionSurface
(MemorySegment address) Constructor used internally to instantiate a java Surface object for a nativecairo_surface_t
instance -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closing a surface will invokefinish()
, which will flush the surface and drop all references to external resources.copyPage()
Emits the current page for backends that support multiple pages, but doesn't clear it, so that the contents of the current page will be retained for the next page.static Surface
createForRectangle
(Surface target, double x, double y, double width, double height) Create a new surface that is a rectangle within the target surface.static <T extends Surface>
TcreateSimilar
(T other, Content content, int width, int height) Create a new surface that is as compatible as possible with an existing surface.static ImageSurface
createSimilarImage
(Surface other, Format format, int width, int height) Create a new image surface that is as compatible as possible for uploading to and the use in conjunction with an existing surface.void
destroy()
Invokes the cleanup action that is normally invoked during garbage collection.void
finish()
This function finishes the surface and drops all references to external resources.flush()
Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state.This function returns the content type of surface which indicates whether the surface contains color and/or alpha information.This function returns the device for a surface.This function returns the previous device offset set bysetDeviceOffset(double, double)
.This function returns the previous device scale set bysetDeviceScale(double, double)
.This function returns the previous fallback resolution set bysetFallbackResolution(double, double)
, or default fallback resolution if never set.void
getFontOptions
(FontOptions options) Retrieves the default font rendering options for the surface.byte[]
getMimeData
(MimeType mimeType) Return mime data previously attached to the surface using the specified mime type.This function returns the type of the backend used to create a surface.static org.gnome.glib.Type
getType()
Get the CairoSurface GTypegetUserData
(UserDataKey key) Return user data previously attached to the surface using the specified key.boolean
Returns whether the surface supports sophisticatedContext.showTextGlyphs(java.lang.String, org.freedesktop.cairo.Glyphs)
operations.mapToImage
(RectangleInt extents) Returns an image surface that is the most efficient mechanism for modifying the backing store of the target surface.Tells cairo that drawing has been done to surface using means other than cairo, and that cairo should reread any cached areas.markDirtyRectangle
(int x, int y, int width, int height) LikemarkDirty()
, but drawing has been done only to the specified rectangle, so that cairo can retain cached contents for other parts of the surface.setDeviceOffset
(double xOffset, double yOffset) Sets an offset that is added to the device coordinates determined by the CTM when drawing to surface .setDeviceScale
(double xScale, double yScale) Sets a scale that is multiplied to the device coordinates determined by the CTM when drawing to surface .setFallbackResolution
(double xPixelsPerInch, double yPixelsPerInch) Set the horizontal and vertical resolution for image fallbacks.setMimeData
(MimeType mimeType, byte[] data) Attach an image in the format mimeType to the surface.setUserData
(UserDataKey key, MemorySegment userData) Attach user data to the surface.showPage()
Emits and clears the current page for backends that support multiple pages.status()
Checks whether an error has previously occurred for this surface.boolean
supportsMimeType
(MimeType mimeType) Return whether this surface supportsmime_type
.void
unmapImage
(ImageSurface image) Unmaps the image surface as returned frommapToImage(RectangleInt)
.
-
Constructor Details
-
Surface
Constructor used internally to instantiate a java Surface object for a nativecairo_surface_t
instance- Parameters:
address
- the memory address of the nativecairo_surface_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. -
createSimilar
Create a new surface that is as compatible as possible with an existing surface. For example the new surface will have the same device scale, fallback resolution and font options as other . Generally, the new surface will also use the same backend as other , unless that is not possible for some reason. The type of the returned surface may be examined withgetSurfaceType()
.Initially the surface contents are all 0 (transparent if contents have transparency, black otherwise.)
Use
createSimilarImage(Surface, Format, int, int)
if you need an image surface which can be painted quickly to the target surface.- Type Parameters:
T
- the type of the created Surface- Parameters:
other
- an existing surface used to select the backend of the new surfacecontent
- the content for the new surfacewidth
- width of the new surface, (in device-space units)height
- height of the new surface (in device-space units)- Returns:
- the newly allocated surface.
- Since:
- 1.0
-
createSimilarImage
Create a new image surface that is as compatible as possible for uploading to and the use in conjunction with an existing surface. However, this surface can still be used like any normal image surface. UnlikecreateSimilar(Surface, Content, int, int)
the new image surface won't inherit the device scale fromother
.Initially the surface contents are all 0 (transparent if contents have transparency, black otherwise.)
Use
createSimilar(Surface, Content, int, int)
if you don't need an image surface.- Parameters:
other
- an existing surface used to select the preference of the new surfaceformat
- the format for the new surfacewidth
- width of the new surface, (in pixels)height
- height of the new surface, (in pixels)- Returns:
- the newly allocated surface
- Since:
- 1.12
-
createForRectangle
public static Surface createForRectangle(Surface target, double x, double y, double width, double height) Create a new surface that is a rectangle within the target surface. All operations drawn to this surface are then clipped and translated onto the target surface. Nothing drawn via this sub-surface outside of its bounds is drawn onto the target surface, making this a useful method for passing constrained child surfaces to library routines that draw directly onto the parent surface, i.e. with no further backend allocations, double buffering or copies.The semantics of subsurfaces have not been finalized yet unless the rectangle is in full device units, is contained within the extents of the target surface, and the target or subsurface's device transforms are not changed.
- Parameters:
target
- an existing surface for which the sub-surface will point tox
- the x-origin of the sub-surface from the top-left of the target surface (in device-space units)y
- the y-origin of the sub-surface from the top-left of the target surface (in device-space units)width
- width of the sub-surface (in device-space units)height
- height of the sub-surface (in device-space units)- Returns:
- the newly allocated surface.
- Since:
- 1.10
-
status
Checks whether an error has previously occurred for this surface.- Returns:
Status.SUCCESS
,Status.NULL_POINTER
,Status.NO_MEMORY
,Status.READ_ERROR
,Status.INVALID_CONTENT
,Status.INVALID_FORMAT
, orStatus.INVALID_VISUAL
.- Since:
- 1.0
-
finish
public void finish()This function finishes the surface and drops all references to external resources. For example, for the Xlib backend it means that cairo will no longer access the drawable, which can be freed. After callingfinish()
the only valid operations on a surface are checking status, getting and setting user, referencing and destroying, and flushing and finishing it. Further drawing to the surface will not affect the surface but will instead trigger aStatus.SURFACE_FINISHED
error.When the Surface instance is destroyed, cairo will call
finish()
if it hasn't been called already, before freeing the resources associated with the surface.- Since:
- 1.0
-
flush
Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state. This function must be called before switching from drawing on the surface with cairo to drawing on it directly with native APIs, or accessing its memory outside of Cairo. If the surface doesn't support direct access, then this function does nothing.- Returns:
- the surface
- Since:
- 1.0
-
getDevice
-
getFontOptions
Retrieves the default font rendering options for the surface. This allows display surfaces to report the correct subpixel order for rendering on them, print surfaces to disable hinting of metrics and so forth. The result can then be used with cairo_scaled_font_create().- Parameters:
options
- a FontOptions object into which to store the retrieved options. All existing values are overwritten- Since:
- 1.0
-
getContent
-
markDirty
-
markDirtyRectangle
LikemarkDirty()
, but drawing has been done only to the specified rectangle, so that cairo can retain cached contents for other parts of the surface.Any cached clip set on the surface will be reset by this function, to make sure that future cairo calls have the clip set that they expect.
- Parameters:
x
- X coordinate of dirty rectangley
- Y coordinate of dirty rectanglewidth
- width of dirty rectangleheight
- height of dirty rectangle- Returns:
- the surface
- Since:
- 1.0
-
setDeviceOffset
Sets an offset that is added to the device coordinates determined by the CTM when drawing to surface . One use case for this function is when we want to create a cairo_surface_t that redirects drawing for a portion of an onscreen surface to an offscreen surface in a way that is completely invisible to the user of the cairo API. Setting a transformation viaContext.translate(double, double)
isn't sufficient to do this, since functions likeContext.deviceToUser(Point)
will expose the hidden offset.Note that the offset affects drawing to the surface as well as using the surface in a source pattern.
- Parameters:
xOffset
- the offset in the X direction, in device unitsyOffset
- the offset in the Y direction, in device units- Returns:
- the surface
- Since:
- 1.0
-
getDeviceOffset
This function returns the previous device offset set bysetDeviceOffset(double, double)
.- Returns:
- the x and y values of the returned Point object contain the offset in the X and Y direction, in device units
- Since:
- 1.2
-
getDeviceScale
This function returns the previous device scale set bysetDeviceScale(double, double)
.- Returns:
- the x and y values of the returned Point object contain the scale in the X and Y direction, in device units
- Since:
- 1.14
-
setDeviceScale
Sets a scale that is multiplied to the device coordinates determined by the CTM when drawing to surface . One common use for this is to render to very high resolution display devices at a scale factor, so that code that assumes 1 pixel will be a certain size will still work. Setting a transformation viaContext.scale(double, double)
isn't sufficient to do this, since functions likeContext.deviceToUser(Point)
will expose the hidden scale.Note that the scale affects drawing to the surface as well as using the surface in a source pattern.
- Parameters:
xScale
- a scale factor in the X directionyScale
- a scale factor in the Y direction- Returns:
- the surface
- Since:
- 1.14
-
setFallbackResolution
Set the horizontal and vertical resolution for image fallbacks.When certain operations aren't supported natively by a backend, cairo will fallback by rendering operations to an image and then overlaying that image onto the output. For backends that are natively vector-oriented, this function can be used to set the resolution used for these image fallbacks, (larger values will result in more detailed images, but also larger file sizes).
Some examples of natively vector-oriented backends are the ps, pdf, and svg backends.
For backends that are natively raster-oriented, image fallbacks are still possible, but they are always performed at the native device resolution. So this function has no effect on those backends.
Note: The fallback resolution only takes effect at the time of completing a page (with
Context.showPage()
orContext.copyPage()
) so there is currently no way to have more than one fallback resolution in effect on a single page.The default fallback resolution is 300 pixels per inch in both dimensions.
- Parameters:
xPixelsPerInch
- horizontal setting for pixels per inchyPixelsPerInch
- vertical setting for pixels per inch- Returns:
- the surface
- Since:
- 1.2
-
getFallbackResolution
This function returns the previous fallback resolution set bysetFallbackResolution(double, double)
, or default fallback resolution if never set.- Returns:
- the x and y values of the returned Point object contain the horizontal and vertical pixels per inch
- Since:
- 1.8
-
getSurfaceType
This function returns the type of the backend used to create a surface. SeeSurfaceType
for available types.- Returns:
- The type of
Surface
. - Since:
- 1.2
-
copyPage
Emits the current page for backends that support multiple pages, but doesn't clear it, so that the contents of the current page will be retained for the next page. UseshowPage()
if you want to get an empty page after the emission.There is a convenience function for this in
Context
, namelyContext.copyPage()
.- Returns:
- the surface
- Since:
- 1.6
-
showPage
Emits and clears the current page for backends that support multiple pages. UsecopyPage()
if you don't want to clear the page.There is a convenience function for this that takes a
Context
namelyContext.showPage()
.- Returns:
- the surface
- Since:
- 1.6
-
hasShowTextGlyphs
public boolean hasShowTextGlyphs()Returns whether the surface supports sophisticatedContext.showTextGlyphs(java.lang.String, org.freedesktop.cairo.Glyphs)
operations. That is, whether it actually uses the provided text and cluster data to aContext.showTextGlyphs(java.lang.String, org.freedesktop.cairo.Glyphs)
call.Note: Even if this function returns
false
, aContext.showTextGlyphs(java.lang.String, org.freedesktop.cairo.Glyphs)
operation targeted at surface will still succeed. It just will act like aContext.showGlyphs(org.freedesktop.cairo.Glyphs)
operation. Users can use this function to avoid computing UTF-8 text and cluster mapping if the target surface does not use it.- Returns:
true
if surface supportsContext.showTextGlyphs(java.lang.String, org.freedesktop.cairo.Glyphs)
,false
otherwise- Since:
- 1.8
-
setMimeData
Attach an image in the format mimeType to the surface. To remove the data from a surface, call this function with same mime type andnull
for data.The attached image (or filename) data can later be used by backends which support it (currently: PDF, PS, SVG and Win32 Printing surfaces) to emit this data instead of making a snapshot of the surface. This approach tends to be faster and requires less memory and disk space.
The recognized MIME types are the following:
MimeType.JPEG
,MimeType.PNG
,MimeType.JP2
,MimeType.URI
,MimeType.UNIQUE_ID
,MimeType.JBIG2
,MimeType.JBIG2_GLOBAL
,MimeType.JBIG2_GLOBAL_ID
,MimeType.CCITT_FAX
,MimeType.CCITT_FAX_PARAMS
.See corresponding backend surface docs for details about which MIME types it can handle. Caution: the associated MIME data will be discarded if you draw on the surface afterwards. Use this function with care.
Even if a backend supports a MIME type, that does not mean cairo will always be able to use the attached MIME data. For example, if the backend does not natively support the compositing operation used to apply the MIME data to the backend. In that case, the MIME data will be ignored. Therefore, to apply an image in all cases, it is best to create an image surface which contains the decoded image data and then attach the MIME data to that. This ensures the image will always be used while still allowing the MIME data to be used whenever possible.
Language binding note: The data is passed to cairo as a memory segment backed by the on-heap region of memory that holds the given byte array. See
MemorySegment.ofArray(byte[])
for more details.- Parameters:
mimeType
- the MIME type of the image datadata
- the image data to attach to the surface- Returns:
- the surface
- Since:
- 1.10
-
getMimeData
Return mime data previously attached to the surface using the specified mime type. If no data has been attached with the given mime type,null
is returned.Language binding note: The returned mime data is a copy of the actual data attached to the surface.
- Parameters:
mimeType
- the mime type of the image data- Returns:
- the image data to attached to the surface
- Since:
- 1.10
-
supportsMimeType
Return whether this surface supportsmime_type
.- Parameters:
mimeType
- the mime type- Returns:
- mimeType
true
if this surface supportsmime_type
,false
otherwise - Since:
- 1.12
-
mapToImage
Returns an image surface that is the most efficient mechanism for modifying the backing store of the target surface. The region retrieved may be limited to the extents ornull
for the whole surface.Note, the use of the original surface as a target or source whilst it is mapped is undefined. The result of mapping the surface multiple times is undefined. Calling
close()
destroy()
orfinish()
on the resulting image surface results in undefined behavior. Letting the image surface get garbage-collected from Java results in undefined behavior. Changing the device transform of the image surface or of the source surface before the image surface is unmapped results in undefined behavior.- Parameters:
extents
- limit the extraction to an rectangular region- Returns:
- the newly allocated surface. This function always returns a valid pointer, but it will return a pointer to a "nil" surface if the original is already in an error state or any other error occurs.
- Since:
- 1.12
-
unmapImage
Unmaps the image surface as returned frommapToImage(RectangleInt)
.The content of the image will be uploaded to the target surface. Afterwards, the image is destroyed.
Using an image surface which wasn't returned by
mapToImage(RectangleInt)
results in undefined behavior.- Parameters:
image
- the currently mapped image- Since:
- 1.12
-
close
public void close()Closing a surface will invokefinish()
, which will flush the surface and drop all references to external resources. A closed surface cannot be used to perform drawing operations and cannot be reopened.Although the Java bindings make an effort to properly dispose native resources using a
Cleaner
, this is not guaranteed to work in all situations. Users must therefore always callclose()
orfinish()
on surfaces (either manually or with a try-with-resources statement) or risk issues like resource exhaustion and data loss.- Specified by:
close
in interfaceAutoCloseable
-
setUserData
Attach user data to the surface. To remove user data from a surface, 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 surface- Returns:
- the key
- Throws:
NullPointerException
- ifkey
isnull
- Since:
- 1.4
-
getUserData
Return user data previously attached to the surface 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 CairoSurface GType- Returns:
- the GType
-