svg_transform.py revision 2843dd8f06ff576409c19fe85c8fcacd7181baeb
# -*- coding: utf-8 -*-
# SVG transformation list parser
#
# Copyright 2010 Louis Simard
#
# This file is part of Scour, http://www.codedread.com/scour/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Small recursive descent parser for SVG transform="" data.
In [1]: from svg_transform import svg_transform_parser
In [3]: svg_transform_parser.parse('translate(50, 50)')
Out[3]: [('translate', [50.0, 50.0])]
In [4]: svg_transform_parser.parse('translate(50)')
Out[4]: [('translate', [50.0])]
In [5]: svg_transform_parser.parse('rotate(36 50,50)')
Out[5]: [('rotate', [36.0, 50.0, 50.0])]
In [6]: svg_transform_parser.parse('rotate(36)')
Out[6]: [('rotate', [36.0])]
In [7]: svg_transform_parser.parse('skewX(20)')
Out[7]: [('skewX', [20.0])]
In [8]: svg_transform_parser.parse('skewY(40)')
Out[8]: [('skewX', [20.0])]
In [9]: svg_transform_parser.parse('scale(2 .5)')
Out[9]: [('scale', [2.0, 0.5])]
In [10]: svg_transform_parser.parse('scale(.5)')
Out[10]: [('scale', [0.5])]
In [11]: svg_transform_parser.parse('matrix(1 0 50 0 1 80)')
Out[11]: [('matrix', [1.0, 0.0, 50.0, 0.0, 1.0, 80.0])]
Multiple transformations are supported:
In [12]: svg_transform_parser.parse('translate(30 -30) rotate(36)')
Out[12]: [('translate', [30.0, -30.0]), ('rotate', [36.0])]
"""
import re
from decimal import *
# Sentinel.
return 'EOF'
lexicon = [
('float', r'[-+]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+\.?))(?:[Ee][-+]?[0-9]+)?'),
('int', r'[-+]?[0-9]+'),
('command', r'(?:matrix|translate|scale|rotate|skew[XY])'),
('coordstart', r'\('),
('coordend', r'\)'),
]
""" Break SVG path data into tokens.
The SVG spec requires that tokens are greedy. This lexer relies on Python's
regexes defaulting to greediness.
This style of implementation was inspired by this article:
"""
parts = []
""" Yield (token_type, str_data) tokens.
The last token will be (EOF, None) where EOF is the singleton object
defined in this module.
"""
if m is not None:
yield (name, m)
break
yield (EOF, None)
class SVGTransformationParser(object):
""" Parse SVG transform="" data into a list of commands.
Each distinct command will take the form of a tuple (type, data). The
`type` is the character string that defines the type of transformation in the
transform data, so either of "translate", "rotate", "scale", "matrix",
"skewX" and "skewY". Data is always a list of numbers contained within the
transformation's parentheses.
See the SVG documentation for the interpretation of the individual elements
for each transformation.
The main method is `parse(text)`. It can only consume actual strings, not
filelike objects or iterators.
"""
self.command_dispatch = {
}
# self.number_tokens = set(['int', 'float'])
""" Parse a string of SVG transform="" data.
"""
commands = []
return commands
numbers = []
# 1st number is mandatory
# 2nd number is optional
if number is not None:
# this number is mandatory
numbers = []
# 1st number is mandatory
# 2nd number is optional
if number is not None:
# but, if the 2nd number is provided, the 3rd is mandatory.
# we can't have just 2.
numbers = []
# all numbers are mandatory
for i in xrange(6):
return x, token
return None, token
else:
return x, token