How to use the svglib.svglib.Svg2RlgAttributeConverter function in svglib

To help you get started, we’ve selected a few svglib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github deeplook / svglib / tests / test_basic.py View on Github external
mapping = (
            ("red", colors.red),
            ("#ff0000", colors.red),
            ("#ff000055", colors.Color(1, 0, 0, 1/3.0)),
            ("#f00", colors.red),
            ("#f00f", colors.red),
            ("rgb(100%,50%,10%)", colors.Color(1, 1.0/255 * 128, 1.0/255 * 26, 1)),
            ("rgb(255, 0, 0)", colors.red),
            ("rgba(255, 255, 128, .5)", colors.Color(1, 1, 1.0/255 * 128, .5)),
            ("fuchsia", colors.Color(1, 0, 1, 1)),
            ("slategrey", colors.HexColor(0x708090)),
            ("transparent", colors.Color(0, 0, 0, 0)),
            ("whatever", None),
        )
        ac = svglib.Svg2RlgAttributeConverter()
        failed = _testit(ac.convertColor, mapping)
        assert len(failed) == 0
github deeplook / svglib / tests / test_basic.py View on Github external
def test_1(self):
        "Test color attribute conversion to CMYK"

        mapping = (
            ("red", force_cmyk(colors.red)),
            ("#ff0000", force_cmyk(colors.red)),
            ("#f00", force_cmyk(colors.red)),
            ("rgb(100%,0%,0%)", force_cmyk(colors.red)),
            ("rgb(255, 0, 0)", force_cmyk(colors.red)),
            ("rgb(0,255, 0)", force_cmyk(colors.Color(0, 1, 0))),
            ("rgb(0, 0, 255)", force_cmyk(colors.Color(0, 0, 1))),
        )
        ac = svglib.Svg2RlgAttributeConverter(color_converter=force_cmyk)
        failed = _testit(ac.convertColor, mapping)
        assert len(failed) == 0
github deeplook / svglib / tests / test_basic.py View on Github external
def test_0(self):
        "Test length list attribute conversion."

        mapping = (
            (" 5cm 5in", [5*cm, 5*inch]),
            (" 5, 5", [5, 5]),
        )
        ac = svglib.Svg2RlgAttributeConverter()
        failed = _testit(ac.convertLengthList, mapping)
        assert len(failed) == 0
github deeplook / svglib / tests / test_basic.py View on Github external
def test_0(self):
        "Test multi-attribute conversion."

        mapping = (
            ("fill: black; stroke: yellow",
                {"fill": "black", "stroke": "yellow"}),
        )
        ac = svglib.Svg2RlgAttributeConverter()
        failed = _testit(ac.parseMultiAttributes, mapping)
        assert len(failed) == 0
github deeplook / svglib / tests / test_basic.py View on Github external
def test_findAttr(self):
        """
        Test various attribute peculiarities.
        """
        ac = svglib.Svg2RlgAttributeConverter()
        # Whitespace in attribute values shouldn't disturb parsing.
        node = etree.XML('
github deeplook / svglib / tests / test_basic.py View on Github external
def test_findAttr_parents(self):
        ac = svglib.Svg2RlgAttributeConverter()
        rect_node = etree.XML(
            ''
        ).getchildren()[0]
        assert ac.findAttr(rect_node, 'fill') == "#ff0"
        assert ac.findAttr(rect_node, 'stroke') == "#008000"
github deeplook / svglib / tests / test_basic.py View on Github external
def test_font_family(self):
        def font_config_available():
            try:
                subprocess.call(["fc-match"])
            except OSError:
                return False
            return True

        converter = svglib.Svg2RlgAttributeConverter()
        # Check PDF standard names are untouched
        assert converter.convertFontFamily('ZapfDingbats') == 'ZapfDingbats'
        assert converter.convertFontFamily('bilbo ZapfDingbats') == 'ZapfDingbats'
        assert converter.convertFontFamily(' bilbo    ZapfDingbats  ') == 'ZapfDingbats'
        assert converter.convertFontFamily(' bilbo,    ZapfDingbats  ') == 'ZapfDingbats'
        if font_config_available():
            # Fontconfig will always provide at least a default font and register
            # that font under the provided font name.
            assert converter.convertFontFamily('SomeFont') == 'SomeFont'
        else:
            # Unknown fonts are converted to Helvetica by default.
            assert converter.convertFontFamily('SomeFont') == 'Helvetica'
        # Check font names with spaces
        assert converter.split_attr_list("'Open Sans', Arial, 'New Times Roman'") == [
            'Open Sans', 'Arial', 'New Times Roman'
        ]
github deeplook / svglib / tests / test_basic.py View on Github external
def test_percentage_conversion(self):
        "Test percentage length attribute conversion."

        ac = svglib.Svg2RlgAttributeConverter()
        ac.set_box(svglib.Box(0, 0, 50, 150))
        # Percentages depend on current viewport and type of attribute
        length = ac.convertLength("1e1%", attr_name='width')
        assert length == 5
        length = ac.convertLength("1e1%", attr_name='height')
        assert length == 15
github deeplook / svglib / svglib / svglib.py View on Github external
def __init__(self, path, attrConverter=None):
        self.attrConverter = attrConverter or Svg2RlgAttributeConverter()
        self.svg_source_file = path
        self.preserve_space = False