inkex.properties module

Property management and parsing, CSS cascading, default value storage

class inkex.properties.AlphaValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.BaseStyleValue

Stores an alpha value (such as opacity), which may be specified as as percentage or absolute value.

Reference: https://www.w3.org/TR/css-color/#typedef-alpha-value

_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

_unparse_value(value: object) str[source]
class inkex.properties.BaseStyleValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: object

A class encapsuling a single CSS declaration / presentation_attribute

_parse_value(value: str, element=None) object[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

_unparse_value(value: object) str[source]
property declaration: str

The css declaration corresponding to the StyleValue object

Returns

the css declaration, such as “fill: #000 !important;”

Return type

str

classmethod factory(declaration: Optional[str] = None, attr_name: Optional[str] = None, value: Optional[object] = None, important: Optional[bool] = False)[source]

Create an attribute

Parameters
  • declaration (str, optional) – the CSS declaration to parse. Defaults to None.

  • attr_name (str, optional) – the attribute name. Defaults to None.

  • value (object, optional) – the attribute value. Defaults to None.

  • important (bool, optional) – whether the attribute is marked !important. Defaults to False.

Raises

Errors may also be raised on parsing, so make sure to handle them

Returns

the parsed style

Return type

BaseStyleValue

static factory_errorhandled(element=None, declaration='', key='', value='')[source]

Error handling for the factory method: if something goes wrong during parsing, ignore the attribute

Parameters
  • element (BaseElement, optional) – The element this declaration is affecting, for finding gradients ect. Defaults to None.

  • declaration (str, optional) – the CSS declaration to parse. Defaults to “”.

  • key (str, optional) – the attribute name. Defaults to “”.

  • value (str, optional) – the attribute value. Defaults to “”.

Returns

The parsed style

Return type

BaseStyleValue

classmethod parse_declaration(declaration: str) Tuple[str, str, bool][source]

Parse a single css declaration

Parameters

declaration (str) – a css declaration such as: “fill: #000 !important;”. The trailing semicolon may be ommitted.

Raises

ValueError – Unable to parse the declaration

Returns

a tuple with key, value and importance

Return type

Tuple[str, str, bool]

parse_value(element=None)[source]

Get parsed property value with resolved urls, color, etc.

Parameters

element (BaseElement) – the SVG element to which this style is applied to currently used for resolving gradients / masks, could be used for computing percentage attributes or calc() attributes [optional]

Returns

parsed property value

Return type

object

unparse_value(value: object) str[source]

“Unparses” an object, i.e. converts an object back to an attribute. If the result is invalid, i.e. can’t be parsed, an exception is raised.

Parameters

value (object) – the object to be unparsed

Returns

the attribute value

Return type

str

class inkex.properties.ColorValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.BaseStyleValue

Stores a color value

Reference: https://drafts.csswg.org/css-color-3/#valuea-def-color

_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

class inkex.properties.EnumValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.BaseStyleValue

Stores a value that can only have a finite set of options

_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

class inkex.properties.FontSizeValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.BaseStyleValue

Logic for the font-size property

_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

class inkex.properties.FontValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.ShorthandValue

Logic for the shorthand font property

_abc_impl = <_abc._abc_data object>
get_shorthand_changes()[source]

calculates the value of affected attributes for this shorthand

Returns

a dictionary containing the new values of the affected attributes

Return type

Dict[str, str]

class inkex.properties.MarkerShorthandValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.ShorthandValue, inkex.properties.URLNoneValue

Logic for the marker shorthand property

_abc_impl = <_abc._abc_data object>
_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

get_shorthand_changes()[source]

calculates the value of affected attributes for this shorthand

Returns

a dictionary containing the new values of the affected attributes

Return type

Dict[str, str]

class inkex.properties.PaintValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.ColorValue, inkex.properties.URLNoneValue

Stores a paint value (such as fill and stroke), which may be specified as color, or url.

Reference: https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint

_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

_unparse_value(value: object)[source]
class inkex.properties.ShorthandValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.BaseStyleValue, abc.ABC

Stores a value that sets other values (e.g. the font shorthand)

_abc_impl = <_abc._abc_data object>
apply_shorthand(style)[source]

Applies a shorthand attribute to its style, respecting the importance state of the individual attributes.

Parameters

style (Style) – the style that the shorthand attribute is contained in, and that the shorthand attribute will be applied on

abstract get_shorthand_changes() Dict[str, str][source]

calculates the value of affected attributes for this shorthand

Returns

a dictionary containing the new values of the affected attributes

Return type

Dict[str, str]

class inkex.properties.URLNoneValue(declaration=None, attr_name=None, value=None, important=False)[source]

Bases: inkex.properties.BaseStyleValue

Stores a marker, which is given as url.

Reference: https://www.w3.org/TR/SVG2/painting.html#VertexMarkerProperties

_parse_value(value: str, element=None)[source]

internal parse method, to be overwritten by derived classes

Parameters
  • value (str) – unparsed value

  • element (BaseElement) – the SVG element to which this style is applied to [optional]

Returns

the parsed value

Return type

object

_unparse_value(value: object)[source]
static element_has_root(element) bool[source]

Checks if an element has a root, i.e. if element.root will fail

inkex.properties.match_url_and_return_element(string: str, svg)[source]

Parses a string containing an url, e.g. “url(#rect1234)”, looks up the element in the svg document and returns it.

Parameters
  • string (str) – the string to parse

  • svg (SvgDocumentElement) – document referenced in the URL

Raises
  • ValueError – if the string has invalid format

  • ValueError – if the referenced element is not found

Returns

the referenced element

Return type

BaseElement