| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.3 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 4 |
// |
| 5 |
// Permission to copy, use, modify, sell and distribute this software |
| 6 |
// is granted provided this copyright notice appears in all copies. |
| 7 |
// This software is provided "as is" without express or implied |
| 8 |
// warranty, and with no claim as to its suitability for any purpose. |
| 9 |
// |
| 10 |
//---------------------------------------------------------------------------- |
| 11 |
// Contact: [email protected] |
| 12 |
// [email protected] |
| 13 |
// http://www.antigrain.com |
| 14 |
//---------------------------------------------------------------------------- |
| 15 |
// |
| 16 |
// SVG path tokenizer. |
| 17 |
// |
| 18 |
//---------------------------------------------------------------------------- |
| 19 |
#ifndef AGG_SVG_PATH_TOKENIZER_INCLUDED |
| 20 |
#define AGG_SVG_PATH_TOKENIZER_INCLUDED |
| 21 |
|
| 22 |
#include "agg_svg_exception.h" |
| 23 |
|
| 24 |
namespace agg |
| 25 |
{ |
| 26 |
namespace svg |
| 27 |
{ |
| 28 |
// SVG path tokenizer. |
| 29 |
// Example: |
| 30 |
// |
| 31 |
// agg::svg::path_tokenizer tok; |
| 32 |
// |
| 33 |
// tok.set_str("M-122.304 84.285L-122.304 84.285 122.203 86.179 "); |
| 34 |
// while(tok.next()) |
| 35 |
// { |
| 36 |
// printf("command='%c' number=%f\n", |
| 37 |
// tok.last_command(), |
| 38 |
// tok.last_number()); |
| 39 |
// } |
| 40 |
// |
| 41 |
// The tokenizer does all the routine job of parsing the SVG paths. |
| 42 |
// It doesn't recognize any graphical primitives, it even doesn't know |
| 43 |
// anything about pairs of coordinates (X,Y). The purpose of this class |
| 44 |
// is to tokenize the numeric values and commands. SVG paths can |
| 45 |
// have single numeric values for Horizontal or Vertical line_to commands |
| 46 |
// as well as more than two coordinates (4 or 6) for Bezier curves |
| 47 |
// depending on the semantics of the command. |
| 48 |
// The behaviour is as follows: |
| 49 |
// |
| 50 |
// Each call to next() returns true if there's new command or new numeric |
| 51 |
// value or false when the path ends. How to interpret the result |
| 52 |
// depends on the sematics of the command. For example, command "C" |
| 53 |
// (cubic Bezier curve) implies 6 floating point numbers preceded by this |
| 54 |
// command. If the command assumes no arguments (like z or Z) the |
| 55 |
// the last_number() values won't change, that is, last_number() always |
| 56 |
// returns the last recognized numeric value, so does last_command(). |
| 57 |
//=============================================================== |
| 58 |
class path_tokenizer |
| 59 |
{ |
| 60 |
public: |
| 61 |
path_tokenizer(); |
| 62 |
|
| 63 |
void set_path_str(const char* str); |
| 64 |
bool next(); |
| 65 |
|
| 66 |
double next(char cmd); |
| 67 |
|
| 68 |
char last_command() const { return m_last_command; } |
| 69 |
double last_number() const { return m_last_number; } |
| 70 |
|
| 71 |
|
| 72 |
private: |
| 73 |
static void init_char_mask(char* mask, const char* char_set); |
| 74 |
|
| 75 |
bool contains(const char* mask, unsigned c) const |
| 76 |
{ |
| 77 |
return (mask[(c >> 3) & (256/8-1)] & (1 << (c & 7))) != 0; |
| 78 |
} |
| 79 |
|
| 80 |
bool is_command(unsigned c) const |
| 81 |
{ |
| 82 |
return contains(m_commands_mask, c); |
| 83 |
} |
| 84 |
|
| 85 |
bool is_numeric(unsigned c) const |
| 86 |
{ |
| 87 |
return contains(m_numeric_mask, c); |
| 88 |
} |
| 89 |
|
| 90 |
bool is_separator(unsigned c) const |
| 91 |
{ |
| 92 |
return contains(m_separators_mask, c); |
| 93 |
} |
| 94 |
|
| 95 |
bool parse_number(); |
| 96 |
|
| 97 |
char m_separators_mask[256/8]; |
| 98 |
char m_commands_mask[256/8]; |
| 99 |
char m_numeric_mask[256/8]; |
| 100 |
|
| 101 |
const char* m_path; |
| 102 |
double m_last_number; |
| 103 |
char m_last_command; |
| 104 |
|
| 105 |
static const char s_commands[]; |
| 106 |
static const char s_numeric[]; |
| 107 |
static const char s_separators[]; |
| 108 |
}; |
| 109 |
|
| 110 |
} //namespace svg |
| 111 |
} //namespace agg |
| 112 |
|
| 113 |
|
| 114 |
#endif |