DALi Version 1.9.32
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / third-party / nanosvg / nanosvg.h
1 /*
2  * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * The SVG parser is based on Anti-Grain Geometry 2.4 SVG example
21  * Copyright (C) 2002-2004 Maxim Shemanarev (McSeem) (http://www.antigrain.com/)
22  *
23  * Arc calculation code based on canvg (https://code.google.com/p/canvg/)
24  *
25  * Bounding box calculation based on http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
26  *
27  */
28
29 #ifndef NANOSVG_H
30 #define NANOSVG_H
31
32 // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
33 //
34 // The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
35 //
36 // NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
37 //
38 // The shapes in the SVG images are transformed by the viewBox and converted to specified units.
39 // That is, you should get the same looking data as your designed in your favorite app.
40 //
41 // NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
42 // to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
43 //
44 // The units passed to NanoSVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
45 // DPI (dots-per-inch) controls how the unit conversion is done.
46 //
47 // If you don't know or care about the units stuff, "px" and 96 should get you going.
48
49
50 /* Example Usage:
51   // Load SVG
52   NSVGimage* image;
53   image = nsvgParseFromFile("test.svg", "px", 96);
54   printf("size: %f x %f\n", image->width, image->height);
55   // Use...
56   for (NSVGshape *shape = image->shapes; shape != NULL; shape = shape->next) {
57     for (NSVGpath *path = shape->paths; path != NULL; path = path->next) {
58       for (int i = 0; i < path->npts-1; i += 3) {
59         float* p = &path->pts[i*2];
60         drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
61       }
62     }
63   }
64   // Delete
65   nsvgDelete(image);
66 */
67
68 enum NSVGpaintType {
69   NSVG_PAINT_NONE = 0,
70   NSVG_PAINT_COLOR = 1,
71   NSVG_PAINT_LINEAR_GRADIENT = 2,
72   NSVG_PAINT_RADIAL_GRADIENT = 3
73 };
74
75 enum NSVGspreadType {
76   NSVG_SPREAD_PAD = 0,
77   NSVG_SPREAD_REFLECT = 1,
78   NSVG_SPREAD_REPEAT = 2
79 };
80
81 enum NSVGlineJoin {
82   NSVG_JOIN_MITER = 0,
83   NSVG_JOIN_ROUND = 1,
84   NSVG_JOIN_BEVEL = 2
85 };
86
87 enum NSVGlineCap {
88   NSVG_CAP_BUTT = 0,
89   NSVG_CAP_ROUND = 1,
90   NSVG_CAP_SQUARE = 2
91 };
92
93 enum NSVGfillRule {
94   NSVG_FILLRULE_NONZERO = 0,
95   NSVG_FILLRULE_EVENODD = 1
96 };
97
98 enum NSVGflags {
99   NSVG_FLAGS_VISIBLE = 0x01
100 };
101
102 typedef struct NSVGgradientStop {
103   unsigned int color;
104   float offset;
105 } NSVGgradientStop;
106
107 typedef struct NSVGgradient {
108   float xform[6];
109   char spread;
110   float fx, fy;
111   int nstops;
112   NSVGgradientStop stops[1];
113 } NSVGgradient;
114
115 typedef struct NSVGpaint {
116   /**
117    * In the original file, using char type (without signed or unsigned) can be interpreted
118    * as 'unsigned char' in some build environments, like ARM architecture.
119    * To prevent the unexpected behavior, we replace 'char type' with 'signed char type' here.
120    */
121   signed char type;
122   union {
123     unsigned int  color;
124     NSVGgradient* gradient;
125   };
126 } NSVGpaint;
127
128 typedef struct NSVGpath
129 {
130   float*           pts;         // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
131   int              npts;        // Total number of bezier points.
132   char             closed;      // Flag indicating if shapes should be treated as closed.
133   float            bounds[4];   // Tight bounding box of the shape [minx,miny,maxx,maxy].
134   struct NSVGpath* next;        // Pointer to next path, or NULL if last element.
135 } NSVGpath;
136
137 typedef struct NSVGshape
138 {
139   char              id[64];     // Optional 'id' attr of the shape or its group
140   NSVGpaint         fill;       // Fill paint
141   NSVGpaint         stroke;     // Stroke paint
142   float             opacity;    // Opacity of the shape.
143   float             strokeWidth; // Stroke width (scaled).
144   float             strokeDashOffset; // Stroke dash offset (scaled).
145   float             strokeDashArray[8]; // Stroke dash array (scaled).
146   char              strokeDashCount; // Number of dash values in dash array.
147   char              strokeLineJoin; // Stroke join type.
148   char              strokeLineCap; // Stroke cap type.
149   float             miterLimit; // Miter limit
150   char              fillRule;   // Fill rule, see NSVGfillRule.
151   unsigned char     flags;      // Logical or of NSVG_FLAGS_* flags
152   float             bounds[4];  // Tight bounding box of the shape [minx,miny,maxx,maxy].
153   NSVGpath*         paths;      // Linked list of paths in the image.
154   struct NSVGshape* next;       // Pointer to next shape, or NULL if last element.
155 } NSVGshape;
156
157 typedef struct NSVGimage
158 {
159   float      width;             // Width of the image.
160   float      height;            // Height of the image.
161   NSVGshape* shapes;            // Linked list of shapes in the image.
162 } NSVGimage;
163
164 // Parses SVG file from a file, returns SVG image as paths.
165 NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
166
167 // Parses SVG file from a null terminated string, returns SVG image as paths.
168 // Important note: changes the string.
169 NSVGimage* nsvgParse(char* input, const char* units, float dpi);
170
171 // Duplicates a path.
172 NSVGpath* nsvgDuplicatePath(NSVGpath* p);
173
174 // Deletes an image.
175 void nsvgDelete(NSVGimage* image);
176
177 #endif // NANOSVG_H
178
179 /**
180  * In the original software, The nanosvg implementation was followed here.
181  * We have moved the implementation to nanosvg.cc.
182  */