nanosvg.h 77 KB

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