Package bar :: Module svgfix
[hide private]
[frames] | no frames]

Module svgfix

Module or simpylying input SVG file structure. In many converters SVG files are created with preserving groups of objects. It results in generating lot of g objets. Each of nested g may have own transformation matrix. What is more, most nested object (text, path, line, etc. may have its own transformation matrix defined). Nesting all those transformation causes lot of confusion and dissalows extracting filnal coordinates of objects in a direct way.

This modlule applies all nested transfomarions and leaves objects with their final coordinates allowing further modules direct extraction of coordinates and dimensions.


Note: Only absolute coordinates (capital letters in segmants names) are parsed properly.

Import Graph
Import Graph

Currently only translate and scale transformations are supprted.

re_trd - transformations dictionary:

Usage exapmle:

>>> import svgfix
>>> _dev_fix_img(dom_svg_object)

See Also: http://www.w3.org/TR/SVG/coords.html#NestedTransformations

Functions [hide private]
 
getTransformMatrix(tr)
Returns: NumPy transformation matrix equivalent to provided transformation string.
 
__defineTransformationMatrix(tr_type, v)
Returns: Transformation matrix for given elementary transformation string.
 
__fixElement(el, gm)
Returns: nothing - only element el is modified.
 
__fixLine(el, cm)
Returns: nothing - only element el is modified.
 
__fixText(el, cm)
Returns: nothing - only element el is modified.
 
__fixPolygon(el, cm)
Returns: nothing - only element el is modified.
 
__fixPolyline(el, cm)
Returns: nothing - only element el is modified.
 
__fixPath(el, cm)
Returns: nothing - only element el is modified.
 
__fixPathDefinition(pathDefinition, cm)
Returns: path difinition transformed using cm matrix
 
__transformPoint(point, matrix)
Returns: List of transformated coordinates (floats): [x',y']
 
__fixHeader(svgdom, pagenumber)
Returns: nothing, svgdom is modified in-place.
 
fixPathDefinition(pathDefinition, cm)
Returns: path difinition transformed using cm matrix.
 
transformPoint(point, matrix)
Returns: List of transformated coordinates (floats): [x',y']
 
fixSvgImage(svgdoc, pagenumber=None, fixHeader=True)
Returns: nothing, svgdom is modified in-place.
Variables [hide private]
  __package__ = 'bar'
Function Details [hide private]

getTransformMatrix(tr)

 
Parameters:
  • tr (string) - Transformation. String extracted from 'transformation' attrubute of SVG element.
Returns:
NumPy transformation matrix equivalent to provided transformation string.

How the function works?

  1. Transformation string is a serie of tanslate or scale commands: eg. 'translate(20,20) scale(.3)' thus we have two sepatate transformations which we need to parse separately and multiply to calculate final matrix.
  2. If an element does not have any transformation, indentity matrix should be returned.

Expamples:

>>> import atlasparser
>>> atlasparser.svgfix.getTransformMatrix("")
array([[ 1.,  0.,  0.],
      [ 0.,  1.,  0.],
      [ 0.,  0.,  1.]])
>>> atlasparser.svgfix.getTransformMatrix("translate(.2,3) scale(.4)")
array([[ 0.4,  0. ,  0.2],
       [ 0. ,  0.4,  3. ],
       [ 0. ,  0. ,  1. ]])

Note: Subsequent transformations should be separated by one or more whitespace chars.

See Also: Details of transforming SVG objects: http://www.w3.org/TR/SVG/coords.html

__defineTransformationMatrix(tr_type, v)

 
Parameters:
  • tr_type (string) - Type of transformation, one of the re_trd dictionary keys.
  • v (tuple) - Tuple of values extracted from transformation string (ammount of translation, scaling factor, matrix elements,...). In other words results of appying regular expression to transformation string.
Returns:
Transformation matrix for given elementary transformation string.

__fixElement(el, gm)

 
Parameters:
  • el (DOM object) - element to be fixed.
  • gm (NumPy array) - Transformation matrix of parrent element (gm - global transformation matrix)
Returns:
nothing - only element el is modified.

