File indexing completed on 2024-07-07 07:16:10

0001 #!/usr/bin/env python3
0002 """
0003 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0004 SPDX-FileCopyrightText: 2020 Noah Davis <noahadvs@gmail.com>
0005 SPDX-FileCopyrightText: 2020 Niccolò Venerandi <niccolo@venerandi.com>
0006 """
0007 import os
0008 import unittest
0009 from lxml import etree
0010 """
0011 This script generates 24px icons based on 22px icons
0012 """
0013 
0014 print("********* Start testing of Test24x24Icons *********")
0015 
0016 # BEGIN globals
0017 # These are needed to prevent nonsense namespaces like ns0 from being
0018 # added to otherwise perfectly fine svg elements and attributes
0019 NAMESPACES = {
0020     "svg": "http://www.w3.org/2000/svg",
0021     "xlink": "http://www.w3.org/1999/xlink",
0022     "inkscape": "http://www.inkscape.org/namespaces/inkscape",
0023     "dc": "http://purl.org/dc/elements/1.1/",
0024     "cc": "http://creativecommons.org/ns#",
0025     "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
0026     "sodipodi": "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
0027 }
0028 for prefix, uri in NAMESPACES.items():
0029     etree.register_namespace(prefix, uri)
0030 
0031 RES_DIR: str = "./res/"
0032 GEN_DIR: str = "./generated/"
0033 
0034 # END globals
0035 
0036 
0037 def get_renderable_elements(root: etree.Element):
0038     """
0039     Get renderable elements that are children of the root of the SVG.
0040 
0041     See the Renderable Elements section of the SVG documentation on MDN web docs:
0042     https://developer.mozilla.org/en-US/docs/Web/SVG/Element#SVG_elements_by_category
0043     """
0044     return root.xpath(
0045         "./svg:a | ./svg:circle | ./svg:ellipse | ./svg:foreignObject | ./svg:g "
0046         + "| ./svg:image | ./svg:line | ./svg:mesh | ./svg:path | ./svg:polygon "
0047         + "| ./svg:polyline | ./svg:rect | ./svg:switch | ./svg:svg | ./svg:symbol "
0048         + "| ./svg:text | ./svg:textPath | ./svg:tspan | ./svg:unknown | ./svg:use",
0049         namespaces=NAMESPACES
0050     )
0051 
0052 
0053 class Test24x24Icons(unittest.TestCase):
0054 
0055     def test_reverse24x24conversion(self):
0056         self.maxDiff = None
0057         for dirpath, dirnames, filenames in os.walk(GEN_DIR):
0058             for f in filenames:
0059 
0060                 # Generated filepath
0061                 gen_filepath = os.path.join(dirpath, f)
0062 
0063                 # Filter out files
0064                 if not (f.endswith('.svg') and '/24' in gen_filepath) or os.path.islink(gen_filepath):
0065                     continue
0066 
0067                 etree.set_default_parser(etree.XMLParser(remove_blank_text=True))
0068 
0069                 # Generated root
0070                 gen_root = etree.parse(gen_filepath).getroot()
0071 
0072                 # Res filepath and root
0073                 res_filepath = gen_filepath.replace(GEN_DIR, RES_DIR, 1).replace('/24', '/22')
0074                 res_root = etree.parse(res_filepath).getroot()
0075 
0076                 # Resize to 22x22
0077                 gen_root.set('viewBox', "0 0 22 22")
0078                 gen_root.set('width', "22")
0079                 gen_root.set('height', "22")
0080 
0081                 # Remove group that moves content down 1px, right 1px
0082                 group = gen_root.find('./{http://www.w3.org/2000/svg}g', NAMESPACES)
0083                 gen_root.extend(get_renderable_elements(group))
0084                 gen_root.remove(group)
0085 
0086                 # Compare elements and attributes
0087                 for gen_elem, res_elem in zip(res_root.iterfind('./svg:*', NAMESPACES),
0088                                               gen_root.iterfind('./svg:*', NAMESPACES)):
0089                     self.assertEqual(res_elem.tag, gen_elem.tag, gen_filepath)
0090                     for attribute in res_elem.items():
0091                         self.assertIn(attribute, gen_elem.items(), gen_filepath)
0092                 pass
0093 
0094 
0095 if __name__ == "__main__":
0096     unittest.main()