[Tizen] Enable png, jpg loader
[platform/core/graphics/tizenvg.git] / README.md
1 [![Continuous-integration](https://github.com/Samsung/thorvg/actions/workflows/actions.yml/badge.svg)](https://github.com/Samsung/thorvg/actions/workflows/actions.yml)
2 [![Gitter](https://badges.gitter.im/thorvg/community.svg)](https://gitter.im/thorvg/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3
4 # ThorVG
5
6 <p align="center">
7   <img width="500" height="346" src="https://github.com/Samsung/thorvg/blob/master/res/thorvg_card2.png">
8 </p>
9
10 ThorVG is a platform-independent portable library for drawing vector-based scenes and animation. It's open-source software that is freely used by a variety of software platforms and applications. ThorVG provides neat and easy APIs. Its library has no dependencies and keeps a super compact size. It serves as the vector graphics engine for Tizen OS that powers many products. <br />
11 <br />
12 The following list shows primitives that are supported by ThorVG: <br />
13  - Shapes: Line, Arc, Curve, Path, Polygon, ...
14  - Filling: Solid, Linear and Radial Gradient
15  - Scene Graph & Affine Transformation (translation, rotation, scale, ...)
16  - Stroking: Width, Join, Cap, Dash
17  - Composition: Blending, Masking, Path Clipping, ...
18  - Pictures: SVG, JPG, PNG, Bitmap, ...
19 <p align="center">
20   <img width="930" height="473" src="https://github.com/Samsung/thorvg/blob/master/res/example_primitives.png">
21 </p>
22 <br />
23 If your program has the main renderer, your program could call ThorVG APIs while switching drawing contexts between the main renderer and ThorVG. During the API calls, ThorVG serializes drawing commands among the volatile paints' nodes then performs synchronous/asynchronous rendering using its backend raster engines. ThorVG supports vector images such as SVG, also expands, supporting other popular formats on demand. On the rendering, it could spawn intermediate frame-buffers for compositing scenes only when it's necessary. The next figure shows you a brief strategy on how to use ThorVG on your system.<br />
24 <br />
25 <p align="center">
26   <img width="900" height="288" src="https://github.com/Samsung/thorvg/blob/master/res/example_flow.png">
27 </p>
28 <br />
29
30 ## Contents
31 - [Building ThorVG](#building-thorvg)
32         - [Meson Build](#meson-build)
33 - [Quick Start](#quick-start)
34 - [SVG](#svg)
35 - [TVG Picture](#tvg-picture)
36 - [Practices](#practices)
37         - [Tizen](#tizen)
38         - [Rive](#rive)
39 - [Examples](#examples)
40 - [Tools](#tools)
41         - [ThorVG Viewer](#thorvg-viewer)
42         - [SVG to PNG](#svg-to-png)
43 - [API Bindings](#api-bindings)
44 - [Issues or Feature Requests](#issues-or-feature-requests)
45
46 [](#contents)
47 <br />
48 ## Building ThorVG
49 ThorVG supports [meson](https://mesonbuild.com/) build system.
50 <br />
51 ### Meson Build
52 Install [meson](http://mesonbuild.com/Getting-meson.html) and [ninja](https://ninja-build.org/) if not already installed.
53
54 Run meson to configure ThorVG:
55 ```
56 meson build
57 ```
58 Run ninja to build & install ThorVG:
59 ```
60 ninja -C build install
61 ```
62 [Back to contents](#contents)
63 <br />
64 <br />
65 ## Quick Start
66 ThorVG renders vector shapes to a given canvas buffer. The following is a quick start to show you how to use the essential APIs.
67
68 First, you should initialize the ThorVG engine:
69
70 ```cpp
71 tvg::Initializer::init(tvg::CanvasEngine::Sw, 0);   //engine method, thread count
72 ```
73
74 Then it would be best if you prepared an empty canvas for drawing on it:
75
76 ```cpp
77 static uint32_t buffer[WIDTH * HEIGHT];                                 //canvas target buffer
78
79 auto canvas = tvg::SwCanvas::gen();                                     //generate a canvas
80 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);  //buffer, stride, w, h, Colorspace
81 ```
82
83 Next you can draw multiple shapes on the canvas:
84
85 ```cpp
86 auto rect = tvg::Shape::gen();               //generate a shape
87 rect->appendRect(50, 50, 200, 200, 20, 20);  //define it as a rounded rectangle (x, y, w, h, rx, ry)
88 rect->fill(100, 100, 100, 255);              //set its color (r, g, b, a)
89 canvas->push(move(rect));                    //push the rectangle into the canvas
90
91 auto circle = tvg::Shape::gen();             //generate a shape
92 circle->appendCircle(400, 400, 100, 100);    //define it as a circle (cx, cy, rx, ry)
93
94 auto fill = tvg::RadialGradient::gen();      //generate a radial gradient
95 fill->radial(400, 400, 150);                 //set the radial gradient geometry info (cx, cy, radius)
96
97 tvg::Fill::ColorStop colorStops[2];          //gradient colors
98 colorStops[0] = {0.0, 255, 255, 255, 255};   //1st color values (offset, r, g, b, a)
99 colorStops[1] = {1.0, 0, 0, 0, 255};         //2nd color values (offset, r, g, b, a)
100 fill->colorStops(colorStops, 2);             //set the gradient colors info
101
102 circle->fill(move(fill));                    //set the circle fill
103 canvas->push(move(circle));                  //push the circle into the canvas
104
105 ```
106
107 This code generates the following result:
108
109 <p align="center">
110   <img width="416" height="411" src="https://github.com/Samsung/thorvg/blob/master/res/example_shapes.png">
111 </p>
112
113 You can also draw you own shapes and use dashed stroking:
114
115 ```cpp
116 auto path = tvg::Shape::gen();               //generate a path
117 path->moveTo(199, 34);                       //set sequential path coordinates
118 path->lineTo(253, 143);
119 path->lineTo(374, 160);
120 path->lineTo(287, 244);
121 path->lineTo(307, 365);
122 path->lineTo(199, 309);
123 path->lineTo(97, 365);
124 path->lineTo(112, 245);
125 path->lineTo(26, 161);
126 path->lineTo(146, 143);
127 path->close();
128
129 path->fill(150, 150, 255, 255);              //path color
130
131 path->stroke(3);                             //stroke width
132 path->stroke(0, 0, 255, 255);                //stroke color
133 path->stroke(tvg::StrokeJoin::Round);        //stroke join style
134 path->stroke(tvg::StrokeCap::Round);         //stroke cap style
135
136 float pattern[2] = {10, 10};                 //stroke dash pattern (line, gap)
137 path->stroke(pattern, 2);                    //set the stroke pattern
138
139 canvas->push(move(path));                    //push the path into the canvas
140
141 ```
142
143 The code generates the following result:
144
145 <p align="center">
146   <img width="300" height="300" src="https://github.com/Samsung/thorvg/blob/master/res/example_path.png">
147 </p>
148
149 Now begin rendering & finish it at a particular time:
150
151 ```cpp
152 canvas->draw();
153 canvas->sync();
154 ```
155
156 Then you can acquire the rendered image from the buffer memory.
157
158 Lastly, terminate the engine after its usage:
159
160 ```cpp
161 tvg::Initializer::term(tvg::CanvasEngine::Sw);
162 ```
163 [Back to contents](#contents)
164 <br />
165 <br />
166 ## SVG
167
168 ThorVG supports SVG (Scalable Vector Graphics) rendering through its SVG interpreter. It satisfies the [SVG Tiny Specification](https://www.w3.org/TR/SVGTiny12/)
169 to keep it lightweight, so it's useful for the embedded systems. Among the SVG Tiny specs, unsupported features in the ThorVG are the following:
170
171  - Animation
172  - Fonts & Text 
173  - Interactivity
174  - Multimedia
175  - Scripting
176
177 The following code snippet shows how to draw SVG image using ThorVG:
178
179 ```cpp
180 auto picture = tvg::Picture::gen();         //generate a picture
181 picture->load("tiger.svg");                 //load SVG file
182 canvas->push(move(picture));                //push the picture into the canvas
183 ```
184
185 The result:
186
187 <p align="center">
188   <img width="300" height="300" src="https://github.com/Samsung/thorvg/blob/master/res/example_tiger.png">
189 </p>
190
191 [Back to contents](#contents)
192 <br />
193 <br />
194 ## TVG Picture
195
196 ThorVG provides the designated vector data format which is called TVG Picture. TVG Picture stores a list of properties of the Paint nodes of a scene in binary form. The data saved in a TVG Picture is optimized beforehand, keeping the resulting file small and the data loading process fast and efficient.
197
198 To save data in a TVG Picture format, ThorVG uses a dedicated module - TVG Saver. It is responsible for optimizing the data of all the scene-tree nodes and saving them in binary format. In the optimization process, the TVG Saver filters out unused information, removing the duplicated properties, merges the overlapping shapes and compresses the data if possible, but keeping the TVG Pictures compatible with the later version of ThorVG libraries.
199
200 The final data size is smaller in comparison to any other text-based vector data format, such as SVG, which in turn decreases the required application resources. This helps not only reduce the number of I/O operations but also reduces the memory bandwidth while loading the data. Thus this is effective if your program uses a big amount of the vector resources.
201
202 Additionally, TVG Picture helps to reduce the resource loading tasks since it can skip interpreting the data stage. That brings the reduced amount of the required runtime memory and rendering tasks that increases the performance.
203
204 Utilizing the TVG Picture allows you to reduce the data size and loading time by more than 30%, on average. Note that the charge in the performance rate depends on the resource size and its complexity.
205
206 <p align="center">
207   <img width="910" height="260" src="https://github.com/Samsung/thorvg/blob/master/res/example_tvgsize.png">
208 </p>
209
210 While TVG Saver exports the scene-tree to the TVG Picture data files(TVG), the TVG Loader imports and restores it to the programmable instances. You can quickly use the ThorVG Viewer to convert files from SVG to TVG(Coming soon).
211
212 <p align="center">
213   <img width="649" height="283" src="https://github.com/Samsung/thorvg/blob/master/res/example_tvg.png">
214 </p>
215
216
217 [Back to contents](#contents)
218 <br />
219 <br />
220 ## Practices
221 ### Tizen
222 ThorVG is integrated into the [Tizen](https://www.tizen.org) platform as the vector graphics engine. It's being used for vector primitive drawings
223 and scalable image contents such as SVG and Lottie Animation among the Tizen powered products.
224
225 <p align="center">
226   <img width="798" height="285" src="https://github.com/Samsung/thorvg/blob/master/res/example_tizen.png">
227 </p>
228
229 [Back to contents](#contents)
230 <br />
231 <br />
232 ### Rive
233 We're also building a [Rive](https://rive.app/) port that supports Rive Animation run through the ThorVG backend. Rive is a brand new animation platform
234 that supports fancy, user-interactive vector animations. For more details see [Rive-Tizen](https://github.com/rive-app/rive-tizen) on [Github](https://github.com/rive-app/).
235
236 <p align="center">
237   <img width="600" height="324" src="https://github.com/Samsung/thorvg/blob/master/res/example_rive.gif">
238 </p>
239
240 [Back to contents](#contents)
241 <br />
242 <br />
243 ## Examples
244 There are various examples available in `thorvg/src/examples` to help you understand ThorVG APIs.
245
246 To execute these examples, you can build them with the following meson option:
247 ```
248 meson -Dexamples=true . build
249 ```
250 Note that these examples require the EFL `elementary` package for launching. If you're using Linux-based OS, you can easily
251 install this package from your OS distribution server. Otherwise, please visit the official [EFL page](https://enlightenment.org/) for more information.
252
253 [Back to contents](#contents)
254 <br />
255 <br />
256 ## Tools
257 ### ThorVG Viewer
258 [ThorVG viewer](https://samsung.github.io/thorvg.viewer) supports immediate rendering through your browser. You can drag & drop SVG files on the page
259 and see the rendering result on the spot.
260
261 [Back to contents](#contents)
262 <br />
263 <br />
264 ### SVG to PNG
265 ThorVG provides an executable `svg2png` converter that generates a PNG file from an SVG file.
266
267 To use `svg2png`, you must turn on this feature in the build option:
268 ```
269 meson -Dtools=svg2png . build
270 ```
271 Alternatively, you can add the `svg2png` value to the `tools` option in `meson_option.txt`. The build output will be located in `{builddir}/src/bin/svg2png/`.
272
273 Examples of the usage of the `svg2png`:
274 ```
275 Usage:
276    svg2png [svgFileName] [Resolution] [bgColor]
277
278 Examples:
279     $ svg2png input.svg
280     $ svg2png input.svg 200x200
281     $ svg2png input.svg 200x200 ff00ff
282 ```
283 [Back to contents](#contents)
284 <br />
285 <br />
286 ## API Bindings
287 Our main development APIs are written in C++, but ThorVG also provides API bindings for C.
288
289 [Back to contents](#contents)
290 <br />
291 <br />
292 ## Issues or Feature Requests
293 For support, please reach us in [Gitter](https://gitter.im/thorvg/community).