Пример #1
0
    def test_get_css_rules_from_xml_stylesheet02(self):
        # xml-stylesheet
        svg = '''<?xml version="1.0" standalone="no"?>
        <?xml-stylesheet type="text/css" href="svg/style8.css" title="default"?>
        <?xml-stylesheet type="text/css" href="svg/ny1.css" title="extras"?>
        <?demo-pi ?>
        <!--comment-->
        <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
          "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
        
        <svg width="600px" height="600px" viewBox="-300 -300 600 600"
          xmlns="http://www.w3.org/2000/svg">
        </svg>
        '''
        parser = SVGParser()
        tree = parser.parse(StringIO(svg))
        root = tree.getroot()

        style_sheets = get_css_style_sheets_from_xml_stylesheet(root)
        self.assertEqual(2, len(style_sheets))

        css_style_sheet = style_sheets[0]

        css_rules = css_style_sheet.css_rules
        self.assertEqual(15, len(css_rules))

        css_rule = css_rules[0]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('svg', css_rule.selector_text)

        css_rule = css_rules[14]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('#inner-petals .segment:hover > .segment-edge',
                         css_rule.selector_text)

        css_rules = style_sheets[1].css_rules
        self.assertEqual(7, len(css_rules))

        css_rule = css_rules[0]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('text', css_rule.selector_text)

        css_rule = css_rules[6]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('.tick', css_rule.selector_text)
Пример #2
0
def main():
    """Usage: python3 sample.py filename
    """
    if len(sys.argv) >= 2:
        source = sys.argv[1]
        print('"{}"'.format(source))
    else:
        source = StringIO(PYTHON_SVG)

    parser = SVGParser()
    tree = parser.parse(source)
    root = tree.getroot()
    for element in root.iter():
        print('node type: {}, node name: \'{}\''.format(
            element.node_type, element.node_name))
        print('- class:', type(element))
        if element.node_type == Node.COMMENT_NODE:
            continue

        print('- tag: \'{}\', tag name: \'{}\''.format(element.tag,
                                                       element.tag_name))
        print('- id: {}'.format(
            None if element.id is None else '\'{}\''.format(element.id)))
        print('- attributes: ' + repr(element.attributes))
        if element.local_name == 'svg':
            vpx, vpy, vpw, vph = element.get_viewport_size()
            print('- viewport: x: {}, y: {}, width: {}, height: {}'.format(
                vpx.value(), vpy.value(), vpw.value(), vph.value()))

        if isinstance(element, SVGGraphicsElement):
            ctm = element.get_ctm()
            print('- ctm: {}'.format(ctm.tolist()))
            bbox = element.get_bbox()
            print('- bbox: x: {}, y: {}, width: {}, height: {}'.format(
                bbox.x, bbox.y, bbox.width, bbox.height))

        if isinstance(element, SVGGeometryElement):
            print('- geometry: {}'.format(element.get_computed_geometry()))
            print('- total length: {:g}'.format(element.get_total_length()))

        print()
Пример #3
0
    def test_get_css_rules_from_svg_document01(self):
        # 'link' and 'style' elements
        svg = '''
        <svg width="5cm" height="4cm" viewBox="0 0 500 400"
             xmlns="http://www.w3.org/2000/svg">
          <link rel="stylesheet" href="svg/style8.css"/>
          <style type="text/css"><![CDATA[
            .Border { fill:none; stroke:blue; stroke-width:1 }
            .Connect { fill:none; stroke:#888888; stroke-width:2 }
            .SamplePath { fill:none; stroke:red; stroke-width:5 }
            .EndPoint { fill:none; stroke:#888888; stroke-width:2 }
            .CtlPoint { fill:#888888; stroke:none }
            .AutoCtlPoint { fill:none; stroke:blue; stroke-width:4 }
            .Label { font-size:22; font-family:Verdana }
          ]]></style>
          <style>
            @media print { /* rule (1) */
              /* hide navigation controls when printing */
              #navigation { display: none }
              @media (max-width: 12cm) { /* rule (2) */
                /* keep notes in flow when printing to narrow pages */
                .note { float: none }
              }
            }
            @media screen and (min-width: 35em),
                   print and (min-width: 40em) {
              #section_navigation { float: left; width: 10em; }
            }
          </style>
          <rect class="Border" x="1" y="1" width="498" height="398" id="rect01" />
          <polyline class="Connect" points="100,200 100,100" id="polyline01" />
          <path class="SamplePath" d="M100,200 C100,100 250,100 250,200
                                               S400,300 400,200" id="path01" />
          <circle class="EndPoint" cx="100" cy="200" r="10" id="circle01" />
          <text class="Label" x="25" y="70" id="text01">
            M100,200 C100,100 250,100 250,200</text>
        </svg>
        '''
        parser = SVGParser()
        tree = parser.parse(StringIO(svg))
        root = tree.getroot()

        style_sheets = get_css_style_sheets_from_svg_document(root)
        self.assertEqual(3, len(style_sheets))

        # <link rel="stylesheet" href="svg/style8.css"/>
        css_style_sheet = style_sheets[0]
        self.assertEqual(15, len(css_style_sheet.css_rules))

        css_rule = css_style_sheet.css_rules[0]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('svg', css_rule.selector_text)

        css_rule = css_style_sheet.css_rules[14]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('#inner-petals .segment:hover > .segment-edge',
                         css_rule.selector_text)

        # <style type="text/css">...
        css_style_sheet = style_sheets[1]
        self.assertEqual(7, len(css_style_sheet.css_rules))

        css_rule = css_style_sheet.css_rules[0]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('.Border', css_rule.selector_text)

        css_rule = css_style_sheet.css_rules[6]
        self.assertEqual(CSSRule.STYLE_RULE, css_rule.type)
        self.assertEqual('.Label', css_rule.selector_text)

        # <style> @media at-rule
        css_style_sheet = style_sheets[2]
        self.assertEqual(2, len(css_style_sheet.css_rules))

        # @media 'print'
        css_rule = css_style_sheet.css_rules[0]
        self.assertEqual(CSSRule.MEDIA_RULE, css_rule.type)
        self.assertEqual('print', css_rule.media.media_text)

        # child rules
        self.assertEqual(2, len(css_rule.css_rules))

        # (1) style rule
        child_css_rule = css_rule.css_rules[0]
        self.assertEqual(CSSRule.STYLE_RULE, child_css_rule.type)
        self.assertEqual('#navigation', child_css_rule.selector_text)

        # (2) @media at-rule
        child_css_rule = css_rule.css_rules[1]
        self.assertEqual(CSSRule.MEDIA_RULE, child_css_rule.type)
        self.assertEqual('(max-width: 12cm)', child_css_rule.media.media_text)

        css_rule = css_style_sheet.css_rules[1]
        self.assertEqual(CSSRule.MEDIA_RULE, css_rule.type)
        self.assertEqual(
            'screen and (min-width: 35em), print and (min-width: 40em)',
            css_rule.media.media_text)