nanosvg.h 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926
  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. #ifndef NANOSVG_H
  29. #define NANOSVG_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
  34. //
  35. // The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
  36. //
  37. // NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
  38. //
  39. // The shapes in the SVG images are transformed by the viewBox and converted to specified units.
  40. // That is, you should get the same looking data as your designed in your favorite app.
  41. //
  42. // NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
  43. // to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
  44. //
  45. // The units passed to NanoVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
  46. // DPI (dots-per-inch) controls how the unit conversion is done.
  47. //
  48. // If you don't know or care about the units stuff, "px" and 96 should get you going.
  49. /* Example Usage:
  50. // Load
  51. NSVGImage* image;
  52. image = nsvgParseFromFile("test.svg", "px", 96);
  53. printf("size: %f x %f\n", image->width, image->height);
  54. // Use...
  55. for (NSVGshape *shape = image->shapes; shape != NULL; shape = shape->next) {
  56. for (NSVGpath *path = shape->paths; path != NULL; path = path->next) {
  57. for (int i = 0; i < path->npts-1; i += 3) {
  58. float* p = &path->pts[i*2];
  59. drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
  60. }
  61. }
  62. }
  63. // Delete
  64. nsvgDelete(image);
  65. */
  66. enum NSVGpaintType {
  67. NSVG_PAINT_NONE = 0,
  68. NSVG_PAINT_COLOR = 1,
  69. NSVG_PAINT_LINEAR_GRADIENT = 2,
  70. NSVG_PAINT_RADIAL_GRADIENT = 3
  71. };
  72. enum NSVGspreadType {
  73. NSVG_SPREAD_PAD = 0,
  74. NSVG_SPREAD_REFLECT = 1,
  75. NSVG_SPREAD_REPEAT = 2
  76. };
  77. enum NSVGlineJoin {
  78. NSVG_JOIN_MITER = 0,
  79. NSVG_JOIN_ROUND = 1,
  80. NSVG_JOIN_BEVEL = 2
  81. };
  82. enum NSVGlineCap {
  83. NSVG_CAP_BUTT = 0,
  84. NSVG_CAP_ROUND = 1,
  85. NSVG_CAP_SQUARE = 2
  86. };
  87. enum NSVGfillRule {
  88. NSVG_FILLRULE_NONZERO = 0,
  89. NSVG_FILLRULE_EVENODD = 1
  90. };
  91. enum NSVGflags {
  92. NSVG_FLAGS_VISIBLE = 0x01
  93. };
  94. typedef struct NSVGgradientStop {
  95. unsigned int color;
  96. float offset;
  97. } NSVGgradientStop;
  98. typedef struct NSVGgradient {
  99. float xform[6];
  100. char spread;
  101. float fx, fy;
  102. int nstops;
  103. NSVGgradientStop stops[1];
  104. } NSVGgradient;
  105. typedef struct NSVGpaint {
  106. char type;
  107. union {
  108. unsigned int color;
  109. NSVGgradient* gradient;
  110. };
  111. } NSVGpaint;
  112. typedef struct NSVGpath
  113. {
  114. float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
  115. int npts; // Total number of bezier points.
  116. char closed; // Flag indicating if shapes should be treated as closed.
  117. float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
  118. struct NSVGpath* next; // Pointer to next path, or NULL if last element.
  119. } NSVGpath;
  120. typedef struct NSVGshape
  121. {
  122. char id[64]; // Optional 'id' attr of the shape or its group
  123. NSVGpaint fill; // Fill paint
  124. NSVGpaint stroke; // Stroke paint
  125. float opacity; // Opacity of the shape.
  126. float strokeWidth; // Stroke width (scaled).
  127. float strokeDashOffset; // Stroke dash offset (scaled).
  128. float strokeDashArray[8]; // Stroke dash array (scaled).
  129. char strokeDashCount; // Number of dash values in dash array.
  130. char strokeLineJoin; // Stroke join type.
  131. char strokeLineCap; // Stroke cap type.
  132. float miterLimit; // Miter limit
  133. char fillRule; // Fill rule, see NSVGfillRule.
  134. unsigned char flags; // Logical or of NSVG_FLAGS_* flags
  135. float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy].
  136. NSVGpath* paths; // Linked list of paths in the image.
  137. struct NSVGshape* next; // Pointer to next shape, or NULL if last element.
  138. } NSVGshape;
  139. typedef struct NSVGimage
  140. {
  141. float width; // Width of the image.
  142. float height; // Height of the image.
  143. NSVGshape* shapes; // Linked list of shapes in the image.
  144. } NSVGimage;
  145. // Parses SVG file from a file, returns SVG image as paths.
  146. NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
  147. // Parses SVG file from a null terminated string, returns SVG image as paths.
  148. // Important note: changes the string.
  149. NSVGimage* nsvgParse(char* input, const char* units, float dpi);
  150. // Deletes list of paths.
  151. void nsvgDelete(NSVGimage* image);
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif // NANOSVG_H
  156. #ifdef NANOSVG_IMPLEMENTATION
  157. #include <string.h>
  158. #include <stdlib.h>
  159. #include <math.h>
  160. #define NSVG_PI (3.14159265358979323846264338327f)
  161. #define NSVG_KAPPA90 (0.5522847493f) // Length proportional to radius of a cubic bezier handle for 90deg arcs.
  162. #define NSVG_ALIGN_MIN 0
  163. #define NSVG_ALIGN_MID 1
  164. #define NSVG_ALIGN_MAX 2
  165. #define NSVG_ALIGN_NONE 0
  166. #define NSVG_ALIGN_MEET 1
  167. #define NSVG_ALIGN_SLICE 2
  168. #define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
  169. #define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
  170. #ifdef _MSC_VER
  171. #pragma warning (disable: 4996) // Switch off security warnings
  172. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  173. #ifdef __cplusplus
  174. #define NSVG_INLINE inline
  175. #else
  176. #define NSVG_INLINE
  177. #endif
  178. #else
  179. #define NSVG_INLINE inline
  180. #endif
  181. static int nsvg__isspace(char c)
  182. {
  183. return strchr(" \t\n\v\f\r", c) != 0;
  184. }
  185. static int nsvg__isdigit(char c)
  186. {
  187. return c >= '0' && c <= '9';
  188. }
  189. static int nsvg__isnum(char c)
  190. {
  191. return strchr("0123456789+-.eE", c) != 0;
  192. }
  193. static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; }
  194. static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; }
  195. // Simple XML parser
  196. #define NSVG_XML_TAG 1
  197. #define NSVG_XML_CONTENT 2
  198. #define NSVG_XML_MAX_ATTRIBS 256
  199. static void nsvg__parseContent(char* s,
  200. void (*contentCb)(void* ud, const char* s),
  201. void* ud)
  202. {
  203. // Trim start white spaces
  204. while (*s && nsvg__isspace(*s)) s++;
  205. if (!*s) return;
  206. if (contentCb)
  207. (*contentCb)(ud, s);
  208. }
  209. static void nsvg__parseElement(char* s,
  210. void (*startelCb)(void* ud, const char* el, const char** attr),
  211. void (*endelCb)(void* ud, const char* el),
  212. void* ud)
  213. {
  214. const char* attr[NSVG_XML_MAX_ATTRIBS];
  215. int nattr = 0;
  216. char* name;
  217. int start = 0;
  218. int end = 0;
  219. char quote;
  220. // Skip white space after the '<'
  221. while (*s && nsvg__isspace(*s)) s++;
  222. // Check if the tag is end tag
  223. if (*s == '/') {
  224. s++;
  225. end = 1;
  226. } else {
  227. start = 1;
  228. }
  229. // Skip comments, data and preprocessor stuff.
  230. if (!*s || *s == '?' || *s == '!')
  231. return;
  232. // Get tag name
  233. name = s;
  234. while (*s && !nsvg__isspace(*s)) s++;
  235. if (*s) { *s++ = '\0'; }
  236. // Get attribs
  237. while (!end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
  238. char* name = NULL;
  239. char* value = NULL;
  240. // Skip white space before the attrib name
  241. while (*s && nsvg__isspace(*s)) s++;
  242. if (!*s) break;
  243. if (*s == '/') {
  244. end = 1;
  245. break;
  246. }
  247. name = s;
  248. // Find end of the attrib name.
  249. while (*s && !nsvg__isspace(*s) && *s != '=') s++;
  250. if (*s) { *s++ = '\0'; }
  251. // Skip until the beginning of the value.
  252. while (*s && *s != '\"' && *s != '\'') s++;
  253. if (!*s) break;
  254. quote = *s;
  255. s++;
  256. // Store value and find the end of it.
  257. value = s;
  258. while (*s && *s != quote) s++;
  259. if (*s) { *s++ = '\0'; }
  260. // Store only well formed attributes
  261. if (name && value) {
  262. attr[nattr++] = name;
  263. attr[nattr++] = value;
  264. }
  265. }
  266. // List terminator
  267. attr[nattr++] = 0;
  268. attr[nattr++] = 0;
  269. // Call callbacks.
  270. if (start && startelCb)
  271. (*startelCb)(ud, name, attr);
  272. if (end && endelCb)
  273. (*endelCb)(ud, name);
  274. }
  275. int nsvg__parseXML(char* input,
  276. void (*startelCb)(void* ud, const char* el, const char** attr),
  277. void (*endelCb)(void* ud, const char* el),
  278. void (*contentCb)(void* ud, const char* s),
  279. void* ud)
  280. {
  281. char* s = input;
  282. char* mark = s;
  283. int state = NSVG_XML_CONTENT;
  284. while (*s) {
  285. if (*s == '<' && state == NSVG_XML_CONTENT) {
  286. // Start of a tag
  287. *s++ = '\0';
  288. nsvg__parseContent(mark, contentCb, ud);
  289. mark = s;
  290. state = NSVG_XML_TAG;
  291. } else if (*s == '>' && state == NSVG_XML_TAG) {
  292. // Start of a content or new tag.
  293. *s++ = '\0';
  294. nsvg__parseElement(mark, startelCb, endelCb, ud);
  295. mark = s;
  296. state = NSVG_XML_CONTENT;
  297. } else {
  298. s++;
  299. }
  300. }
  301. return 1;
  302. }
  303. /* Simple SVG parser. */
  304. #define NSVG_MAX_ATTR 128
  305. enum NSVGgradientUnits {
  306. NSVG_USER_SPACE = 0,
  307. NSVG_OBJECT_SPACE = 1
  308. };
  309. #define NSVG_MAX_DASHES 8
  310. enum NSVGunits {
  311. NSVG_UNITS_USER,
  312. NSVG_UNITS_PX,
  313. NSVG_UNITS_PT,
  314. NSVG_UNITS_PC,
  315. NSVG_UNITS_MM,
  316. NSVG_UNITS_CM,
  317. NSVG_UNITS_IN,
  318. NSVG_UNITS_PERCENT,
  319. NSVG_UNITS_EM,
  320. NSVG_UNITS_EX
  321. };
  322. typedef struct NSVGcoordinate {
  323. float value;
  324. int units;
  325. } NSVGcoordinate;
  326. typedef struct NSVGlinearData {
  327. NSVGcoordinate x1, y1, x2, y2;
  328. } NSVGlinearData;
  329. typedef struct NSVGradialData {
  330. NSVGcoordinate cx, cy, r, fx, fy;
  331. } NSVGradialData;
  332. typedef struct NSVGgradientData
  333. {
  334. char id[64];
  335. char ref[64];
  336. char type;
  337. union {
  338. NSVGlinearData linear;
  339. NSVGradialData radial;
  340. };
  341. char spread;
  342. char units;
  343. float xform[6];
  344. int nstops;
  345. NSVGgradientStop* stops;
  346. struct NSVGgradientData* next;
  347. } NSVGgradientData;
  348. typedef struct NSVGattrib
  349. {
  350. char id[64];
  351. float xform[6];
  352. unsigned int fillColor;
  353. unsigned int strokeColor;
  354. float opacity;
  355. float fillOpacity;
  356. float strokeOpacity;
  357. char fillGradient[64];
  358. char strokeGradient[64];
  359. float strokeWidth;
  360. float strokeDashOffset;
  361. float strokeDashArray[NSVG_MAX_DASHES];
  362. int strokeDashCount;
  363. char strokeLineJoin;
  364. char strokeLineCap;
  365. float miterLimit;
  366. char fillRule;
  367. float fontSize;
  368. unsigned int stopColor;
  369. float stopOpacity;
  370. float stopOffset;
  371. char hasFill;
  372. char hasStroke;
  373. char visible;
  374. } NSVGattrib;
  375. typedef struct NSVGparser
  376. {
  377. NSVGattrib attr[NSVG_MAX_ATTR];
  378. int attrHead;
  379. float* pts;
  380. int npts;
  381. int cpts;
  382. NSVGpath* plist;
  383. NSVGimage* image;
  384. NSVGgradientData* gradients;
  385. NSVGshape* shapesTail;
  386. float viewMinx, viewMiny, viewWidth, viewHeight;
  387. int alignX, alignY, alignType;
  388. float dpi;
  389. char pathFlag;
  390. char defsFlag;
  391. } NSVGparser;
  392. static void nsvg__xformIdentity(float* t)
  393. {
  394. t[0] = 1.0f; t[1] = 0.0f;
  395. t[2] = 0.0f; t[3] = 1.0f;
  396. t[4] = 0.0f; t[5] = 0.0f;
  397. }
  398. static void nsvg__xformSetTranslation(float* t, float tx, float ty)
  399. {
  400. t[0] = 1.0f; t[1] = 0.0f;
  401. t[2] = 0.0f; t[3] = 1.0f;
  402. t[4] = tx; t[5] = ty;
  403. }
  404. static void nsvg__xformSetScale(float* t, float sx, float sy)
  405. {
  406. t[0] = sx; t[1] = 0.0f;
  407. t[2] = 0.0f; t[3] = sy;
  408. t[4] = 0.0f; t[5] = 0.0f;
  409. }
  410. static void nsvg__xformSetSkewX(float* t, float a)
  411. {
  412. t[0] = 1.0f; t[1] = 0.0f;
  413. t[2] = tanf(a); t[3] = 1.0f;
  414. t[4] = 0.0f; t[5] = 0.0f;
  415. }
  416. static void nsvg__xformSetSkewY(float* t, float a)
  417. {
  418. t[0] = 1.0f; t[1] = tanf(a);
  419. t[2] = 0.0f; t[3] = 1.0f;
  420. t[4] = 0.0f; t[5] = 0.0f;
  421. }
  422. static void nsvg__xformSetRotation(float* t, float a)
  423. {
  424. float cs = cosf(a), sn = sinf(a);
  425. t[0] = cs; t[1] = sn;
  426. t[2] = -sn; t[3] = cs;
  427. t[4] = 0.0f; t[5] = 0.0f;
  428. }
  429. static void nsvg__xformMultiply(float* t, float* s)
  430. {
  431. float t0 = t[0] * s[0] + t[1] * s[2];
  432. float t2 = t[2] * s[0] + t[3] * s[2];
  433. float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
  434. t[1] = t[0] * s[1] + t[1] * s[3];
  435. t[3] = t[2] * s[1] + t[3] * s[3];
  436. t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
  437. t[0] = t0;
  438. t[2] = t2;
  439. t[4] = t4;
  440. }
  441. static void nsvg__xformInverse(float* inv, float* t)
  442. {
  443. double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
  444. if (det > -1e-6 && det < 1e-6) {
  445. nsvg__xformIdentity(t);
  446. return;
  447. }
  448. invdet = 1.0 / det;
  449. inv[0] = (float)(t[3] * invdet);
  450. inv[2] = (float)(-t[2] * invdet);
  451. inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
  452. inv[1] = (float)(-t[1] * invdet);
  453. inv[3] = (float)(t[0] * invdet);
  454. inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
  455. }
  456. static void nsvg__xformPremultiply(float* t, float* s)
  457. {
  458. float s2[6];
  459. memcpy(s2, s, sizeof(float)*6);
  460. nsvg__xformMultiply(s2, t);
  461. memcpy(t, s2, sizeof(float)*6);
  462. }
  463. static void nsvg__xformPoint(float* dx, float* dy, float x, float y, float* t)
  464. {
  465. *dx = x*t[0] + y*t[2] + t[4];
  466. *dy = x*t[1] + y*t[3] + t[5];
  467. }
  468. static void nsvg__xformVec(float* dx, float* dy, float x, float y, float* t)
  469. {
  470. *dx = x*t[0] + y*t[2];
  471. *dy = x*t[1] + y*t[3];
  472. }
  473. #define NSVG_EPSILON (1e-12)
  474. static int nsvg__ptInBounds(float* pt, float* bounds)
  475. {
  476. return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3];
  477. }
  478. static double nsvg__evalBezier(double t, double p0, double p1, double p2, double p3)
  479. {
  480. double it = 1.0-t;
  481. return it*it*it*p0 + 3.0*it*it*t*p1 + 3.0*it*t*t*p2 + t*t*t*p3;
  482. }
  483. static void nsvg__curveBounds(float* bounds, float* curve)
  484. {
  485. int i, j, count;
  486. double roots[2], a, b, c, b2ac, t, v;
  487. float* v0 = &curve[0];
  488. float* v1 = &curve[2];
  489. float* v2 = &curve[4];
  490. float* v3 = &curve[6];
  491. // Start the bounding box by end points
  492. bounds[0] = nsvg__minf(v0[0], v3[0]);
  493. bounds[1] = nsvg__minf(v0[1], v3[1]);
  494. bounds[2] = nsvg__maxf(v0[0], v3[0]);
  495. bounds[3] = nsvg__maxf(v0[1], v3[1]);
  496. // Bezier curve fits inside the convex hull of it's control points.
  497. // If control points are inside the bounds, we're done.
  498. if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
  499. return;
  500. // Add bezier curve inflection points in X and Y.
  501. for (i = 0; i < 2; i++) {
  502. a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
  503. b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
  504. c = 3.0 * v1[i] - 3.0 * v0[i];
  505. count = 0;
  506. if (fabs(a) < NSVG_EPSILON) {
  507. if (fabs(b) > NSVG_EPSILON) {
  508. t = -c / b;
  509. if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
  510. roots[count++] = t;
  511. }
  512. } else {
  513. b2ac = b*b - 4.0*c*a;
  514. if (b2ac > NSVG_EPSILON) {
  515. t = (-b + sqrt(b2ac)) / (2.0 * a);
  516. if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
  517. roots[count++] = t;
  518. t = (-b - sqrt(b2ac)) / (2.0 * a);
  519. if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
  520. roots[count++] = t;
  521. }
  522. }
  523. for (j = 0; j < count; j++) {
  524. v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
  525. bounds[0+i] = nsvg__minf(bounds[0+i], (float)v);
  526. bounds[2+i] = nsvg__maxf(bounds[2+i], (float)v);
  527. }
  528. }
  529. }
  530. static NSVGparser* nsvg__createParser()
  531. {
  532. NSVGparser* p;
  533. p = (NSVGparser*)malloc(sizeof(NSVGparser));
  534. if (p == NULL) goto error;
  535. memset(p, 0, sizeof(NSVGparser));
  536. p->image = (NSVGimage*)malloc(sizeof(NSVGimage));
  537. if (p->image == NULL) goto error;
  538. memset(p->image, 0, sizeof(NSVGimage));
  539. // Init style
  540. nsvg__xformIdentity(p->attr[0].xform);
  541. memset(p->attr[0].id, 0, sizeof p->attr[0].id);
  542. p->attr[0].fillColor = NSVG_RGB(0,0,0);
  543. p->attr[0].strokeColor = NSVG_RGB(0,0,0);
  544. p->attr[0].opacity = 1;
  545. p->attr[0].fillOpacity = 1;
  546. p->attr[0].strokeOpacity = 1;
  547. p->attr[0].stopOpacity = 1;
  548. p->attr[0].strokeWidth = 1;
  549. p->attr[0].strokeLineJoin = NSVG_JOIN_MITER;
  550. p->attr[0].strokeLineCap = NSVG_CAP_BUTT;
  551. p->attr[0].miterLimit = 4;
  552. p->attr[0].fillRule = NSVG_FILLRULE_NONZERO;
  553. p->attr[0].hasFill = 1;
  554. p->attr[0].visible = 1;
  555. return p;
  556. error:
  557. if (p) {
  558. if (p->image) free(p->image);
  559. free(p);
  560. }
  561. return NULL;
  562. }
  563. static void nsvg__deletePaths(NSVGpath* path)
  564. {
  565. while (path) {
  566. NSVGpath *next = path->next;
  567. if (path->pts != NULL)
  568. free(path->pts);
  569. free(path);
  570. path = next;
  571. }
  572. }
  573. static void nsvg__deletePaint(NSVGpaint* paint)
  574. {
  575. if (paint->type == NSVG_PAINT_LINEAR_GRADIENT || paint->type == NSVG_PAINT_RADIAL_GRADIENT)
  576. free(paint->gradient);
  577. }
  578. static void nsvg__deleteGradientData(NSVGgradientData* grad)
  579. {
  580. NSVGgradientData* next;
  581. while (grad != NULL) {
  582. next = grad->next;
  583. free(grad->stops);
  584. free(grad);
  585. grad = next;
  586. }
  587. }
  588. static void nsvg__deleteParser(NSVGparser* p)
  589. {
  590. if (p != NULL) {
  591. nsvg__deletePaths(p->plist);
  592. nsvg__deleteGradientData(p->gradients);
  593. nsvgDelete(p->image);
  594. free(p->pts);
  595. free(p);
  596. }
  597. }
  598. static void nsvg__resetPath(NSVGparser* p)
  599. {
  600. p->npts = 0;
  601. }
  602. static void nsvg__addPoint(NSVGparser* p, float x, float y)
  603. {
  604. if (p->npts+1 > p->cpts) {
  605. p->cpts = p->cpts ? p->cpts*2 : 8;
  606. p->pts = (float*)realloc(p->pts, p->cpts*2*sizeof(float));
  607. if (!p->pts) return;
  608. }
  609. p->pts[p->npts*2+0] = x;
  610. p->pts[p->npts*2+1] = y;
  611. p->npts++;
  612. }
  613. static void nsvg__moveTo(NSVGparser* p, float x, float y)
  614. {
  615. if (p->npts > 0) {
  616. p->pts[(p->npts-1)*2+0] = x;
  617. p->pts[(p->npts-1)*2+1] = y;
  618. } else {
  619. nsvg__addPoint(p, x, y);
  620. }
  621. }
  622. static void nsvg__lineTo(NSVGparser* p, float x, float y)
  623. {
  624. float px,py, dx,dy;
  625. if (p->npts > 0) {
  626. px = p->pts[(p->npts-1)*2+0];
  627. py = p->pts[(p->npts-1)*2+1];
  628. dx = x - px;
  629. dy = y - py;
  630. nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f);
  631. nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f);
  632. nsvg__addPoint(p, x, y);
  633. }
  634. }
  635. static void nsvg__cubicBezTo(NSVGparser* p, float cpx1, float cpy1, float cpx2, float cpy2, float x, float y)
  636. {
  637. nsvg__addPoint(p, cpx1, cpy1);
  638. nsvg__addPoint(p, cpx2, cpy2);
  639. nsvg__addPoint(p, x, y);
  640. }
  641. static NSVGattrib* nsvg__getAttr(NSVGparser* p)
  642. {
  643. return &p->attr[p->attrHead];
  644. }
  645. static void nsvg__pushAttr(NSVGparser* p)
  646. {
  647. if (p->attrHead < NSVG_MAX_ATTR-1) {
  648. p->attrHead++;
  649. memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead-1], sizeof(NSVGattrib));
  650. }
  651. }
  652. static void nsvg__popAttr(NSVGparser* p)
  653. {
  654. if (p->attrHead > 0)
  655. p->attrHead--;
  656. }
  657. static float nsvg__actualOrigX(NSVGparser* p)
  658. {
  659. return p->viewMinx;
  660. }
  661. static float nsvg__actualOrigY(NSVGparser* p)
  662. {
  663. return p->viewMiny;
  664. }
  665. static float nsvg__actualWidth(NSVGparser* p)
  666. {
  667. return p->viewWidth;
  668. }
  669. static float nsvg__actualHeight(NSVGparser* p)
  670. {
  671. return p->viewHeight;
  672. }
  673. static float nsvg__actualLength(NSVGparser* p)
  674. {
  675. float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p);
  676. return sqrtf(w*w + h*h) / sqrtf(2.0f);
  677. }
  678. static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, float orig, float length)
  679. {
  680. NSVGattrib* attr = nsvg__getAttr(p);
  681. switch (c.units) {
  682. case NSVG_UNITS_USER: return c.value;
  683. case NSVG_UNITS_PX: return c.value;
  684. case NSVG_UNITS_PT: return c.value / 72.0f * p->dpi;
  685. case NSVG_UNITS_PC: return c.value / 6.0f * p->dpi;
  686. case NSVG_UNITS_MM: return c.value / 25.4f * p->dpi;
  687. case NSVG_UNITS_CM: return c.value / 2.54f * p->dpi;
  688. case NSVG_UNITS_IN: return c.value * p->dpi;
  689. case NSVG_UNITS_EM: return c.value * attr->fontSize;
  690. case NSVG_UNITS_EX: return c.value * attr->fontSize * 0.52f; // x-height of Helvetica.
  691. case NSVG_UNITS_PERCENT: return orig + c.value / 100.0f * length;
  692. default: return c.value;
  693. }
  694. return c.value;
  695. }
  696. static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id)
  697. {
  698. NSVGgradientData* grad = p->gradients;
  699. while (grad) {
  700. if (strcmp(grad->id, id) == 0)
  701. return grad;
  702. grad = grad->next;
  703. }
  704. return NULL;
  705. }
  706. static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const float* localBounds, char* paintType)
  707. {
  708. NSVGattrib* attr = nsvg__getAttr(p);
  709. NSVGgradientData* data = NULL;
  710. NSVGgradientData* ref = NULL;
  711. NSVGgradientStop* stops = NULL;
  712. NSVGgradient* grad;
  713. float ox, oy, sw, sh, sl;
  714. int nstops = 0;
  715. data = nsvg__findGradientData(p, id);
  716. if (data == NULL) return NULL;
  717. // TODO: use ref to fill in all unset values too.
  718. ref = data;
  719. while (ref != NULL) {
  720. if (stops == NULL && ref->stops != NULL) {
  721. stops = ref->stops;
  722. nstops = ref->nstops;
  723. break;
  724. }
  725. ref = nsvg__findGradientData(p, ref->ref);
  726. }
  727. if (stops == NULL) return NULL;
  728. grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1));
  729. if (grad == NULL) return NULL;
  730. // The shape width and height.
  731. if (data->units == NSVG_OBJECT_SPACE) {
  732. ox = localBounds[0];
  733. oy = localBounds[1];
  734. sw = localBounds[2] - localBounds[0];
  735. sh = localBounds[3] - localBounds[1];
  736. } else {
  737. ox = nsvg__actualOrigX(p);
  738. oy = nsvg__actualOrigY(p);
  739. sw = nsvg__actualWidth(p);
  740. sh = nsvg__actualHeight(p);
  741. }
  742. sl = sqrtf(sw*sw + sh*sh) / sqrtf(2.0f);
  743. if (data->type == NSVG_PAINT_LINEAR_GRADIENT) {
  744. float x1, y1, x2, y2, dx, dy;
  745. x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw);
  746. y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh);
  747. x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw);
  748. y2 = nsvg__convertToPixels(p, data->linear.y2, oy, sh);
  749. // Calculate transform aligned to the line
  750. dx = x2 - x1;
  751. dy = y2 - y1;
  752. grad->xform[0] = dy; grad->xform[1] = -dx;
  753. grad->xform[2] = dx; grad->xform[3] = dy;
  754. grad->xform[4] = x1; grad->xform[5] = y1;
  755. } else {
  756. float cx, cy, fx, fy, r;
  757. cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw);
  758. cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh);
  759. fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw);
  760. fy = nsvg__convertToPixels(p, data->radial.fy, oy, sh);
  761. r = nsvg__convertToPixels(p, data->radial.r, 0, sl);
  762. // Calculate transform aligned to the circle
  763. grad->xform[0] = r; grad->xform[1] = 0;
  764. grad->xform[2] = 0; grad->xform[3] = r;
  765. grad->xform[4] = cx; grad->xform[5] = cy;
  766. grad->fx = fx / r;
  767. grad->fy = fy / r;
  768. }
  769. nsvg__xformMultiply(grad->xform, data->xform);
  770. nsvg__xformMultiply(grad->xform, attr->xform);
  771. grad->spread = data->spread;
  772. memcpy(grad->stops, stops, nstops*sizeof(NSVGgradientStop));
  773. grad->nstops = nstops;
  774. *paintType = data->type;
  775. return grad;
  776. }
  777. static float nsvg__getAverageScale(float* t)
  778. {
  779. float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
  780. float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
  781. return (sx + sy) * 0.5f;
  782. }
  783. static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform)
  784. {
  785. NSVGpath* path;
  786. float curve[4*2], curveBounds[4];
  787. int i, first = 1;
  788. for (path = shape->paths; path != NULL; path = path->next) {
  789. nsvg__xformPoint(&curve[0], &curve[1], path->pts[0], path->pts[1], xform);
  790. for (i = 0; i < path->npts-1; i += 3) {
  791. nsvg__xformPoint(&curve[2], &curve[3], path->pts[(i+1)*2], path->pts[(i+1)*2+1], xform);
  792. nsvg__xformPoint(&curve[4], &curve[5], path->pts[(i+2)*2], path->pts[(i+2)*2+1], xform);
  793. nsvg__xformPoint(&curve[6], &curve[7], path->pts[(i+3)*2], path->pts[(i+3)*2+1], xform);
  794. nsvg__curveBounds(curveBounds, curve);
  795. if (first) {
  796. bounds[0] = curveBounds[0];
  797. bounds[1] = curveBounds[1];
  798. bounds[2] = curveBounds[2];
  799. bounds[3] = curveBounds[3];
  800. first = 0;
  801. } else {
  802. bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
  803. bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
  804. bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
  805. bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
  806. }
  807. curve[0] = curve[6];
  808. curve[1] = curve[7];
  809. }
  810. }
  811. }
  812. static void nsvg__addShape(NSVGparser* p)
  813. {
  814. NSVGattrib* attr = nsvg__getAttr(p);
  815. float scale = 1.0f;
  816. NSVGshape* shape;
  817. NSVGpath* path;
  818. int i;
  819. if (p->plist == NULL)
  820. return;
  821. shape = (NSVGshape*)malloc(sizeof(NSVGshape));
  822. if (shape == NULL) goto error;
  823. memset(shape, 0, sizeof(NSVGshape));
  824. memcpy(shape->id, attr->id, sizeof shape->id);
  825. scale = nsvg__getAverageScale(attr->xform);
  826. shape->strokeWidth = attr->strokeWidth * scale;
  827. shape->strokeDashOffset = attr->strokeDashOffset * scale;
  828. shape->strokeDashCount = (char)attr->strokeDashCount;
  829. for (i = 0; i < attr->strokeDashCount; i++)
  830. shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale;
  831. shape->strokeLineJoin = attr->strokeLineJoin;
  832. shape->strokeLineCap = attr->strokeLineCap;
  833. shape->miterLimit = attr->miterLimit;
  834. shape->fillRule = attr->fillRule;
  835. shape->opacity = attr->opacity;
  836. shape->paths = p->plist;
  837. p->plist = NULL;
  838. // Calculate shape bounds
  839. shape->bounds[0] = shape->paths->bounds[0];
  840. shape->bounds[1] = shape->paths->bounds[1];
  841. shape->bounds[2] = shape->paths->bounds[2];
  842. shape->bounds[3] = shape->paths->bounds[3];
  843. for (path = shape->paths->next; path != NULL; path = path->next) {
  844. shape->bounds[0] = nsvg__minf(shape->bounds[0], path->bounds[0]);
  845. shape->bounds[1] = nsvg__minf(shape->bounds[1], path->bounds[1]);
  846. shape->bounds[2] = nsvg__maxf(shape->bounds[2], path->bounds[2]);
  847. shape->bounds[3] = nsvg__maxf(shape->bounds[3], path->bounds[3]);
  848. }
  849. // Set fill
  850. if (attr->hasFill == 0) {
  851. shape->fill.type = NSVG_PAINT_NONE;
  852. } else if (attr->hasFill == 1) {
  853. shape->fill.type = NSVG_PAINT_COLOR;
  854. shape->fill.color = attr->fillColor;
  855. shape->fill.color |= (unsigned int)(attr->fillOpacity*255) << 24;
  856. } else if (attr->hasFill == 2) {
  857. float inv[6], localBounds[4];
  858. nsvg__xformInverse(inv, attr->xform);
  859. nsvg__getLocalBounds(localBounds, shape, inv);
  860. shape->fill.gradient = nsvg__createGradient(p, attr->fillGradient, localBounds, &shape->fill.type);
  861. if (shape->fill.gradient == NULL) {
  862. shape->fill.type = NSVG_PAINT_NONE;
  863. }
  864. }
  865. // Set stroke
  866. if (attr->hasStroke == 0) {
  867. shape->stroke.type = NSVG_PAINT_NONE;
  868. } else if (attr->hasStroke == 1) {
  869. shape->stroke.type = NSVG_PAINT_COLOR;
  870. shape->stroke.color = attr->strokeColor;
  871. shape->stroke.color |= (unsigned int)(attr->strokeOpacity*255) << 24;
  872. } else if (attr->hasStroke == 2) {
  873. float inv[6], localBounds[4];
  874. nsvg__xformInverse(inv, attr->xform);
  875. nsvg__getLocalBounds(localBounds, shape, inv);
  876. shape->stroke.gradient = nsvg__createGradient(p, attr->strokeGradient, localBounds, &shape->stroke.type);
  877. if (shape->stroke.gradient == NULL)
  878. shape->stroke.type = NSVG_PAINT_NONE;
  879. }
  880. // Set flags
  881. shape->flags = (attr->visible ? NSVG_FLAGS_VISIBLE : 0x00);
  882. // Add to tail
  883. if (p->image->shapes == NULL)
  884. p->image->shapes = shape;
  885. else
  886. p->shapesTail->next = shape;
  887. p->shapesTail = shape;
  888. return;
  889. error:
  890. if (shape) free(shape);
  891. }
  892. static void nsvg__addPath(NSVGparser* p, char closed)
  893. {
  894. NSVGattrib* attr = nsvg__getAttr(p);
  895. NSVGpath* path = NULL;
  896. float bounds[4];
  897. float* curve;
  898. int i;
  899. if (p->npts < 4)
  900. return;
  901. if (closed)
  902. nsvg__lineTo(p, p->pts[0], p->pts[1]);
  903. path = (NSVGpath*)malloc(sizeof(NSVGpath));
  904. if (path == NULL) goto error;
  905. memset(path, 0, sizeof(NSVGpath));
  906. path->pts = (float*)malloc(p->npts*2*sizeof(float));
  907. if (path->pts == NULL) goto error;
  908. path->closed = closed;
  909. path->npts = p->npts;
  910. // Transform path.
  911. for (i = 0; i < p->npts; ++i)
  912. nsvg__xformPoint(&path->pts[i*2], &path->pts[i*2+1], p->pts[i*2], p->pts[i*2+1], attr->xform);
  913. // Find bounds
  914. for (i = 0; i < path->npts-1; i += 3) {
  915. curve = &path->pts[i*2];
  916. nsvg__curveBounds(bounds, curve);
  917. if (i == 0) {
  918. path->bounds[0] = bounds[0];
  919. path->bounds[1] = bounds[1];
  920. path->bounds[2] = bounds[2];
  921. path->bounds[3] = bounds[3];
  922. } else {
  923. path->bounds[0] = nsvg__minf(path->bounds[0], bounds[0]);
  924. path->bounds[1] = nsvg__minf(path->bounds[1], bounds[1]);
  925. path->bounds[2] = nsvg__maxf(path->bounds[2], bounds[2]);
  926. path->bounds[3] = nsvg__maxf(path->bounds[3], bounds[3]);
  927. }
  928. }
  929. path->next = p->plist;
  930. p->plist = path;
  931. return;
  932. error:
  933. if (path != NULL) {
  934. if (path->pts != NULL) free(path->pts);
  935. free(path);
  936. }
  937. }
  938. // We roll our own string to float because the std library one uses locale and messes things up.
  939. static double nsvg__atof(const char* s)
  940. {
  941. char* cur = (char*)s;
  942. char* end = NULL;
  943. double res = 0.0, sign = 1.0;
  944. long long intPart = 0, fracPart = 0;
  945. char hasIntPart = 0, hasFracPart = 0;
  946. // Parse optional sign
  947. if (*cur == '+') {
  948. cur++;
  949. } else if (*cur == '-') {
  950. sign = -1;
  951. cur++;
  952. }
  953. // Parse integer part
  954. if (nsvg__isdigit(*cur)) {
  955. // Parse digit sequence
  956. intPart = (double)strtoll(cur, &end, 10);
  957. if (cur != end) {
  958. res = (double)intPart;
  959. hasIntPart = 1;
  960. cur = end;
  961. }
  962. }
  963. // Parse fractional part.
  964. if (*cur == '.') {
  965. cur++; // Skip '.'
  966. if (nsvg__isdigit(*cur)) {
  967. // Parse digit sequence
  968. fracPart = strtoll(cur, &end, 10);
  969. if (cur != end) {
  970. res += (double)fracPart / pow(10.0, (double)(end - cur));
  971. hasFracPart = 1;
  972. cur = end;
  973. }
  974. }
  975. }
  976. // A valid number should have integer or fractional part.
  977. if (!hasIntPart && !hasFracPart)
  978. return 0.0;
  979. // Parse optional exponent
  980. if (*cur == 'e' || *cur == 'E') {
  981. int expPart = 0;
  982. cur++; // skip 'E'
  983. expPart = strtol(cur, &end, 10); // Parse digit sequence with sign
  984. if (cur != end) {
  985. res *= pow(10.0, (double)expPart);
  986. }
  987. }
  988. return res * sign;
  989. }
  990. static const char* nsvg__parseNumber(const char* s, char* it, const int size)
  991. {
  992. const int last = size-1;
  993. int i = 0;
  994. // sign
  995. if (*s == '-' || *s == '+') {
  996. if (i < last) it[i++] = *s;
  997. s++;
  998. }
  999. // integer part
  1000. while (*s && nsvg__isdigit(*s)) {
  1001. if (i < last) it[i++] = *s;
  1002. s++;
  1003. }
  1004. if (*s == '.') {
  1005. // decimal point
  1006. if (i < last) it[i++] = *s;
  1007. s++;
  1008. // fraction part
  1009. while (*s && nsvg__isdigit(*s)) {
  1010. if (i < last) it[i++] = *s;
  1011. s++;
  1012. }
  1013. }
  1014. // exponent
  1015. if (*s == 'e' || *s == 'E') {
  1016. if (i < last) it[i++] = *s;
  1017. s++;
  1018. if (*s == '-' || *s == '+') {
  1019. if (i < last) it[i++] = *s;
  1020. s++;
  1021. }
  1022. while (*s && nsvg__isdigit(*s)) {
  1023. if (i < last) it[i++] = *s;
  1024. s++;
  1025. }
  1026. }
  1027. it[i] = '\0';
  1028. return s;
  1029. }
  1030. static const char* nsvg__getNextPathItem(const char* s, char* it)
  1031. {
  1032. it[0] = '\0';
  1033. // Skip white spaces and commas
  1034. while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
  1035. if (!*s) return s;
  1036. if (*s == '-' || *s == '+' || *s == '.' || nsvg__isdigit(*s)) {
  1037. s = nsvg__parseNumber(s, it, 64);
  1038. } else {
  1039. // Parse command
  1040. it[0] = *s++;
  1041. it[1] = '\0';
  1042. return s;
  1043. }
  1044. return s;
  1045. }
  1046. static unsigned int nsvg__parseColorHex(const char* str)
  1047. {
  1048. unsigned int c = 0, r = 0, g = 0, b = 0;
  1049. int n = 0;
  1050. str++; // skip #
  1051. // Calculate number of characters.
  1052. while(str[n] && !nsvg__isspace(str[n]))
  1053. n++;
  1054. if (n == 6) {
  1055. sscanf(str, "%x", &c);
  1056. } else if (n == 3) {
  1057. sscanf(str, "%x", &c);
  1058. c = (c&0xf) | ((c&0xf0) << 4) | ((c&0xf00) << 8);
  1059. c |= c<<4;
  1060. }
  1061. r = (c >> 16) & 0xff;
  1062. g = (c >> 8) & 0xff;
  1063. b = c & 0xff;
  1064. return NSVG_RGB(r,g,b);
  1065. }
  1066. static unsigned int nsvg__parseColorRGB(const char* str)
  1067. {
  1068. int r = -1, g = -1, b = -1;
  1069. char s1[32]="", s2[32]="";
  1070. sscanf(str + 4, "%d%[%%, \t]%d%[%%, \t]%d", &r, s1, &g, s2, &b);
  1071. if (strchr(s1, '%')) {
  1072. return NSVG_RGB((r*255)/100,(g*255)/100,(b*255)/100);
  1073. } else {
  1074. return NSVG_RGB(r,g,b);
  1075. }
  1076. }
  1077. typedef struct NSVGNamedColor {
  1078. const char* name;
  1079. unsigned int color;
  1080. } NSVGNamedColor;
  1081. NSVGNamedColor nsvg__colors[] = {
  1082. { "red", NSVG_RGB(255, 0, 0) },
  1083. { "green", NSVG_RGB( 0, 128, 0) },
  1084. { "blue", NSVG_RGB( 0, 0, 255) },
  1085. { "yellow", NSVG_RGB(255, 255, 0) },
  1086. { "cyan", NSVG_RGB( 0, 255, 255) },
  1087. { "magenta", NSVG_RGB(255, 0, 255) },
  1088. { "black", NSVG_RGB( 0, 0, 0) },
  1089. { "grey", NSVG_RGB(128, 128, 128) },
  1090. { "gray", NSVG_RGB(128, 128, 128) },
  1091. { "white", NSVG_RGB(255, 255, 255) },
  1092. #ifdef NANOSVG_ALL_COLOR_KEYWORDS
  1093. { "aliceblue", NSVG_RGB(240, 248, 255) },
  1094. { "antiquewhite", NSVG_RGB(250, 235, 215) },
  1095. { "aqua", NSVG_RGB( 0, 255, 255) },
  1096. { "aquamarine", NSVG_RGB(127, 255, 212) },
  1097. { "azure", NSVG_RGB(240, 255, 255) },
  1098. { "beige", NSVG_RGB(245, 245, 220) },
  1099. { "bisque", NSVG_RGB(255, 228, 196) },
  1100. { "blanchedalmond", NSVG_RGB(255, 235, 205) },
  1101. { "blueviolet", NSVG_RGB(138, 43, 226) },
  1102. { "brown", NSVG_RGB(165, 42, 42) },
  1103. { "burlywood", NSVG_RGB(222, 184, 135) },
  1104. { "cadetblue", NSVG_RGB( 95, 158, 160) },
  1105. { "chartreuse", NSVG_RGB(127, 255, 0) },
  1106. { "chocolate", NSVG_RGB(210, 105, 30) },
  1107. { "coral", NSVG_RGB(255, 127, 80) },
  1108. { "cornflowerblue", NSVG_RGB(100, 149, 237) },
  1109. { "cornsilk", NSVG_RGB(255, 248, 220) },
  1110. { "crimson", NSVG_RGB(220, 20, 60) },
  1111. { "darkblue", NSVG_RGB( 0, 0, 139) },
  1112. { "darkcyan", NSVG_RGB( 0, 139, 139) },
  1113. { "darkgoldenrod", NSVG_RGB(184, 134, 11) },
  1114. { "darkgray", NSVG_RGB(169, 169, 169) },
  1115. { "darkgreen", NSVG_RGB( 0, 100, 0) },
  1116. { "darkgrey", NSVG_RGB(169, 169, 169) },
  1117. { "darkkhaki", NSVG_RGB(189, 183, 107) },
  1118. { "darkmagenta", NSVG_RGB(139, 0, 139) },
  1119. { "darkolivegreen", NSVG_RGB( 85, 107, 47) },
  1120. { "darkorange", NSVG_RGB(255, 140, 0) },
  1121. { "darkorchid", NSVG_RGB(153, 50, 204) },
  1122. { "darkred", NSVG_RGB(139, 0, 0) },
  1123. { "darksalmon", NSVG_RGB(233, 150, 122) },
  1124. { "darkseagreen", NSVG_RGB(143, 188, 143) },
  1125. { "darkslateblue", NSVG_RGB( 72, 61, 139) },
  1126. { "darkslategray", NSVG_RGB( 47, 79, 79) },
  1127. { "darkslategrey", NSVG_RGB( 47, 79, 79) },
  1128. { "darkturquoise", NSVG_RGB( 0, 206, 209) },
  1129. { "darkviolet", NSVG_RGB(148, 0, 211) },
  1130. { "deeppink", NSVG_RGB(255, 20, 147) },
  1131. { "deepskyblue", NSVG_RGB( 0, 191, 255) },
  1132. { "dimgray", NSVG_RGB(105, 105, 105) },
  1133. { "dimgrey", NSVG_RGB(105, 105, 105) },
  1134. { "dodgerblue", NSVG_RGB( 30, 144, 255) },
  1135. { "firebrick", NSVG_RGB(178, 34, 34) },
  1136. { "floralwhite", NSVG_RGB(255, 250, 240) },
  1137. { "forestgreen", NSVG_RGB( 34, 139, 34) },
  1138. { "fuchsia", NSVG_RGB(255, 0, 255) },
  1139. { "gainsboro", NSVG_RGB(220, 220, 220) },
  1140. { "ghostwhite", NSVG_RGB(248, 248, 255) },
  1141. { "gold", NSVG_RGB(255, 215, 0) },
  1142. { "goldenrod", NSVG_RGB(218, 165, 32) },
  1143. { "greenyellow", NSVG_RGB(173, 255, 47) },
  1144. { "honeydew", NSVG_RGB(240, 255, 240) },
  1145. { "hotpink", NSVG_RGB(255, 105, 180) },
  1146. { "indianred", NSVG_RGB(205, 92, 92) },
  1147. { "indigo", NSVG_RGB( 75, 0, 130) },
  1148. { "ivory", NSVG_RGB(255, 255, 240) },
  1149. { "khaki", NSVG_RGB(240, 230, 140) },
  1150. { "lavender", NSVG_RGB(230, 230, 250) },
  1151. { "lavenderblush", NSVG_RGB(255, 240, 245) },
  1152. { "lawngreen", NSVG_RGB(124, 252, 0) },
  1153. { "lemonchiffon", NSVG_RGB(255, 250, 205) },
  1154. { "lightblue", NSVG_RGB(173, 216, 230) },
  1155. { "lightcoral", NSVG_RGB(240, 128, 128) },
  1156. { "lightcyan", NSVG_RGB(224, 255, 255) },
  1157. { "lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
  1158. { "lightgray", NSVG_RGB(211, 211, 211) },
  1159. { "lightgreen", NSVG_RGB(144, 238, 144) },
  1160. { "lightgrey", NSVG_RGB(211, 211, 211) },
  1161. { "lightpink", NSVG_RGB(255, 182, 193) },
  1162. { "lightsalmon", NSVG_RGB(255, 160, 122) },
  1163. { "lightseagreen", NSVG_RGB( 32, 178, 170) },
  1164. { "lightskyblue", NSVG_RGB(135, 206, 250) },
  1165. { "lightslategray", NSVG_RGB(119, 136, 153) },
  1166. { "lightslategrey", NSVG_RGB(119, 136, 153) },
  1167. { "lightsteelblue", NSVG_RGB(176, 196, 222) },
  1168. { "lightyellow", NSVG_RGB(255, 255, 224) },
  1169. { "lime", NSVG_RGB( 0, 255, 0) },
  1170. { "limegreen", NSVG_RGB( 50, 205, 50) },
  1171. { "linen", NSVG_RGB(250, 240, 230) },
  1172. { "maroon", NSVG_RGB(128, 0, 0) },
  1173. { "mediumaquamarine", NSVG_RGB(102, 205, 170) },
  1174. { "mediumblue", NSVG_RGB( 0, 0, 205) },
  1175. { "mediumorchid", NSVG_RGB(186, 85, 211) },
  1176. { "mediumpurple", NSVG_RGB(147, 112, 219) },
  1177. { "mediumseagreen", NSVG_RGB( 60, 179, 113) },
  1178. { "mediumslateblue", NSVG_RGB(123, 104, 238) },
  1179. { "mediumspringgreen", NSVG_RGB( 0, 250, 154) },
  1180. { "mediumturquoise", NSVG_RGB( 72, 209, 204) },
  1181. { "mediumvioletred", NSVG_RGB(199, 21, 133) },
  1182. { "midnightblue", NSVG_RGB( 25, 25, 112) },
  1183. { "mintcream", NSVG_RGB(245, 255, 250) },
  1184. { "mistyrose", NSVG_RGB(255, 228, 225) },
  1185. { "moccasin", NSVG_RGB(255, 228, 181) },
  1186. { "navajowhite", NSVG_RGB(255, 222, 173) },
  1187. { "navy", NSVG_RGB( 0, 0, 128) },
  1188. { "oldlace", NSVG_RGB(253, 245, 230) },
  1189. { "olive", NSVG_RGB(128, 128, 0) },
  1190. { "olivedrab", NSVG_RGB(107, 142, 35) },
  1191. { "orange", NSVG_RGB(255, 165, 0) },
  1192. { "orangered", NSVG_RGB(255, 69, 0) },
  1193. { "orchid", NSVG_RGB(218, 112, 214) },
  1194. { "palegoldenrod", NSVG_RGB(238, 232, 170) },
  1195. { "palegreen", NSVG_RGB(152, 251, 152) },
  1196. { "paleturquoise", NSVG_RGB(175, 238, 238) },
  1197. { "palevioletred", NSVG_RGB(219, 112, 147) },
  1198. { "papayawhip", NSVG_RGB(255, 239, 213) },
  1199. { "peachpuff", NSVG_RGB(255, 218, 185) },
  1200. { "peru", NSVG_RGB(205, 133, 63) },
  1201. { "pink", NSVG_RGB(255, 192, 203) },
  1202. { "plum", NSVG_RGB(221, 160, 221) },
  1203. { "powderblue", NSVG_RGB(176, 224, 230) },
  1204. { "purple", NSVG_RGB(128, 0, 128) },
  1205. { "rosybrown", NSVG_RGB(188, 143, 143) },
  1206. { "royalblue", NSVG_RGB( 65, 105, 225) },
  1207. { "saddlebrown", NSVG_RGB(139, 69, 19) },
  1208. { "salmon", NSVG_RGB(250, 128, 114) },
  1209. { "sandybrown", NSVG_RGB(244, 164, 96) },
  1210. { "seagreen", NSVG_RGB( 46, 139, 87) },
  1211. { "seashell", NSVG_RGB(255, 245, 238) },
  1212. { "sienna", NSVG_RGB(160, 82, 45) },
  1213. { "silver", NSVG_RGB(192, 192, 192) },
  1214. { "skyblue", NSVG_RGB(135, 206, 235) },
  1215. { "slateblue", NSVG_RGB(106, 90, 205) },
  1216. { "slategray", NSVG_RGB(112, 128, 144) },
  1217. { "slategrey", NSVG_RGB(112, 128, 144) },
  1218. { "snow", NSVG_RGB(255, 250, 250) },
  1219. { "springgreen", NSVG_RGB( 0, 255, 127) },
  1220. { "steelblue", NSVG_RGB( 70, 130, 180) },
  1221. { "tan", NSVG_RGB(210, 180, 140) },
  1222. { "teal", NSVG_RGB( 0, 128, 128) },
  1223. { "thistle", NSVG_RGB(216, 191, 216) },
  1224. { "tomato", NSVG_RGB(255, 99, 71) },
  1225. { "turquoise", NSVG_RGB( 64, 224, 208) },
  1226. { "violet", NSVG_RGB(238, 130, 238) },
  1227. { "wheat", NSVG_RGB(245, 222, 179) },
  1228. { "whitesmoke", NSVG_RGB(245, 245, 245) },
  1229. { "yellowgreen", NSVG_RGB(154, 205, 50) },
  1230. #endif
  1231. };
  1232. static unsigned int nsvg__parseColorName(const char* str)
  1233. {
  1234. int i, ncolors = sizeof(nsvg__colors) / sizeof(NSVGNamedColor);
  1235. for (i = 0; i < ncolors; i++) {
  1236. if (strcmp(nsvg__colors[i].name, str) == 0) {
  1237. return nsvg__colors[i].color;
  1238. }
  1239. }
  1240. return NSVG_RGB(128, 128, 128);
  1241. }
  1242. static unsigned int nsvg__parseColor(const char* str)
  1243. {
  1244. size_t len = 0;
  1245. while(*str == ' ') ++str;
  1246. len = strlen(str);
  1247. if (len >= 1 && *str == '#')
  1248. return nsvg__parseColorHex(str);
  1249. else if (len >= 4 && str[0] == 'r' && str[1] == 'g' && str[2] == 'b' && str[3] == '(')
  1250. return nsvg__parseColorRGB(str);
  1251. return nsvg__parseColorName(str);
  1252. }
  1253. static float nsvg__parseOpacity(const char* str)
  1254. {
  1255. float val = 0;
  1256. sscanf(str, "%f", &val);
  1257. if (val < 0.0f) val = 0.0f;
  1258. if (val > 1.0f) val = 1.0f;
  1259. return val;
  1260. }
  1261. static float nsvg__parseMiterLimit(const char* str)
  1262. {
  1263. float val = 0;
  1264. sscanf(str, "%f", &val);
  1265. if (val < 0.0f) val = 0.0f;
  1266. return val;
  1267. }
  1268. static int nsvg__parseUnits(const char* units)
  1269. {
  1270. if (units[0] == 'p' && units[1] == 'x')
  1271. return NSVG_UNITS_PX;
  1272. else if (units[0] == 'p' && units[1] == 't')
  1273. return NSVG_UNITS_PT;
  1274. else if (units[0] == 'p' && units[1] == 'c')
  1275. return NSVG_UNITS_PC;
  1276. else if (units[0] == 'm' && units[1] == 'm')
  1277. return NSVG_UNITS_MM;
  1278. else if (units[0] == 'c' && units[1] == 'm')
  1279. return NSVG_UNITS_CM;
  1280. else if (units[0] == 'i' && units[1] == 'n')
  1281. return NSVG_UNITS_IN;
  1282. else if (units[0] == '%')
  1283. return NSVG_UNITS_PERCENT;
  1284. else if (units[0] == 'e' && units[1] == 'm')
  1285. return NSVG_UNITS_EM;
  1286. else if (units[0] == 'e' && units[1] == 'x')
  1287. return NSVG_UNITS_EX;
  1288. return NSVG_UNITS_USER;
  1289. }
  1290. static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str)
  1291. {
  1292. NSVGcoordinate coord = {0, NSVG_UNITS_USER};
  1293. char units[32]="";
  1294. sscanf(str, "%f%31s", &coord.value, units);
  1295. coord.units = nsvg__parseUnits(units);
  1296. return coord;
  1297. }
  1298. static NSVGcoordinate nsvg__coord(float v, int units)
  1299. {
  1300. NSVGcoordinate coord = {v, units};
  1301. return coord;
  1302. }
  1303. static float nsvg__parseCoordinate(NSVGparser* p, const char* str, float orig, float length)
  1304. {
  1305. NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
  1306. return nsvg__convertToPixels(p, coord, orig, length);
  1307. }
  1308. static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int* na)
  1309. {
  1310. const char* end;
  1311. const char* ptr;
  1312. char it[64];
  1313. *na = 0;
  1314. ptr = str;
  1315. while (*ptr && *ptr != '(') ++ptr;
  1316. if (*ptr == 0)
  1317. return 1;
  1318. end = ptr;
  1319. while (*end && *end != ')') ++end;
  1320. if (*end == 0)
  1321. return 1;
  1322. while (ptr < end) {
  1323. if (*ptr == '-' || *ptr == '+' || *ptr == '.' || nsvg__isdigit(*ptr)) {
  1324. if (*na >= maxNa) return 0;
  1325. ptr = nsvg__parseNumber(ptr, it, 64);
  1326. args[(*na)++] = (float)nsvg__atof(it);
  1327. } else {
  1328. ++ptr;
  1329. }
  1330. }
  1331. return (int)(end - str);
  1332. }
  1333. static int nsvg__parseMatrix(float* xform, const char* str)
  1334. {
  1335. float t[6];
  1336. int na = 0;
  1337. int len = nsvg__parseTransformArgs(str, t, 6, &na);
  1338. if (na != 6) return len;
  1339. memcpy(xform, t, sizeof(float)*6);
  1340. return len;
  1341. }
  1342. static int nsvg__parseTranslate(float* xform, const char* str)
  1343. {
  1344. float args[2];
  1345. float t[6];
  1346. int na = 0;
  1347. int len = nsvg__parseTransformArgs(str, args, 2, &na);
  1348. if (na == 1) args[1] = 0.0;
  1349. nsvg__xformSetTranslation(t, args[0], args[1]);
  1350. memcpy(xform, t, sizeof(float)*6);
  1351. return len;
  1352. }
  1353. static int nsvg__parseScale(float* xform, const char* str)
  1354. {
  1355. float args[2];
  1356. int na = 0;
  1357. float t[6];
  1358. int len = nsvg__parseTransformArgs(str, args, 2, &na);
  1359. if (na == 1) args[1] = args[0];
  1360. nsvg__xformSetScale(t, args[0], args[1]);
  1361. memcpy(xform, t, sizeof(float)*6);
  1362. return len;
  1363. }
  1364. static int nsvg__parseSkewX(float* xform, const char* str)
  1365. {
  1366. float args[1];
  1367. int na = 0;
  1368. float t[6];
  1369. int len = nsvg__parseTransformArgs(str, args, 1, &na);
  1370. nsvg__xformSetSkewX(t, args[0]/180.0f*NSVG_PI);
  1371. memcpy(xform, t, sizeof(float)*6);
  1372. return len;
  1373. }
  1374. static int nsvg__parseSkewY(float* xform, const char* str)
  1375. {
  1376. float args[1];
  1377. int na = 0;
  1378. float t[6];
  1379. int len = nsvg__parseTransformArgs(str, args, 1, &na);
  1380. nsvg__xformSetSkewY(t, args[0]/180.0f*NSVG_PI);
  1381. memcpy(xform, t, sizeof(float)*6);
  1382. return len;
  1383. }
  1384. static int nsvg__parseRotate(float* xform, const char* str)
  1385. {
  1386. float args[3];
  1387. int na = 0;
  1388. float m[6];
  1389. float t[6];
  1390. int len = nsvg__parseTransformArgs(str, args, 3, &na);
  1391. if (na == 1)
  1392. args[1] = args[2] = 0.0f;
  1393. nsvg__xformIdentity(m);
  1394. if (na > 1) {
  1395. nsvg__xformSetTranslation(t, -args[1], -args[2]);
  1396. nsvg__xformMultiply(m, t);
  1397. }
  1398. nsvg__xformSetRotation(t, args[0]/180.0f*NSVG_PI);
  1399. nsvg__xformMultiply(m, t);
  1400. if (na > 1) {
  1401. nsvg__xformSetTranslation(t, args[1], args[2]);
  1402. nsvg__xformMultiply(m, t);
  1403. }
  1404. memcpy(xform, m, sizeof(float)*6);
  1405. return len;
  1406. }
  1407. static void nsvg__parseTransform(float* xform, const char* str)
  1408. {
  1409. float t[6];
  1410. nsvg__xformIdentity(xform);
  1411. while (*str)
  1412. {
  1413. if (strncmp(str, "matrix", 6) == 0)
  1414. str += nsvg__parseMatrix(t, str);
  1415. else if (strncmp(str, "translate", 9) == 0)
  1416. str += nsvg__parseTranslate(t, str);
  1417. else if (strncmp(str, "scale", 5) == 0)
  1418. str += nsvg__parseScale(t, str);
  1419. else if (strncmp(str, "rotate", 6) == 0)
  1420. str += nsvg__parseRotate(t, str);
  1421. else if (strncmp(str, "skewX", 5) == 0)
  1422. str += nsvg__parseSkewX(t, str);
  1423. else if (strncmp(str, "skewY", 5) == 0)
  1424. str += nsvg__parseSkewY(t, str);
  1425. else{
  1426. ++str;
  1427. continue;
  1428. }
  1429. nsvg__xformPremultiply(xform, t);
  1430. }
  1431. }
  1432. static void nsvg__parseUrl(char* id, const char* str)
  1433. {
  1434. int i = 0;
  1435. str += 4; // "url(";
  1436. if (*str == '#')
  1437. str++;
  1438. while (i < 63 && *str != ')') {
  1439. id[i] = *str++;
  1440. i++;
  1441. }
  1442. id[i] = '\0';
  1443. }
  1444. static char nsvg__parseLineCap(const char* str)
  1445. {
  1446. if (strcmp(str, "butt") == 0)
  1447. return NSVG_CAP_BUTT;
  1448. else if (strcmp(str, "round") == 0)
  1449. return NSVG_CAP_ROUND;
  1450. else if (strcmp(str, "square") == 0)
  1451. return NSVG_CAP_SQUARE;
  1452. // TODO: handle inherit.
  1453. return NSVG_CAP_BUTT;
  1454. }
  1455. static char nsvg__parseLineJoin(const char* str)
  1456. {
  1457. if (strcmp(str, "miter") == 0)
  1458. return NSVG_JOIN_MITER;
  1459. else if (strcmp(str, "round") == 0)
  1460. return NSVG_JOIN_ROUND;
  1461. else if (strcmp(str, "bevel") == 0)
  1462. return NSVG_JOIN_BEVEL;
  1463. // TODO: handle inherit.
  1464. return NSVG_CAP_BUTT;
  1465. }
  1466. static char nsvg__parseFillRule(const char* str)
  1467. {
  1468. if (strcmp(str, "nonzero") == 0)
  1469. return NSVG_FILLRULE_NONZERO;
  1470. else if (strcmp(str, "evenodd") == 0)
  1471. return NSVG_FILLRULE_EVENODD;
  1472. // TODO: handle inherit.
  1473. return NSVG_FILLRULE_NONZERO;
  1474. }
  1475. static const char* nsvg__getNextDashItem(const char* s, char* it)
  1476. {
  1477. int n = 0;
  1478. it[0] = '\0';
  1479. // Skip white spaces and commas
  1480. while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
  1481. // Advance until whitespace, comma or end.
  1482. while (*s && (!nsvg__isspace(*s) && *s != ',')) {
  1483. if (n < 63)
  1484. it[n++] = *s;
  1485. s++;
  1486. }
  1487. it[n++] = '\0';
  1488. return s;
  1489. }
  1490. static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, float* strokeDashArray)
  1491. {
  1492. char item[64];
  1493. int count = 0, i;
  1494. float sum = 0.0f;
  1495. // Handle "none"
  1496. if (str[0] == 'n')
  1497. return 0;
  1498. // Parse dashes
  1499. while (*str) {
  1500. str = nsvg__getNextDashItem(str, item);
  1501. if (!*item) break;
  1502. if (count < NSVG_MAX_DASHES)
  1503. strokeDashArray[count++] = fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
  1504. }
  1505. for (i = 0; i < count; i++)
  1506. sum += strokeDashArray[i];
  1507. if (sum <= 1e-6f)
  1508. count = 0;
  1509. return count;
  1510. }
  1511. static void nsvg__parseStyle(NSVGparser* p, const char* str);
  1512. static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value)
  1513. {
  1514. float xform[6];
  1515. NSVGattrib* attr = nsvg__getAttr(p);
  1516. if (!attr) return 0;
  1517. if (strcmp(name, "style") == 0) {
  1518. nsvg__parseStyle(p, value);
  1519. } else if (strcmp(name, "display") == 0) {
  1520. if (strcmp(value, "none") == 0)
  1521. attr->visible = 0;
  1522. // Don't reset ->visible on display:inline, one display:none hides the whole subtree
  1523. } else if (strcmp(name, "fill") == 0) {
  1524. if (strcmp(value, "none") == 0) {
  1525. attr->hasFill = 0;
  1526. } else if (strncmp(value, "url(", 4) == 0) {
  1527. attr->hasFill = 2;
  1528. nsvg__parseUrl(attr->fillGradient, value);
  1529. } else {
  1530. attr->hasFill = 1;
  1531. attr->fillColor = nsvg__parseColor(value);
  1532. }
  1533. } else if (strcmp(name, "opacity") == 0) {
  1534. attr->opacity = nsvg__parseOpacity(value);
  1535. } else if (strcmp(name, "fill-opacity") == 0) {
  1536. attr->fillOpacity = nsvg__parseOpacity(value);
  1537. } else if (strcmp(name, "stroke") == 0) {
  1538. if (strcmp(value, "none") == 0) {
  1539. attr->hasStroke = 0;
  1540. } else if (strncmp(value, "url(", 4) == 0) {
  1541. attr->hasStroke = 2;
  1542. nsvg__parseUrl(attr->strokeGradient, value);
  1543. } else {
  1544. attr->hasStroke = 1;
  1545. attr->strokeColor = nsvg__parseColor(value);
  1546. }
  1547. } else if (strcmp(name, "stroke-width") == 0) {
  1548. attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
  1549. } else if (strcmp(name, "stroke-dasharray") == 0) {
  1550. attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
  1551. } else if (strcmp(name, "stroke-dashoffset") == 0) {
  1552. attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
  1553. } else if (strcmp(name, "stroke-opacity") == 0) {
  1554. attr->strokeOpacity = nsvg__parseOpacity(value);
  1555. } else if (strcmp(name, "stroke-linecap") == 0) {
  1556. attr->strokeLineCap = nsvg__parseLineCap(value);
  1557. } else if (strcmp(name, "stroke-linejoin") == 0) {
  1558. attr->strokeLineJoin = nsvg__parseLineJoin(value);
  1559. } else if (strcmp(name, "stroke-miterlimit") == 0) {
  1560. attr->miterLimit = nsvg__parseMiterLimit(value);
  1561. } else if (strcmp(name, "fill-rule") == 0) {
  1562. attr->fillRule = nsvg__parseFillRule(value);
  1563. } else if (strcmp(name, "font-size") == 0) {
  1564. attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
  1565. } else if (strcmp(name, "transform") == 0) {
  1566. nsvg__parseTransform(xform, value);
  1567. nsvg__xformPremultiply(attr->xform, xform);
  1568. } else if (strcmp(name, "stop-color") == 0) {
  1569. attr->stopColor = nsvg__parseColor(value);
  1570. } else if (strcmp(name, "stop-opacity") == 0) {
  1571. attr->stopOpacity = nsvg__parseOpacity(value);
  1572. } else if (strcmp(name, "offset") == 0) {
  1573. attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
  1574. } else if (strcmp(name, "id") == 0) {
  1575. strncpy(attr->id, value, 63);
  1576. attr->id[63] = '\0';
  1577. } else {
  1578. return 0;
  1579. }
  1580. return 1;
  1581. }
  1582. static int nsvg__parseNameValue(NSVGparser* p, const char* start, const char* end)
  1583. {
  1584. const char* str;
  1585. const char* val;
  1586. char name[512];
  1587. char value[512];
  1588. int n;
  1589. str = start;
  1590. while (str < end && *str != ':') ++str;
  1591. val = str;
  1592. // Right Trim
  1593. while (str > start && (*str == ':' || nsvg__isspace(*str))) --str;
  1594. ++str;
  1595. n = (int)(str - start);
  1596. if (n > 511) n = 511;
  1597. if (n) memcpy(name, start, n);
  1598. name[n] = 0;
  1599. while (val < end && (*val == ':' || nsvg__isspace(*val))) ++val;
  1600. n = (int)(end - val);
  1601. if (n > 511) n = 511;
  1602. if (n) memcpy(value, val, n);
  1603. value[n] = 0;
  1604. return nsvg__parseAttr(p, name, value);
  1605. }
  1606. static void nsvg__parseStyle(NSVGparser* p, const char* str)
  1607. {
  1608. const char* start;
  1609. const char* end;
  1610. while (*str) {
  1611. // Left Trim
  1612. while(*str && nsvg__isspace(*str)) ++str;
  1613. start = str;
  1614. while(*str && *str != ';') ++str;
  1615. end = str;
  1616. // Right Trim
  1617. while (end > start && (*end == ';' || nsvg__isspace(*end))) --end;
  1618. ++end;
  1619. nsvg__parseNameValue(p, start, end);
  1620. if (*str) ++str;
  1621. }
  1622. }
  1623. static void nsvg__parseAttribs(NSVGparser* p, const char** attr)
  1624. {
  1625. int i;
  1626. for (i = 0; attr[i]; i += 2)
  1627. {
  1628. if (strcmp(attr[i], "style") == 0)
  1629. nsvg__parseStyle(p, attr[i + 1]);
  1630. else
  1631. nsvg__parseAttr(p, attr[i], attr[i + 1]);
  1632. }
  1633. }
  1634. static int nsvg__getArgsPerElement(char cmd)
  1635. {
  1636. switch (cmd) {
  1637. case 'v':
  1638. case 'V':
  1639. case 'h':
  1640. case 'H':
  1641. return 1;
  1642. case 'm':
  1643. case 'M':
  1644. case 'l':
  1645. case 'L':
  1646. case 't':
  1647. case 'T':
  1648. return 2;
  1649. case 'q':
  1650. case 'Q':
  1651. case 's':
  1652. case 'S':
  1653. return 4;
  1654. case 'c':
  1655. case 'C':
  1656. return 6;
  1657. case 'a':
  1658. case 'A':
  1659. return 7;
  1660. }
  1661. return 0;
  1662. }
  1663. static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
  1664. {
  1665. if (rel) {
  1666. *cpx += args[0];
  1667. *cpy += args[1];
  1668. } else {
  1669. *cpx = args[0];
  1670. *cpy = args[1];
  1671. }
  1672. nsvg__moveTo(p, *cpx, *cpy);
  1673. }
  1674. static void nsvg__pathLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
  1675. {
  1676. if (rel) {
  1677. *cpx += args[0];
  1678. *cpy += args[1];
  1679. } else {
  1680. *cpx = args[0];
  1681. *cpy = args[1];
  1682. }
  1683. nsvg__lineTo(p, *cpx, *cpy);
  1684. }
  1685. static void nsvg__pathHLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
  1686. {
  1687. if (rel)
  1688. *cpx += args[0];
  1689. else
  1690. *cpx = args[0];
  1691. nsvg__lineTo(p, *cpx, *cpy);
  1692. }
  1693. static void nsvg__pathVLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
  1694. {
  1695. if (rel)
  1696. *cpy += args[0];
  1697. else
  1698. *cpy = args[0];
  1699. nsvg__lineTo(p, *cpx, *cpy);
  1700. }
  1701. static void nsvg__pathCubicBezTo(NSVGparser* p, float* cpx, float* cpy,
  1702. float* cpx2, float* cpy2, float* args, int rel)
  1703. {
  1704. float x2, y2, cx1, cy1, cx2, cy2;
  1705. if (rel) {
  1706. cx1 = *cpx + args[0];
  1707. cy1 = *cpy + args[1];
  1708. cx2 = *cpx + args[2];
  1709. cy2 = *cpy + args[3];
  1710. x2 = *cpx + args[4];
  1711. y2 = *cpy + args[5];
  1712. } else {
  1713. cx1 = args[0];
  1714. cy1 = args[1];
  1715. cx2 = args[2];
  1716. cy2 = args[3];
  1717. x2 = args[4];
  1718. y2 = args[5];
  1719. }
  1720. nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
  1721. *cpx2 = cx2;
  1722. *cpy2 = cy2;
  1723. *cpx = x2;
  1724. *cpy = y2;
  1725. }
  1726. static void nsvg__pathCubicBezShortTo(NSVGparser* p, float* cpx, float* cpy,
  1727. float* cpx2, float* cpy2, float* args, int rel)
  1728. {
  1729. float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
  1730. x1 = *cpx;
  1731. y1 = *cpy;
  1732. if (rel) {
  1733. cx2 = *cpx + args[0];
  1734. cy2 = *cpy + args[1];
  1735. x2 = *cpx + args[2];
  1736. y2 = *cpy + args[3];
  1737. } else {
  1738. cx2 = args[0];
  1739. cy2 = args[1];
  1740. x2 = args[2];
  1741. y2 = args[3];
  1742. }
  1743. cx1 = 2*x1 - *cpx2;
  1744. cy1 = 2*y1 - *cpy2;
  1745. nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
  1746. *cpx2 = cx2;
  1747. *cpy2 = cy2;
  1748. *cpx = x2;
  1749. *cpy = y2;
  1750. }
  1751. static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy,
  1752. float* cpx2, float* cpy2, float* args, int rel)
  1753. {
  1754. float x1, y1, x2, y2, cx, cy;
  1755. float cx1, cy1, cx2, cy2;
  1756. x1 = *cpx;
  1757. y1 = *cpy;
  1758. if (rel) {
  1759. cx = *cpx + args[0];
  1760. cy = *cpy + args[1];
  1761. x2 = *cpx + args[2];
  1762. y2 = *cpy + args[3];
  1763. } else {
  1764. cx = args[0];
  1765. cy = args[1];
  1766. x2 = args[2];
  1767. y2 = args[3];
  1768. }
  1769. // Convert to cubic bezier
  1770. cx1 = x1 + 2.0f/3.0f*(cx - x1);
  1771. cy1 = y1 + 2.0f/3.0f*(cy - y1);
  1772. cx2 = x2 + 2.0f/3.0f*(cx - x2);
  1773. cy2 = y2 + 2.0f/3.0f*(cy - y2);
  1774. nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
  1775. *cpx2 = cx;
  1776. *cpy2 = cy;
  1777. *cpx = x2;
  1778. *cpy = y2;
  1779. }
  1780. static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy,
  1781. float* cpx2, float* cpy2, float* args, int rel)
  1782. {
  1783. float x1, y1, x2, y2, cx, cy;
  1784. float cx1, cy1, cx2, cy2;
  1785. x1 = *cpx;
  1786. y1 = *cpy;
  1787. if (rel) {
  1788. x2 = *cpx + args[0];
  1789. y2 = *cpy + args[1];
  1790. } else {
  1791. x2 = args[0];
  1792. y2 = args[1];
  1793. }
  1794. cx = 2*x1 - *cpx2;
  1795. cy = 2*y1 - *cpy2;
  1796. // Convert to cubix bezier
  1797. cx1 = x1 + 2.0f/3.0f*(cx - x1);
  1798. cy1 = y1 + 2.0f/3.0f*(cy - y1);
  1799. cx2 = x2 + 2.0f/3.0f*(cx - x2);
  1800. cy2 = y2 + 2.0f/3.0f*(cy - y2);
  1801. nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
  1802. *cpx2 = cx;
  1803. *cpy2 = cy;
  1804. *cpx = x2;
  1805. *cpy = y2;
  1806. }
  1807. static float nsvg__sqr(float x) { return x*x; }
  1808. static float nsvg__vmag(float x, float y) { return sqrtf(x*x + y*y); }
  1809. static float nsvg__vecrat(float ux, float uy, float vx, float vy)
  1810. {
  1811. return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy));
  1812. }
  1813. static float nsvg__vecang(float ux, float uy, float vx, float vy)
  1814. {
  1815. float r = nsvg__vecrat(ux,uy, vx,vy);
  1816. if (r < -1.0f) r = -1.0f;
  1817. if (r > 1.0f) r = 1.0f;
  1818. return ((ux*vy < uy*vx) ? -1.0f : 1.0f) * acosf(r);
  1819. }
  1820. static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
  1821. {
  1822. // Ported from canvg (https://code.google.com/p/canvg/)
  1823. float rx, ry, rotx;
  1824. float x1, y1, x2, y2, cx, cy, dx, dy, d;
  1825. float x1p, y1p, cxp, cyp, s, sa, sb;
  1826. float ux, uy, vx, vy, a1, da;
  1827. float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6];
  1828. float sinrx, cosrx;
  1829. int fa, fs;
  1830. int i, ndivs;
  1831. float hda, kappa;
  1832. rx = fabsf(args[0]); // y radius
  1833. ry = fabsf(args[1]); // x radius
  1834. rotx = args[2] / 180.0f * NSVG_PI; // x rotation angle
  1835. fa = fabsf(args[3]) > 1e-6 ? 1 : 0; // Large arc
  1836. fs = fabsf(args[4]) > 1e-6 ? 1 : 0; // Sweep direction
  1837. x1 = *cpx; // start point
  1838. y1 = *cpy;
  1839. if (rel) { // end point
  1840. x2 = *cpx + args[5];
  1841. y2 = *cpy + args[6];
  1842. } else {
  1843. x2 = args[5];
  1844. y2 = args[6];
  1845. }
  1846. dx = x1 - x2;
  1847. dy = y1 - y2;
  1848. d = sqrtf(dx*dx + dy*dy);
  1849. if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
  1850. // The arc degenerates to a line
  1851. nsvg__lineTo(p, x2, y2);
  1852. *cpx = x2;
  1853. *cpy = y2;
  1854. return;
  1855. }
  1856. sinrx = sinf(rotx);
  1857. cosrx = cosf(rotx);
  1858. // Convert to center point parameterization.
  1859. // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
  1860. // 1) Compute x1', y1'
  1861. x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
  1862. y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
  1863. d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry);
  1864. if (d > 1) {
  1865. d = sqrtf(d);
  1866. rx *= d;
  1867. ry *= d;
  1868. }
  1869. // 2) Compute cx', cy'
  1870. s = 0.0f;
  1871. sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p);
  1872. sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p);
  1873. if (sa < 0.0f) sa = 0.0f;
  1874. if (sb > 0.0f)
  1875. s = sqrtf(sa / sb);
  1876. if (fa == fs)
  1877. s = -s;
  1878. cxp = s * rx * y1p / ry;
  1879. cyp = s * -ry * x1p / rx;
  1880. // 3) Compute cx,cy from cx',cy'
  1881. cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp;
  1882. cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp;
  1883. // 4) Calculate theta1, and delta theta.
  1884. ux = (x1p - cxp) / rx;
  1885. uy = (y1p - cyp) / ry;
  1886. vx = (-x1p - cxp) / rx;
  1887. vy = (-y1p - cyp) / ry;
  1888. a1 = nsvg__vecang(1.0f,0.0f, ux,uy); // Initial angle
  1889. da = nsvg__vecang(ux,uy, vx,vy); // Delta angle
  1890. // if (vecrat(ux,uy,vx,vy) <= -1.0f) da = NSVG_PI;
  1891. // if (vecrat(ux,uy,vx,vy) >= 1.0f) da = 0;
  1892. if (fs == 0 && da > 0)
  1893. da -= 2 * NSVG_PI;
  1894. else if (fs == 1 && da < 0)
  1895. da += 2 * NSVG_PI;
  1896. // Approximate the arc using cubic spline segments.
  1897. t[0] = cosrx; t[1] = sinrx;
  1898. t[2] = -sinrx; t[3] = cosrx;
  1899. t[4] = cx; t[5] = cy;
  1900. // Split arc into max 90 degree segments.
  1901. // The loop assumes an iteration per end point (including start and end), this +1.
  1902. ndivs = (int)(fabsf(da) / (NSVG_PI*0.5f) + 1.0f);
  1903. hda = (da / (float)ndivs) / 2.0f;
  1904. kappa = fabsf(4.0f / 3.0f * (1.0f - cosf(hda)) / sinf(hda));
  1905. if (da < 0.0f)
  1906. kappa = -kappa;
  1907. for (i = 0; i <= ndivs; i++) {
  1908. a = a1 + da * ((float)i/(float)ndivs);
  1909. dx = cosf(a);
  1910. dy = sinf(a);
  1911. nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t); // position
  1912. nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa, t); // tangent
  1913. if (i > 0)
  1914. nsvg__cubicBezTo(p, px+ptanx,py+ptany, x-tanx, y-tany, x, y);
  1915. px = x;
  1916. py = y;
  1917. ptanx = tanx;
  1918. ptany = tany;
  1919. }
  1920. *cpx = x2;
  1921. *cpy = y2;
  1922. }
  1923. static void nsvg__parsePath(NSVGparser* p, const char** attr)
  1924. {
  1925. const char* s = NULL;
  1926. char cmd = '\0';
  1927. float args[10];
  1928. int nargs;
  1929. int rargs = 0;
  1930. float cpx, cpy, cpx2, cpy2;
  1931. const char* tmp[4];
  1932. char closedFlag;
  1933. int i;
  1934. char item[64];
  1935. for (i = 0; attr[i]; i += 2) {
  1936. if (strcmp(attr[i], "d") == 0) {
  1937. s = attr[i + 1];
  1938. } else {
  1939. tmp[0] = attr[i];
  1940. tmp[1] = attr[i + 1];
  1941. tmp[2] = 0;
  1942. tmp[3] = 0;
  1943. nsvg__parseAttribs(p, tmp);
  1944. }
  1945. }
  1946. if (s) {
  1947. nsvg__resetPath(p);
  1948. cpx = 0; cpy = 0;
  1949. cpx2 = 0; cpy2 = 0;
  1950. closedFlag = 0;
  1951. nargs = 0;
  1952. while (*s) {
  1953. s = nsvg__getNextPathItem(s, item);
  1954. if (!*item) break;
  1955. if (nsvg__isnum(item[0])) {
  1956. if (nargs < 10)
  1957. args[nargs++] = (float)nsvg__atof(item);
  1958. if (nargs >= rargs) {
  1959. switch (cmd) {
  1960. case 'm':
  1961. case 'M':
  1962. nsvg__pathMoveTo(p, &cpx, &cpy, args, cmd == 'm' ? 1 : 0);
  1963. // Moveto can be followed by multiple coordinate pairs,
  1964. // which should be treated as linetos.
  1965. cmd = (cmd == 'm') ? 'l' : 'L';
  1966. rargs = nsvg__getArgsPerElement(cmd);
  1967. cpx2 = cpx; cpy2 = cpy;
  1968. break;
  1969. case 'l':
  1970. case 'L':
  1971. nsvg__pathLineTo(p, &cpx, &cpy, args, cmd == 'l' ? 1 : 0);
  1972. cpx2 = cpx; cpy2 = cpy;
  1973. break;
  1974. case 'H':
  1975. case 'h':
  1976. nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd == 'h' ? 1 : 0);
  1977. cpx2 = cpx; cpy2 = cpy;
  1978. break;
  1979. case 'V':
  1980. case 'v':
  1981. nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd == 'v' ? 1 : 0);
  1982. cpx2 = cpx; cpy2 = cpy;
  1983. break;
  1984. case 'C':
  1985. case 'c':
  1986. nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'c' ? 1 : 0);
  1987. break;
  1988. case 'S':
  1989. case 's':
  1990. nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 's' ? 1 : 0);
  1991. break;
  1992. case 'Q':
  1993. case 'q':
  1994. nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'q' ? 1 : 0);
  1995. break;
  1996. case 'T':
  1997. case 't':
  1998. nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 't' ? 1 : 0);
  1999. break;
  2000. case 'A':
  2001. case 'a':
  2002. nsvg__pathArcTo(p, &cpx, &cpy, args, cmd == 'a' ? 1 : 0);
  2003. cpx2 = cpx; cpy2 = cpy;
  2004. break;
  2005. default:
  2006. if (nargs >= 2) {
  2007. cpx = args[nargs-2];
  2008. cpy = args[nargs-1];
  2009. cpx2 = cpx; cpy2 = cpy;
  2010. }
  2011. break;
  2012. }
  2013. nargs = 0;
  2014. }
  2015. } else {
  2016. cmd = item[0];
  2017. rargs = nsvg__getArgsPerElement(cmd);
  2018. if (cmd == 'M' || cmd == 'm') {
  2019. // Commit path.
  2020. if (p->npts > 0)
  2021. nsvg__addPath(p, closedFlag);
  2022. // Start new subpath.
  2023. nsvg__resetPath(p);
  2024. closedFlag = 0;
  2025. nargs = 0;
  2026. } else if (cmd == 'Z' || cmd == 'z') {
  2027. closedFlag = 1;
  2028. // Commit path.
  2029. if (p->npts > 0) {
  2030. // Move current point to first point
  2031. cpx = p->pts[0];
  2032. cpy = p->pts[1];
  2033. cpx2 = cpx; cpy2 = cpy;
  2034. nsvg__addPath(p, closedFlag);
  2035. }
  2036. // Start new subpath.
  2037. nsvg__resetPath(p);
  2038. nsvg__moveTo(p, cpx, cpy);
  2039. closedFlag = 0;
  2040. nargs = 0;
  2041. }
  2042. }
  2043. }
  2044. // Commit path.
  2045. if (p->npts)
  2046. nsvg__addPath(p, closedFlag);
  2047. }
  2048. nsvg__addShape(p);
  2049. }
  2050. static void nsvg__parseRect(NSVGparser* p, const char** attr)
  2051. {
  2052. float x = 0.0f;
  2053. float y = 0.0f;
  2054. float w = 0.0f;
  2055. float h = 0.0f;
  2056. float rx = -1.0f; // marks not set
  2057. float ry = -1.0f;
  2058. int i;
  2059. for (i = 0; attr[i]; i += 2) {
  2060. if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2061. if (strcmp(attr[i], "x") == 0) x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
  2062. if (strcmp(attr[i], "y") == 0) y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
  2063. if (strcmp(attr[i], "width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p));
  2064. if (strcmp(attr[i], "height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p));
  2065. if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
  2066. if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
  2067. }
  2068. }
  2069. if (rx < 0.0f && ry > 0.0f) rx = ry;
  2070. if (ry < 0.0f && rx > 0.0f) ry = rx;
  2071. if (rx < 0.0f) rx = 0.0f;
  2072. if (ry < 0.0f) ry = 0.0f;
  2073. if (rx > w/2.0f) rx = w/2.0f;
  2074. if (ry > h/2.0f) ry = h/2.0f;
  2075. if (w != 0.0f && h != 0.0f) {
  2076. nsvg__resetPath(p);
  2077. if (rx < 0.00001f || ry < 0.0001f) {
  2078. nsvg__moveTo(p, x, y);
  2079. nsvg__lineTo(p, x+w, y);
  2080. nsvg__lineTo(p, x+w, y+h);
  2081. nsvg__lineTo(p, x, y+h);
  2082. } else {
  2083. // Rounded rectangle
  2084. nsvg__moveTo(p, x+rx, y);
  2085. nsvg__lineTo(p, x+w-rx, y);
  2086. nsvg__cubicBezTo(p, x+w-rx*(1-NSVG_KAPPA90), y, x+w, y+ry*(1-NSVG_KAPPA90), x+w, y+ry);
  2087. nsvg__lineTo(p, x+w, y+h-ry);
  2088. nsvg__cubicBezTo(p, x+w, y+h-ry*(1-NSVG_KAPPA90), x+w-rx*(1-NSVG_KAPPA90), y+h, x+w-rx, y+h);
  2089. nsvg__lineTo(p, x+rx, y+h);
  2090. nsvg__cubicBezTo(p, x+rx*(1-NSVG_KAPPA90), y+h, x, y+h-ry*(1-NSVG_KAPPA90), x, y+h-ry);
  2091. nsvg__lineTo(p, x, y+ry);
  2092. nsvg__cubicBezTo(p, x, y+ry*(1-NSVG_KAPPA90), x+rx*(1-NSVG_KAPPA90), y, x+rx, y);
  2093. }
  2094. nsvg__addPath(p, 1);
  2095. nsvg__addShape(p);
  2096. }
  2097. }
  2098. static void nsvg__parseCircle(NSVGparser* p, const char** attr)
  2099. {
  2100. float cx = 0.0f;
  2101. float cy = 0.0f;
  2102. float r = 0.0f;
  2103. int i;
  2104. for (i = 0; attr[i]; i += 2) {
  2105. if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2106. if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
  2107. if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
  2108. if (strcmp(attr[i], "r") == 0) r = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p)));
  2109. }
  2110. }
  2111. if (r > 0.0f) {
  2112. nsvg__resetPath(p);
  2113. nsvg__moveTo(p, cx+r, cy);
  2114. nsvg__cubicBezTo(p, cx+r, cy+r*NSVG_KAPPA90, cx+r*NSVG_KAPPA90, cy+r, cx, cy+r);
  2115. nsvg__cubicBezTo(p, cx-r*NSVG_KAPPA90, cy+r, cx-r, cy+r*NSVG_KAPPA90, cx-r, cy);
  2116. nsvg__cubicBezTo(p, cx-r, cy-r*NSVG_KAPPA90, cx-r*NSVG_KAPPA90, cy-r, cx, cy-r);
  2117. nsvg__cubicBezTo(p, cx+r*NSVG_KAPPA90, cy-r, cx+r, cy-r*NSVG_KAPPA90, cx+r, cy);
  2118. nsvg__addPath(p, 1);
  2119. nsvg__addShape(p);
  2120. }
  2121. }
  2122. static void nsvg__parseEllipse(NSVGparser* p, const char** attr)
  2123. {
  2124. float cx = 0.0f;
  2125. float cy = 0.0f;
  2126. float rx = 0.0f;
  2127. float ry = 0.0f;
  2128. int i;
  2129. for (i = 0; attr[i]; i += 2) {
  2130. if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2131. if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
  2132. if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
  2133. if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
  2134. if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
  2135. }
  2136. }
  2137. if (rx > 0.0f && ry > 0.0f) {
  2138. nsvg__resetPath(p);
  2139. nsvg__moveTo(p, cx+rx, cy);
  2140. nsvg__cubicBezTo(p, cx+rx, cy+ry*NSVG_KAPPA90, cx+rx*NSVG_KAPPA90, cy+ry, cx, cy+ry);
  2141. nsvg__cubicBezTo(p, cx-rx*NSVG_KAPPA90, cy+ry, cx-rx, cy+ry*NSVG_KAPPA90, cx-rx, cy);
  2142. nsvg__cubicBezTo(p, cx-rx, cy-ry*NSVG_KAPPA90, cx-rx*NSVG_KAPPA90, cy-ry, cx, cy-ry);
  2143. nsvg__cubicBezTo(p, cx+rx*NSVG_KAPPA90, cy-ry, cx+rx, cy-ry*NSVG_KAPPA90, cx+rx, cy);
  2144. nsvg__addPath(p, 1);
  2145. nsvg__addShape(p);
  2146. }
  2147. }
  2148. static void nsvg__parseLine(NSVGparser* p, const char** attr)
  2149. {
  2150. float x1 = 0.0;
  2151. float y1 = 0.0;
  2152. float x2 = 0.0;
  2153. float y2 = 0.0;
  2154. int i;
  2155. for (i = 0; attr[i]; i += 2) {
  2156. if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2157. if (strcmp(attr[i], "x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
  2158. if (strcmp(attr[i], "y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
  2159. if (strcmp(attr[i], "x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
  2160. if (strcmp(attr[i], "y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
  2161. }
  2162. }
  2163. nsvg__resetPath(p);
  2164. nsvg__moveTo(p, x1, y1);
  2165. nsvg__lineTo(p, x2, y2);
  2166. nsvg__addPath(p, 0);
  2167. nsvg__addShape(p);
  2168. }
  2169. static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag)
  2170. {
  2171. int i;
  2172. const char* s;
  2173. float args[2];
  2174. int nargs, npts = 0;
  2175. char item[64];
  2176. nsvg__resetPath(p);
  2177. for (i = 0; attr[i]; i += 2) {
  2178. if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2179. if (strcmp(attr[i], "points") == 0) {
  2180. s = attr[i + 1];
  2181. nargs = 0;
  2182. while (*s) {
  2183. s = nsvg__getNextPathItem(s, item);
  2184. args[nargs++] = (float)nsvg__atof(item);
  2185. if (nargs >= 2) {
  2186. if (npts == 0)
  2187. nsvg__moveTo(p, args[0], args[1]);
  2188. else
  2189. nsvg__lineTo(p, args[0], args[1]);
  2190. nargs = 0;
  2191. npts++;
  2192. }
  2193. }
  2194. }
  2195. }
  2196. }
  2197. nsvg__addPath(p, (char)closeFlag);
  2198. nsvg__addShape(p);
  2199. }
  2200. static void nsvg__parseSVG(NSVGparser* p, const char** attr)
  2201. {
  2202. int i;
  2203. for (i = 0; attr[i]; i += 2) {
  2204. if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2205. if (strcmp(attr[i], "width") == 0) {
  2206. p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 1.0f);
  2207. } else if (strcmp(attr[i], "height") == 0) {
  2208. p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 1.0f);
  2209. } else if (strcmp(attr[i], "viewBox") == 0) {
  2210. sscanf(attr[i + 1], "%f%*[%%, \t]%f%*[%%, \t]%f%*[%%, \t]%f", &p->viewMinx, &p->viewMiny, &p->viewWidth, &p->viewHeight);
  2211. } else if (strcmp(attr[i], "preserveAspectRatio") == 0) {
  2212. if (strstr(attr[i + 1], "none") != 0) {
  2213. // No uniform scaling
  2214. p->alignType = NSVG_ALIGN_NONE;
  2215. } else {
  2216. // Parse X align
  2217. if (strstr(attr[i + 1], "xMin") != 0)
  2218. p->alignX = NSVG_ALIGN_MIN;
  2219. else if (strstr(attr[i + 1], "xMid") != 0)
  2220. p->alignX = NSVG_ALIGN_MID;
  2221. else if (strstr(attr[i + 1], "xMax") != 0)
  2222. p->alignX = NSVG_ALIGN_MAX;
  2223. // Parse X align
  2224. if (strstr(attr[i + 1], "yMin") != 0)
  2225. p->alignY = NSVG_ALIGN_MIN;
  2226. else if (strstr(attr[i + 1], "yMid") != 0)
  2227. p->alignY = NSVG_ALIGN_MID;
  2228. else if (strstr(attr[i + 1], "yMax") != 0)
  2229. p->alignY = NSVG_ALIGN_MAX;
  2230. // Parse meet/slice
  2231. p->alignType = NSVG_ALIGN_MEET;
  2232. if (strstr(attr[i + 1], "slice") != 0)
  2233. p->alignType = NSVG_ALIGN_SLICE;
  2234. }
  2235. }
  2236. }
  2237. }
  2238. }
  2239. static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type)
  2240. {
  2241. int i;
  2242. NSVGgradientData* grad = (NSVGgradientData*)malloc(sizeof(NSVGgradientData));
  2243. if (grad == NULL) return;
  2244. memset(grad, 0, sizeof(NSVGgradientData));
  2245. grad->units = NSVG_OBJECT_SPACE;
  2246. grad->type = type;
  2247. if (grad->type == NSVG_PAINT_LINEAR_GRADIENT) {
  2248. grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
  2249. grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
  2250. grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
  2251. grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
  2252. } else if (grad->type == NSVG_PAINT_RADIAL_GRADIENT) {
  2253. grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
  2254. grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
  2255. grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
  2256. }
  2257. nsvg__xformIdentity(grad->xform);
  2258. for (i = 0; attr[i]; i += 2) {
  2259. if (strcmp(attr[i], "id") == 0) {
  2260. strncpy(grad->id, attr[i+1], 63);
  2261. grad->id[63] = '\0';
  2262. } else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
  2263. if (strcmp(attr[i], "gradientUnits") == 0) {
  2264. if (strcmp(attr[i+1], "objectBoundingBox") == 0)
  2265. grad->units = NSVG_OBJECT_SPACE;
  2266. else
  2267. grad->units = NSVG_USER_SPACE;
  2268. } else if (strcmp(attr[i], "gradientTransform") == 0) {
  2269. nsvg__parseTransform(grad->xform, attr[i + 1]);
  2270. } else if (strcmp(attr[i], "cx") == 0) {
  2271. grad->radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
  2272. } else if (strcmp(attr[i], "cy") == 0) {
  2273. grad->radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
  2274. } else if (strcmp(attr[i], "r") == 0) {
  2275. grad->radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
  2276. } else if (strcmp(attr[i], "fx") == 0) {
  2277. grad->radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
  2278. } else if (strcmp(attr[i], "fy") == 0) {
  2279. grad->radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
  2280. } else if (strcmp(attr[i], "x1") == 0) {
  2281. grad->linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
  2282. } else if (strcmp(attr[i], "y1") == 0) {
  2283. grad->linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
  2284. } else if (strcmp(attr[i], "x2") == 0) {
  2285. grad->linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
  2286. } else if (strcmp(attr[i], "y2") == 0) {
  2287. grad->linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
  2288. } else if (strcmp(attr[i], "spreadMethod") == 0) {
  2289. if (strcmp(attr[i+1], "pad") == 0)
  2290. grad->spread = NSVG_SPREAD_PAD;
  2291. else if (strcmp(attr[i+1], "reflect") == 0)
  2292. grad->spread = NSVG_SPREAD_REFLECT;
  2293. else if (strcmp(attr[i+1], "repeat") == 0)
  2294. grad->spread = NSVG_SPREAD_REPEAT;
  2295. } else if (strcmp(attr[i], "xlink:href") == 0) {
  2296. const char *href = attr[i+1];
  2297. strncpy(grad->ref, href+1, 62);
  2298. grad->ref[62] = '\0';
  2299. }
  2300. }
  2301. }
  2302. grad->next = p->gradients;
  2303. p->gradients = grad;
  2304. }
  2305. static void nsvg__parseGradientStop(NSVGparser* p, const char** attr)
  2306. {
  2307. NSVGattrib* curAttr = nsvg__getAttr(p);
  2308. NSVGgradientData* grad;
  2309. NSVGgradientStop* stop;
  2310. int i, idx;
  2311. curAttr->stopOffset = 0;
  2312. curAttr->stopColor = 0;
  2313. curAttr->stopOpacity = 1.0f;
  2314. for (i = 0; attr[i]; i += 2) {
  2315. nsvg__parseAttr(p, attr[i], attr[i + 1]);
  2316. }
  2317. // Add stop to the last gradient.
  2318. grad = p->gradients;
  2319. if (grad == NULL) return;
  2320. grad->nstops++;
  2321. grad->stops = (NSVGgradientStop*)realloc(grad->stops, sizeof(NSVGgradientStop)*grad->nstops);
  2322. if (grad->stops == NULL) return;
  2323. // Insert
  2324. idx = grad->nstops-1;
  2325. for (i = 0; i < grad->nstops-1; i++) {
  2326. if (curAttr->stopOffset < grad->stops[i].offset) {
  2327. idx = i;
  2328. break;
  2329. }
  2330. }
  2331. if (idx != grad->nstops-1) {
  2332. for (i = grad->nstops-1; i > idx; i--)
  2333. grad->stops[i] = grad->stops[i-1];
  2334. }
  2335. stop = &grad->stops[idx];
  2336. stop->color = curAttr->stopColor;
  2337. stop->color |= (unsigned int)(curAttr->stopOpacity*255) << 24;
  2338. stop->offset = curAttr->stopOffset;
  2339. }
  2340. static void nsvg__startElement(void* ud, const char* el, const char** attr)
  2341. {
  2342. NSVGparser* p = (NSVGparser*)ud;
  2343. if (p->defsFlag) {
  2344. // Skip everything but gradients in defs
  2345. if (strcmp(el, "linearGradient") == 0) {
  2346. nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
  2347. } else if (strcmp(el, "radialGradient") == 0) {
  2348. nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
  2349. } else if (strcmp(el, "stop") == 0) {
  2350. nsvg__parseGradientStop(p, attr);
  2351. }
  2352. return;
  2353. }
  2354. if (strcmp(el, "g") == 0) {
  2355. nsvg__pushAttr(p);
  2356. nsvg__parseAttribs(p, attr);
  2357. } else if (strcmp(el, "path") == 0) {
  2358. if (p->pathFlag) // Do not allow nested paths.
  2359. return;
  2360. nsvg__pushAttr(p);
  2361. nsvg__parsePath(p, attr);
  2362. nsvg__popAttr(p);
  2363. } else if (strcmp(el, "rect") == 0) {
  2364. nsvg__pushAttr(p);
  2365. nsvg__parseRect(p, attr);
  2366. nsvg__popAttr(p);
  2367. } else if (strcmp(el, "circle") == 0) {
  2368. nsvg__pushAttr(p);
  2369. nsvg__parseCircle(p, attr);
  2370. nsvg__popAttr(p);
  2371. } else if (strcmp(el, "ellipse") == 0) {
  2372. nsvg__pushAttr(p);
  2373. nsvg__parseEllipse(p, attr);
  2374. nsvg__popAttr(p);
  2375. } else if (strcmp(el, "line") == 0) {
  2376. nsvg__pushAttr(p);
  2377. nsvg__parseLine(p, attr);
  2378. nsvg__popAttr(p);
  2379. } else if (strcmp(el, "polyline") == 0) {
  2380. nsvg__pushAttr(p);
  2381. nsvg__parsePoly(p, attr, 0);
  2382. nsvg__popAttr(p);
  2383. } else if (strcmp(el, "polygon") == 0) {
  2384. nsvg__pushAttr(p);
  2385. nsvg__parsePoly(p, attr, 1);
  2386. nsvg__popAttr(p);
  2387. } else if (strcmp(el, "linearGradient") == 0) {
  2388. nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
  2389. } else if (strcmp(el, "radialGradient") == 0) {
  2390. nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
  2391. } else if (strcmp(el, "stop") == 0) {
  2392. nsvg__parseGradientStop(p, attr);
  2393. } else if (strcmp(el, "defs") == 0) {
  2394. p->defsFlag = 1;
  2395. } else if (strcmp(el, "svg") == 0) {
  2396. nsvg__parseSVG(p, attr);
  2397. }
  2398. }
  2399. static void nsvg__endElement(void* ud, const char* el)
  2400. {
  2401. NSVGparser* p = (NSVGparser*)ud;
  2402. if (strcmp(el, "g") == 0) {
  2403. nsvg__popAttr(p);
  2404. } else if (strcmp(el, "path") == 0) {
  2405. p->pathFlag = 0;
  2406. } else if (strcmp(el, "defs") == 0) {
  2407. p->defsFlag = 0;
  2408. }
  2409. }
  2410. static void nsvg__content(void* ud, const char* s)
  2411. {
  2412. NSVG_NOTUSED(ud);
  2413. NSVG_NOTUSED(s);
  2414. // empty
  2415. }
  2416. static void nsvg__imageBounds(NSVGparser* p, float* bounds)
  2417. {
  2418. NSVGshape* shape;
  2419. shape = p->image->shapes;
  2420. if (shape == NULL) {
  2421. bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
  2422. return;
  2423. }
  2424. bounds[0] = shape->bounds[0];
  2425. bounds[1] = shape->bounds[1];
  2426. bounds[2] = shape->bounds[2];
  2427. bounds[3] = shape->bounds[3];
  2428. for (shape = shape->next; shape != NULL; shape = shape->next) {
  2429. bounds[0] = nsvg__minf(bounds[0], shape->bounds[0]);
  2430. bounds[1] = nsvg__minf(bounds[1], shape->bounds[1]);
  2431. bounds[2] = nsvg__maxf(bounds[2], shape->bounds[2]);
  2432. bounds[3] = nsvg__maxf(bounds[3], shape->bounds[3]);
  2433. }
  2434. }
  2435. static float nsvg__viewAlign(float content, float container, int type)
  2436. {
  2437. if (type == NSVG_ALIGN_MIN)
  2438. return 0;
  2439. else if (type == NSVG_ALIGN_MAX)
  2440. return container - content;
  2441. // mid
  2442. return (container - content) * 0.5f;
  2443. }
  2444. static void nsvg__scaleGradient(NSVGgradient* grad, float tx, float ty, float sx, float sy)
  2445. {
  2446. float t[6];
  2447. nsvg__xformSetTranslation(t, tx, ty);
  2448. nsvg__xformMultiply (grad->xform, t);
  2449. nsvg__xformSetScale(t, sx, sy);
  2450. nsvg__xformMultiply (grad->xform, t);
  2451. }
  2452. static void nsvg__scaleToViewbox(NSVGparser* p, const char* units)
  2453. {
  2454. NSVGshape* shape;
  2455. NSVGpath* path;
  2456. float tx, ty, sx, sy, us, bounds[4], t[6], avgs;
  2457. int i;
  2458. float* pt;
  2459. // Guess image size if not set completely.
  2460. nsvg__imageBounds(p, bounds);
  2461. if (p->viewWidth == 0) {
  2462. if (p->image->width > 0) {
  2463. p->viewWidth = p->image->width;
  2464. } else {
  2465. p->viewMinx = bounds[0];
  2466. p->viewWidth = bounds[2] - bounds[0];
  2467. }
  2468. }
  2469. if (p->viewHeight == 0) {
  2470. if (p->image->height > 0) {
  2471. p->viewHeight = p->image->height;
  2472. } else {
  2473. p->viewMiny = bounds[1];
  2474. p->viewHeight = bounds[3] - bounds[1];
  2475. }
  2476. }
  2477. if (p->image->width == 0)
  2478. p->image->width = p->viewWidth;
  2479. if (p->image->height == 0)
  2480. p->image->height = p->viewHeight;
  2481. tx = -p->viewMinx;
  2482. ty = -p->viewMiny;
  2483. sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
  2484. sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
  2485. // Unit scaling
  2486. us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
  2487. // Fix aspect ratio
  2488. if (p->alignType == NSVG_ALIGN_MEET) {
  2489. // fit whole image into viewbox
  2490. sx = sy = nsvg__minf(sx, sy);
  2491. tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
  2492. ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
  2493. } else if (p->alignType == NSVG_ALIGN_SLICE) {
  2494. // fill whole viewbox with image
  2495. sx = sy = nsvg__maxf(sx, sy);
  2496. tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
  2497. ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
  2498. }
  2499. // Transform
  2500. sx *= us;
  2501. sy *= us;
  2502. avgs = (sx+sy) / 2.0f;
  2503. for (shape = p->image->shapes; shape != NULL; shape = shape->next) {
  2504. shape->bounds[0] = (shape->bounds[0] + tx) * sx;
  2505. shape->bounds[1] = (shape->bounds[1] + ty) * sy;
  2506. shape->bounds[2] = (shape->bounds[2] + tx) * sx;
  2507. shape->bounds[3] = (shape->bounds[3] + ty) * sy;
  2508. for (path = shape->paths; path != NULL; path = path->next) {
  2509. path->bounds[0] = (path->bounds[0] + tx) * sx;
  2510. path->bounds[1] = (path->bounds[1] + ty) * sy;
  2511. path->bounds[2] = (path->bounds[2] + tx) * sx;
  2512. path->bounds[3] = (path->bounds[3] + ty) * sy;
  2513. for (i =0; i < path->npts; i++) {
  2514. pt = &path->pts[i*2];
  2515. pt[0] = (pt[0] + tx) * sx;
  2516. pt[1] = (pt[1] + ty) * sy;
  2517. }
  2518. }
  2519. if (shape->fill.type == NSVG_PAINT_LINEAR_GRADIENT || shape->fill.type == NSVG_PAINT_RADIAL_GRADIENT) {
  2520. nsvg__scaleGradient(shape->fill.gradient, tx,ty, sx,sy);
  2521. memcpy(t, shape->fill.gradient->xform, sizeof(float)*6);
  2522. nsvg__xformInverse(shape->fill.gradient->xform, t);
  2523. }
  2524. if (shape->stroke.type == NSVG_PAINT_LINEAR_GRADIENT || shape->stroke.type == NSVG_PAINT_RADIAL_GRADIENT) {
  2525. nsvg__scaleGradient(shape->stroke.gradient, tx,ty, sx,sy);
  2526. memcpy(t, shape->stroke.gradient->xform, sizeof(float)*6);
  2527. nsvg__xformInverse(shape->stroke.gradient->xform, t);
  2528. }
  2529. shape->strokeWidth *= avgs;
  2530. shape->strokeDashOffset *= avgs;
  2531. for (i = 0; i < shape->strokeDashCount; i++)
  2532. shape->strokeDashArray[i] *= avgs;
  2533. }
  2534. }
  2535. NSVGimage* nsvgParse(char* input, const char* units, float dpi)
  2536. {
  2537. NSVGparser* p;
  2538. NSVGimage* ret = 0;
  2539. p = nsvg__createParser();
  2540. if (p == NULL) {
  2541. return NULL;
  2542. }
  2543. p->dpi = dpi;
  2544. nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
  2545. // Scale to viewBox
  2546. nsvg__scaleToViewbox(p, units);
  2547. ret = p->image;
  2548. p->image = NULL;
  2549. nsvg__deleteParser(p);
  2550. return ret;
  2551. }
  2552. NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi)
  2553. {
  2554. FILE* fp = NULL;
  2555. size_t size;
  2556. char* data = NULL;
  2557. NSVGimage* image = NULL;
  2558. fp = fopen(filename, "rb");
  2559. if (!fp) goto error;
  2560. fseek(fp, 0, SEEK_END);
  2561. size = ftell(fp);
  2562. fseek(fp, 0, SEEK_SET);
  2563. data = (char*)malloc(size+1);
  2564. if (data == NULL) goto error;
  2565. if (fread(data, 1, size, fp) != size) goto error;
  2566. data[size] = '\0'; // Must be null terminated.
  2567. fclose(fp);
  2568. image = nsvgParse(data, units, dpi);
  2569. free(data);
  2570. return image;
  2571. error:
  2572. if (fp) fclose(fp);
  2573. if (data) free(data);
  2574. if (image) nsvgDelete(image);
  2575. return NULL;
  2576. }
  2577. void nsvgDelete(NSVGimage* image)
  2578. {
  2579. NSVGshape *snext, *shape;
  2580. if (image == NULL) return;
  2581. shape = image->shapes;
  2582. while (shape != NULL) {
  2583. snext = shape->next;
  2584. nsvg__deletePaths(shape->paths);
  2585. nsvg__deletePaint(&shape->fill);
  2586. nsvg__deletePaint(&shape->stroke);
  2587. free(shape);
  2588. shape = snext;
  2589. }
  2590. free(image);
  2591. }
  2592. #endif