Converts coordinates in given element to absolute values depending on element type. Steps:

  1. Check, if given element has any transformation defined. If yes, compute transformation matrix for this element as well as new transformation matix..
  2. Correct coordinates of element depending on its type.
  3. Perform element-dependent corredtion (scale font size, stroke, etc.)

__fixLine(el, cm)

 
Parameters:
  • el (DOM object) - Line SVG element to be modified.
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
nothing - only element el is modified.

Transforms line element of SVG file to final coordinates. Function assumes that element has correctly defined x1,y1 and x2,y2 attributes. If an unhandled excepion is raised, it means that element is defined incorretly.

__fixText(el, cm)

 
Parameters:
  • el (DOM object) - Text SVG element to be modified.
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
nothing - only element el is modified.

Transforms text element of SVG file to final coordinates. Initial coordinates are optional. Function tried to scale font size. If final font size is less than 1px it is forced to be 1px.

__fixPolygon(el, cm)

 
Parameters:
  • el (DOM object) - Polygon SVG element to be modified.
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
nothing - only element el is modified.
Requires:
  • Coordinates in 'points' attrubute has to be separated by whitespaces.
  • Correctly defined coordinated eg. points='2.3,-5.0 34,5'.

    Funtion transforms all points in polygon one by one using provided transformation matrix.

__fixPolyline(el, cm)

 
Parameters:
  • el (DOM object) - Polyline SVG element to be modified.
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
nothing - only element el is modified.
Requires:
  • Coordinates in 'points' attrubute has to be separated by whitespaces.
  • Correctly defined coordinated eg. points='2.3,-5.0 34,5'.

    Funtion transforms all points in polygon one by one using provided transformation matrix.

To Do: In final version all polylines should be broken into lines. It is important because of further line <-> label assingment. Currenty, polylines are not broken into pieces.

__fixPath(el, cm)

 
Parameters:
  • el (DOM object) - Path SVG element to be modified.
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
nothing - only element el is modified.

Requires: Segments of paths has to be separated by whitespace so we split 'd' string by whitespace.

Funtion transforms all points in path one by one using provided transformation matrix.

__fixPathDefinition(pathDefinition, cm)

 
Parameters:
  • pathDefinition (string) - path 'd' attribute value to be modified
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
path difinition transformed using cm matrix

__transformPoint(point, matrix)

 
Parameters:
  • point (list) - List/tuple of two elements from which point coordinates could be extracted. Coordinates may be either strings, integers or floats. Mapping all values to floats is applied before calculations.
  • matrix (NumPy array 3x3) - Transformation matrix to be applied.
Returns:
List of transformated coordinates (floats): [x',y']

Function simply transforms fiven point from one coodrinates system into another defined by transformation matrix.

__fixHeader(svgdom, pagenumber)

 
Parameters:
  • svgdom (DOM object) - Whole SVG document
  • pagenumber (integer) - Number of slide to parse
Returns:
nothing, svgdom is modified in-place.

Function changes viewPort,viewBox and other defined parameters od SVG document in order to correct errors made by converters. Those properties are to be fixed before further operations.

Note: New attributes and their values are defined manually in configuration module. Reason is that they differ among atlases and (esspecially) converters.

fixPathDefinition(pathDefinition, cm)

 
Parameters:
  • pathDefinition (string) - path 'd' attribute value to be modified
  • cm (NumPy array) - Transformation matrix to be applied.
Returns:
path difinition transformed using cm matrix. Alias for __fixPathDefinition for external usage

transformPoint(point, matrix)

 
Parameters:
  • point (list) - List/tuple of two elements from which point coordinates could be extracted. Coordinates may be either strings, integers or floats. Mapping all values to floats is applied before calculations.
  • matrix (NumPy array 3x3) - Transformation matrix to be applied.
Returns:
List of transformated coordinates (floats): [x',y']

Function simply transforms fiven point from one coodrinates system into another defined by transformation matrix.

Alias for __transformPoint created for external usage.

fixSvgImage(svgdoc, pagenumber=None, fixHeader=True)

 
Parameters:
  • svgdoc (DOM object) - Whole SVG document.
  • pagenumber (integer) - Number of slide to parse. If set to none, this parameter would not be used
Returns:
nothing, svgdom is modified in-place. Performs all operations related to fixing SVG file. Function fixes header as well applies all transformations for given SVG file.