PageRenderTime 67ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/ImageMagick-6.3.2/coders/svg.c

https://bitbucket.org/sisko/operation-caribou
C | 4256 lines | 3759 code | 192 blank | 305 comment | 954 complexity | 52537d1ad8c6371a664884897946fa37 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % SSSSS V V GGGG %
  7. % SS V V G %
  8. % SSS V V G GG %
  9. % SS V V G G %
  10. % SSSSS V GGG %
  11. % %
  12. % %
  13. % Read/Write Scalable Vector Graphics Format. %
  14. % %
  15. % Software Design %
  16. % John Cristy %
  17. % William Radcliffe %
  18. % March 2000 %
  19. % %
  20. % %
  21. % Copyright 1999-2006 ImageMagick Studio LLC, a non-profit organization %
  22. % dedicated to making software imaging solutions freely available. %
  23. % %
  24. % You may not use this file except in compliance with the License. You may %
  25. % obtain a copy of the License at %
  26. % %
  27. % http://www.imagemagick.org/script/license.php %
  28. % %
  29. % Unless required by applicable law or agreed to in writing, software %
  30. % distributed under the License is distributed on an "AS IS" BASIS, %
  31. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
  32. % See the License for the specific language governing permissions and %
  33. % limitations under the License. %
  34. % %
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. %
  37. %
  38. */
  39. /*
  40. Include declarations.
  41. */
  42. #include "magick/studio.h"
  43. #include "magick/annotate.h"
  44. #include "magick/blob.h"
  45. #include "magick/blob-private.h"
  46. #include "magick/constitute.h"
  47. #include "magick/composite-private.h"
  48. #include "magick/draw.h"
  49. #include "magick/exception.h"
  50. #include "magick/exception-private.h"
  51. #include "magick/gem.h"
  52. #include "magick/image.h"
  53. #include "magick/image-private.h"
  54. #include "magick/list.h"
  55. #include "magick/log.h"
  56. #include "magick/magick.h"
  57. #include "magick/memory_.h"
  58. #include "magick/quantum.h"
  59. #include "magick/property.h"
  60. #include "magick/resource_.h"
  61. #include "magick/static.h"
  62. #include "magick/string_.h"
  63. #include "magick/token.h"
  64. #include "magick/utility.h"
  65. #if defined(HasXML)
  66. # if defined(__WINDOWS__)
  67. # if defined(__MINGW32__)
  68. # define _MSC_VER
  69. # endif
  70. # include <win32config.h>
  71. # endif
  72. # include <libxml/parser.h>
  73. # include <libxml/xmlmemory.h>
  74. # include <libxml/parserInternals.h>
  75. # include <libxml/xmlerror.h>
  76. #endif
  77. #if defined(HasAUTOTRACE)
  78. #include "types.h"
  79. #include "image-header.h"
  80. #include "fit.h"
  81. #include "output.h"
  82. #include "pxl-outline.h"
  83. #include "atquantize.h"
  84. #include "thin-image.h"
  85. char
  86. *version_string = "AutoTrace version 0.24a";
  87. #endif
  88. #if defined(HasRSVG)
  89. #include "librsvg/rsvg.h"
  90. #endif
  91. /*
  92. Define declarations.
  93. */
  94. #define MVGPrintf (void) fprintf
  95. /*
  96. Typedef declarations.
  97. */
  98. typedef struct _BoundingBox
  99. {
  100. double
  101. x,
  102. y,
  103. width,
  104. height;
  105. } BoundingBox;
  106. typedef struct _ElementInfo
  107. {
  108. double
  109. cx,
  110. cy,
  111. major,
  112. minor,
  113. angle;
  114. } ElementInfo;
  115. typedef struct _SVGInfo
  116. {
  117. FILE
  118. *file;
  119. ExceptionInfo
  120. *exception;
  121. Image
  122. *image;
  123. const ImageInfo
  124. *image_info;
  125. AffineMatrix
  126. affine;
  127. unsigned long
  128. width,
  129. height;
  130. char
  131. *size,
  132. *title,
  133. *comment;
  134. int
  135. n;
  136. double
  137. *scale,
  138. pointsize;
  139. ElementInfo
  140. element;
  141. SegmentInfo
  142. segment;
  143. BoundingBox
  144. bounds,
  145. view_box;
  146. PointInfo
  147. radius;
  148. char
  149. *stop_color,
  150. *offset,
  151. *text,
  152. *vertices,
  153. *url;
  154. #if defined(HasXML)
  155. xmlParserCtxtPtr
  156. parser;
  157. xmlDocPtr
  158. document;
  159. #endif
  160. } SVGInfo;
  161. /*
  162. Forward declarations.
  163. */
  164. static MagickBooleanType
  165. WriteSVGImage(const ImageInfo *,Image *);
  166. /*
  167. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  168. % %
  169. % %
  170. % %
  171. % I s S V G %
  172. % %
  173. % %
  174. % %
  175. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  176. %
  177. % IsSVG()() returns MagickTrue if the image format type, identified by the
  178. % magick string, is SVG.
  179. %
  180. % The format of the ReadSVGImage method is:
  181. %
  182. % MagickBooleanType IsSVG(const unsigned char *magick,const size_t length)
  183. %
  184. % A description of each parameter follows:
  185. %
  186. % o magick: This string is generally the first few bytes of an image file
  187. % or blob.
  188. %
  189. % o length: Specifies the length of the magick string.
  190. %
  191. %
  192. */
  193. static MagickBooleanType IsSVG(const unsigned char *magick,const size_t length)
  194. {
  195. if (length < 4)
  196. return(MagickFalse);
  197. if (LocaleNCompare((char *) magick,"?xml",4) == 0)
  198. return(MagickTrue);
  199. return(MagickFalse);
  200. }
  201. #if defined(HasXML)
  202. /*
  203. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  204. % %
  205. % %
  206. % %
  207. % R e a d S V G I m a g e %
  208. % %
  209. % %
  210. % %
  211. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  212. %
  213. % ReadSVGImage() reads a Scalable Vector Gaphics file and returns it. It
  214. % allocates the memory necessary for the new Image structure and returns a
  215. % pointer to the new image.
  216. %
  217. % The format of the ReadSVGImage method is:
  218. %
  219. % Image *ReadSVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
  220. %
  221. % A description of each parameter follows:
  222. %
  223. % o image_info: The image info.
  224. %
  225. % o exception: return any errors or warnings in this structure.
  226. %
  227. */
  228. static double GetUserSpaceCoordinateValue(const SVGInfo *svg_info,int type,
  229. const char *string)
  230. {
  231. char
  232. *p,
  233. token[MaxTextExtent];
  234. double
  235. value;
  236. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",string);
  237. assert(string != (const char *) NULL);
  238. p=(char *) string;
  239. GetMagickToken(p,&p,token);
  240. value=atof(token);
  241. if (strchr(token,'%') != (char *) NULL)
  242. {
  243. double
  244. alpha,
  245. beta;
  246. if (type > 0)
  247. return(svg_info->view_box.width*value/100.0);
  248. if (type < 0)
  249. return(svg_info->view_box.height*value/100.0);
  250. alpha=value-svg_info->view_box.width;
  251. beta=value-svg_info->view_box.height;
  252. return(hypot(alpha,beta)/sqrt(2.0)/100.0);
  253. }
  254. GetMagickToken(p,&p,token);
  255. if (LocaleNCompare(token,"cm",2) == 0)
  256. return(DefaultResolution*svg_info->scale[0]/2.54*value);
  257. if (LocaleNCompare(token,"em",2) == 0)
  258. return(svg_info->pointsize*value);
  259. if (LocaleNCompare(token,"ex",2) == 0)
  260. return(svg_info->pointsize*value/2.0);
  261. if (LocaleNCompare(token,"in",2) == 0)
  262. return(DefaultResolution*svg_info->scale[0]*value);
  263. if (LocaleNCompare(token,"mm",2) == 0)
  264. return(DefaultResolution*svg_info->scale[0]/25.4*value);
  265. if (LocaleNCompare(token,"pc",2) == 0)
  266. return(DefaultResolution*svg_info->scale[0]/6.0*value);
  267. if (LocaleNCompare(token,"pt",2) == 0)
  268. return(svg_info->scale[0]*value);
  269. if (LocaleNCompare(token,"px",2) == 0)
  270. return(value);
  271. return(value);
  272. }
  273. static char **GetStyleTokens(void *context,const char *text,int *number_tokens)
  274. {
  275. char
  276. **tokens;
  277. register const char
  278. *p,
  279. *q;
  280. register long
  281. i;
  282. SVGInfo
  283. *svg_info;
  284. svg_info=(SVGInfo *) context;
  285. *number_tokens=0;
  286. if (text == (const char *) NULL)
  287. return((char **) NULL);
  288. /*
  289. Determine the number of arguments.
  290. */
  291. for (p=text; *p != '\0'; p++)
  292. if ((*p == ':') || (*p == ';'))
  293. (*number_tokens)++;
  294. tokens=(char **) AcquireMagickMemory((*number_tokens+2)*sizeof(*tokens));
  295. if (tokens == (char **) NULL)
  296. {
  297. (void) ThrowMagickException(svg_info->exception,GetMagickModule(),
  298. ResourceLimitError,"MemoryAllocationFailed","`%s'",text);
  299. return((char **) NULL);
  300. }
  301. /*
  302. Convert string to an ASCII list.
  303. */
  304. i=0;
  305. p=text;
  306. for (q=p; *q != '\0'; q++)
  307. {
  308. if ((*q != ':') && (*q != ';') && (*q != '\0'))
  309. continue;
  310. tokens[i]=AcquireString(p);
  311. (void) CopyMagickString(tokens[i],p,(size_t) (q-p+1));
  312. StripString(tokens[i++]);
  313. p=q+1;
  314. }
  315. tokens[i]=AcquireString(p);
  316. (void) CopyMagickString(tokens[i],p,(size_t) (q-p+1));
  317. StripString(tokens[i++]);
  318. tokens[i]=(char *) NULL;
  319. return(tokens);
  320. }
  321. static char **GetTransformTokens(void *context,const char *text,
  322. int *number_tokens)
  323. {
  324. char
  325. **tokens;
  326. register const char
  327. *p,
  328. *q;
  329. register long
  330. i;
  331. SVGInfo
  332. *svg_info;
  333. svg_info=(SVGInfo *) context;
  334. *number_tokens=0;
  335. if (text == (const char *) NULL)
  336. return((char **) NULL);
  337. /*
  338. Determine the number of arguments.
  339. */
  340. for (p=text; *p != '\0'; p++)
  341. {
  342. if (*p == '(')
  343. (*number_tokens)+=2;
  344. }
  345. tokens=(char **) AcquireMagickMemory((*number_tokens+2)*sizeof(*tokens));
  346. if (tokens == (char **) NULL)
  347. {
  348. (void) ThrowMagickException(svg_info->exception,GetMagickModule(),
  349. ResourceLimitError,"MemoryAllocationFailed","`%s'",text);
  350. return((char **) NULL);
  351. }
  352. /*
  353. Convert string to an ASCII list.
  354. */
  355. i=0;
  356. p=text;
  357. for (q=p; *q != '\0'; q++)
  358. {
  359. if ((*q != '(') && (*q != ')') && (*q != '\0'))
  360. continue;
  361. tokens[i]=AcquireString(p);
  362. (void) CopyMagickString(tokens[i],p,(size_t) (q-p+1));
  363. StripString(tokens[i++]);
  364. p=q+1;
  365. }
  366. tokens[i]=AcquireString(p);
  367. (void) CopyMagickString(tokens[i],p,(size_t) (q-p+1));
  368. StripString(tokens[i++]);
  369. tokens[i]=(char *) NULL;
  370. return(tokens);
  371. }
  372. #if defined(__cplusplus) || defined(c_plusplus)
  373. extern "C" {
  374. #endif
  375. static int SVGIsStandalone(void *context)
  376. {
  377. SVGInfo
  378. *svg_info;
  379. /*
  380. Is this document tagged standalone?
  381. */
  382. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.SVGIsStandalone()");
  383. svg_info=(SVGInfo *) context;
  384. return(svg_info->document->standalone == 1);
  385. }
  386. static int SVGHasInternalSubset(void *context)
  387. {
  388. SVGInfo
  389. *svg_info;
  390. /*
  391. Does this document has an internal subset?
  392. */
  393. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  394. " SAX.SVGHasInternalSubset()");
  395. svg_info=(SVGInfo *) context;
  396. return(svg_info->document->intSubset != NULL);
  397. }
  398. static int SVGHasExternalSubset(void *context)
  399. {
  400. SVGInfo
  401. *svg_info;
  402. /*
  403. Does this document has an external subset?
  404. */
  405. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  406. " SAX.SVGHasExternalSubset()");
  407. svg_info=(SVGInfo *) context;
  408. return(svg_info->document->extSubset != NULL);
  409. }
  410. static void SVGInternalSubset(void *context,const xmlChar *name,
  411. const xmlChar *external_id,const xmlChar *system_id)
  412. {
  413. SVGInfo
  414. *svg_info;
  415. /*
  416. Does this document has an internal subset?
  417. */
  418. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  419. " SAX.internalSubset(%s, %s, %s)",(char *) name,
  420. (external_id != (const xmlChar *) NULL ? (char *) external_id : "none"),
  421. (system_id != (const xmlChar *) NULL ? (char *) system_id : "none"));
  422. svg_info=(SVGInfo *) context;
  423. (void) xmlCreateIntSubset(svg_info->document,name,external_id,system_id);
  424. }
  425. static xmlParserInputPtr SVGResolveEntity(void *context,
  426. const xmlChar *public_id,const xmlChar *system_id)
  427. {
  428. SVGInfo
  429. *svg_info;
  430. xmlParserInputPtr
  431. stream;
  432. /*
  433. Special entity resolver, better left to the parser, it has more
  434. context than the application layer. The default behaviour is to
  435. not resolve the entities, in that case the ENTITY_REF nodes are
  436. built in the structure (and the parameter values).
  437. */
  438. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  439. " SAX.resolveEntity(%s, %s)",
  440. (public_id != (const xmlChar *) NULL ? (char *) public_id : "none"),
  441. (system_id != (const xmlChar *) NULL ? (char *) system_id : "none"));
  442. svg_info=(SVGInfo *) context;
  443. stream=xmlLoadExternalEntity((const char *) system_id,(const char *)
  444. public_id,svg_info->parser);
  445. return(stream);
  446. }
  447. static xmlEntityPtr SVGGetEntity(void *context,const xmlChar *name)
  448. {
  449. SVGInfo
  450. *svg_info;
  451. /*
  452. Get an entity by name.
  453. */
  454. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.SVGGetEntity(%s)",
  455. name);
  456. svg_info=(SVGInfo *) context;
  457. return(xmlGetDocEntity(svg_info->document,name));
  458. }
  459. static xmlEntityPtr SVGGetParameterEntity(void *context,const xmlChar *name)
  460. {
  461. SVGInfo
  462. *svg_info;
  463. /*
  464. Get a parameter entity by name.
  465. */
  466. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  467. " SAX.getParameterEntity(%s)",name);
  468. svg_info=(SVGInfo *) context;
  469. return(xmlGetParameterEntity(svg_info->document,name));
  470. }
  471. static void SVGEntityDeclaration(void *context,const xmlChar *name,int type,
  472. const xmlChar *public_id,const xmlChar *system_id,xmlChar *content)
  473. {
  474. SVGInfo
  475. *svg_info;
  476. /*
  477. An entity definition has been parsed.
  478. */
  479. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  480. " SAX.entityDecl(%s, %d, %s, %s, %s)",name,type,
  481. public_id != (xmlChar *) NULL ? (char *) public_id : "none",
  482. system_id != (xmlChar *) NULL ? (char *) system_id : "none",content);
  483. svg_info=(SVGInfo *) context;
  484. if (svg_info->parser->inSubset == 1)
  485. (void) xmlAddDocEntity(svg_info->document,name,type,public_id,system_id,
  486. content);
  487. else
  488. if (svg_info->parser->inSubset == 2)
  489. (void) xmlAddDtdEntity(svg_info->document,name,type,public_id,system_id,
  490. content);
  491. }
  492. static void SVGAttributeDeclaration(void *context,const xmlChar *element,
  493. const xmlChar *name,int type,int value,const xmlChar *default_value,
  494. xmlEnumerationPtr tree)
  495. {
  496. SVGInfo
  497. *svg_info;
  498. xmlChar
  499. *fullname,
  500. *prefix;
  501. xmlParserCtxtPtr
  502. parser;
  503. /*
  504. An attribute definition has been parsed.
  505. */
  506. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  507. " SAX.attributeDecl(%s, %s, %d, %d, %s, ...)",element,name,type,value,
  508. default_value);
  509. svg_info=(SVGInfo *) context;
  510. fullname=(xmlChar *) NULL;
  511. prefix=(xmlChar *) NULL;
  512. parser=svg_info->parser;
  513. fullname=(xmlChar *) xmlSplitQName(parser,name,&prefix);
  514. if (parser->inSubset == 1)
  515. (void) xmlAddAttributeDecl(&parser->vctxt,svg_info->document->intSubset,
  516. element,fullname,prefix,(xmlAttributeType) type,
  517. (xmlAttributeDefault) value,default_value,tree);
  518. else
  519. if (parser->inSubset == 2)
  520. (void) xmlAddAttributeDecl(&parser->vctxt,svg_info->document->extSubset,
  521. element,fullname,prefix,(xmlAttributeType) type,
  522. (xmlAttributeDefault) value,default_value,tree);
  523. if (prefix != (xmlChar *) NULL)
  524. xmlFree(prefix);
  525. if (fullname != (xmlChar *) NULL)
  526. xmlFree(fullname);
  527. }
  528. static void SVGElementDeclaration(void *context,const xmlChar *name,int type,
  529. xmlElementContentPtr content)
  530. {
  531. SVGInfo
  532. *svg_info;
  533. xmlParserCtxtPtr
  534. parser;
  535. /*
  536. An element definition has been parsed.
  537. */
  538. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  539. " SAX.elementDecl(%s, %d, ...)",name,type);
  540. svg_info=(SVGInfo *) context;
  541. parser=svg_info->parser;
  542. if (parser->inSubset == 1)
  543. (void) xmlAddElementDecl(&parser->vctxt,svg_info->document->intSubset,
  544. name,(xmlElementTypeVal) type,content);
  545. else
  546. if (parser->inSubset == 2)
  547. (void) xmlAddElementDecl(&parser->vctxt,svg_info->document->extSubset,
  548. name,(xmlElementTypeVal) type,content);
  549. }
  550. static void SVGNotationDeclaration(void *context,const xmlChar *name,
  551. const xmlChar *public_id,const xmlChar *system_id)
  552. {
  553. SVGInfo
  554. *svg_info;
  555. xmlParserCtxtPtr
  556. parser;
  557. /*
  558. What to do when a notation declaration has been parsed.
  559. */
  560. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  561. " SAX.notationDecl(%s, %s, %s)",name,
  562. public_id != (const xmlChar *) NULL ? (char *) public_id : "none",
  563. system_id != (const xmlChar *) NULL ? (char *) system_id : "none");
  564. svg_info=(SVGInfo *) context;
  565. parser=svg_info->parser;
  566. if (parser->inSubset == 1)
  567. (void) xmlAddNotationDecl(&parser->vctxt,svg_info->document->intSubset,
  568. name,public_id,system_id);
  569. else
  570. if (parser->inSubset == 2)
  571. (void) xmlAddNotationDecl(&parser->vctxt,svg_info->document->intSubset,
  572. name,public_id,system_id);
  573. }
  574. static void SVGUnparsedEntityDeclaration(void *context,const xmlChar *name,
  575. const xmlChar *public_id,const xmlChar *system_id,const xmlChar *notation)
  576. {
  577. SVGInfo
  578. *svg_info;
  579. /*
  580. What to do when an unparsed entity declaration is parsed.
  581. */
  582. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  583. " SAX.unparsedEntityDecl(%s, %s, %s, %s)",name,
  584. public_id != (xmlChar *) NULL ? (char *) public_id : "none",
  585. system_id != (xmlChar *) NULL ? (char *) system_id : "none",notation);
  586. svg_info=(SVGInfo *) context;
  587. (void) xmlAddDocEntity(svg_info->document,name,
  588. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,public_id,system_id,notation);
  589. }
  590. static void SVGSetDocumentLocator(void *context,xmlSAXLocatorPtr location)
  591. {
  592. SVGInfo
  593. *svg_info;
  594. /*
  595. Receive the document locator at startup, actually xmlDefaultSAXLocator.
  596. */
  597. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  598. " SAX.setDocumentLocator()");
  599. svg_info=(SVGInfo *) context;
  600. }
  601. static void SVGStartDocument(void *context)
  602. {
  603. SVGInfo
  604. *svg_info;
  605. xmlParserCtxtPtr
  606. parser;
  607. /*
  608. Called when the document start being processed.
  609. */
  610. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.startDocument()");
  611. svg_info=(SVGInfo *) context;
  612. GetExceptionInfo(svg_info->exception);
  613. parser=svg_info->parser;
  614. svg_info->document=xmlNewDoc(parser->version);
  615. if (svg_info->document == (xmlDocPtr) NULL)
  616. return;
  617. if (parser->encoding == NULL)
  618. svg_info->document->encoding=(const xmlChar *) NULL;
  619. else
  620. svg_info->document->encoding=xmlStrdup(parser->encoding);
  621. svg_info->document->standalone=parser->standalone;
  622. }
  623. static void SVGEndDocument(void *context)
  624. {
  625. SVGInfo
  626. *svg_info;
  627. /*
  628. Called when the document end has been detected.
  629. */
  630. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.endDocument()");
  631. svg_info=(SVGInfo *) context;
  632. if (svg_info->offset != (char *) NULL)
  633. svg_info->offset=(char *) RelinquishMagickMemory(svg_info->offset);
  634. if (svg_info->stop_color != (char *) NULL)
  635. svg_info->stop_color=(char *) RelinquishMagickMemory(svg_info->stop_color);
  636. if (svg_info->scale != (double *) NULL)
  637. svg_info->scale=(double *) RelinquishMagickMemory(svg_info->scale);
  638. if (svg_info->text != (char *) NULL)
  639. svg_info->text=(char *) RelinquishMagickMemory(svg_info->text);
  640. if (svg_info->vertices != (char *) NULL)
  641. svg_info->vertices=(char *) RelinquishMagickMemory(svg_info->vertices);
  642. if (svg_info->url != (char *) NULL)
  643. svg_info->url=(char *) RelinquishMagickMemory(svg_info->url);
  644. #if defined(HasXML)
  645. if (svg_info->document != (xmlDocPtr) NULL)
  646. {
  647. xmlFreeDoc(svg_info->document);
  648. svg_info->document=(xmlDocPtr) NULL;
  649. }
  650. #endif
  651. }
  652. static void SVGStartElement(void *context,const xmlChar *name,
  653. const xmlChar **attributes)
  654. {
  655. char
  656. *color,
  657. id[MaxTextExtent],
  658. *p,
  659. token[MaxTextExtent],
  660. **tokens,
  661. *units;
  662. const char
  663. *keyword,
  664. *value;
  665. int
  666. number_tokens;
  667. SVGInfo
  668. *svg_info;
  669. register long
  670. i,
  671. j;
  672. /*
  673. Called when an opening tag has been processed.
  674. */
  675. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.startElement(%s",
  676. name);
  677. svg_info=(SVGInfo *) context;
  678. svg_info->n++;
  679. svg_info->scale=(double *) ResizeMagickMemory(svg_info->scale,
  680. (svg_info->n+1)*sizeof(*svg_info->scale));
  681. if (svg_info->scale == (double *) NULL)
  682. {
  683. (void) ThrowMagickException(svg_info->exception,GetMagickModule(),
  684. ResourceLimitError,"MemoryAllocationFailed","`%s'",name);
  685. return;
  686. }
  687. svg_info->scale[svg_info->n]=svg_info->scale[svg_info->n-1];
  688. color=AcquireString("none");
  689. units=AcquireString("userSpaceOnUse");
  690. value=(const char *) NULL;
  691. if (attributes != (const xmlChar **) NULL)
  692. for (i=0; (attributes[i] != (const xmlChar *) NULL); i+=2)
  693. {
  694. keyword=(const char *) attributes[i];
  695. value=(const char *) attributes[i+1];
  696. switch (*keyword)
  697. {
  698. case 'C':
  699. case 'c':
  700. {
  701. if (LocaleCompare(keyword,"cx") == 0)
  702. {
  703. svg_info->element.cx=
  704. GetUserSpaceCoordinateValue(svg_info,1,value);
  705. break;
  706. }
  707. if (LocaleCompare(keyword,"cy") == 0)
  708. {
  709. svg_info->element.cy=
  710. GetUserSpaceCoordinateValue(svg_info,-1,value);
  711. break;
  712. }
  713. break;
  714. }
  715. case 'F':
  716. case 'f':
  717. {
  718. if (LocaleCompare(keyword,"fx") == 0)
  719. {
  720. svg_info->element.major=
  721. GetUserSpaceCoordinateValue(svg_info,1,value);
  722. break;
  723. }
  724. if (LocaleCompare(keyword,"fy") == 0)
  725. {
  726. svg_info->element.minor=
  727. GetUserSpaceCoordinateValue(svg_info,-1,value);
  728. break;
  729. }
  730. break;
  731. }
  732. case 'H':
  733. case 'h':
  734. {
  735. if (LocaleCompare(keyword,"height") == 0)
  736. {
  737. svg_info->bounds.height=
  738. GetUserSpaceCoordinateValue(svg_info,-1,value);
  739. break;
  740. }
  741. break;
  742. }
  743. case 'I':
  744. case 'i':
  745. {
  746. if (LocaleCompare(keyword,"id") == 0)
  747. {
  748. (void) CopyMagickString(id,value,MaxTextExtent);
  749. break;
  750. }
  751. break;
  752. }
  753. case 'R':
  754. case 'r':
  755. {
  756. if (LocaleCompare(keyword,"r") == 0)
  757. {
  758. svg_info->element.angle=
  759. GetUserSpaceCoordinateValue(svg_info,0,value);
  760. break;
  761. }
  762. break;
  763. }
  764. case 'W':
  765. case 'w':
  766. {
  767. if (LocaleCompare(keyword,"width") == 0)
  768. {
  769. svg_info->bounds.width=
  770. GetUserSpaceCoordinateValue(svg_info,1,value);
  771. break;
  772. }
  773. break;
  774. }
  775. case 'X':
  776. case 'x':
  777. {
  778. if (LocaleCompare(keyword,"x") == 0)
  779. {
  780. svg_info->bounds.x=GetUserSpaceCoordinateValue(svg_info,1,value);
  781. break;
  782. }
  783. if (LocaleCompare(keyword,"x1") == 0)
  784. {
  785. svg_info->segment.x1=
  786. GetUserSpaceCoordinateValue(svg_info,1,value);
  787. break;
  788. }
  789. if (LocaleCompare(keyword,"x2") == 0)
  790. {
  791. svg_info->segment.x2=
  792. GetUserSpaceCoordinateValue(svg_info,1,value);
  793. break;
  794. }
  795. break;
  796. }
  797. case 'Y':
  798. case 'y':
  799. {
  800. if (LocaleCompare(keyword,"y") == 0)
  801. {
  802. svg_info->bounds.y=GetUserSpaceCoordinateValue(svg_info,-1,value);
  803. break;
  804. }
  805. if (LocaleCompare(keyword,"y1") == 0)
  806. {
  807. svg_info->segment.y1=
  808. GetUserSpaceCoordinateValue(svg_info,-1,value);
  809. break;
  810. }
  811. if (LocaleCompare(keyword,"y2") == 0)
  812. {
  813. svg_info->segment.y2=
  814. GetUserSpaceCoordinateValue(svg_info,-1,value);
  815. break;
  816. }
  817. break;
  818. }
  819. default:
  820. break;
  821. }
  822. }
  823. switch (*name)
  824. {
  825. case 'C':
  826. case 'c':
  827. {
  828. if (LocaleCompare((char *) name,"circle") == 0)
  829. {
  830. MVGPrintf(svg_info->file,"push graphic-context\n");
  831. break;
  832. }
  833. if (LocaleCompare((char *) name,"clipPath") == 0)
  834. {
  835. MVGPrintf(svg_info->file,"push clip-path '%s'\n",id);
  836. break;
  837. }
  838. break;
  839. }
  840. case 'D':
  841. case 'd':
  842. {
  843. if (LocaleCompare((char *) name,"defs") == 0)
  844. {
  845. MVGPrintf(svg_info->file,"push defs\n");
  846. break;
  847. }
  848. break;
  849. }
  850. case 'E':
  851. case 'e':
  852. {
  853. if (LocaleCompare((char *) name,"ellipse") == 0)
  854. {
  855. MVGPrintf(svg_info->file,"push graphic-context\n");
  856. break;
  857. }
  858. break;
  859. }
  860. case 'G':
  861. case 'g':
  862. {
  863. if (LocaleCompare((char *) name,"g") == 0)
  864. {
  865. MVGPrintf(svg_info->file,"push graphic-context\n");
  866. break;
  867. }
  868. break;
  869. }
  870. case 'I':
  871. case 'i':
  872. {
  873. if (LocaleCompare((char *) name,"image") == 0)
  874. {
  875. MVGPrintf(svg_info->file,"push graphic-context\n");
  876. break;
  877. }
  878. break;
  879. }
  880. case 'L':
  881. case 'l':
  882. {
  883. if (LocaleCompare((char *) name,"line") == 0)
  884. {
  885. MVGPrintf(svg_info->file,"push graphic-context\n");
  886. break;
  887. }
  888. if (LocaleCompare((char *) name,"linearGradient") == 0)
  889. {
  890. MVGPrintf(svg_info->file,"push gradient '%s' linear %g,%g %g,%g\n",id,
  891. svg_info->segment.x1,svg_info->segment.y1,svg_info->segment.x2,
  892. svg_info->segment.y2);
  893. break;
  894. }
  895. break;
  896. }
  897. case 'P':
  898. case 'p':
  899. {
  900. if (LocaleCompare((char *) name,"path") == 0)
  901. {
  902. MVGPrintf(svg_info->file,"push graphic-context\n");
  903. break;
  904. }
  905. if (LocaleCompare((char *) name,"pattern") == 0)
  906. {
  907. MVGPrintf(svg_info->file,"push pattern '%s' %g,%g %g,%g\n",id,
  908. svg_info->bounds.x,svg_info->bounds.y,svg_info->bounds.width,
  909. svg_info->bounds.height);
  910. break;
  911. }
  912. if (LocaleCompare((char *) name,"polygon") == 0)
  913. {
  914. MVGPrintf(svg_info->file,"push graphic-context\n");
  915. break;
  916. }
  917. if (LocaleCompare((char *) name,"polyline") == 0)
  918. {
  919. MVGPrintf(svg_info->file,"push graphic-context\n");
  920. break;
  921. }
  922. break;
  923. }
  924. case 'R':
  925. case 'r':
  926. {
  927. if (LocaleCompare((char *) name,"radialGradient") == 0)
  928. {
  929. MVGPrintf(svg_info->file,"push gradient '%s' radial %g,%g %g,%g %g\n",
  930. id,svg_info->element.cx,svg_info->element.cy,
  931. svg_info->element.major,svg_info->element.minor,
  932. svg_info->element.angle);
  933. break;
  934. }
  935. if (LocaleCompare((char *) name,"rect") == 0)
  936. {
  937. MVGPrintf(svg_info->file,"push graphic-context\n");
  938. break;
  939. }
  940. break;
  941. }
  942. case 'S':
  943. case 's':
  944. {
  945. if (LocaleCompare((char *) name,"svg") == 0)
  946. {
  947. MVGPrintf(svg_info->file,"push graphic-context\n");
  948. break;
  949. }
  950. break;
  951. }
  952. case 'T':
  953. case 't':
  954. {
  955. if (LocaleCompare((char *) name,"text") == 0)
  956. {
  957. MVGPrintf(svg_info->file,"push graphic-context\n");
  958. break;
  959. }
  960. if (LocaleCompare((char *) name,"tspan") == 0)
  961. {
  962. if (*svg_info->text != '\0')
  963. {
  964. DrawInfo
  965. *draw_info;
  966. TypeMetric
  967. metrics;
  968. char
  969. *text;
  970. text=EscapeString(svg_info->text,'\'');
  971. MVGPrintf(svg_info->file,"text %g,%g '%s'\n",svg_info->bounds.x,
  972. svg_info->bounds.y,text);
  973. text=(char *) RelinquishMagickMemory(text);
  974. draw_info=CloneDrawInfo(svg_info->image_info,(DrawInfo *) NULL);
  975. draw_info->pointsize=svg_info->pointsize;
  976. draw_info->text=AcquireString(svg_info->text);
  977. (void) ConcatenateString(&draw_info->text," ");
  978. GetTypeMetrics(svg_info->image,draw_info,&metrics);
  979. svg_info->bounds.x+=metrics.width;
  980. draw_info=DestroyDrawInfo(draw_info);
  981. *svg_info->text='\0';
  982. }
  983. MVGPrintf(svg_info->file,"push graphic-context\n");
  984. break;
  985. }
  986. break;
  987. }
  988. default:
  989. break;
  990. }
  991. if (attributes != (const xmlChar **) NULL)
  992. for (i=0; (attributes[i] != (const xmlChar *) NULL); i+=2)
  993. {
  994. keyword=(const char *) attributes[i];
  995. value=(const char *) attributes[i+1];
  996. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  997. " %s = %s",keyword,value);
  998. switch (*keyword)
  999. {
  1000. case 'A':
  1001. case 'a':
  1002. {
  1003. if (LocaleCompare(keyword,"angle") == 0)
  1004. {
  1005. MVGPrintf(svg_info->file,"angle %g\n",
  1006. GetUserSpaceCoordinateValue(svg_info,0,value));
  1007. break;
  1008. }
  1009. break;
  1010. }
  1011. case 'C':
  1012. case 'c':
  1013. {
  1014. if (LocaleCompare(keyword,"clip-path") == 0)
  1015. {
  1016. MVGPrintf(svg_info->file,"clip-path '%s'\n",value);
  1017. break;
  1018. }
  1019. if (LocaleCompare(keyword,"clip-rule") == 0)
  1020. {
  1021. MVGPrintf(svg_info->file,"clip-rule '%s'\n",value);
  1022. break;
  1023. }
  1024. if (LocaleCompare(keyword,"clipPathUnits") == 0)
  1025. {
  1026. (void) CloneString(&units,value);
  1027. MVGPrintf(svg_info->file,"clip-units '%s'\n",value);
  1028. break;
  1029. }
  1030. if (LocaleCompare(keyword,"color") == 0)
  1031. {
  1032. (void) CloneString(&color,value);
  1033. break;
  1034. }
  1035. if (LocaleCompare(keyword,"cx") == 0)
  1036. {
  1037. svg_info->element.cx=
  1038. GetUserSpaceCoordinateValue(svg_info,1,value);
  1039. break;
  1040. }
  1041. if (LocaleCompare(keyword,"cy") == 0)
  1042. {
  1043. svg_info->element.cy=
  1044. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1045. break;
  1046. }
  1047. break;
  1048. }
  1049. case 'D':
  1050. case 'd':
  1051. {
  1052. if (LocaleCompare(keyword,"d") == 0)
  1053. {
  1054. (void) CloneString(&svg_info->vertices,value);
  1055. break;
  1056. }
  1057. if (LocaleCompare(keyword,"dx") == 0)
  1058. {
  1059. svg_info->bounds.x+=
  1060. GetUserSpaceCoordinateValue(svg_info,1,value);
  1061. break;
  1062. }
  1063. if (LocaleCompare(keyword,"dy") == 0)
  1064. {
  1065. svg_info->bounds.y+=
  1066. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1067. break;
  1068. }
  1069. break;
  1070. }
  1071. case 'F':
  1072. case 'f':
  1073. {
  1074. if (LocaleCompare(keyword,"fill") == 0)
  1075. {
  1076. if (LocaleCompare(value,"currentColor") == 0)
  1077. {
  1078. MVGPrintf(svg_info->file,"fill '%s'\n",color);
  1079. break;
  1080. }
  1081. MVGPrintf(svg_info->file,"fill '%s'\n",value);
  1082. break;
  1083. }
  1084. if (LocaleCompare(keyword,"fillcolor") == 0)
  1085. {
  1086. MVGPrintf(svg_info->file,"fill '%s'\n",value);
  1087. break;
  1088. }
  1089. if (LocaleCompare(keyword,"fill-rule") == 0)
  1090. {
  1091. MVGPrintf(svg_info->file,"fill-rule '%s'\n",value);
  1092. break;
  1093. }
  1094. if (LocaleCompare(keyword,"fill-opacity") == 0)
  1095. {
  1096. MVGPrintf(svg_info->file,"fill-opacity '%s'\n",value);
  1097. break;
  1098. }
  1099. if (LocaleCompare(keyword,"font-family") == 0)
  1100. {
  1101. MVGPrintf(svg_info->file,"font-family '%s'\n",value);
  1102. break;
  1103. }
  1104. if (LocaleCompare(keyword,"font-stretch") == 0)
  1105. {
  1106. MVGPrintf(svg_info->file,"font-stretch '%s'\n",value);
  1107. break;
  1108. }
  1109. if (LocaleCompare(keyword,"font-style") == 0)
  1110. {
  1111. MVGPrintf(svg_info->file,"font-style '%s'\n",value);
  1112. break;
  1113. }
  1114. if (LocaleCompare(keyword,"font-size") == 0)
  1115. {
  1116. svg_info->pointsize=GetUserSpaceCoordinateValue(svg_info,0,value);
  1117. MVGPrintf(svg_info->file,"font-size %g\n",svg_info->pointsize);
  1118. break;
  1119. }
  1120. if (LocaleCompare(keyword,"font-weight") == 0)
  1121. {
  1122. MVGPrintf(svg_info->file,"font-weight '%s'\n",value);
  1123. break;
  1124. }
  1125. break;
  1126. }
  1127. case 'G':
  1128. case 'g':
  1129. {
  1130. if (LocaleCompare(keyword,"gradientTransform") == 0)
  1131. {
  1132. AffineMatrix
  1133. affine,
  1134. current,
  1135. transform;
  1136. GetAffineMatrix(&transform);
  1137. (void) LogMagickEvent(CoderEvent,GetMagickModule()," ");
  1138. tokens=GetTransformTokens(context,value,&number_tokens);
  1139. for (j=0; j < (number_tokens-1); j+=2)
  1140. {
  1141. keyword=(char *) tokens[j];
  1142. if (keyword == (char *) NULL)
  1143. continue;
  1144. value=(char *) tokens[j+1];
  1145. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  1146. " %s: %s",keyword,value);
  1147. current=transform;
  1148. GetAffineMatrix(&affine);
  1149. switch (*keyword)
  1150. {
  1151. case 'M':
  1152. case 'm':
  1153. {
  1154. if (LocaleCompare(keyword,"matrix") == 0)
  1155. {
  1156. p=(char *) value;
  1157. GetMagickToken(p,&p,token);
  1158. affine.sx=atof(value);
  1159. GetMagickToken(p,&p,token);
  1160. if (*token == ',')
  1161. GetMagickToken(p,&p,token);
  1162. affine.rx=atof(token);
  1163. GetMagickToken(p,&p,token);
  1164. if (*token == ',')
  1165. GetMagickToken(p,&p,token);
  1166. affine.ry=atof(token);
  1167. GetMagickToken(p,&p,token);
  1168. if (*token == ',')
  1169. GetMagickToken(p,&p,token);
  1170. affine.sy=atof(token);
  1171. GetMagickToken(p,&p,token);
  1172. if (*token == ',')
  1173. GetMagickToken(p,&p,token);
  1174. affine.tx=atof(token);
  1175. GetMagickToken(p,&p,token);
  1176. if (*token == ',')
  1177. GetMagickToken(p,&p,token);
  1178. affine.ty=atof(token);
  1179. break;
  1180. }
  1181. break;
  1182. }
  1183. case 'R':
  1184. case 'r':
  1185. {
  1186. if (LocaleCompare(keyword,"rotate") == 0)
  1187. {
  1188. double
  1189. angle;
  1190. angle=GetUserSpaceCoordinateValue(svg_info,0,value);
  1191. affine.sx=cos(DegreesToRadians(fmod(angle,360.0)));
  1192. affine.rx=sin(DegreesToRadians(fmod(angle,360.0)));
  1193. affine.ry=(-sin(DegreesToRadians(fmod(angle,360.0))));
  1194. affine.sy=cos(DegreesToRadians(fmod(angle,360.0)));
  1195. break;
  1196. }
  1197. break;
  1198. }
  1199. case 'S':
  1200. case 's':
  1201. {
  1202. if (LocaleCompare(keyword,"scale") == 0)
  1203. {
  1204. for (p=(char *) value; *p != '\0'; p++)
  1205. if ((isspace((int) ((unsigned char) *p)) != 0) ||
  1206. (*p == ','))
  1207. break;
  1208. affine.sx=GetUserSpaceCoordinateValue(svg_info,1,value);
  1209. affine.sy=affine.sx;
  1210. if (*p != '\0')
  1211. affine.sy=
  1212. GetUserSpaceCoordinateValue(svg_info,-1,p+1);
  1213. svg_info->scale[svg_info->n]=ExpandAffine(&affine);
  1214. break;
  1215. }
  1216. if (LocaleCompare(keyword,"skewX") == 0)
  1217. {
  1218. affine.sx=svg_info->affine.sx;
  1219. affine.ry=tan(DegreesToRadians(fmod(
  1220. GetUserSpaceCoordinateValue(svg_info,1,value),
  1221. 360.0)));
  1222. affine.sy=svg_info->affine.sy;
  1223. break;
  1224. }
  1225. if (LocaleCompare(keyword,"skewY") == 0)
  1226. {
  1227. affine.sx=svg_info->affine.sx;
  1228. affine.rx=tan(DegreesToRadians(fmod(
  1229. GetUserSpaceCoordinateValue(svg_info,-1,value),
  1230. 360.0)));
  1231. affine.sy=svg_info->affine.sy;
  1232. break;
  1233. }
  1234. break;
  1235. }
  1236. case 'T':
  1237. case 't':
  1238. {
  1239. if (LocaleCompare(keyword,"translate") == 0)
  1240. {
  1241. for (p=(char *) value; *p != '\0'; p++)
  1242. if ((isspace((int) ((unsigned char) *p)) != 0) ||
  1243. (*p == ','))
  1244. break;
  1245. affine.tx=GetUserSpaceCoordinateValue(svg_info,1,value);
  1246. affine.ty=affine.tx;
  1247. if (*p != '\0')
  1248. affine.ty=
  1249. GetUserSpaceCoordinateValue(svg_info,-1,p+1);
  1250. break;
  1251. }
  1252. break;
  1253. }
  1254. default:
  1255. break;
  1256. }
  1257. transform.sx=current.sx*affine.sx+current.ry*affine.rx;
  1258. transform.rx=current.rx*affine.sx+current.sy*affine.rx;
  1259. transform.ry=current.sx*affine.ry+current.ry*affine.sy;
  1260. transform.sy=current.rx*affine.ry+current.sy*affine.sy;
  1261. transform.tx=current.sx*affine.tx+current.ry*affine.ty+
  1262. current.tx;
  1263. transform.ty=current.rx*affine.tx+current.sy*affine.ty+
  1264. current.ty;
  1265. }
  1266. MVGPrintf(svg_info->file,"affine %g %g %g %g %g %g\n",
  1267. transform.sx,transform.rx,transform.ry,transform.sy,
  1268. transform.tx,transform.ty);
  1269. for (j=0; tokens[j] != (char *) NULL; j++)
  1270. tokens[j]=(char *) RelinquishMagickMemory(tokens[j]);
  1271. tokens=(char **) RelinquishMagickMemory(tokens);
  1272. break;
  1273. }
  1274. if (LocaleCompare(keyword,"gradientUnits") == 0)
  1275. {
  1276. (void) CloneString(&units,value);
  1277. MVGPrintf(svg_info->file,"gradient-units '%s'\n",value);
  1278. break;
  1279. }
  1280. break;
  1281. }
  1282. case 'H':
  1283. case 'h':
  1284. {
  1285. if (LocaleCompare(keyword,"height") == 0)
  1286. {
  1287. svg_info->bounds.height=
  1288. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1289. break;
  1290. }
  1291. if (LocaleCompare(keyword,"href") == 0)
  1292. {
  1293. (void) CloneString(&svg_info->url,value);
  1294. break;
  1295. }
  1296. break;
  1297. }
  1298. case 'M':
  1299. case 'm':
  1300. {
  1301. if (LocaleCompare(keyword,"major") == 0)
  1302. {
  1303. svg_info->element.major=
  1304. GetUserSpaceCoordinateValue(svg_info,1,value);
  1305. break;
  1306. }
  1307. if (LocaleCompare(keyword,"minor") == 0)
  1308. {
  1309. svg_info->element.minor=
  1310. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1311. break;
  1312. }
  1313. break;
  1314. }
  1315. case 'O':
  1316. case 'o':
  1317. {
  1318. if (LocaleCompare(keyword,"offset") == 0)
  1319. {
  1320. (void) CloneString(&svg_info->offset,value);
  1321. break;
  1322. }
  1323. if (LocaleCompare(keyword,"opacity") == 0)
  1324. {
  1325. MVGPrintf(svg_info->file,"opacity '%s'\n",value);
  1326. break;
  1327. }
  1328. break;
  1329. }
  1330. case 'P':
  1331. case 'p':
  1332. {
  1333. if (LocaleCompare(keyword,"path") == 0)
  1334. {
  1335. (void) CloneString(&svg_info->url,value);
  1336. break;
  1337. }
  1338. if (LocaleCompare(keyword,"points") == 0)
  1339. {
  1340. (void) CloneString(&svg_info->vertices,value);
  1341. break;
  1342. }
  1343. break;
  1344. }
  1345. case 'R':
  1346. case 'r':
  1347. {
  1348. if (LocaleCompare(keyword,"r") == 0)
  1349. {
  1350. svg_info->element.major=
  1351. GetUserSpaceCoordinateValue(svg_info,1,value);
  1352. svg_info->element.minor=
  1353. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1354. break;
  1355. }
  1356. if (LocaleCompare(keyword,"rotate") == 0)
  1357. {
  1358. double
  1359. angle;
  1360. angle=GetUserSpaceCoordinateValue(svg_info,0,value);
  1361. MVGPrintf(svg_info->file,"translate %g,%g\n",svg_info->bounds.x,
  1362. svg_info->bounds.y);
  1363. svg_info->bounds.x=0;
  1364. svg_info->bounds.y=0;
  1365. MVGPrintf(svg_info->file,"rotate %g\n",angle);
  1366. break;
  1367. }
  1368. if (LocaleCompare(keyword,"rx") == 0)
  1369. {
  1370. if (LocaleCompare((char *) name,"ellipse") == 0)
  1371. svg_info->element.major=
  1372. GetUserSpaceCoordinateValue(svg_info,1,value);
  1373. else
  1374. svg_info->radius.x=
  1375. GetUserSpaceCoordinateValue(svg_info,1,value);
  1376. break;
  1377. }
  1378. if (LocaleCompare(keyword,"ry") == 0)
  1379. {
  1380. if (LocaleCompare((char *) name,"ellipse") == 0)
  1381. svg_info->element.minor=
  1382. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1383. else
  1384. svg_info->radius.y=
  1385. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1386. break;
  1387. }
  1388. break;
  1389. }
  1390. case 'S':
  1391. case 's':
  1392. {
  1393. if (LocaleCompare(keyword,"stop-color") == 0)
  1394. {
  1395. (void) CloneString(&svg_info->stop_color,value);
  1396. break;
  1397. }
  1398. if (LocaleCompare(keyword,"stroke") == 0)
  1399. {
  1400. if (LocaleCompare(value,"currentColor") == 0)
  1401. {
  1402. MVGPrintf(svg_info->file,"stroke '%s'\n",color);
  1403. break;
  1404. }
  1405. MVGPrintf(svg_info->file,"stroke '%s'\n",value);
  1406. break;
  1407. }
  1408. if (LocaleCompare(keyword,"stroke-antialiasing") == 0)
  1409. {
  1410. MVGPrintf(svg_info->file,"stroke-antialias %d\n",
  1411. LocaleCompare(value,"true") == 0);
  1412. break;
  1413. }
  1414. if (LocaleCompare(keyword,"stroke-dasharray") == 0)
  1415. {
  1416. MVGPrintf(svg_info->file,"stroke-dasharray %s\n",value);
  1417. break;
  1418. }
  1419. if (LocaleCompare(keyword,"stroke-dashoffset") == 0)
  1420. {
  1421. MVGPrintf(svg_info->file,"stroke-dashoffset %s\n",value);
  1422. break;
  1423. }
  1424. if (LocaleCompare(keyword,"stroke-linecap") == 0)
  1425. {
  1426. MVGPrintf(svg_info->file,"stroke-linecap '%s'\n",value);
  1427. break;
  1428. }
  1429. if (LocaleCompare(keyword,"stroke-linejoin") == 0)
  1430. {
  1431. MVGPrintf(svg_info->file,"stroke-linejoin '%s'\n",value);
  1432. break;
  1433. }
  1434. if (LocaleCompare(keyword,"stroke-miterlimit") == 0)
  1435. {
  1436. MVGPrintf(svg_info->file,"stroke-miterlimit '%s'\n",value);
  1437. break;
  1438. }
  1439. if (LocaleCompare(keyword,"stroke-opacity") == 0)
  1440. {
  1441. MVGPrintf(svg_info->file,"stroke-opacity '%s'\n",value);
  1442. break;
  1443. }
  1444. if (LocaleCompare(keyword,"stroke-width") == 0)
  1445. {
  1446. MVGPrintf(svg_info->file,"stroke-width %g\n",
  1447. GetUserSpaceCoordinateValue(svg_info,1,value));
  1448. break;
  1449. }
  1450. if (LocaleCompare(keyword,"style") == 0)
  1451. {
  1452. (void) LogMagickEvent(CoderEvent,GetMagickModule()," ");
  1453. tokens=GetStyleTokens(context,value,&number_tokens);
  1454. for (j=0; j < (number_tokens-1); j+=2)
  1455. {
  1456. keyword=(char *) tokens[j];
  1457. value=(char *) tokens[j+1];
  1458. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  1459. " %s: %s",keyword,value);
  1460. switch (*keyword)
  1461. {
  1462. case 'C':
  1463. case 'c':
  1464. {
  1465. if (LocaleCompare(keyword,"clip-path") == 0)
  1466. {
  1467. MVGPrintf(svg_info->file,"clip-path '%s'\n",value);
  1468. break;
  1469. }
  1470. if (LocaleCompare(keyword,"clip-rule") == 0)
  1471. {
  1472. MVGPrintf(svg_info->file,"clip-rule '%s'\n",value);
  1473. break;
  1474. }
  1475. if (LocaleCompare(keyword,"clipPathUnits") == 0)
  1476. {
  1477. (void) CloneString(&units,value);
  1478. MVGPrintf(svg_info->file,"clip-units '%s'\n",value);
  1479. break;
  1480. }
  1481. if (LocaleCompare(keyword,"color") == 0)
  1482. {
  1483. (void) CloneString(&color,value);
  1484. break;
  1485. }
  1486. break;
  1487. }
  1488. case 'F':
  1489. case 'f':
  1490. {
  1491. if (LocaleCompare(keyword,"fill") == 0)
  1492. {
  1493. if (LocaleCompare(value,"currentColor") == 0)
  1494. {
  1495. MVGPrintf(svg_info->file,"fill '%s'\n",color);
  1496. break;
  1497. }
  1498. MVGPrintf(svg_info->file,"fill '%s'\n",value);
  1499. break;
  1500. }
  1501. if (LocaleCompare(keyword,"fillcolor") == 0)
  1502. {
  1503. MVGPrintf(svg_info->file,"fill '%s'\n",value);
  1504. break;
  1505. }
  1506. if (LocaleCompare(keyword,"fill-rule") == 0)
  1507. {
  1508. MVGPrintf(svg_info->file,"fill-rule '%s'\n",value);
  1509. break;
  1510. }
  1511. if (LocaleCompare(keyword,"fill-opacity") == 0)
  1512. {
  1513. MVGPrintf(svg_info->file,"fill-opacity '%s'\n",value);
  1514. break;
  1515. }
  1516. if (LocaleCompare(keyword,"font-family") == 0)
  1517. {
  1518. MVGPrintf(svg_info->file,"font-family '%s'\n",value);
  1519. break;
  1520. }
  1521. if (LocaleCompare(keyword,"font-stretch") == 0)
  1522. {
  1523. MVGPrintf(svg_info->file,"font-stretch '%s'\n",value);
  1524. break;
  1525. }
  1526. if (LocaleCompare(keyword,"font-style") == 0)
  1527. {
  1528. MVGPrintf(svg_info->file,"font-style '%s'\n",value);
  1529. break;
  1530. }
  1531. if (LocaleCompare(keyword,"font-size") == 0)
  1532. {
  1533. svg_info->pointsize=GetUserSpaceCoordinateValue(
  1534. svg_info,0,value);
  1535. MVGPrintf(svg_info->file,"font-size %g\n",
  1536. svg_info->pointsize);
  1537. break;
  1538. }
  1539. if (LocaleCompare(keyword,"font-weight") == 0)
  1540. {
  1541. MVGPrintf(svg_info->file,"font-weight '%s'\n",value);
  1542. break;
  1543. }
  1544. break;
  1545. }
  1546. case 'O':
  1547. case 'o':
  1548. {
  1549. if (LocaleCompare(keyword,"offset") == 0)
  1550. {
  1551. MVGPrintf(svg_info->file,"offset %g\n",
  1552. GetUserSpaceCoordinateValue(svg_info,1,value));
  1553. break;
  1554. }
  1555. if (LocaleCompare(keyword,"opacity") == 0)
  1556. {
  1557. MVGPrintf(svg_info->file,"opacity '%s'\n",value);
  1558. break;
  1559. }
  1560. break;
  1561. }
  1562. case 'S':
  1563. case 's':
  1564. {
  1565. if (LocaleCompare(keyword,"stop-color") == 0)
  1566. {
  1567. (void) CloneString(&svg_info->stop_color,value);
  1568. break;
  1569. }
  1570. if (LocaleCompare(keyword,"stroke") == 0)
  1571. {
  1572. if (LocaleCompare(value,"currentColor") == 0)
  1573. {
  1574. MVGPrintf(svg_info->file,"stroke '%s'\n",color);
  1575. break;
  1576. }
  1577. MVGPrintf(svg_info->file,"stroke '%s'\n",value);
  1578. break;
  1579. }
  1580. if (LocaleCompare(keyword,"stroke-antialiasing") == 0)
  1581. {
  1582. MVGPrintf(svg_info->file,"stroke-antialias %d\n",
  1583. LocaleCompare(value,"true") == 0);
  1584. break;
  1585. }
  1586. if (LocaleCompare(keyword,"stroke-dasharray") == 0)
  1587. {
  1588. MVGPrintf(svg_info->file,"stroke-dasharray %s\n",value);
  1589. break;
  1590. }
  1591. if (LocaleCompare(keyword,"stroke-dashoffset") == 0)
  1592. {
  1593. MVGPrintf(svg_info->file,"stroke-dashoffset %s\n",
  1594. value);
  1595. break;
  1596. }
  1597. if (LocaleCompare(keyword,"stroke-linecap") == 0)
  1598. {
  1599. MVGPrintf(svg_info->file,"stroke-linecap '%s'\n",value);
  1600. break;
  1601. }
  1602. if (LocaleCompare(keyword,"stroke-linejoin") == 0)
  1603. {
  1604. MVGPrintf(svg_info->file,"stroke-linejoin '%s'\n",
  1605. value);
  1606. break;
  1607. }
  1608. if (LocaleCompare(keyword,"stroke-miterlimit") == 0)
  1609. {
  1610. MVGPrintf(svg_info->file,"stroke-miterlimit '%s'\n",
  1611. value);
  1612. break;
  1613. }
  1614. if (LocaleCompare(keyword,"stroke-opacity") == 0)
  1615. {
  1616. MVGPrintf(svg_info->file,"stroke-opacity '%s'\n",value);
  1617. break;
  1618. }
  1619. if (LocaleCompare(keyword,"stroke-width") == 0)
  1620. {
  1621. MVGPrintf(svg_info->file,"stroke-width %g\n",
  1622. GetUserSpaceCoordinateValue(svg_info,1,value));
  1623. break;
  1624. }
  1625. break;
  1626. }
  1627. case 't':
  1628. case 'T':
  1629. {
  1630. if (LocaleCompare(keyword,"text-align") == 0)
  1631. {
  1632. MVGPrintf(svg_info->file,"text-align '%s'\n",value);
  1633. break;
  1634. }
  1635. if (LocaleCompare(keyword,"text-anchor") == 0)
  1636. {
  1637. MVGPrintf(svg_info->file,"text-anchor '%s'\n",value);
  1638. break;
  1639. }
  1640. if (LocaleCompare(keyword,"text-decoration") == 0)
  1641. {
  1642. if (LocaleCompare(value,"underline") == 0)
  1643. MVGPrintf(svg_info->file,"decorate underline\n");
  1644. if (LocaleCompare(value,"line-through") == 0)
  1645. MVGPrintf(svg_info->file,"decorate line-through\n");
  1646. if (LocaleCompare(value,"overline") == 0)
  1647. MVGPrintf(svg_info->file,"decorate overline\n");
  1648. break;
  1649. }
  1650. if (LocaleCompare(keyword,"text-antialiasing") == 0)
  1651. {
  1652. MVGPrintf(svg_info->file,"text-antialias %d\n",
  1653. LocaleCompare(value,"true") == 0);
  1654. break;
  1655. }
  1656. break;
  1657. }
  1658. default:
  1659. break;
  1660. }
  1661. }
  1662. for (j=0; tokens[j] != (char *) NULL; j++)
  1663. tokens[j]=(char *) RelinquishMagickMemory(tokens[j]);
  1664. tokens=(char **) RelinquishMagickMemory(tokens);
  1665. break;
  1666. }
  1667. break;
  1668. }
  1669. case 'T':
  1670. case 't':
  1671. {
  1672. if (LocaleCompare(keyword,"text-align") == 0)
  1673. {
  1674. MVGPrintf(svg_info->file,"text-align '%s'\n",value);
  1675. break;
  1676. }
  1677. if (LocaleCompare(keyword,"text-anchor") == 0)
  1678. {
  1679. MVGPrintf(svg_info->file,"text-anchor '%s'\n",value);
  1680. break;
  1681. }
  1682. if (LocaleCompare(keyword,"text-decoration") == 0)
  1683. {
  1684. if (LocaleCompare(value,"underline") == 0)
  1685. MVGPrintf(svg_info->file,"decorate underline\n");
  1686. if (LocaleCompare(value,"line-through") == 0)
  1687. MVGPrintf(svg_info->file,"decorate line-through\n");
  1688. if (LocaleCompare(value,"overline") == 0)
  1689. MVGPrintf(svg_info->file,"decorate overline\n");
  1690. break;
  1691. }
  1692. if (LocaleCompare(keyword,"text-antialiasing") == 0)
  1693. {
  1694. MVGPrintf(svg_info->file,"text-antialias %d\n",
  1695. LocaleCompare(value,"true") == 0);
  1696. break;
  1697. }
  1698. if (LocaleCompare(keyword,"transform") == 0)
  1699. {
  1700. AffineMatrix
  1701. affine,
  1702. current,
  1703. transform;
  1704. GetAffineMatrix(&transform);
  1705. (void) LogMagickEvent(CoderEvent,GetMagickModule()," ");
  1706. tokens=GetTransformTokens(context,value,&number_tokens);
  1707. for (j=0; j < (number_tokens-1); j+=2)
  1708. {
  1709. keyword=(char *) tokens[j];
  1710. value=(char *) tokens[j+1];
  1711. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  1712. " %s: %s",keyword,value);
  1713. current=transform;
  1714. GetAffineMatrix(&affine);
  1715. switch (*keyword)
  1716. {
  1717. case 'M':
  1718. case 'm':
  1719. {
  1720. if (LocaleCompare(keyword,"matrix") == 0)
  1721. {
  1722. p=(char *) value;
  1723. GetMagickToken(p,&p,token);
  1724. affine.sx=atof(value);
  1725. GetMagickToken(p,&p,token);
  1726. if (*token == ',')
  1727. GetMagickToken(p,&p,token);
  1728. affine.rx=atof(token);
  1729. GetMagickToken(p,&p,token);
  1730. if (*token == ',')
  1731. GetMagickToken(p,&p,token);
  1732. affine.ry=atof(token);
  1733. GetMagickToken(p,&p,token);
  1734. if (*token == ',')
  1735. GetMagickToken(p,&p,token);
  1736. affine.sy=atof(token);
  1737. GetMagickToken(p,&p,token);
  1738. if (*token == ',')
  1739. GetMagickToken(p,&p,token);
  1740. affine.tx=atof(token);
  1741. GetMagickToken(p,&p,token);
  1742. if (*token == ',')
  1743. GetMagickToken(p,&p,token);
  1744. affine.ty=atof(token);
  1745. break;
  1746. }
  1747. break;
  1748. }
  1749. case 'R':
  1750. case 'r':
  1751. {
  1752. if (LocaleCompare(keyword,"rotate") == 0)
  1753. {
  1754. double
  1755. angle;
  1756. angle=GetUserSpaceCoordinateValue(svg_info,0,value);
  1757. affine.sx=cos(DegreesToRadians(fmod(angle,360.0)));
  1758. affine.rx=sin(DegreesToRadians(fmod(angle,360.0)));
  1759. affine.ry=(-sin(DegreesToRadians(fmod(angle,360.0))));
  1760. affine.sy=cos(DegreesToRadians(fmod(angle,360.0)));
  1761. affine.tx=svg_info->bounds.x;
  1762. affine.ty=svg_info->bounds.y;
  1763. break;
  1764. }
  1765. break;
  1766. }
  1767. case 'S':
  1768. case 's':
  1769. {
  1770. if (LocaleCompare(keyword,"scale") == 0)
  1771. {
  1772. for (p=(char *) value; *p != '\0'; p++)
  1773. if ((isspace((int) ((unsigned char) *p)) != 0) ||
  1774. (*p == ','))
  1775. break;
  1776. affine.sx=GetUserSpaceCoordinateValue(svg_info,1,value);
  1777. affine.sy=affine.sx;
  1778. if (*p != '\0')
  1779. affine.sy=GetUserSpaceCoordinateValue(svg_info,-1,
  1780. p+1);
  1781. svg_info->scale[svg_info->n]=ExpandAffine(&affine);
  1782. break;
  1783. }
  1784. if (LocaleCompare(keyword,"skewX") == 0)
  1785. {
  1786. affine.sx=svg_info->affine.sx;
  1787. affine.ry=tan(DegreesToRadians(fmod(
  1788. GetUserSpaceCoordinateValue(svg_info,1,value),
  1789. 360.0)));
  1790. affine.sy=svg_info->affine.sy;
  1791. break;
  1792. }
  1793. if (LocaleCompare(keyword,"skewY") == 0)
  1794. {
  1795. affine.sx=svg_info->affine.sx;
  1796. affine.rx=tan(DegreesToRadians(fmod(
  1797. GetUserSpaceCoordinateValue(svg_info,-1,value),
  1798. 360.0)));
  1799. affine.sy=svg_info->affine.sy;
  1800. break;
  1801. }
  1802. break;
  1803. }
  1804. case 'T':
  1805. case 't':
  1806. {
  1807. if (LocaleCompare(keyword,"translate") == 0)
  1808. {
  1809. for (p=(char *) value; *p != '\0'; p++)
  1810. if ((isspace((int) ((unsigned char) *p)) != 0) ||
  1811. (*p == ','))
  1812. break;
  1813. affine.tx=GetUserSpaceCoordinateValue(svg_info,1,value);
  1814. affine.ty=affine.tx;
  1815. if (*p != '\0')
  1816. affine.ty=GetUserSpaceCoordinateValue(svg_info,-1,
  1817. p+1);
  1818. break;
  1819. }
  1820. break;
  1821. }
  1822. default:
  1823. break;
  1824. }
  1825. transform.sx=current.sx*affine.sx+current.ry*affine.rx;
  1826. transform.rx=current.rx*affine.sx+current.sy*affine.rx;
  1827. transform.ry=current.sx*affine.ry+current.ry*affine.sy;
  1828. transform.sy=current.rx*affine.ry+current.sy*affine.sy;
  1829. transform.tx=current.sx*affine.tx+current.ry*affine.ty+
  1830. current.tx;
  1831. transform.ty=current.rx*affine.tx+current.sy*affine.ty+
  1832. current.ty;
  1833. }
  1834. MVGPrintf(svg_info->file,"affine %g %g %g %g %g %g\n",
  1835. transform.sx,transform.rx,transform.ry,transform.sy,
  1836. transform.tx,transform.ty);
  1837. for (j=0; tokens[j] != (char *) NULL; j++)
  1838. tokens[j]=(char *) RelinquishMagickMemory(tokens[j]);
  1839. tokens=(char **) RelinquishMagickMemory(tokens);
  1840. break;
  1841. }
  1842. break;
  1843. }
  1844. case 'V':
  1845. case 'v':
  1846. {
  1847. if (LocaleCompare(keyword,"verts") == 0)
  1848. {
  1849. (void) CloneString(&svg_info->vertices,value);
  1850. break;
  1851. }
  1852. if (LocaleCompare(keyword,"viewBox") == 0)
  1853. {
  1854. p=(char *) value;
  1855. GetMagickToken(p,&p,token);
  1856. svg_info->view_box.x=atof(token);
  1857. GetMagickToken(p,&p,token);
  1858. if (*token == ',')
  1859. GetMagickToken(p,&p,token);
  1860. svg_info->view_box.y=atof(token);
  1861. GetMagickToken(p,&p,token);
  1862. if (*token == ',')
  1863. GetMagickToken(p,&p,token);
  1864. svg_info->view_box.width=atof(token);
  1865. if (svg_info->bounds.width == 0)
  1866. svg_info->bounds.width=svg_info->view_box.width;
  1867. GetMagickToken(p,&p,token);
  1868. if (*token == ',')
  1869. GetMagickToken(p,&p,token);
  1870. svg_info->view_box.height=atof(token);
  1871. if (svg_info->bounds.height == 0)
  1872. svg_info->bounds.height=svg_info->view_box.height;
  1873. break;
  1874. }
  1875. break;
  1876. }
  1877. case 'W':
  1878. case 'w':
  1879. {
  1880. if (LocaleCompare(keyword,"width") == 0)
  1881. {
  1882. svg_info->bounds.width=
  1883. GetUserSpaceCoordinateValue(svg_info,1,value);
  1884. break;
  1885. }
  1886. break;
  1887. }
  1888. case 'X':
  1889. case 'x':
  1890. {
  1891. if (LocaleCompare(keyword,"x") == 0)
  1892. {
  1893. svg_info->bounds.x=GetUserSpaceCoordinateValue(svg_info,1,value);
  1894. break;
  1895. }
  1896. if (LocaleCompare(keyword,"xlink:href") == 0)
  1897. {
  1898. (void) CloneString(&svg_info->url,value);
  1899. break;
  1900. }
  1901. if (LocaleCompare(keyword,"x1") == 0)
  1902. {
  1903. svg_info->segment.x1=
  1904. GetUserSpaceCoordinateValue(svg_info,1,value);
  1905. break;
  1906. }
  1907. if (LocaleCompare(keyword,"x2") == 0)
  1908. {
  1909. svg_info->segment.x2=
  1910. GetUserSpaceCoordinateValue(svg_info,1,value);
  1911. break;
  1912. }
  1913. break;
  1914. }
  1915. case 'Y':
  1916. case 'y':
  1917. {
  1918. if (LocaleCompare(keyword,"y") == 0)
  1919. {
  1920. svg_info->bounds.y=GetUserSpaceCoordinateValue(svg_info,-1,value);
  1921. break;
  1922. }
  1923. if (LocaleCompare(keyword,"y1") == 0)
  1924. {
  1925. svg_info->segment.y1=
  1926. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1927. break;
  1928. }
  1929. if (LocaleCompare(keyword,"y2") == 0)
  1930. {
  1931. svg_info->segment.y2=
  1932. GetUserSpaceCoordinateValue(svg_info,-1,value);
  1933. break;
  1934. }
  1935. break;
  1936. }
  1937. default:
  1938. break;
  1939. }
  1940. }
  1941. if (LocaleCompare((char *) name,"svg") == 0)
  1942. {
  1943. if (svg_info->document->encoding != (const xmlChar *) NULL)
  1944. MVGPrintf(svg_info->file,"encoding \"%s\"\n",
  1945. (char *) svg_info->document->encoding);
  1946. if (attributes != (const xmlChar **) NULL)
  1947. {
  1948. double
  1949. sx,
  1950. sy;
  1951. if ((svg_info->view_box.width == 0.0) ||
  1952. (svg_info->view_box.height == 0.0))
  1953. svg_info->view_box=svg_info->bounds;
  1954. svg_info->width=(unsigned long) (svg_info->bounds.width+0.5);
  1955. svg_info->height=(unsigned long) (svg_info->bounds.height+0.5);
  1956. MVGPrintf(svg_info->file,"viewbox 0 0 %lu %lu\n",svg_info->width,
  1957. svg_info->height);
  1958. sx=(double) svg_info->width/svg_info->view_box.width;
  1959. sy=(double) svg_info->height/svg_info->view_box.height;
  1960. MVGPrintf(svg_info->file,"affine %g 0 0 %g %g %g\n",sx,sy,
  1961. -sx*svg_info->view_box.x,-sy*svg_info->view_box.y);
  1962. }
  1963. }
  1964. (void) LogMagickEvent(CoderEvent,GetMagickModule()," )");
  1965. units=(char *) RelinquishMagickMemory(units);
  1966. if (color != (char *) NULL)
  1967. color=(char *) RelinquishMagickMemory(color);
  1968. }
  1969. static void SVGEndElement(void *context,const xmlChar *name)
  1970. {
  1971. SVGInfo
  1972. *svg_info;
  1973. /*
  1974. Called when the end of an element has been detected.
  1975. */
  1976. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  1977. " SAX.endElement(%s)",name);
  1978. svg_info=(SVGInfo *) context;
  1979. switch (*name)
  1980. {
  1981. case 'C':
  1982. case 'c':
  1983. {
  1984. if (LocaleCompare((char *) name,"circle") == 0)
  1985. {
  1986. MVGPrintf(svg_info->file,"circle %g,%g %g,%g\n",svg_info->element.cx,
  1987. svg_info->element.cy,svg_info->element.cx,svg_info->element.cy+
  1988. svg_info->element.minor);
  1989. MVGPrintf(svg_info->file,"pop graphic-context\n");
  1990. break;
  1991. }
  1992. if (LocaleCompare((char *) name,"clipPath") == 0)
  1993. {
  1994. MVGPrintf(svg_info->file,"pop clip-path\n");
  1995. break;
  1996. }
  1997. break;
  1998. }
  1999. case 'D':
  2000. case 'd':
  2001. {
  2002. if (LocaleCompare((char *) name,"defs") == 0)
  2003. {
  2004. MVGPrintf(svg_info->file,"pop defs\n");
  2005. break;
  2006. }
  2007. if (LocaleCompare((char *) name,"desc") == 0)
  2008. {
  2009. register char
  2010. *p;
  2011. if (*svg_info->text == '\0')
  2012. break;
  2013. (void) fputc('#',svg_info->file);
  2014. for (p=svg_info->text; *p != '\0'; p++)
  2015. {
  2016. (void) fputc(*p,svg_info->file);
  2017. if (*p == '\n')
  2018. (void) fputc('#',svg_info->file);
  2019. }
  2020. (void) fputc('\n',svg_info->file);
  2021. *svg_info->text='\0';
  2022. break;
  2023. }
  2024. break;
  2025. }
  2026. case 'E':
  2027. case 'e':
  2028. {
  2029. if (LocaleCompare((char *) name,"ellipse") == 0)
  2030. {
  2031. double
  2032. angle;
  2033. angle=svg_info->element.angle;
  2034. MVGPrintf(svg_info->file,"ellipse %g,%g %g,%g 0,360\n",
  2035. svg_info->element.cx,svg_info->element.cy,
  2036. angle == 0.0 ? svg_info->element.major : svg_info->element.minor,
  2037. angle == 0.0 ? svg_info->element.minor : svg_info->element.major);
  2038. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2039. break;
  2040. }
  2041. break;
  2042. }
  2043. case 'G':
  2044. case 'g':
  2045. {
  2046. if (LocaleCompare((char *) name,"g") == 0)
  2047. {
  2048. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2049. break;
  2050. }
  2051. break;
  2052. }
  2053. case 'I':
  2054. case 'i':
  2055. {
  2056. if (LocaleCompare((char *) name,"image") == 0)
  2057. {
  2058. MVGPrintf(svg_info->file,"image Over %g,%g %g,%g '%s'\n",
  2059. svg_info->bounds.x,svg_info->bounds.y,svg_info->bounds.width,
  2060. svg_info->bounds.height,svg_info->url);
  2061. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2062. break;
  2063. }
  2064. break;
  2065. }
  2066. case 'L':
  2067. case 'l':
  2068. {
  2069. if (LocaleCompare((char *) name,"line") == 0)
  2070. {
  2071. MVGPrintf(svg_info->file,"line %g,%g %g,%g\n",svg_info->segment.x1,
  2072. svg_info->segment.y1,svg_info->segment.x2,svg_info->segment.y2);
  2073. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2074. break;
  2075. }
  2076. if (LocaleCompare((char *) name,"linearGradient") == 0)
  2077. {
  2078. MVGPrintf(svg_info->file,"pop gradient\n");
  2079. break;
  2080. }
  2081. break;
  2082. }
  2083. case 'P':
  2084. case 'p':
  2085. {
  2086. if (LocaleCompare((char *) name,"pattern") == 0)
  2087. {
  2088. MVGPrintf(svg_info->file,"pop pattern\n");
  2089. break;
  2090. }
  2091. if (LocaleCompare((char *) name,"path") == 0)
  2092. {
  2093. MVGPrintf(svg_info->file,"path '%s'\n",svg_info->vertices);
  2094. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2095. break;
  2096. }
  2097. if (LocaleCompare((char *) name,"polygon") == 0)
  2098. {
  2099. MVGPrintf(svg_info->file,"polygon %s\n",svg_info->vertices);
  2100. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2101. break;
  2102. }
  2103. if (LocaleCompare((char *) name,"polyline") == 0)
  2104. {
  2105. MVGPrintf(svg_info->file,"polyline %s\n",svg_info->vertices);
  2106. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2107. break;
  2108. }
  2109. break;
  2110. }
  2111. case 'R':
  2112. case 'r':
  2113. {
  2114. if (LocaleCompare((char *) name,"radialGradient") == 0)
  2115. {
  2116. MVGPrintf(svg_info->file,"pop gradient\n");
  2117. break;
  2118. }
  2119. if (LocaleCompare((char *) name,"rect") == 0)
  2120. {
  2121. if ((svg_info->radius.x == 0.0) && (svg_info->radius.y == 0.0))
  2122. {
  2123. MVGPrintf(svg_info->file,"rectangle %g,%g %g,%g\n",
  2124. svg_info->bounds.x,svg_info->bounds.y,
  2125. svg_info->bounds.x+svg_info->bounds.width,
  2126. svg_info->bounds.y+svg_info->bounds.height);
  2127. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2128. break;
  2129. }
  2130. if (svg_info->radius.x == 0.0)
  2131. svg_info->radius.x=svg_info->radius.y;
  2132. if (svg_info->radius.y == 0.0)
  2133. svg_info->radius.y=svg_info->radius.x;
  2134. MVGPrintf(svg_info->file,"roundRectangle %g,%g %g,%g %g,%g\n",
  2135. svg_info->bounds.x,svg_info->bounds.y,svg_info->bounds.x+
  2136. svg_info->bounds.width,svg_info->bounds.y+svg_info->bounds.height,
  2137. svg_info->radius.x,svg_info->radius.y);
  2138. svg_info->radius.x=0.0;
  2139. svg_info->radius.y=0.0;
  2140. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2141. break;
  2142. }
  2143. break;
  2144. }
  2145. case 'S':
  2146. case 's':
  2147. {
  2148. if (LocaleCompare((char *) name,"stop") == 0)
  2149. {
  2150. MVGPrintf(svg_info->file,"stop-color '%s' %s\n",svg_info->stop_color,
  2151. svg_info->offset);
  2152. break;
  2153. }
  2154. if (LocaleCompare((char *) name,"svg") == 0)
  2155. {
  2156. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2157. break;
  2158. }
  2159. break;
  2160. }
  2161. case 'T':
  2162. case 't':
  2163. {
  2164. if (LocaleCompare((char *) name,"text") == 0)
  2165. {
  2166. if (*svg_info->text != '\0')
  2167. {
  2168. char
  2169. *text;
  2170. text=EscapeString(svg_info->text,'\'');
  2171. StripString(text);
  2172. MVGPrintf(svg_info->file,"text %g,%g '%s'\n",svg_info->bounds.x,
  2173. svg_info->bounds.y,text);
  2174. text=(char *) RelinquishMagickMemory(text);
  2175. *svg_info->text='\0';
  2176. }
  2177. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2178. break;
  2179. }
  2180. if (LocaleCompare((char *) name,"tspan") == 0)
  2181. {
  2182. if (*svg_info->text != '\0')
  2183. {
  2184. DrawInfo
  2185. *draw_info;
  2186. TypeMetric
  2187. metrics;
  2188. char
  2189. *text;
  2190. text=EscapeString(svg_info->text,'\'');
  2191. StripString(text);
  2192. MVGPrintf(svg_info->file,"text %g,%g '%s'\n",svg_info->bounds.x,
  2193. svg_info->bounds.y,text);
  2194. text=(char *) RelinquishMagickMemory(text);
  2195. draw_info=CloneDrawInfo(svg_info->image_info,(DrawInfo *) NULL);
  2196. draw_info->pointsize=svg_info->pointsize;
  2197. draw_info->text=AcquireString(svg_info->text);
  2198. (void) ConcatenateString(&draw_info->text," ");
  2199. GetTypeMetrics(svg_info->image,draw_info,&metrics);
  2200. svg_info->bounds.x+=metrics.width;
  2201. draw_info=DestroyDrawInfo(draw_info);
  2202. *svg_info->text='\0';
  2203. }
  2204. MVGPrintf(svg_info->file,"pop graphic-context\n");
  2205. break;
  2206. }
  2207. if (LocaleCompare((char *) name,"title") == 0)
  2208. {
  2209. if (*svg_info->text == '\0')
  2210. break;
  2211. (void) CloneString(&svg_info->title,svg_info->text);
  2212. *svg_info->text='\0';
  2213. break;
  2214. }
  2215. break;
  2216. }
  2217. default:
  2218. break;
  2219. }
  2220. *svg_info->text='\0';
  2221. (void) ResetMagickMemory(&svg_info->element,0,sizeof(svg_info->element));
  2222. (void) ResetMagickMemory(&svg_info->segment,0,sizeof(svg_info->segment));
  2223. svg_info->n--;
  2224. }
  2225. static void SVGCharacters(void *context,const xmlChar *c,int length)
  2226. {
  2227. register char
  2228. *p;
  2229. register long
  2230. i;
  2231. SVGInfo
  2232. *svg_info;
  2233. /*
  2234. Receiving some characters from the parser.
  2235. */
  2236. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  2237. " SAX.characters(%s,%d)",c,length);
  2238. svg_info=(SVGInfo *) context;
  2239. if (svg_info->text != (char *) NULL)
  2240. svg_info->text=(char *) ResizeMagickMemory(svg_info->text,
  2241. strlen(svg_info->text)+length+MaxTextExtent);
  2242. else
  2243. {
  2244. svg_info->text=(char *) AcquireMagickMemory(length+MaxTextExtent);
  2245. if (svg_info->text != (char *) NULL)
  2246. *svg_info->text='\0';
  2247. }
  2248. if (svg_info->text == (char *) NULL)
  2249. return;
  2250. p=svg_info->text+strlen(svg_info->text);
  2251. for (i=0; i < length; i++)
  2252. *p++=c[i];
  2253. *p='\0';
  2254. }
  2255. static void SVGReference(void *context,const xmlChar *name)
  2256. {
  2257. SVGInfo
  2258. *svg_info;
  2259. xmlParserCtxtPtr
  2260. parser;
  2261. /*
  2262. Called when an entity reference is detected.
  2263. */
  2264. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.reference(%s)",
  2265. name);
  2266. svg_info=(SVGInfo *) context;
  2267. parser=svg_info->parser;
  2268. if (*name == '#')
  2269. (void) xmlAddChild(parser->node,xmlNewCharRef(svg_info->document,name));
  2270. else
  2271. (void) xmlAddChild(parser->node,xmlNewReference(svg_info->document,name));
  2272. }
  2273. static void SVGIgnorableWhitespace(void *context,const xmlChar *c,int length)
  2274. {
  2275. SVGInfo
  2276. *svg_info;
  2277. /*
  2278. Receiving some ignorable whitespaces from the parser.
  2279. */
  2280. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  2281. " SAX.ignorableWhitespace(%.30s, %d)",c,length);
  2282. svg_info=(SVGInfo *) context;
  2283. }
  2284. static void SVGProcessingInstructions(void *context,const xmlChar *target,
  2285. const xmlChar *data)
  2286. {
  2287. SVGInfo
  2288. *svg_info;
  2289. /*
  2290. A processing instruction has been parsed.
  2291. */
  2292. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  2293. " SAX.processingInstruction(%s, %s)",target,data);
  2294. svg_info=(SVGInfo *) context;
  2295. }
  2296. static void SVGComment(void *context,const xmlChar *value)
  2297. {
  2298. SVGInfo
  2299. *svg_info;
  2300. /*
  2301. A comment has been parsed.
  2302. */
  2303. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.comment(%s)",
  2304. value);
  2305. svg_info=(SVGInfo *) context;
  2306. if (svg_info->comment != (char *) NULL)
  2307. (void) ConcatenateString(&svg_info->comment,"\n");
  2308. (void) ConcatenateString(&svg_info->comment,(char *) value);
  2309. }
  2310. static void SVGWarning(void *context,const char *format,...)
  2311. {
  2312. char
  2313. *message,
  2314. reason[MaxTextExtent];
  2315. SVGInfo
  2316. *svg_info;
  2317. va_list
  2318. operands;
  2319. /**
  2320. Display and format a warning messages, gives file, line, position and
  2321. extra parameters.
  2322. */
  2323. va_start(operands,format);
  2324. svg_info=(SVGInfo *) context;
  2325. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.warning: ");
  2326. (void) LogMagickEvent(CoderEvent,GetMagickModule(),format,operands);
  2327. #if !defined(HAVE_VSNPRINTF)
  2328. (void) vsprintf(reason,format,operands);
  2329. #else
  2330. (void) vsnprintf(reason,MaxTextExtent,format,operands);
  2331. #endif
  2332. message=GetExceptionMessage(errno);
  2333. (void) ThrowMagickException(svg_info->exception,GetMagickModule(),
  2334. DelegateWarning,reason,"`%s`",message);
  2335. message=(char *) RelinquishMagickMemory(message);
  2336. va_end(operands);
  2337. }
  2338. static void SVGError(void *context,const char *format,...)
  2339. {
  2340. char
  2341. *message,
  2342. reason[MaxTextExtent];
  2343. SVGInfo
  2344. *svg_info;
  2345. va_list
  2346. operands;
  2347. /*
  2348. Display and format a error formats, gives file, line, position and
  2349. extra parameters.
  2350. */
  2351. va_start(operands,format);
  2352. svg_info=(SVGInfo *) context;
  2353. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.error: ");
  2354. (void) LogMagickEvent(CoderEvent,GetMagickModule(),format,operands);
  2355. #if !defined(HAVE_VSNPRINTF)
  2356. (void) vsprintf(reason,format,operands);
  2357. #else
  2358. (void) vsnprintf(reason,MaxTextExtent,format,operands);
  2359. #endif
  2360. message=GetExceptionMessage(errno);
  2361. (void) ThrowMagickException(svg_info->exception,GetMagickModule(),CoderError,
  2362. reason,"`%s`",message);
  2363. message=(char *) RelinquishMagickMemory(message);
  2364. va_end(operands);
  2365. }
  2366. static void SVGCDataBlock(void *context,const xmlChar *value,int length)
  2367. {
  2368. SVGInfo
  2369. *svg_info;
  2370. xmlNodePtr
  2371. child;
  2372. xmlParserCtxtPtr
  2373. parser;
  2374. /*
  2375. Called when a pcdata block has been parsed.
  2376. */
  2377. (void) LogMagickEvent(CoderEvent,GetMagickModule()," SAX.pcdata(%s, %d)",
  2378. value,length);
  2379. svg_info=(SVGInfo *) context;
  2380. parser=svg_info->parser;
  2381. child=xmlGetLastChild(parser->node);
  2382. if ((child != (xmlNodePtr) NULL) && (child->type == XML_CDATA_SECTION_NODE))
  2383. {
  2384. xmlTextConcat(child,value,length);
  2385. return;
  2386. }
  2387. (void) xmlAddChild(parser->node,xmlNewCDataBlock(parser->myDoc,value,length));
  2388. }
  2389. static void SVGExternalSubset(void *context,const xmlChar *name,
  2390. const xmlChar *external_id,const xmlChar *system_id)
  2391. {
  2392. SVGInfo
  2393. *svg_info;
  2394. xmlParserCtxt
  2395. parser_context;
  2396. xmlParserCtxtPtr
  2397. parser;
  2398. xmlParserInputPtr
  2399. input;
  2400. /*
  2401. Does this document has an external subset?
  2402. */
  2403. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  2404. " SAX.externalSubset(%s, %s, %s)",name,
  2405. (external_id != (const xmlChar *) NULL ? (char *) external_id : "none"),
  2406. (system_id != (const xmlChar *) NULL ? (char *) system_id : "none"));
  2407. svg_info=(SVGInfo *) context;
  2408. parser=svg_info->parser;
  2409. if (((external_id == NULL) && (system_id == NULL)) ||
  2410. ((parser->validate == 0) || (parser->wellFormed == 0) ||
  2411. (svg_info->document == 0)))
  2412. return;
  2413. input=SVGResolveEntity(context,external_id,system_id);
  2414. if (input == NULL)
  2415. return;
  2416. (void) xmlNewDtd(svg_info->document,name,external_id,system_id);
  2417. parser_context=(*parser);
  2418. parser->inputTab=(xmlParserInputPtr *) xmlMalloc(5*sizeof(*parser->inputTab));
  2419. if (parser->inputTab == (xmlParserInputPtr *) NULL)
  2420. {
  2421. parser->errNo=XML_ERR_NO_MEMORY;
  2422. parser->input=parser_context.input;
  2423. parser->inputNr=parser_context.inputNr;
  2424. parser->inputMax=parser_context.inputMax;
  2425. parser->inputTab=parser_context.inputTab;
  2426. return;
  2427. }
  2428. parser->inputNr=0;
  2429. parser->inputMax=5;
  2430. parser->input=NULL;
  2431. xmlPushInput(parser,input);
  2432. (void) xmlSwitchEncoding(parser,xmlDetectCharEncoding(parser->input->cur,4));
  2433. if (input->filename == (char *) NULL)
  2434. input->filename=(char *) xmlStrdup(system_id);
  2435. input->line=1;
  2436. input->col=1;
  2437. input->base=parser->input->cur;
  2438. input->cur=parser->input->cur;
  2439. input->free=NULL;
  2440. xmlParseExternalSubset(parser,external_id,system_id);
  2441. while (parser->inputNr > 1)
  2442. (void) xmlPopInput(parser);
  2443. xmlFreeInputStream(parser->input);
  2444. xmlFree(parser->inputTab);
  2445. parser->input=parser_context.input;
  2446. parser->inputNr=parser_context.inputNr;
  2447. parser->inputMax=parser_context.inputMax;
  2448. parser->inputTab=parser_context.inputTab;
  2449. }
  2450. #if defined(HasRSVG)
  2451. static void SVGSetImageSize(int *width,int *height,gpointer context)
  2452. {
  2453. Image
  2454. *image;
  2455. image=(Image *) context;
  2456. *width=(int) (*width*image->x_resolution/72.0);
  2457. *height=(int) (*height*image->y_resolution/72.0);
  2458. }
  2459. #endif
  2460. #if defined(__cplusplus) || defined(c_plusplus)
  2461. }
  2462. #endif
  2463. static Image *ReadSVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
  2464. {
  2465. xmlSAXHandler
  2466. SAXModules =
  2467. {
  2468. SVGInternalSubset,
  2469. SVGIsStandalone,
  2470. SVGHasInternalSubset,
  2471. SVGHasExternalSubset,
  2472. SVGResolveEntity,
  2473. SVGGetEntity,
  2474. SVGEntityDeclaration,
  2475. SVGNotationDeclaration,
  2476. SVGAttributeDeclaration,
  2477. SVGElementDeclaration,
  2478. SVGUnparsedEntityDeclaration,
  2479. SVGSetDocumentLocator,
  2480. SVGStartDocument,
  2481. SVGEndDocument,
  2482. SVGStartElement,
  2483. SVGEndElement,
  2484. SVGReference,
  2485. SVGCharacters,
  2486. SVGIgnorableWhitespace,
  2487. SVGProcessingInstructions,
  2488. SVGComment,
  2489. SVGWarning,
  2490. SVGError,
  2491. SVGError,
  2492. SVGGetParameterEntity,
  2493. SVGCDataBlock,
  2494. SVGExternalSubset
  2495. };
  2496. char
  2497. filename[MaxTextExtent];
  2498. FILE
  2499. *file;
  2500. Image
  2501. *image;
  2502. int
  2503. status,
  2504. unique_file;
  2505. long
  2506. n;
  2507. SVGInfo
  2508. svg_info;
  2509. unsigned char
  2510. message[MaxTextExtent];
  2511. xmlSAXHandlerPtr
  2512. sax_handler;
  2513. /*
  2514. Open image file.
  2515. */
  2516. assert(image_info != (const ImageInfo *) NULL);
  2517. assert(image_info->signature == MagickSignature);
  2518. assert(exception != (ExceptionInfo *) NULL);
  2519. if (image_info->debug != MagickFalse)
  2520. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  2521. image_info->filename);
  2522. assert(exception->signature == MagickSignature);
  2523. image=AllocateImage(image_info);
  2524. status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
  2525. if (status == MagickFalse)
  2526. {
  2527. image=DestroyImageList(image);
  2528. return((Image *) NULL);
  2529. }
  2530. #if defined(HasRSVG)
  2531. {
  2532. GdkPixbuf
  2533. *pixel_info;
  2534. GError
  2535. *error;
  2536. long
  2537. y;
  2538. PixelPacket
  2539. fill_color;
  2540. QuantumInfo
  2541. quantum_info;
  2542. register const guchar
  2543. *p;
  2544. register long
  2545. x;
  2546. register PixelPacket
  2547. *q;
  2548. RsvgHandle
  2549. *svg_info;
  2550. svg_info=rsvg_handle_new();
  2551. if (svg_info == (RsvgHandle *) NULL)
  2552. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  2553. rsvg_handle_set_size_callback(svg_info,SVGSetImageSize,image,NULL);
  2554. rsvg_handle_set_dpi_x_y(svg_info,
  2555. image->x_resolution == 0.0 ? 72.0 : image->x_resolution,
  2556. image->y_resolution == 0.0 ? 72.0 : image->y_resolution);
  2557. error=(GError *) NULL;
  2558. while ((n=ReadBlob(image,MaxTextExtent,message)) != 0)
  2559. (void) rsvg_handle_write(svg_info,message,n,&error);
  2560. rsvg_handle_close(svg_info,&error);
  2561. if (error != (GError *) NULL)
  2562. g_error_free(error);
  2563. pixel_info=rsvg_handle_get_pixbuf(svg_info);
  2564. rsvg_handle_free(svg_info);
  2565. if (pixel_info == (GdkPixbuf *) NULL)
  2566. {
  2567. rsvg_handle_free(svg_info);
  2568. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  2569. }
  2570. image->columns=gdk_pixbuf_get_width(pixel_info);
  2571. image->rows=gdk_pixbuf_get_height(pixel_info);
  2572. image->matte=MagickTrue;
  2573. (void) SetImageBackgroundColor(image);
  2574. GetQuantumInfo(image_info,&quantum_info);
  2575. p=gdk_pixbuf_get_pixels(pixel_info);
  2576. for (y=0; y < (long) image->rows; y++)
  2577. {
  2578. q=GetImagePixels(image,0,y,image->columns,1);
  2579. if (q == (PixelPacket *) NULL)
  2580. break;
  2581. for (x=0; x < (long) image->columns; x++)
  2582. {
  2583. fill_color.red=ScaleCharToQuantum(*p++);
  2584. fill_color.green=ScaleCharToQuantum(*p++);
  2585. fill_color.blue=ScaleCharToQuantum(*p++);
  2586. fill_color.opacity=QuantumRange-ScaleCharToQuantum(*p++);
  2587. MagickCompositeOver(&fill_color,fill_color.opacity,q,(MagickRealType)
  2588. q->opacity,q);
  2589. q++;
  2590. }
  2591. if (SyncImagePixels(image) == MagickFalse)
  2592. break;
  2593. if (image->previous == (Image *) NULL)
  2594. if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
  2595. (QuantumTick(y,image->rows) != MagickFalse))
  2596. {
  2597. status=image->progress_monitor(LoadImageTag,y,image->rows,
  2598. image->client_data);
  2599. if (status == MagickFalse)
  2600. break;
  2601. }
  2602. }
  2603. g_object_unref(G_OBJECT(pixel_info));
  2604. CloseBlob(image);
  2605. return(GetFirstImageInList(image));
  2606. }
  2607. #endif
  2608. /*
  2609. Open draw file.
  2610. */
  2611. file=(FILE *) NULL;
  2612. unique_file=AcquireUniqueFileResource(filename);
  2613. if (unique_file != -1)
  2614. file=fdopen(unique_file,"w");
  2615. if ((unique_file == -1) || (file == (FILE *) NULL))
  2616. {
  2617. (void) CopyMagickString(image->filename,filename,MaxTextExtent);
  2618. ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
  2619. image->filename);
  2620. image=DestroyImageList(image);
  2621. return((Image *) NULL);
  2622. }
  2623. /*
  2624. Parse SVG file.
  2625. */
  2626. (void) ResetMagickMemory(&svg_info,0,sizeof(svg_info));
  2627. svg_info.file=file;
  2628. svg_info.exception=exception;
  2629. svg_info.image=image;
  2630. svg_info.image_info=image_info;
  2631. svg_info.text=AcquireString("");
  2632. svg_info.scale=(double *) AcquireMagickMemory(sizeof(*svg_info.scale));
  2633. if (svg_info.scale == (double *) NULL)
  2634. ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
  2635. GetAffineMatrix(&svg_info.affine);
  2636. svg_info.scale[0]=ExpandAffine(&svg_info.affine);
  2637. svg_info.bounds.width=image->columns;
  2638. svg_info.bounds.height=image->rows;
  2639. if (image_info->size != (char *) NULL)
  2640. (void) CloneString(&svg_info.size,image_info->size);
  2641. if (image->debug != MagickFalse)
  2642. (void) LogMagickEvent(CoderEvent,GetMagickModule(),"begin SAX");
  2643. xmlInitParser();
  2644. (void) xmlSubstituteEntitiesDefault(1);
  2645. sax_handler=(&SAXModules);
  2646. n=ReadBlob(image,MaxTextExtent,message);
  2647. if (n > 0)
  2648. {
  2649. svg_info.parser=xmlCreatePushParserCtxt(sax_handler,&svg_info,(char *)
  2650. message,n,image->filename);
  2651. while ((n=ReadBlob(image,MaxTextExtent,message)) != 0)
  2652. {
  2653. status=xmlParseChunk(svg_info.parser,(char *) message,(int) n,0);
  2654. if (status != 0)
  2655. break;
  2656. }
  2657. }
  2658. (void) xmlParseChunk(svg_info.parser,(char *) message,0,1);
  2659. xmlFreeParserCtxt(svg_info.parser);
  2660. if (image->debug != MagickFalse)
  2661. (void) LogMagickEvent(CoderEvent,GetMagickModule(),"end SAX");
  2662. xmlCleanupParser();
  2663. (void) fclose(file);
  2664. CloseBlob(image);
  2665. image->columns=svg_info.width;
  2666. image->rows=svg_info.height;
  2667. if (exception->severity >= ErrorException)
  2668. {
  2669. image=DestroyImage(image);
  2670. return((Image *) NULL);
  2671. }
  2672. if (image_info->ping == MagickFalse)
  2673. {
  2674. ImageInfo
  2675. *read_info;
  2676. /*
  2677. Draw image.
  2678. */
  2679. image=DestroyImage(image);
  2680. image=(Image *) NULL;
  2681. read_info=CloneImageInfo(image_info);
  2682. SetImageInfoBlob(read_info,(void *) NULL,0);
  2683. (void) FormatMagickString(read_info->filename,MaxTextExtent,"mvg:%s",
  2684. filename);
  2685. image=ReadImage(read_info,exception);
  2686. read_info=DestroyImageInfo(read_info);
  2687. if (image != (Image *) NULL)
  2688. (void) CopyMagickString(image->filename,image_info->filename,
  2689. MaxTextExtent);
  2690. }
  2691. /*
  2692. Free resources.
  2693. */
  2694. if (svg_info.title != (char *) NULL)
  2695. {
  2696. if (image != (Image *) NULL)
  2697. (void) SetImageProperty(image,"title",svg_info.title);
  2698. svg_info.title=(char *) RelinquishMagickMemory(svg_info.title);
  2699. }
  2700. if (svg_info.comment != (char *) NULL)
  2701. {
  2702. if (image != (Image *) NULL)
  2703. (void) SetImageProperty(image,"Comment",svg_info.comment);
  2704. svg_info.comment=(char *) RelinquishMagickMemory(svg_info.comment);
  2705. }
  2706. (void) RelinquishUniqueFileResource(filename);
  2707. return(GetFirstImageInList(image));
  2708. }
  2709. #endif
  2710. /*
  2711. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2712. % %
  2713. % %
  2714. % %
  2715. % R e g i s t e r S V G I m a g e %
  2716. % %
  2717. % %
  2718. % %
  2719. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2720. %
  2721. % RegisterSVGImage() adds attributes for the SVG image format to
  2722. % the list of supported formats. The attributes include the image format
  2723. % tag, a method to read and/or write the format, whether the format
  2724. % supports the saving of more than one frame to the same file or blob,
  2725. % whether the format supports native in-memory I/O, and a brief
  2726. % description of the format.
  2727. %
  2728. % The format of the RegisterSVGImage method is:
  2729. %
  2730. % RegisterSVGImage(void)
  2731. %
  2732. */
  2733. ModuleExport void RegisterSVGImage(void)
  2734. {
  2735. char
  2736. version[MaxTextExtent];
  2737. MagickInfo
  2738. *entry;
  2739. #if defined(HasRSVG)
  2740. rsvg_init();
  2741. #endif
  2742. *version='\0';
  2743. #if defined(LIBXML_DOTTED_VERSION)
  2744. (void) CopyMagickString(version,"XML " LIBXML_DOTTED_VERSION,MaxTextExtent);
  2745. #endif
  2746. entry=SetMagickInfo("SVG");
  2747. #if defined(HasXML)
  2748. entry->decoder=(DecoderHandler *) ReadSVGImage;
  2749. #endif
  2750. entry->encoder=(EncoderHandler *) WriteSVGImage;
  2751. entry->seekable_stream=MagickFalse;
  2752. entry->description=ConstantString("Scalable Vector Graphics");
  2753. if (*version != '\0')
  2754. entry->version=ConstantString(version);
  2755. entry->magick=(MagickHandler *) IsSVG;
  2756. entry->module=ConstantString("SVG");
  2757. (void) RegisterMagickInfo(entry);
  2758. *version='\0';
  2759. #if defined(LIBXML_DOTTED_VERSION)
  2760. (void) CopyMagickString(version,"XML " LIBXML_DOTTED_VERSION,MaxTextExtent);
  2761. #endif
  2762. entry=SetMagickInfo("SVGZ");
  2763. #if defined(HasXML)
  2764. entry->decoder=(DecoderHandler *) ReadSVGImage;
  2765. #endif
  2766. entry->encoder=(EncoderHandler *) WriteSVGImage;
  2767. entry->seekable_stream=MagickFalse;
  2768. entry->description=ConstantString("Compressed Scalable Vector Graphics");
  2769. if (*version != '\0')
  2770. entry->version=ConstantString(version);
  2771. entry->magick=(MagickHandler *) IsSVG;
  2772. entry->module=ConstantString("SVG");
  2773. (void) RegisterMagickInfo(entry);
  2774. }
  2775. /*
  2776. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2777. % %
  2778. % %
  2779. % %
  2780. % U n r e g i s t e r S V G I m a g e %
  2781. % %
  2782. % %
  2783. % %
  2784. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2785. %
  2786. % UnregisterSVGImage() removes format registrations made by the
  2787. % SVG module from the list of supported formats.
  2788. %
  2789. % The format of the UnregisterSVGImage method is:
  2790. %
  2791. % UnregisterSVGImage(void)
  2792. %
  2793. */
  2794. ModuleExport void UnregisterSVGImage(void)
  2795. {
  2796. (void) UnregisterMagickInfo("SVG");
  2797. (void) UnregisterMagickInfo("SVGZ");
  2798. }
  2799. /*
  2800. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2801. % %
  2802. % %
  2803. % %
  2804. % W r i t e S V G I m a g e %
  2805. % %
  2806. % %
  2807. % %
  2808. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2809. %
  2810. % WriteSVGImage() writes a image in the SVG - XML based W3C standard
  2811. % format.
  2812. %
  2813. % The format of the WriteSVGImage method is:
  2814. %
  2815. % MagickBooleanType WriteSVGImage(const ImageInfo *image_info,Image *image)
  2816. %
  2817. % A description of each parameter follows.
  2818. %
  2819. % o image_info: The image info.
  2820. %
  2821. % o image: The image.
  2822. %
  2823. %
  2824. */
  2825. #if defined(HasAUTOTRACE)
  2826. static MagickBooleanType WriteSVGImage(const ImageInfo *image_info,Image *image)
  2827. {
  2828. FILE
  2829. *output_file;
  2830. fitting_opts_type
  2831. fit_info;
  2832. image_header_type
  2833. image_header;
  2834. bitmap_type
  2835. bitmap;
  2836. ImageType
  2837. image_type;
  2838. long
  2839. j;
  2840. MagickOffsetType
  2841. number_pixels;
  2842. pixel_outline_list_type
  2843. pixels;
  2844. PixelPacket
  2845. p;
  2846. PixelPacket
  2847. *pixel;
  2848. QuantizeObj
  2849. *quantize_info;
  2850. spline_list_array_type
  2851. splines;
  2852. output_write
  2853. output_writer;
  2854. register long
  2855. i;
  2856. unsigned long
  2857. number_planes,
  2858. point,
  2859. thin;
  2860. thin=MagickFalse;
  2861. quantize_info=(QuantizeObj *) NULL;
  2862. pixel=(&p);
  2863. fit_info=new_fitting_opts();
  2864. output_writer=output_get_handler("svg");
  2865. if (output_writer == NULL)
  2866. ThrowWriterException(DelegateError,"UnableToWriteSVGFormat");
  2867. image_type=GetImageType(image);
  2868. number_planes=3;
  2869. if ((image_type == BilevelType) || (image_type == GrayscaleType))
  2870. number_planes=1;
  2871. bitmap.np=number_planes;
  2872. bitmap.dimensions.width=image->columns;
  2873. bitmap.dimensions.height=image->rows;
  2874. number_pixels=image->columns*image->rows;
  2875. if (number_pixels != (size_t) number_pixels)
  2876. ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  2877. bitmap.bitmap=(unsigned char *)
  2878. AcquireMagickMemory(number_planes*image->columns*image->rows);
  2879. if (bitmap.bitmap == (unsigned char *) NULL)
  2880. ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  2881. point=0;
  2882. for (j=0; j < image->rows; j++)
  2883. {
  2884. for (i=0; i < image->columns; i++)
  2885. {
  2886. p=GetOnePixel(image,i,j);
  2887. bitmap.bitmap[point++]=pixel->red;
  2888. if (number_planes == 3)
  2889. {
  2890. bitmap.bitmap[point++]=pixel->green;
  2891. bitmap.bitmap[point++]=pixel->blue;
  2892. }
  2893. }
  2894. }
  2895. image_header.width=DIMENSIONS_WIDTH(bitmap.dimensions);
  2896. image_header.height=DIMENSIONS_HEIGHT(bitmap.dimensions);
  2897. if ((fit_info.color_count > 0) && (BITMAP_PLANES(bitmap) == 3))
  2898. quantize(bitmap.bitmap,bitmap.bitmap,DIMENSIONS_WIDTH(bitmap.dimensions),
  2899. DIMENSIONS_HEIGHT(bitmap.dimensions),fit_info.color_count,
  2900. fit_info.bgColor,&quantize_info);
  2901. if (thin)
  2902. thin_image(&bitmap);
  2903. pixels=find_outline_pixels (bitmap);
  2904. (bitmap.bitmap=(char *) RelinquishMagickMemory((bitmap.bitmap);
  2905. splines=fitted_splines(pixels,&fit_info);
  2906. output_file=MagickOpenStream(image->filename,"w");
  2907. if (output_file == (FILE *) NULL)
  2908. return(status);
  2909. output_writer(output_file,image->filename,0,0,image_header.width,
  2910. image_header.height,splines);
  2911. return(MagickTrue);
  2912. }
  2913. #else
  2914. static void AffineToTransform(Image *image,AffineMatrix *affine)
  2915. {
  2916. char
  2917. transform[MaxTextExtent];
  2918. if ((fabs(affine->tx) < MagickEpsilon) && (fabs(affine->ty) < MagickEpsilon))
  2919. {
  2920. if ((fabs(affine->rx) < MagickEpsilon) &&
  2921. (fabs(affine->ry) < MagickEpsilon))
  2922. {
  2923. if ((fabs(affine->sx-1.0) < MagickEpsilon) &&
  2924. (fabs(affine->sy-1.0) < MagickEpsilon))
  2925. {
  2926. (void) WriteBlobString(image,"\">\n");
  2927. return;
  2928. }
  2929. (void) FormatMagickString(transform,MaxTextExtent,
  2930. "\" transform=\"scale(%g,%g)\">\n",affine->sx,affine->sy);
  2931. (void) WriteBlobString(image,transform);
  2932. return;
  2933. }
  2934. else
  2935. {
  2936. if ((fabs(affine->sx-affine->sy) < MagickEpsilon) &&
  2937. (fabs(affine->rx+affine->ry) < MagickEpsilon) &&
  2938. (fabs(affine->sx*affine->sx+affine->rx*affine->rx-1.0) <
  2939. 2*MagickEpsilon))
  2940. {
  2941. double
  2942. theta;
  2943. theta=(180.0/MagickPI)*atan2(affine->rx,affine->sx);
  2944. (void) FormatMagickString(transform,MaxTextExtent,
  2945. "\" transform=\"rotate(%g)\">\n",theta);
  2946. (void) WriteBlobString(image,transform);
  2947. return;
  2948. }
  2949. }
  2950. }
  2951. else
  2952. {
  2953. if ((fabs(affine->sx-1.0) < MagickEpsilon) &&
  2954. (fabs(affine->rx) < MagickEpsilon) &&
  2955. (fabs(affine->ry) < MagickEpsilon) &&
  2956. (fabs(affine->sy-1.0) < MagickEpsilon))
  2957. {
  2958. (void) FormatMagickString(transform,MaxTextExtent,
  2959. "\" transform=\"translate(%g,%g)\">\n",affine->tx,affine->ty);
  2960. (void) WriteBlobString(image,transform);
  2961. return;
  2962. }
  2963. }
  2964. (void) FormatMagickString(transform,MaxTextExtent,
  2965. "\" transform=\"matrix(%g %g %g %g %g %g)\">\n",affine->sx,affine->rx,
  2966. affine->ry,affine->sy,affine->tx,affine->ty);
  2967. (void) WriteBlobString(image,transform);
  2968. }
  2969. static MagickBooleanType IsPoint(const char *point)
  2970. {
  2971. char
  2972. *p;
  2973. long
  2974. value;
  2975. value=strtol(point,&p,10);
  2976. return(p != point ? MagickTrue : MagickFalse);
  2977. }
  2978. static MagickBooleanType WriteSVGImage(const ImageInfo *image_info,Image *image)
  2979. {
  2980. #define BezierQuantum 200
  2981. AffineMatrix
  2982. affine;
  2983. char
  2984. keyword[MaxTextExtent],
  2985. message[MaxTextExtent],
  2986. name[MaxTextExtent],
  2987. *p,
  2988. *q,
  2989. *token,
  2990. type[MaxTextExtent];
  2991. const char
  2992. *value;
  2993. int
  2994. n;
  2995. long
  2996. j;
  2997. MagickBooleanType
  2998. active,
  2999. status;
  3000. PointInfo
  3001. point;
  3002. PrimitiveInfo
  3003. *primitive_info;
  3004. PrimitiveType
  3005. primitive_type;
  3006. register long
  3007. x;
  3008. register long
  3009. i;
  3010. size_t
  3011. length;
  3012. SVGInfo
  3013. svg_info;
  3014. unsigned long
  3015. number_points;
  3016. /*
  3017. Open output image file.
  3018. */
  3019. assert(image_info != (const ImageInfo *) NULL);
  3020. assert(image_info->signature == MagickSignature);
  3021. assert(image != (Image *) NULL);
  3022. assert(image->signature == MagickSignature);
  3023. if (image->debug != MagickFalse)
  3024. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  3025. value=GetImageProperty(image,"[MVG]");
  3026. if (value == (char *) NULL)
  3027. ThrowWriterException(CoderError,"NoImageVectorGraphics");
  3028. status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
  3029. if (status == MagickFalse)
  3030. return(status);
  3031. /*
  3032. Write SVG header.
  3033. */
  3034. (void) WriteBlobString(image,"<?xml version=\"1.0\" standalone=\"no\"?>\n");
  3035. (void) WriteBlobString(image,
  3036. "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"\n");
  3037. (void) WriteBlobString(image,
  3038. " \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n");
  3039. (void) FormatMagickString(message,MaxTextExtent,
  3040. "<svg width=\"%lu\" height=\"%lu\">\n",image->columns,image->rows);
  3041. (void) WriteBlobString(image,message);
  3042. /*
  3043. Allocate primitive info memory.
  3044. */
  3045. number_points=2047;
  3046. primitive_info=(PrimitiveInfo *)
  3047. AcquireMagickMemory(number_points*sizeof(*primitive_info));
  3048. if (primitive_info == (PrimitiveInfo *) NULL)
  3049. ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  3050. GetAffineMatrix(&affine);
  3051. token=AcquireString(value);
  3052. active=MagickFalse;
  3053. n=0;
  3054. status=MagickTrue;
  3055. for (q=(char *) value; *q != '\0'; )
  3056. {
  3057. /*
  3058. Interpret graphic primitive.
  3059. */
  3060. GetMagickToken(q,&q,keyword);
  3061. if (*keyword == '\0')
  3062. break;
  3063. if (*keyword == '#')
  3064. {
  3065. /*
  3066. Comment.
  3067. */
  3068. if (active != MagickFalse)
  3069. {
  3070. AffineToTransform(image,&affine);
  3071. active=MagickFalse;
  3072. }
  3073. (void) WriteBlobString(image,"<desc>");
  3074. (void) WriteBlobString(image,keyword+1);
  3075. for ( ; (*q != '\n') && (*q != '\0'); q++)
  3076. switch (*q)
  3077. {
  3078. case '<': (void) WriteBlobString(image,"&lt;"); break;
  3079. case '>': (void) WriteBlobString(image,"&gt;"); break;
  3080. case '&': (void) WriteBlobString(image,"&amp;"); break;
  3081. default: (void) WriteBlobByte(image,*q); break;
  3082. }
  3083. (void) WriteBlobString(image,"</desc>\n");
  3084. continue;
  3085. }
  3086. primitive_type=UndefinedPrimitive;
  3087. switch (*keyword)
  3088. {
  3089. case ';':
  3090. break;
  3091. case 'a':
  3092. case 'A':
  3093. {
  3094. if (LocaleCompare("affine",keyword) == 0)
  3095. {
  3096. GetMagickToken(q,&q,token);
  3097. affine.sx=atof(token);
  3098. GetMagickToken(q,&q,token);
  3099. if (*token == ',')
  3100. GetMagickToken(q,&q,token);
  3101. affine.rx=atof(token);
  3102. GetMagickToken(q,&q,token);
  3103. if (*token == ',')
  3104. GetMagickToken(q,&q,token);
  3105. affine.ry=atof(token);
  3106. GetMagickToken(q,&q,token);
  3107. if (*token == ',')
  3108. GetMagickToken(q,&q,token);
  3109. affine.sy=atof(token);
  3110. GetMagickToken(q,&q,token);
  3111. if (*token == ',')
  3112. GetMagickToken(q,&q,token);
  3113. affine.tx=atof(token);
  3114. GetMagickToken(q,&q,token);
  3115. if (*token == ',')
  3116. GetMagickToken(q,&q,token);
  3117. affine.ty=atof(token);
  3118. break;
  3119. }
  3120. if (LocaleCompare("angle",keyword) == 0)
  3121. {
  3122. GetMagickToken(q,&q,token);
  3123. affine.rx=atof(token);
  3124. affine.ry=atof(token);
  3125. break;
  3126. }
  3127. if (LocaleCompare("arc",keyword) == 0)
  3128. {
  3129. primitive_type=ArcPrimitive;
  3130. break;
  3131. }
  3132. status=MagickFalse;
  3133. break;
  3134. }
  3135. case 'b':
  3136. case 'B':
  3137. {
  3138. if (LocaleCompare("bezier",keyword) == 0)
  3139. {
  3140. primitive_type=BezierPrimitive;
  3141. break;
  3142. }
  3143. status=MagickFalse;
  3144. break;
  3145. }
  3146. case 'c':
  3147. case 'C':
  3148. {
  3149. if (LocaleCompare("clip-path",keyword) == 0)
  3150. {
  3151. GetMagickToken(q,&q,token);
  3152. (void) FormatMagickString(message,MaxTextExtent,
  3153. "clip-path:url(#%s);",token);
  3154. (void) WriteBlobString(image,message);
  3155. break;
  3156. }
  3157. if (LocaleCompare("clip-rule",keyword) == 0)
  3158. {
  3159. GetMagickToken(q,&q,token);
  3160. (void) FormatMagickString(message,MaxTextExtent,
  3161. "clip-rule:%s;",token);
  3162. (void) WriteBlobString(image,message);
  3163. break;
  3164. }
  3165. if (LocaleCompare("clip-units",keyword) == 0)
  3166. {
  3167. GetMagickToken(q,&q,token);
  3168. (void) FormatMagickString(message,MaxTextExtent,
  3169. "clipPathUnits=%s;",token);
  3170. (void) WriteBlobString(image,message);
  3171. break;
  3172. }
  3173. if (LocaleCompare("circle",keyword) == 0)
  3174. {
  3175. primitive_type=CirclePrimitive;
  3176. break;
  3177. }
  3178. if (LocaleCompare("color",keyword) == 0)
  3179. {
  3180. primitive_type=ColorPrimitive;
  3181. break;
  3182. }
  3183. status=MagickFalse;
  3184. break;
  3185. }
  3186. case 'd':
  3187. case 'D':
  3188. {
  3189. if (LocaleCompare("decorate",keyword) == 0)
  3190. {
  3191. GetMagickToken(q,&q,token);
  3192. (void) FormatMagickString(message,MaxTextExtent,
  3193. "text-decoration:%s;",token);
  3194. (void) WriteBlobString(image,message);
  3195. break;
  3196. }
  3197. status=MagickFalse;
  3198. break;
  3199. }
  3200. case 'e':
  3201. case 'E':
  3202. {
  3203. if (LocaleCompare("ellipse",keyword) == 0)
  3204. {
  3205. primitive_type=EllipsePrimitive;
  3206. break;
  3207. }
  3208. status=MagickFalse;
  3209. break;
  3210. }
  3211. case 'f':
  3212. case 'F':
  3213. {
  3214. if (LocaleCompare("fill",keyword) == 0)
  3215. {
  3216. GetMagickToken(q,&q,token);
  3217. (void) FormatMagickString(message,MaxTextExtent,"fill:%s;",
  3218. token);
  3219. (void) WriteBlobString(image,message);
  3220. break;
  3221. }
  3222. if (LocaleCompare("fill-rule",keyword) == 0)
  3223. {
  3224. GetMagickToken(q,&q,token);
  3225. (void) FormatMagickString(message,MaxTextExtent,
  3226. "fill-rule:%s;",token);
  3227. (void) WriteBlobString(image,message);
  3228. break;
  3229. }
  3230. if (LocaleCompare("fill-opacity",keyword) == 0)
  3231. {
  3232. GetMagickToken(q,&q,token);
  3233. (void) FormatMagickString(message,MaxTextExtent,
  3234. "fill-opacity:%s;",token);
  3235. (void) WriteBlobString(image,message);
  3236. break;
  3237. }
  3238. if (LocaleCompare("font-family",keyword) == 0)
  3239. {
  3240. GetMagickToken(q,&q,token);
  3241. (void) FormatMagickString(message,MaxTextExtent,
  3242. "font-family:%s;",token);
  3243. (void) WriteBlobString(image,message);
  3244. break;
  3245. }
  3246. if (LocaleCompare("font-stretch",keyword) == 0)
  3247. {
  3248. GetMagickToken(q,&q,token);
  3249. (void) FormatMagickString(message,MaxTextExtent,
  3250. "font-stretch:%s;",token);
  3251. (void) WriteBlobString(image,message);
  3252. break;
  3253. }
  3254. if (LocaleCompare("font-style",keyword) == 0)
  3255. {
  3256. GetMagickToken(q,&q,token);
  3257. (void) FormatMagickString(message,MaxTextExtent,
  3258. "font-style:%s;",token);
  3259. (void) WriteBlobString(image,message);
  3260. break;
  3261. }
  3262. if (LocaleCompare("font-size",keyword) == 0)
  3263. {
  3264. GetMagickToken(q,&q,token);
  3265. (void) FormatMagickString(message,MaxTextExtent,
  3266. "font-size:%s;",token);
  3267. (void) WriteBlobString(image,message);
  3268. break;
  3269. }
  3270. if (LocaleCompare("font-weight",keyword) == 0)
  3271. {
  3272. GetMagickToken(q,&q,token);
  3273. (void) FormatMagickString(message,MaxTextExtent,
  3274. "font-weight:%s;",token);
  3275. (void) WriteBlobString(image,message);
  3276. break;
  3277. }
  3278. status=MagickFalse;
  3279. break;
  3280. }
  3281. case 'g':
  3282. case 'G':
  3283. {
  3284. if (LocaleCompare("gradient-units",keyword) == 0)
  3285. {
  3286. GetMagickToken(q,&q,token);
  3287. break;
  3288. }
  3289. if (LocaleCompare("text-align",keyword) == 0)
  3290. {
  3291. GetMagickToken(q,&q,token);
  3292. (void) FormatMagickString(message,MaxTextExtent,
  3293. "text-align %s ",token);
  3294. (void) WriteBlobString(image,message);
  3295. break;
  3296. }
  3297. if (LocaleCompare("text-anchor",keyword) == 0)
  3298. {
  3299. GetMagickToken(q,&q,token);
  3300. (void) FormatMagickString(message,MaxTextExtent,
  3301. "text-anchor %s ",token);
  3302. (void) WriteBlobString(image,message);
  3303. break;
  3304. }
  3305. status=MagickFalse;
  3306. break;
  3307. }
  3308. case 'i':
  3309. case 'I':
  3310. {
  3311. if (LocaleCompare("image",keyword) == 0)
  3312. {
  3313. GetMagickToken(q,&q,token);
  3314. primitive_type=ImagePrimitive;
  3315. break;
  3316. }
  3317. status=MagickFalse;
  3318. break;
  3319. }
  3320. case 'l':
  3321. case 'L':
  3322. {
  3323. if (LocaleCompare("line",keyword) == 0)
  3324. {
  3325. primitive_type=LinePrimitive;
  3326. break;
  3327. }
  3328. status=MagickFalse;
  3329. break;
  3330. }
  3331. case 'm':
  3332. case 'M':
  3333. {
  3334. if (LocaleCompare("matte",keyword) == 0)
  3335. {
  3336. primitive_type=MattePrimitive;
  3337. break;
  3338. }
  3339. status=MagickFalse;
  3340. break;
  3341. }
  3342. case 'o':
  3343. case 'O':
  3344. {
  3345. if (LocaleCompare("opacity",keyword) == 0)
  3346. {
  3347. GetMagickToken(q,&q,token);
  3348. (void) FormatMagickString(message,MaxTextExtent,"opacity %s ",
  3349. token);
  3350. (void) WriteBlobString(image,message);
  3351. break;
  3352. }
  3353. status=MagickFalse;
  3354. break;
  3355. }
  3356. case 'p':
  3357. case 'P':
  3358. {
  3359. if (LocaleCompare("path",keyword) == 0)
  3360. {
  3361. primitive_type=PathPrimitive;
  3362. break;
  3363. }
  3364. if (LocaleCompare("point",keyword) == 0)
  3365. {
  3366. primitive_type=PointPrimitive;
  3367. break;
  3368. }
  3369. if (LocaleCompare("polyline",keyword) == 0)
  3370. {
  3371. primitive_type=PolylinePrimitive;
  3372. break;
  3373. }
  3374. if (LocaleCompare("polygon",keyword) == 0)
  3375. {
  3376. primitive_type=PolygonPrimitive;
  3377. break;
  3378. }
  3379. if (LocaleCompare("pop",keyword) == 0)
  3380. {
  3381. GetMagickToken(q,&q,token);
  3382. if (LocaleCompare("clip-path",token) == 0)
  3383. {
  3384. (void) WriteBlobString(image,"</clipPath>\n");
  3385. break;
  3386. }
  3387. if (LocaleCompare("defs",token) == 0)
  3388. {
  3389. (void) WriteBlobString(image,"</defs>\n");
  3390. break;
  3391. }
  3392. if (LocaleCompare("gradient",token) == 0)
  3393. {
  3394. (void) FormatMagickString(message,MaxTextExtent,
  3395. "</%sGradient>\n",type);
  3396. (void) WriteBlobString(image,message);
  3397. break;
  3398. }
  3399. if (LocaleCompare("graphic-context",token) == 0)
  3400. {
  3401. n--;
  3402. if (n < 0)
  3403. ThrowWriterException(DrawError,
  3404. "UnbalancedGraphicContextPushPop");
  3405. (void) WriteBlobString(image,"</g>\n");
  3406. }
  3407. if (LocaleCompare("pattern",token) == 0)
  3408. {
  3409. (void) WriteBlobString(image,"</pattern>\n");
  3410. break;
  3411. }
  3412. if (LocaleCompare("defs",token) == 0)
  3413. (void) WriteBlobString(image,"</g>\n");
  3414. break;
  3415. }
  3416. if (LocaleCompare("push",keyword) == 0)
  3417. {
  3418. GetMagickToken(q,&q,token);
  3419. if (LocaleCompare("clip-path",token) == 0)
  3420. {
  3421. GetMagickToken(q,&q,token);
  3422. (void) FormatMagickString(message,MaxTextExtent,
  3423. "<clipPath id=\"%s\">\n",token);
  3424. (void) WriteBlobString(image,message);
  3425. break;
  3426. }
  3427. if (LocaleCompare("defs",token) == 0)
  3428. {
  3429. (void) WriteBlobString(image,"<defs>\n");
  3430. break;
  3431. }
  3432. if (LocaleCompare("gradient",token) == 0)
  3433. {
  3434. GetMagickToken(q,&q,token);
  3435. (void) CopyMagickString(name,token,MaxTextExtent);
  3436. GetMagickToken(q,&q,token);
  3437. (void) CopyMagickString(type,token,MaxTextExtent);
  3438. GetMagickToken(q,&q,token);
  3439. svg_info.segment.x1=atof(token);
  3440. svg_info.element.cx=atof(token);
  3441. GetMagickToken(q,&q,token);
  3442. if (*token == ',')
  3443. GetMagickToken(q,&q,token);
  3444. svg_info.segment.y1=atof(token);
  3445. svg_info.element.cy=atof(token);
  3446. GetMagickToken(q,&q,token);
  3447. if (*token == ',')
  3448. GetMagickToken(q,&q,token);
  3449. svg_info.segment.x2=atof(token);
  3450. svg_info.element.major=atof(token);
  3451. GetMagickToken(q,&q,token);
  3452. if (*token == ',')
  3453. GetMagickToken(q,&q,token);
  3454. svg_info.segment.y2=atof(token);
  3455. svg_info.element.minor=atof(token);
  3456. (void) FormatMagickString(message,MaxTextExtent,
  3457. "<%sGradient id=\"%s\" x1=\"%g\" y1=\"%g\" x2=\"%g\" "
  3458. "y2=\"%g\">\n",type,name,svg_info.segment.x1,
  3459. svg_info.segment.y1,svg_info.segment.x2,svg_info.segment.y2);
  3460. if (LocaleCompare(type,"radial") == 0)
  3461. {
  3462. GetMagickToken(q,&q,token);
  3463. if (*token == ',')
  3464. GetMagickToken(q,&q,token);
  3465. svg_info.element.angle=atof(token);
  3466. (void) FormatMagickString(message,MaxTextExtent,
  3467. "<%sGradient id=\"%s\" cx=\"%g\" cy=\"%g\" r=\"%g\" "
  3468. "fx=\"%g\" fy=\"%g\">\n",type,name,svg_info.element.cx,
  3469. svg_info.element.cy,svg_info.element.angle,
  3470. svg_info.element.major,svg_info.element.minor);
  3471. }
  3472. (void) WriteBlobString(image,message);
  3473. break;
  3474. }
  3475. if (LocaleCompare("graphic-context",token) == 0)
  3476. {
  3477. n++;
  3478. if (active)
  3479. {
  3480. AffineToTransform(image,&affine);
  3481. active=MagickFalse;
  3482. }
  3483. (void) WriteBlobString(image,"<g style=\"");
  3484. active=MagickTrue;
  3485. }
  3486. if (LocaleCompare("pattern",token) == 0)
  3487. {
  3488. GetMagickToken(q,&q,token);
  3489. (void) CopyMagickString(name,token,MaxTextExtent);
  3490. GetMagickToken(q,&q,token);
  3491. svg_info.bounds.x=atof(token);
  3492. GetMagickToken(q,&q,token);
  3493. if (*token == ',')
  3494. GetMagickToken(q,&q,token);
  3495. svg_info.bounds.y=atof(token);
  3496. GetMagickToken(q,&q,token);
  3497. if (*token == ',')
  3498. GetMagickToken(q,&q,token);
  3499. svg_info.bounds.width=atof(token);
  3500. GetMagickToken(q,&q,token);
  3501. if (*token == ',')
  3502. GetMagickToken(q,&q,token);
  3503. svg_info.bounds.height=atof(token);
  3504. (void) FormatMagickString(message,MaxTextExtent,
  3505. "<pattern id=\"%s\" x=\"%g\" y=\"%g\" width=\"%g\" "
  3506. "height=\"%g\">\n",name,svg_info.bounds.x,svg_info.bounds.y,
  3507. svg_info.bounds.width,svg_info.bounds.height);
  3508. (void) WriteBlobString(image,message);
  3509. break;
  3510. }
  3511. break;
  3512. }
  3513. status=MagickFalse;
  3514. break;
  3515. }
  3516. case 'r':
  3517. case 'R':
  3518. {
  3519. if (LocaleCompare("rectangle",keyword) == 0)
  3520. {
  3521. primitive_type=RectanglePrimitive;
  3522. break;
  3523. }
  3524. if (LocaleCompare("roundRectangle",keyword) == 0)
  3525. {
  3526. primitive_type=RoundRectanglePrimitive;
  3527. break;
  3528. }
  3529. if (LocaleCompare("rotate",keyword) == 0)
  3530. {
  3531. GetMagickToken(q,&q,token);
  3532. (void) FormatMagickString(message,MaxTextExtent,"rotate(%s) ",
  3533. token);
  3534. (void) WriteBlobString(image,message);
  3535. break;
  3536. }
  3537. status=MagickFalse;
  3538. break;
  3539. }
  3540. case 's':
  3541. case 'S':
  3542. {
  3543. if (LocaleCompare("scale",keyword) == 0)
  3544. {
  3545. GetMagickToken(q,&q,token);
  3546. affine.sx=atof(token);
  3547. GetMagickToken(q,&q,token);
  3548. if (*token == ',')
  3549. GetMagickToken(q,&q,token);
  3550. affine.sy=atof(token);
  3551. break;
  3552. }
  3553. if (LocaleCompare("skewX",keyword) == 0)
  3554. {
  3555. GetMagickToken(q,&q,token);
  3556. (void) FormatMagickString(message,MaxTextExtent,"skewX(%s) ",
  3557. token);
  3558. (void) WriteBlobString(image,message);
  3559. break;
  3560. }
  3561. if (LocaleCompare("skewY",keyword) == 0)
  3562. {
  3563. GetMagickToken(q,&q,token);
  3564. (void) FormatMagickString(message,MaxTextExtent,"skewY(%s) ",
  3565. token);
  3566. (void) WriteBlobString(image,message);
  3567. break;
  3568. }
  3569. if (LocaleCompare("stop-color",keyword) == 0)
  3570. {
  3571. char
  3572. color[MaxTextExtent];
  3573. GetMagickToken(q,&q,token);
  3574. (void) CopyMagickString(color,token,MaxTextExtent);
  3575. GetMagickToken(q,&q,token);
  3576. (void) FormatMagickString(message,MaxTextExtent,
  3577. " <stop offset=\"%s\" stop-color=\"%s\" />\n",token,color);
  3578. (void) WriteBlobString(image,message);
  3579. break;
  3580. }
  3581. if (LocaleCompare("stroke",keyword) == 0)
  3582. {
  3583. GetMagickToken(q,&q,token);
  3584. (void) FormatMagickString(message,MaxTextExtent,"stroke:%s;",
  3585. token);
  3586. (void) WriteBlobString(image,message);
  3587. break;
  3588. }
  3589. if (LocaleCompare("stroke-antialias",keyword) == 0)
  3590. {
  3591. GetMagickToken(q,&q,token);
  3592. (void) FormatMagickString(message,MaxTextExtent,
  3593. "stroke-antialias:%s;",token);
  3594. (void) WriteBlobString(image,message);
  3595. break;
  3596. }
  3597. if (LocaleCompare("stroke-dasharray",keyword) == 0)
  3598. {
  3599. if (IsPoint(q))
  3600. {
  3601. long
  3602. k;
  3603. p=q;
  3604. GetMagickToken(p,&p,token);
  3605. for (k=0; IsPoint(token); k++)
  3606. GetMagickToken(p,&p,token);
  3607. (void) WriteBlobString(image,"stroke-dasharray:");
  3608. for (j=0; j < k; j++)
  3609. {
  3610. GetMagickToken(q,&q,token);
  3611. (void) FormatMagickString(message,MaxTextExtent,"%s ",
  3612. token);
  3613. (void) WriteBlobString(image,message);
  3614. }
  3615. (void) WriteBlobString(image,";");
  3616. break;
  3617. }
  3618. GetMagickToken(q,&q,token);
  3619. (void) FormatMagickString(message,MaxTextExtent,
  3620. "stroke-dasharray:%s;",token);
  3621. (void) WriteBlobString(image,message);
  3622. break;
  3623. }
  3624. if (LocaleCompare("stroke-dashoffset",keyword) == 0)
  3625. {
  3626. GetMagickToken(q,&q,token);
  3627. (void) FormatMagickString(message,MaxTextExtent,
  3628. "stroke-dashoffset:%s;",token);
  3629. (void) WriteBlobString(image,message);
  3630. break;
  3631. }
  3632. if (LocaleCompare("stroke-linecap",keyword) == 0)
  3633. {
  3634. GetMagickToken(q,&q,token);
  3635. (void) FormatMagickString(message,MaxTextExtent,
  3636. "stroke-linecap:%s;",token);
  3637. (void) WriteBlobString(image,message);
  3638. break;
  3639. }
  3640. if (LocaleCompare("stroke-linejoin",keyword) == 0)
  3641. {
  3642. GetMagickToken(q,&q,token);
  3643. (void) FormatMagickString(message,MaxTextExtent,
  3644. "stroke-linejoin:%s;",token);
  3645. (void) WriteBlobString(image,message);
  3646. break;
  3647. }
  3648. if (LocaleCompare("stroke-miterlimit",keyword) == 0)
  3649. {
  3650. GetMagickToken(q,&q,token);
  3651. (void) FormatMagickString(message,MaxTextExtent,
  3652. "stroke-miterlimit:%s;",token);
  3653. (void) WriteBlobString(image,message);
  3654. break;
  3655. }
  3656. if (LocaleCompare("stroke-opacity",keyword) == 0)
  3657. {
  3658. GetMagickToken(q,&q,token);
  3659. (void) FormatMagickString(message,MaxTextExtent,
  3660. "stroke-opacity:%s;",token);
  3661. (void) WriteBlobString(image,message);
  3662. break;
  3663. }
  3664. if (LocaleCompare("stroke-width",keyword) == 0)
  3665. {
  3666. GetMagickToken(q,&q,token);
  3667. (void) FormatMagickString(message,MaxTextExtent,
  3668. "stroke-width:%s;",token);
  3669. (void) WriteBlobString(image,message);
  3670. continue;
  3671. }
  3672. status=MagickFalse;
  3673. break;
  3674. }
  3675. case 't':
  3676. case 'T':
  3677. {
  3678. if (LocaleCompare("text",keyword) == 0)
  3679. {
  3680. primitive_type=TextPrimitive;
  3681. break;
  3682. }
  3683. if (LocaleCompare("text-antialias",keyword) == 0)
  3684. {
  3685. GetMagickToken(q,&q,token);
  3686. (void) FormatMagickString(message,MaxTextExtent,
  3687. "text-antialias:%s;",token);
  3688. (void) WriteBlobString(image,message);
  3689. break;
  3690. }
  3691. if (LocaleCompare("tspan",keyword) == 0)
  3692. {
  3693. primitive_type=TextPrimitive;
  3694. break;
  3695. }
  3696. if (LocaleCompare("translate",keyword) == 0)
  3697. {
  3698. GetMagickToken(q,&q,token);
  3699. affine.tx=atof(token);
  3700. GetMagickToken(q,&q,token);
  3701. if (*token == ',')
  3702. GetMagickToken(q,&q,token);
  3703. affine.ty=atof(token);
  3704. break;
  3705. }
  3706. status=MagickFalse;
  3707. break;
  3708. }
  3709. case 'v':
  3710. case 'V':
  3711. {
  3712. if (LocaleCompare("viewbox",keyword) == 0)
  3713. {
  3714. GetMagickToken(q,&q,token);
  3715. if (*token == ',')
  3716. GetMagickToken(q,&q,token);
  3717. GetMagickToken(q,&q,token);
  3718. if (*token == ',')
  3719. GetMagickToken(q,&q,token);
  3720. GetMagickToken(q,&q,token);
  3721. if (*token == ',')
  3722. GetMagickToken(q,&q,token);
  3723. GetMagickToken(q,&q,token);
  3724. break;
  3725. }
  3726. status=MagickFalse;
  3727. break;
  3728. }
  3729. default:
  3730. {
  3731. status=MagickFalse;
  3732. break;
  3733. }
  3734. }
  3735. if (status == MagickFalse)
  3736. break;
  3737. if (primitive_type == UndefinedPrimitive)
  3738. continue;
  3739. /*
  3740. Parse the primitive attributes.
  3741. */
  3742. i=0;
  3743. j=0;
  3744. for (x=0; *q != '\0'; x++)
  3745. {
  3746. /*
  3747. Define points.
  3748. */
  3749. if (IsPoint(q) == MagickFalse)
  3750. break;
  3751. GetMagickToken(q,&q,token);
  3752. point.x=atof(token);
  3753. GetMagickToken(q,&q,token);
  3754. if (*token == ',')
  3755. GetMagickToken(q,&q,token);
  3756. point.y=atof(token);
  3757. GetMagickToken(q,(char **) NULL,token);
  3758. if (*token == ',')
  3759. GetMagickToken(q,&q,token);
  3760. primitive_info[i].primitive=primitive_type;
  3761. primitive_info[i].point=point;
  3762. primitive_info[i].coordinates=0;
  3763. primitive_info[i].method=FloodfillMethod;
  3764. i++;
  3765. if (i < (long) (number_points-6*BezierQuantum-360))
  3766. continue;
  3767. number_points+=6*BezierQuantum+360;
  3768. primitive_info=(PrimitiveInfo *) ResizeMagickMemory(primitive_info,
  3769. number_points*sizeof(*primitive_info));
  3770. if (primitive_info == (PrimitiveInfo *) NULL)
  3771. {
  3772. (void) ThrowMagickException(&image->exception,GetMagickModule(),
  3773. ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
  3774. break;
  3775. }
  3776. }
  3777. primitive_info[j].primitive=primitive_type;
  3778. primitive_info[j].coordinates=x;
  3779. primitive_info[j].method=FloodfillMethod;
  3780. primitive_info[j].text=(char *) NULL;
  3781. if (active)
  3782. {
  3783. AffineToTransform(image,&affine);
  3784. active=MagickFalse;
  3785. }
  3786. active=MagickFalse;
  3787. switch (primitive_type)
  3788. {
  3789. case PointPrimitive:
  3790. default:
  3791. {
  3792. if (primitive_info[j].coordinates != 1)
  3793. {
  3794. status=MagickFalse;
  3795. break;
  3796. }
  3797. break;
  3798. }
  3799. case LinePrimitive:
  3800. {
  3801. if (primitive_info[j].coordinates != 2)
  3802. {
  3803. status=MagickFalse;
  3804. break;
  3805. }
  3806. (void) FormatMagickString(message,MaxTextExtent,
  3807. " <line x1=\"%g\" y1=\"%g\" x2=\"%g\" y2=\"%g\"/>\n",
  3808. primitive_info[j].point.x,primitive_info[j].point.y,
  3809. primitive_info[j+1].point.x,primitive_info[j+1].point.y);
  3810. (void) WriteBlobString(image,message);
  3811. break;
  3812. }
  3813. case RectanglePrimitive:
  3814. {
  3815. if (primitive_info[j].coordinates != 2)
  3816. {
  3817. status=MagickFalse;
  3818. break;
  3819. }
  3820. (void) FormatMagickString(message,MaxTextExtent,
  3821. " <rect x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\"/>\n",
  3822. primitive_info[j].point.x,primitive_info[j].point.y,
  3823. primitive_info[j+1].point.x-primitive_info[j].point.x,
  3824. primitive_info[j+1].point.y-primitive_info[j].point.y);
  3825. (void) WriteBlobString(image,message);
  3826. break;
  3827. }
  3828. case RoundRectanglePrimitive:
  3829. {
  3830. if (primitive_info[j].coordinates != 3)
  3831. {
  3832. status=MagickFalse;
  3833. break;
  3834. }
  3835. (void) FormatMagickString(message,MaxTextExtent,
  3836. " <rect x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" rx=\"%g\" "
  3837. "ry=\"%g\"/>\n",primitive_info[j].point.x,primitive_info[j].point.y,
  3838. primitive_info[j+1].point.x-primitive_info[j].point.x,
  3839. primitive_info[j+1].point.y-primitive_info[j].point.y,
  3840. primitive_info[j+2].point.x,primitive_info[j+2].point.y);
  3841. (void) WriteBlobString(image,message);
  3842. break;
  3843. }
  3844. case ArcPrimitive:
  3845. {
  3846. if (primitive_info[j].coordinates != 3)
  3847. {
  3848. status=MagickFalse;
  3849. break;
  3850. }
  3851. break;
  3852. }
  3853. case EllipsePrimitive:
  3854. {
  3855. if (primitive_info[j].coordinates != 3)
  3856. {
  3857. status=MagickFalse;
  3858. break;
  3859. }
  3860. (void) FormatMagickString(message,MaxTextExtent,
  3861. " <ellipse cx=\"%g\" cy=\"%g\" rx=\"%g\" ry=\"%g\"/>\n",
  3862. primitive_info[j].point.x,primitive_info[j].point.y,
  3863. primitive_info[j+1].point.x,primitive_info[j+1].point.y);
  3864. (void) WriteBlobString(image,message);
  3865. break;
  3866. }
  3867. case CirclePrimitive:
  3868. {
  3869. double
  3870. alpha,
  3871. beta;
  3872. if (primitive_info[j].coordinates != 2)
  3873. {
  3874. status=MagickFalse;
  3875. break;
  3876. }
  3877. alpha=primitive_info[j+1].point.x-primitive_info[j].point.x;
  3878. beta=primitive_info[j+1].point.y-primitive_info[j].point.y;
  3879. (void) FormatMagickString(message,MaxTextExtent,
  3880. " <circle cx=\"%g\" cy=\"%g\" r=\"%g\"/>\n",
  3881. primitive_info[j].point.x,primitive_info[j].point.y,
  3882. hypot(alpha,beta));
  3883. (void) WriteBlobString(image,message);
  3884. break;
  3885. }
  3886. case PolylinePrimitive:
  3887. {
  3888. if (primitive_info[j].coordinates < 2)
  3889. {
  3890. status=MagickFalse;
  3891. break;
  3892. }
  3893. (void) CopyMagickString(message," <polyline points=\"",MaxTextExtent);
  3894. (void) WriteBlobString(image,message);
  3895. length=strlen(message);
  3896. for ( ; j < i; j++)
  3897. {
  3898. (void) FormatMagickString(message,MaxTextExtent,"%g,%g ",
  3899. primitive_info[j].point.x,primitive_info[j].point.y);
  3900. length+=strlen(message);
  3901. if (length >= 80)
  3902. {
  3903. (void) WriteBlobString(image,"\n ");
  3904. length=strlen(message)+5;
  3905. }
  3906. (void) WriteBlobString(image,message);
  3907. }
  3908. (void) WriteBlobString(image,"\"/>\n");
  3909. break;
  3910. }
  3911. case PolygonPrimitive:
  3912. {
  3913. if (primitive_info[j].coordinates < 3)
  3914. {
  3915. status=MagickFalse;
  3916. break;
  3917. }
  3918. primitive_info[i]=primitive_info[j];
  3919. primitive_info[i].coordinates=0;
  3920. primitive_info[j].coordinates++;
  3921. i++;
  3922. (void) CopyMagickString(message," <polygon points=\"",MaxTextExtent);
  3923. (void) WriteBlobString(image,message);
  3924. length=strlen(message);
  3925. for ( ; j < i; j++)
  3926. {
  3927. (void) FormatMagickString(message,MaxTextExtent,"%g,%g ",
  3928. primitive_info[j].point.x,primitive_info[j].point.y);
  3929. length+=strlen(message);
  3930. if (length >= 80)
  3931. {
  3932. (void) WriteBlobString(image,"\n ");
  3933. length=strlen(message)+5;
  3934. }
  3935. (void) WriteBlobString(image,message);
  3936. }
  3937. (void) WriteBlobString(image,"\"/>\n");
  3938. break;
  3939. }
  3940. case BezierPrimitive:
  3941. {
  3942. if (primitive_info[j].coordinates < 3)
  3943. {
  3944. status=MagickFalse;
  3945. break;
  3946. }
  3947. break;
  3948. }
  3949. case PathPrimitive:
  3950. {
  3951. int
  3952. number_attributes;
  3953. GetMagickToken(q,&q,token);
  3954. number_attributes=1;
  3955. for (p=token; *p != '\0'; p++)
  3956. if (isalpha((int) *p))
  3957. number_attributes++;
  3958. if (i > (long) (number_points-6*BezierQuantum*number_attributes-1))
  3959. {
  3960. number_points+=6*BezierQuantum*number_attributes;
  3961. primitive_info=(PrimitiveInfo *) ResizeMagickMemory(primitive_info,
  3962. number_points*sizeof(*primitive_info));
  3963. if (primitive_info == (PrimitiveInfo *) NULL)
  3964. {
  3965. (void) ThrowMagickException(&image->exception,GetMagickModule(),
  3966. ResourceLimitError,"MemoryAllocationFailed","`%s'",
  3967. image->filename);
  3968. break;
  3969. }
  3970. }
  3971. (void) WriteBlobString(image," <path d=\"");
  3972. (void) WriteBlobString(image,token);
  3973. (void) WriteBlobString(image,"\"/>\n");
  3974. break;
  3975. }
  3976. case ColorPrimitive:
  3977. case MattePrimitive:
  3978. {
  3979. if (primitive_info[j].coordinates != 1)
  3980. {
  3981. status=MagickFalse;
  3982. break;
  3983. }
  3984. GetMagickToken(q,&q,token);
  3985. if (LocaleCompare("point",token) == 0)
  3986. primitive_info[j].method=PointMethod;
  3987. if (LocaleCompare("replace",token) == 0)
  3988. primitive_info[j].method=ReplaceMethod;
  3989. if (LocaleCompare("floodfill",token) == 0)
  3990. primitive_info[j].method=FloodfillMethod;
  3991. if (LocaleCompare("filltoborder",token) == 0)
  3992. primitive_info[j].method=FillToBorderMethod;
  3993. if (LocaleCompare("reset",token) == 0)
  3994. primitive_info[j].method=ResetMethod;
  3995. break;
  3996. }
  3997. case TextPrimitive:
  3998. {
  3999. register char
  4000. *p;
  4001. if (primitive_info[j].coordinates != 1)
  4002. {
  4003. status=MagickFalse;
  4004. break;
  4005. }
  4006. GetMagickToken(q,&q,token);
  4007. (void) FormatMagickString(message,MaxTextExtent,
  4008. " <text x=\"%g\" y=\"%g\">",primitive_info[j].point.x,
  4009. primitive_info[j].point.y);
  4010. (void) WriteBlobString(image,message);
  4011. for (p=token; *p != '\0'; p++)
  4012. switch (*p)
  4013. {
  4014. case '<': (void) WriteBlobString(image,"&lt;"); break;
  4015. case '>': (void) WriteBlobString(image,"&gt;"); break;
  4016. case '&': (void) WriteBlobString(image,"&amp;"); break;
  4017. default: (void) WriteBlobByte(image,*p); break;
  4018. }
  4019. (void) WriteBlobString(image,"</text>\n");
  4020. break;
  4021. }
  4022. case ImagePrimitive:
  4023. {
  4024. if (primitive_info[j].coordinates != 2)
  4025. {
  4026. status=MagickFalse;
  4027. break;
  4028. }
  4029. GetMagickToken(q,&q,token);
  4030. (void) FormatMagickString(message,MaxTextExtent,
  4031. " <image x=\"%g\" y=\"%g\" width=\"%g\" height=\"%g\" "
  4032. "xlink:href=\"%s\"/>\n",primitive_info[j].point.x,
  4033. primitive_info[j].point.y,primitive_info[j+1].point.x,
  4034. primitive_info[j+1].point.y,token);
  4035. (void) WriteBlobString(image,message);
  4036. break;
  4037. }
  4038. }
  4039. if (primitive_info == (PrimitiveInfo *) NULL)
  4040. break;
  4041. primitive_info[i].primitive=UndefinedPrimitive;
  4042. if (status == MagickFalse)
  4043. break;
  4044. }
  4045. (void) WriteBlobString(image,"</svg>\n");
  4046. /*
  4047. Free resources.
  4048. */
  4049. token=(char *) RelinquishMagickMemory(token);
  4050. if (primitive_info != (PrimitiveInfo *) NULL)
  4051. primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
  4052. CloseBlob(image);
  4053. return(status);
  4054. }
  4055. #endif