gi-gtk4-4.0.12: Gtk 4.x bindings
CopyrightWill Thompson and Iñaki García Etxebarria
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellNone
LanguageHaskell2010

GI.Gtk.Objects.Svg

Description

A paintable implementation that renders SVG, with animations.

GtkSvg objects are created by parsing a subset of SVG, including SVG animations.

GtkSvg fills or strokes paths with symbolic or fixed colors. It can have multiple states, and paths can be included in a subset of the states. The special 'empty' state is always available. States can have animations, and the transition between different states can also be animated.

To show a static SVG image, it is enough to load the the SVG and use it like any other paintable.

To play an SVG animation, use svgSetFrameClock to connect the paintable to a frame clock, and call svgPlay after loading the SVG. The animation can be paused using svgPause.

To find out what states a GtkSvg has, use svgGetNStates. To set the current state, use svgSetState.

Error handling

Loading an SVG into GtkSvg will always produce a (possibly empty) paintable. GTK will drop things that it can't handle and try to make sense of the rest.

To track errors during parsing or rendering, connect to the Svg::error signal.

For parsing errors in the GTK_SVG_ERROR domain, the functions SvgError.get_start(), SvgError.get_end(), SvgError.get_element() and SvgError.get_attribute() can be used to obtain information about where the error occurred.

The supported subset of SVG

The paintable supports much of SVG 2, with some exceptions.

Among the graphical elements, <textPath> and <foreignObject> are not supported.

Among the structural elements, <a> and <view> are not supported.

In the <filter> element, the following primitives are not supported: feConvolveMatrix, feDiffuseLighting, feMorphology, feSpecularLighting and feTurbulence.

Support for the mask attribute is limited to just a url referring to the <mask> element by ID.

In animation elements, the parsing of begin and end attributes is limited, and the min and max attributes are not supported.

Lastly, there is only minimal CSS support (the style attribute, but not <style>), and no interactivity.

SVG Extensions

The paintable supports a number of custom attributes that offer a convenient way to define states, transitions and animations. For example,

<circle cx='5' cy='5' r='5' gpa:states='0 1' gpa:animation-type='automatic' gpa:animation-direction='segment' gpa:animation-duration='600ms'/>

defines the circle to be shown in states 0 and 1, and animates a segment of the circle.

<image src="svg-renderer1.svg">

Note that the generated animations are implemented using standard SVG attributes (visibility, stroke-dasharray, stroke-dashoffset, pathLength and filter@). Setting these attributes in your SVG is therefore going to interfere with generated animations.

To connect general SVG animations to the states of the paintable, use the custom gpa:states(...) condition in the begin and end attributes of SVG animation elements. For example,

<animate href='path1' attributeName='fill' begin='gpa:states(0).begin' dur='300ms' fill='freeze' from='black' to='magenta'/>

will make the fill color of path1 transition from black to magenta when the renderer enters state 0.

<image src="svg-renderer2.svg">

The gpa:states(...) condition triggers for upcoming state changes as well, to support fade-out transitions. For example,

<animate href='path1' attributeName='opacity' begin='gpa:states(0).end -300ms' dur='300ms' fill='freeze' from='1' to='0'/>

will start a fade-out of path1 300ms before state 0 ends.

In addition to the gpa:fill and gpa:stroke attributes, symbolic colors can also be specified as a custom paint server reference, like this: url(gpa:warning). This works in fill and stroke attributes, but also when specifying colors in SVG animation attributes like to or values.

Note that the SVG syntax allows for a fallback RGB color to be specified after the url, for compatibility with other SVG consumers:

fill='url(gpa:#warning) orange'

