SVGKParser.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. SVGKParser.h
  3. The main parser for SVGKit. All the magic starts here. Either use:
  4. A: +parseSourceUsingDefaultSVGKParser
  5. ...or use:
  6. B: 1. -initWithSource:
  7. B: 2. -addDefaultSVGParserExtensions
  8. B: ...
  9. B: 3. (as many times as you need) -addParserExtension:
  10. B: ...
  11. B: 4. -parseSynchronously
  12. Note that "A" above does ALL the steps in B for you. If you need a custom set of Parser Extensions, you'll need to
  13. do all the steps in B yourself
  14. PARSING
  15. ---
  16. Actual parsing of an SVG is split into three places:
  17. 1. High level, XML parsing: this file (SVGKParser)
  18. 2. Mid level, parsing the structure of a document, and special XML tags: any class that extends "SVGKParserExtension"
  19. 3. Mid level, parsing SVG tags only: SVGKParserSVG (it's an extension that just does base SVG)
  20. 4. Low level, parsing individual tags within a file, and precise co-ordinates: all the "SVG...Element" classes parse themselves
  21. IDEALLY, we'd like to change that to:
  22. 1. High level, XML parsing: this file (SVGKParser)
  23. 2. Mid level, parsing the structure of a document, and special XML tags: any class that extends "SVGKParserExtension"
  24. 3. Mid level, parsing SVG tags only, but also handling all the different tags: SVGKParserSVG
  25. 4. Lowest level, parsing co-ordinate lists, numbers, strings: yacc/lex parser (in an unnamed class that hasn't been written yet)
  26. */
  27. #import <Foundation/Foundation.h>
  28. #import "SVGKSource.h"
  29. #import "SVGKParserExtension.h"
  30. #import "SVGKParseResult.h"
  31. #import "SVGElement.h"
  32. /*! RECOMMENDED: leave this set to 1 to get warnings about "legal, but poorly written" SVG */
  33. #define PARSER_WARN_FOR_ANONYMOUS_SVG_G_TAGS 1
  34. /*! Verbose parser logging - ONLY needed if you have an SVG file that's failing to load / crashing */
  35. #define DEBUG_VERBOSE_LOG_EVERY_TAG 0
  36. #define DEBUG_XML_PARSER 0
  37. @interface SVGKParser : NSObject {
  38. @private
  39. NSMutableString *_storedChars;
  40. //NSMutableArray *_elementStack;
  41. NSMutableArray * _stackOfParserExtensions;
  42. Node * _parentOfCurrentNode;
  43. }
  44. @property(nonatomic,strong,readonly) SVGKSource* source;
  45. @property(nonatomic,strong,readonly) NSMutableArray* externalStylesheets;
  46. @property(nonatomic,strong,readonly) SVGKParseResult* currentParseRun;
  47. @property(nonatomic,strong) NSMutableArray* parserExtensions;
  48. @property(nonatomic,strong) NSMutableDictionary* parserKnownNamespaces; /**< maps "uri" to "array of parser-extensions" */
  49. #pragma mark - NEW
  50. /**
  51. If you kept the SVGKParser instance when you started a parse, you can
  52. hand that instance to another thread and the OTHER thread can trigger
  53. a cancel.
  54. It is not instantaneous, but kicks in as soon as more data is read from
  55. the raw bytes-stream, so it's pretty quick
  56. */
  57. +(void) cancelParser:(SVGKParser*) parserToCancel;
  58. /**
  59. Creates an SVGKParser, and adds the "standard" extensions for parsing
  60. a standard SVG file; you can then add any of your own custom extensions
  61. before triggering the parse with e.g. "parseSynchronously"
  62. */
  63. +(SVGKParser *) newParserWithDefaultSVGKParserExtensions:(SVGKSource *)source;
  64. /**
  65. Delegates to [self newParserWithDefaultSVGKParserExtensions:], and then auto-starts
  66. the parse SYNCHRONOUSLY (may take anything from 0.001 seconds up to 30+ seconds
  67. for a huge SVG file).
  68. Returns the fully-parsed result, including any errors
  69. */
  70. + (SVGKParseResult*) parseSourceUsingDefaultSVGKParser:(SVGKSource*) source;
  71. /**
  72. This MIGHT now be safe to call multiple times on different threads
  73. (NB: the only reason it wasn't safe before was major bugs in libxml
  74. that break libxml in horrible ways, see the source code to this class
  75. for more info)
  76. */
  77. - (SVGKParseResult*) parseSynchronously;
  78. +(NSDictionary *) NSDictionaryFromCSSAttributes: (Attr*) styleAttribute;
  79. #pragma mark - OLD - POTENTIALLY DELETE THESE ONCE THEY'VE ALL BEEN CHECKED AND CONVERTED
  80. - (id)initWithSource:(SVGKSource *)doc;
  81. /*! Adds the default SVG-tag parsers (everything in the SVG namespace); you should always use these, unless you
  82. are massively customizing SVGKit's parser! */
  83. -(void) addDefaultSVGParserExtensions;
  84. /*! NB: you ALMOST ALWAYS want to first call "addDefaultSVGParserExtensions" */
  85. - (void) addParserExtension:(NSObject<SVGKParserExtension>*) extension;
  86. @end