| Copyright | Will Thompson and Iñaki García Etxebarria |
|---|---|
| License | LGPL-2.1 |
| Maintainer | Iñaki García Etxebarria |
| Safe Haskell | None |
| Language | Haskell2010 |
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
- newtype Svg = Svg (ManagedPtr Svg)
- class (GObject o, IsDescendantOf Svg o) => IsSvg o
- toSvg :: (MonadIO m, IsSvg o) => o -> m Svg
- svgGetFeatures :: (HasCallStack, MonadIO m, IsSvg a) => a -> m [SvgFeatures]
- svgGetNStates :: (HasCallStack, MonadIO m, IsSvg a) => a -> m Word32
- svgGetState :: (HasCallStack, MonadIO m, IsSvg a) => a -> m Word32
- svgGetWeight :: (HasCallStack, MonadIO m, IsSvg a) => a -> m Double
- svgLoadFromBytes :: (HasCallStack, MonadIO m, IsSvg a) => a -> Bytes -> m ()
- svgLoadFromResource :: (HasCallStack, MonadIO m, IsSvg a) => a -> Text -> m ()
- svgNew :: (HasCallStack, MonadIO m) => m Svg
- svgNewFromBytes :: (HasCallStack, MonadIO m) => Bytes -> m Svg
- svgNewFromResource :: (HasCallStack, MonadIO m) => Text -> m Svg
- svgPause :: (HasCallStack, MonadIO m, IsSvg a) => a -> m ()
- svgPlay :: (HasCallStack, MonadIO m, IsSvg a) => a -> m ()
- svgSerialize :: (HasCallStack, MonadIO m, IsSvg a) => a -> m Bytes
- svgSetFeatures :: (HasCallStack, MonadIO m, IsSvg a) => a -> [SvgFeatures] -> m ()
- svgSetFrameClock :: (HasCallStack, MonadIO m, IsSvg a, IsFrameClock b) => a -> b -> m ()
- svgSetState :: (HasCallStack, MonadIO m, IsSvg a) => a -> Word32 -> m ()
- svgSetWeight :: (HasCallStack, MonadIO m, IsSvg a) => a -> Double -> m ()
- svgWriteToFile :: (HasCallStack, MonadIO m, IsSvg a) => a -> Text -> m ()
- constructSvgFeatures :: (IsSvg o, MonadIO m) => [SvgFeatures] -> m (GValueConstruct o)
- getSvgFeatures :: (MonadIO m, IsSvg o) => o -> m [SvgFeatures]
- setSvgFeatures :: (MonadIO m, IsSvg o) => o -> [SvgFeatures] -> m ()
- constructSvgPlaying :: (IsSvg o, MonadIO m) => Bool -> m (GValueConstruct o)
- getSvgPlaying :: (MonadIO m, IsSvg o) => o -> m Bool
- setSvgPlaying :: (MonadIO m, IsSvg o) => o -> Bool -> m ()
- clearSvgResource :: (MonadIO m, IsSvg o) => o -> m ()
- constructSvgResource :: (IsSvg o, MonadIO m) => Text -> m (GValueConstruct o)
- getSvgResource :: (MonadIO m, IsSvg o) => o -> m (Maybe Text)
- setSvgResource :: (MonadIO m, IsSvg o) => o -> Text -> m ()
- constructSvgState :: (IsSvg o, MonadIO m) => Word32 -> m (GValueConstruct o)
- getSvgState :: (MonadIO m, IsSvg o) => o -> m Word32
- setSvgState :: (MonadIO m, IsSvg o) => o -> Word32 -> m ()
- constructSvgWeight :: (IsSvg o, MonadIO m) => Double -> m (GValueConstruct o)
- getSvgWeight :: (MonadIO m, IsSvg o) => o -> m Double
- setSvgWeight :: (MonadIO m, IsSvg o) => o -> Double -> m ()
- type SvgErrorCallback = GError -> IO ()
- afterSvgError :: (IsSvg a, MonadIO m) => a -> ((?self :: a) => SvgErrorCallback) -> m SignalHandlerId
- onSvgError :: (IsSvg a, MonadIO m) => a -> ((?self :: a) => SvgErrorCallback) -> m SignalHandlerId
Exported types
Memory-managed wrapper type.
Constructors
| Svg (ManagedPtr Svg) |
Instances
| Eq Svg Source # | |
| GObject Svg Source # | |
Defined in GI.Gtk.Objects.Svg | |
| ManagedPtrNewtype Svg Source # | |
Defined in GI.Gtk.Objects.Svg Methods toManagedPtr :: Svg -> ManagedPtr Svg Source # | |
| TypedObject Svg Source # | |
| HasParentTypes Svg Source # | |
Defined in GI.Gtk.Objects.Svg | |
| IsGValue (Maybe Svg) Source # | Convert |
| type ParentTypes Svg Source # | |
Defined in GI.Gtk.Objects.Svg | |
class (GObject o, IsDescendantOf Svg o) => IsSvg o Source #
Instances
| (GObject o, IsDescendantOf Svg o) => IsSvg o Source # | |
Defined in GI.Gtk.Objects.Svg | |
Methods
Click to display all available methods, including inherited ones
Methods
bindProperty, bindPropertyFull, computeConcreteSize, forceFloating, freezeNotify, getv, invalidateContents, invalidateSize, isFloating, loadFromBytes, loadFromResource, notify, notifyByPspec, pause, play, ref, refSink, runDispose, serialize, snapshot, snapshotSymbolic, snapshotWithWeight, stealData, stealQdata, thawNotify, unref, watchClosure, writeToFile.
Getters
getCurrentImage, getData, getFeatures, getFlags, getIntrinsicAspectRatio, getIntrinsicHeight, getIntrinsicWidth, getNStates, getProperty, getQdata, getState, getWeight.
Setters
setData, setDataFull, setFeatures, setFrameClock, setProperty, setState, setWeight.
getFeatures
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> m [SvgFeatures] | Returns: the enabled features |
Returns the currently enabled features.
Since: 4.22
getNStates
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> m Word32 | Returns: the state |
Gets the current state of the paintable.
Since: 4.22
getWeight
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> m Double | Returns: the weight |
Gets the value of the weight property.
Since: 4.22
loadFromBytes
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> Bytes |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> Text |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m) | |
| => m Svg | Returns: the paintable |
Creates a new, empty SVG paintable.
Since: 4.22
newFromBytes
Arguments
| :: (HasCallStack, MonadIO m) | |
| => Bytes |
|
| -> m Svg | Returns: the paintable |
Parses the SVG data in bytes and creates a paintable.
Since: 4.22
newFromResource
Arguments
| :: (HasCallStack, MonadIO m) | |
| => Text |
|
| -> m Svg | Returns: the paintable |
Parses the SVG data in the resource and creates a paintable.
Since: 4.22
pause
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> m () |
Stop any playing animations and state transitions.
Animations can be paused and started repeatedly.
Since: 4.22
play
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> m () |
Start playing animations and state transitions.
Animations can be paused and started repeatedly.
Since: 4.22
serialize
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> [SvgFeatures] |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a, IsFrameClock b) | |
| => a |
|
| -> b |
|
| -> m () |
Sets a frame clock.
Without a frame clock, GtkSvg will not advance animations.
Since: 4.22
setState
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> Word32 |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> Double |
|
| -> 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
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> Text |
|
| -> m () | (Can throw |
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
setsvg [ #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
setsvg [ #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
setsvg [ #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
setsvg [ #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
setsvg [ #weight:=value ]
Signals
error
type SvgErrorCallback Source #
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