nanosvg.h 76 KB

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