In contrast to SVG 1.1 and 2.0, we allow the transform attribute to be animated with @<animate>`.

Since: 4.22

Synopsis

Exported types

newtype Svg Source #

Memory-managed wrapper type.

Constructors

Svg (ManagedPtr Svg) 

Instances

Instances details
Eq Svg Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

Methods

(==) :: Svg -> Svg -> Bool Source #

(/=) :: Svg -> Svg -> Bool Source #

GObject Svg Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

ManagedPtrNewtype Svg Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

TypedObject Svg Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

HasParentTypes Svg Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

IsGValue (Maybe Svg) Source #

Convert Svg to and from GValue. See toGValue and fromGValue.

Instance details

Defined in GI.Gtk.Objects.Svg

type ParentTypes Svg Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

class (GObject o, IsDescendantOf Svg o) => IsSvg o Source #

Type class for types which can be safely cast to Svg, for instance with toSvg.

Instances

Instances details
(GObject o, IsDescendantOf Svg o) => IsSvg o Source # 
Instance details

Defined in GI.Gtk.Objects.Svg

toSvg :: (MonadIO m, IsSvg o) => o -> m Svg Source #

Cast to Svg, for types for which this is known to be safe. For general casts, use castTo.

Methods

getFeatures

svgGetFeatures Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m [SvgFeatures]

Returns: the enabled features

Returns the currently enabled features.

Since: 4.22

getNStates

svgGetNStates Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m Word32

Returns: the number of states

Gets the number of states defined in the SVG.

Note that there is always an empty state, which does not count towards this number. If this function returns the value N, the meaningful states of the SVG are 0, 1, ..., N - 1 and GTK_SVG_STATE_EMPTY.

Since: 4.22

getState

svgGetState Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m Word32

Returns: the state

Gets the current state of the paintable.

Since: 4.22

getWeight

svgGetWeight Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m Double

Returns: the weight

Gets the value of the weight property.

Since: 4.22

loadFromBytes

svgLoadFromBytes Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> Bytes

bytes: the data to load

-> m () 

Loads SVG content into an existing SVG paintable.

To track errors while loading SVG content, connect to the Svg::error signal.

This clears any previously loaded content.

Since: 4.22

loadFromResource

svgLoadFromResource Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> Text

path: the resource path

-> m () 

Loads SVG content into an existing SVG paintable.

To track errors while loading SVG content, connect to the Svg::error signal.

This clears any previously loaded content.

Since: 4.22

new

svgNew Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> m Svg

Returns: the paintable

Creates a new, empty SVG paintable.

Since: 4.22

newFromBytes

svgNewFromBytes Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> Bytes

bytes: the data

-> m Svg

Returns: the paintable

Parses the SVG data in bytes and creates a paintable.

Since: 4.22

newFromResource

svgNewFromResource Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> Text

path: the resource path

-> m Svg

Returns: the paintable

Parses the SVG data in the resource and creates a paintable.

Since: 4.22

pause

svgPause Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m () 

Stop any playing animations and state transitions.

Animations can be paused and started repeatedly.

Since: 4.22

play

svgPlay Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m () 

Start playing animations and state transitions.

Animations can be paused and started repeatedly.

Since: 4.22

serialize

svgSerialize Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> m Bytes

Returns: the serialized contents

Serializes the content of the renderer as SVG.

The SVG will be similar to the orignally loaded one, but is not guaranteed to be 100% identical.

This function serializes the DOM, i.e. the results of parsing the SVG. It does not reflect the effect of applying animations.

Since: 4.22

setFeatures

svgSetFeatures Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> [SvgFeatures]

features: features to enable

-> m () 

Enables or disables features of the SVG paintable.

By default, all features are enabled.

Note that this call only has an effect before the SVG is loaded.

Since: 4.22

setFrameClock

svgSetFrameClock Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a, IsFrameClock b) 
=> a

self: an SVG paintable

-> b

clock: the frame clock

-> m () 

Sets a frame clock.

Without a frame clock, GtkSvg will not advance animations.

Since: 4.22

setState

svgSetState Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> Word32

state: the state to set, as a value between 0 and 63, or GTK_SVG_STATE_EMPTY

-> m () 

Sets the state of the paintable.

Use svgGetNStates to find out what states self has.

If the paintable is currently playing, the state change will apply transitions that are defined in the SVG. If the paintable is not playing, the state change will take effect instantaneously.

Since: 4.22

setWeight

svgSetWeight Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> Double

weight: the font weight, as a value between -1 and 1000

-> m () 

Sets the weight that is used when rendering.

The weight affects the effective linewidth when stroking paths.

The default value of -1 means to use the font weight from CSS.

Since: 4.22

writeToFile

svgWriteToFile Source #

Arguments

:: (HasCallStack, MonadIO m, IsSvg a) 
=> a

self: an SVG paintable

-> Text

filename: the file to save to

-> m ()

(Can throw GError)

Serializes the paintable, and saves the result to a file.

Since: 4.22

Properties

features

Enabled features for this paintable.

Note that features have to be set before loading SVG data to take effect.

Since: 4.22

constructSvgFeatures :: (IsSvg o, MonadIO m) => [SvgFeatures] -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “features” property. This is rarely needed directly, but it is used by new.

getSvgFeatures :: (MonadIO m, IsSvg o) => o -> m [SvgFeatures] Source #

Get the value of the “features” property. When overloading is enabled, this is equivalent to

get svg #features

setSvgFeatures :: (MonadIO m, IsSvg o) => o -> [SvgFeatures] -> m () Source #

Set the value of the “features” property. When overloading is enabled, this is equivalent to

set svg [ #features := value ]

playing

Whether the paintable is currently animating its content.

To set this property, use the svgPlay and svgPause functions.

Since: 4.22

constructSvgPlaying :: (IsSvg o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “playing” property. This is rarely needed directly, but it is used by new.

getSvgPlaying :: (MonadIO m, IsSvg o) => o -> m Bool Source #

Get the value of the “playing” property. When overloading is enabled, this is equivalent to

get svg #playing

setSvgPlaying :: (MonadIO m, IsSvg o) => o -> Bool -> m () Source #

Set the value of the “playing” property. When overloading is enabled, this is equivalent to

set svg [ #playing := value ]

resource

Resource to load SVG data from.

This property is meant to create a paintable from a resource in ui files.

Since: 4.22

clearSvgResource :: (MonadIO m, IsSvg o) => o -> m () Source #

Set the value of the “resource” property to Nothing. When overloading is enabled, this is equivalent to

clear #resource

constructSvgResource :: (IsSvg o, MonadIO m) => Text -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “resource” property. This is rarely needed directly, but it is used by new.

getSvgResource :: (MonadIO m, IsSvg o) => o -> m (Maybe Text) Source #

Get the value of the “resource” property. When overloading is enabled, this is equivalent to

get svg #resource

setSvgResource :: (MonadIO m, IsSvg o) => o -> Text -> m () Source #

Set the value of the “resource” property. When overloading is enabled, this is equivalent to

set svg [ #resource := value ]

state

The current state of the renderer.

This can be a number between 0 and 63, or the special value (unsigned int) -1 to indicate the 'empty' state in which nothing is drawn.

Since: 4.22

constructSvgState :: (IsSvg o, MonadIO m) => Word32 -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “state” property. This is rarely needed directly, but it is used by new.

getSvgState :: (MonadIO m, IsSvg o) => o -> m Word32 Source #

Get the value of the “state” property. When overloading is enabled, this is equivalent to

get svg #state

setSvgState :: (MonadIO m, IsSvg o) => o -> Word32 -> m () Source #

Set the value of the “state” property. When overloading is enabled, this is equivalent to

set svg [ #state := value ]

weight

If not set to -1, this value overrides the weight used when rendering the paintable.

Since: 4.22

constructSvgWeight :: (IsSvg o, MonadIO m) => Double -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “weight” property. This is rarely needed directly, but it is used by new.

getSvgWeight :: (MonadIO m, IsSvg o) => o -> m Double Source #

Get the value of the “weight” property. When overloading is enabled, this is equivalent to

get svg #weight

setSvgWeight :: (MonadIO m, IsSvg o) => o -> Double -> m () Source #

Set the value of the “weight” property. When overloading is enabled, this is equivalent to

set svg [ #weight := value ]

Signals

error

type SvgErrorCallback Source #

Arguments

 = GError

error: the error

-> IO () 

Signals that an error occurred.

Errors can occur both during parsing and during rendering.

The expected error values are in the [errorgtk.SvgError] enumeration, context information about the location of parsing errors can be obtained with the various gtk_svg_error functions.

Parsing errors are never fatal, so the parsing will resume after the error. Errors may however cause parts of the given data or even all of it to not be parsed at all. So it is a useful idea to check that the parsing succeeds by connecting to this signal.

::: note This signal is emitted in the middle of parsing or rendering, and if you handle it, you must be careful. Logging the errors you receive is fine, but modifying the widget hierarchy or changing the paintable state definitively isn't.

If in doubt, defer to an idle.

Since: 4.22

afterSvgError :: (IsSvg a, MonadIO m) => a -> ((?self :: a) => SvgErrorCallback) -> m SignalHandlerId Source #

Connect a signal handler for the error signal, to be run after the default handler. When overloading is enabled, this is equivalent to

after svg #error callback

By default the object invoking the signal is not passed to the callback. If you need to access it, you can use the implit ?self parameter. Note that this requires activating the ImplicitParams GHC extension.

onSvgError :: (IsSvg a, MonadIO m) => a -> ((?self :: a) => SvgErrorCallback) -> m SignalHandlerId Source #

Connect a signal handler for the error signal, to be run before the default handler. When overloading is enabled, this is equivalent to

on svg #error callback