| 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 (a subset of) SVG, with animations.
GtkSvg objects are created by parsing a subset of SVG,
including SVG animations.
The 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 animation, and the transition between
different states can also be animated.
To find out what states a GtkSvg has, use svgGetNStates.
To set the current state, use svgSetState.
To play the animations in an SVG file, use
svgSetFrameClock to connect the paintable to a frame clock,
and then use svgPlay to start the animation.
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 assume a pathLengh value of 1.
Setting pathLength in your SVG is therefore going to break such
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">
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>.
The supported subset of SVG
The renderer does not support text or images, only paths. From the
shape elements of SVG, only <circle>, <ellipse>, <rect> and
<path> are supported, leaving out <line>, <polyline> and
<polygon>.
In <defs>, only <clipPath>, <mask>, gradients and shapes are
supported, not <filter>, <pattern> or other things.
Gradient templating is not implemented, and radial gradients with
fx,fy != cx,cy are not supported.
The support for filters is limited to filter functions minus
drop-shadow() plus a custom alpha-level() function, which
implements one particular case of feComponentTransfer.
The transform-origin and transform-box attributes are not supported.
The 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 by, min and max attributes are not supported.
Lastly, there is no CSS support, and no interactivity.
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 rednering, 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.
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
- 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 ()
- 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
- 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 ()
- 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 ()
- constructSvgResource :: (IsSvg o, MonadIO m) => Text -> m (GValueConstruct o)
- 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 # | |
| TypedObject Svg Source # | |
Defined in GI.Gtk.Objects.Svg | |
| HasParentTypes Svg Source # | |
Defined in GI.Gtk.Objects.Svg | |
| IsGValue (Maybe Svg) Source # | Convert |
Defined in GI.Gtk.Objects.Svg Methods gvalueGType_ :: IO GType # gvalueSet_ :: Ptr GValue -> Maybe Svg -> IO () # gvalueGet_ :: Ptr GValue -> IO (Maybe Svg) # | |
| type ParentTypes Svg 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, notify, notifyByPspec, pause, play, ref, refSink, runDispose, serialize, snapshot, snapshotSymbolic, snapshotWithWeight, stealData, stealQdata, thawNotify, unref, watchClosure, writeToFile.
Getters
getCurrentImage, getData, getFlags, getIntrinsicAspectRatio, getIntrinsicHeight, getIntrinsicWidth, getNStates, getProperty, getQdata, getState, getWeight.
Setters
setData, setDataFull, setFrameClock, setProperty, setState, setWeight.
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
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.
Animations can be paused and started repeatedly.
Since: 4.22
play
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> m () |
Start playing animations.
Note that this is necessary for state changes as well.
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
setFrameClock
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a, IsFrameClock b) | |
| => a |
|
| -> b |
|
| -> m () |
Sets a frame clock.
Without a frame clock, GTK has to rely on simple timeouts to run 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.
Note that svgPlay must have been
called for the SVG paintable to react to state changes.
Since: 4.22
setWeight
Arguments
| :: (HasCallStack, MonadIO m, IsSvg a) | |
| => a |
|
| -> Double |
|
| -> m () |
Sets the weight that is used when rendering.
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
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
Construct-only property to create a paintable from a resource in ui files.
Since: 4.22
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.
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 #
Arguments
| = GError |
|
| -> 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