Using Graphic objects: SVG Data

At the runtime of the application, the content of existing SVG resources can't be exchanged nor new resources can be added. This prevents the SVG resources from being suitable to provide image contents determined dynamically at the runtime e.g. loaded from an SD memory card, received from a network adapter or rendered by a third-party application.

In order to be able to deal with such demanding application cases, the Mosaic framework provides the class Graphics::SVG. Unlike SVG resources, objects of this class are optimized to represent and manage SVG image contents being obtained or rendered dynamically at the runtime of the GUI application - thus unknown at the compilation time. Such objects can then be assigned to an SVG view or SVG bitmap, similarly to how regular SVG resources are used. The contents stored inside the SVG Data objects are then displayed on the screen or rasterized inside an off-screen bitmap. When the content of the SVG Data object changes, the associated views and bitmaps are automatically updated.

Take in account SVG compatibility

SVG specification and the feature set are very extensive. The authors of Embedded Wizard have therefore decided to support only a subset of the entire SVG functionality carefully considering which features are useful and practical on less powerful embedded systems. For complete compatibility overview, please see the section SVG versions and compatibility.

Add new SVG Data object

To add a new SVG Data object just at the design time of a GUI component do following:

First ensure that the Templates window is visible.

In Templates window switch to the folder Resources.

In the folder locate the template SVG Data.

Drag & Drop the template into the Composer window:

Eventually name the new added SVG Data object.

Inspect the SVG Data object

As long as the SVG Data object is selected you can inspect and modify its properties conveniently in the Inspector window as demonstrated with the property SVGString in the screenshot below. Please note, for better overview the below screenshot has been made with Inspector window being configured to sort the properties by categories:

This is in so far worth mentioning as all following sections describe diverse features of the SVG Data object by explicitly referring to its corresponding properties. If you are not familiar with the concept of a property and the usage of Inspector window, please read first the preceding chapter Compositing component appearance.

Connect the SVG Data object to a view

The SVG Data object serves as data container where raw SVG image information (scalable vector graphics) is stored. In order to display this image on the screen you have to connect the SVG Data object to the SVG view. The view will thereupon display the SVG image. The SVG Data object can also be assigned to an SVG Bitmap object whereby the SVG image is rasterized as an off-screen bitmap. Such bitmap can then be used as any other regular bitmap within your project.

To connect an SVG Data object to a view or bitmap you have to assign this object to the property SVG of the affected SVG view or SVG Bitmap objects. With the Inspector Assistant you can conveniently select the right object when you edit the initialization expression for the property SVG. For example:

As soon as you have connected the both, the information provided in the SVG Data object appears within the view (or is rasterized inside the off-screen bitmap). If desired, you can assign the same SVG Data object to several views. If during the runtime of the application, the content of the SVG Data object changes, all associated views and bitmaps are updated automatically.

Specify SVG image content

The SVG image content to represent by an SVG Data object is specified in its property SVGString. This property expects a string describing the SVG image as XML text. In the simplest case, you can copy the content of an SVG file and assign it as string to the property. The following example demonstrates the initialization of the property with a string describing a simple SVG image containing two rounded corner rectangles:

var string svg_text = "<svg width='100%' height='100%' viewBox='0 0 100 100'>" + " <rect width='100%' height='100%' rx='20' fill='darkgreen'/>" + " <rect x='10%' y='10%' width='80%' height='80%' rx='10' fill='gold'/>" + "</svg>"; // Assign the string containing the SVG information to the 'SVG_Data' object. SVG_Data.SVGString = svg_text;

Being a string, the value of the property can change at the runtime. You can even create new SVG contents dynamically by composing the desired SVG elements and attributes inside the string. Each modification of the property SVGString provokes an alternation of the represented SVG image and an update of all associated views and off-screen bitmaps.

TIP

In SVG XML syntax, attribute values are enclosed between double ("..") or single quote ('..') signs. Double quote signs, however, are used in Chora syntax to indicate the beginning and the end of a string literal. Therefore be careful when you copy/paste the content of an SVG file into a string literal. The double quote signs existing eventually inside the file will provoke error messages. To prevent that problem, it will be sufficient to replace inside the copied SVG content all double quote by single quote signs. Alternatively you can prefix the double quote signs by the backslash sign (e.g. width=\"100%\").

Handle SVG update and error events

The SVG Data object is intended to process SVG image information obtained at the runtime of the application, e.g. read from an SD card. Since this SVG image information is encoded as ordinary string, it can theoretically contain any text, even text which is not valid for SVG evaluation. During the processing of SVG contents, Embedded Wizard verifies the syntactical correctness of the provided string and in case of an error, aborts the processing. In your application you can react to such event by providing a slot method and connecting it to the SVG Data's property OnError. In case of an error, the slot method is signaled and inside the method the event can be handled.

With the second property OnUpdate your implementation can react to any state change during the lifetime of the SVG Data object. In particular, the slot method associated to this property is signaled each time, when the content of the SVG Data object has changed. In practice it is the case after the alternation of its SVGString property. The following steps describe how to react to the events:

First add a new slot method to your GUI component.

To react to error events, assign the slot method to the property OnError of the SVG Data object.

To react to status update events, assign the slot method to the property OnUpdate of the SVG Data object.

Open the slot method for editing.

In the Code Editor implement your desired operation to execute when the event occurs.

During the execution of the associated slot methods, your implementation can invoke the SVG Data object's methods GetSVGParserStatus() and GetSVGParserErrorPos() to query the current status (e.g. error cause) and in error case the error position within the processed string.