def test_should_prefix_style_url_references(self): src = self.get_test_image_path('test_svg_with_style_url_ref.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 0, 0, crop=False, optimize=True, prefix='123') svg = file_get_contents(dst) self.assertEqual( '<?xml version="1.0" encoding="utf-8"?>\n' + '<svg data-prefix="b23" id="b23_Layer_1" style="enable-background:new 0 0 64 64;" version="1.1" viewBox="0 0 64 64" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n\n' + '<linearGradient gradientUnits="userSpaceOnUse" id="b23_SVGID_1_" x1="0" x2="64" y1="32.3013" y2="32.3013">\n' + '<stop offset="0" style="stop-color:#B90404"/>\n' + '<stop offset="1" style="stop-color:#8E0000"/>\n' + '</linearGradient>\n' + '<path d="36.3,83.7 0.1,62.7 0,20.8 36.1,0 72.2,21.1 72.4,62.9" style="fill:url(#b23_SVGID_1_);"/>\n' + '</svg>', svg) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_enforce_prefix_always_starting_with_letter(self): src = self.get_test_image_path('test_svg_with_ids.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 0, 0, crop=False, optimize=True, prefix='foo') svg = file_get_contents(dst) self.assertEqual( '<?xml version="1.0" encoding="utf-8"?>\n' + \ '<svg data-prefix="foo" id="foo_Layer_1" style="enable-background:new 0 0 164.4 83.7;" version="1.1" viewBox="0 0 164.4 83.7" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n' + \ '<polygon id="foo_base" points="36.3,83.7 0.1,62.7 0,20.8 36.1,0 72.2,21.1 72.4,62.9 "/>\n' + \ '<polygon id="foo_cuffs" points="36.4,62.9 54.5,73 54.5,52.4 "/>\n' + \ '<g id="foo_test" transform="matrix(0.250391,0,0,0.250391,36.1,46.1023)">\n' + \ '<use height="512px" width="512px" x="0" xlink:href="#foo_Image1" y="0"/>\n' + \ '</g>\n' + \ '<defs>\n' + \ '<image height="512px" id="foo_Image1" width="512px" xlink:href="data:image/png;base64,broken image"/>\n' + \ '</defs>\n' + \ '</svg>', svg ) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_not_change_pure_vector_image(self): src = self.get_test_image_path('test.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 0, 0, crop=False, optimize=False) # svg file without any bitmap data embedded should be the same # image after resize (without optimizations)... self.assertTrue(is_same_file(src, dst)) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_ignore_embedded_bitmap_if_image_data_cannot_be_read_or_processed(self): src = self.get_test_image_path('test_svg_with_broken_png.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 0, 0, crop=False, optimize=False) # svg file with broken bitmap data should be the same as the # original without optimizations (ignored)... self.assertTrue(is_same_file(src, dst)) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_ignore_embedded_bitmap_if_image_cannot_be_b64_decoded(self): src = self.get_test_image_path('test_svg_with_broken_b64_encoding.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 0, 0, crop=False, optimize=False) # svg file with broken bitmap data (base64 decoding error) should # contain the same embedded bitmap data as the original self.assertTrue(is_same_file(src, dst)) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_crop_to_new_aspect_ratio_based_on_center_position_for_portrait(self): src = self.get_test_image_path('test_svg_crop_portrait.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 400, 400, prefix='') svg = file_get_contents(dst) self.assertEqual( '<?xml version="1.0" encoding="utf-8"?>\n' + \ '<svg id="Layer_1" style="enable-background:new 0 40 80 80;" version="1.1" viewBox="0 40 80 80" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n' + \ '<polygon fill="red" points="36.3,83.7 0.1,62.7 0,20.8 36.1,0 72.2,21.1 72.4,62.9 "/>\n' + \ '</svg>', svg ) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_inline_style(self): src = self.get_test_image_path('test_svg_with_style.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 164.4, 83.7, optimize=True, prefix='') svg = file_get_contents(dst) self.assertEqual( '<?xml version="1.0" encoding="utf-8"?>\n' + \ '<svg id="Layer_1" style="enable-background:new 0 0 164 84;" version="1.1" viewBox="0 0 164 84" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n' + \ '\n' + \ '<polygon points="36.3,83.7 0.1,62.7 0,20.8 36.1,0 72.2,21.1 72.4,62.9 " style="fill:#E8318A;"/>\n' + \ '<polygon points="36.4,62.9 54.5,73 54.5,52.4 " style="fill:#BF2174;"/>\n' + \ '<polygon points="36.4,62.9 36.7,42.1 54.5,52.4 " style="opacity:0.56;fill:#EA53A2;"/>\n' + \ '</svg>', svg ) finally: if os.path.isfile(dst): os.unlink(dst)
def test_should_prefix_clip_path_url_references(self): src = self.get_test_image_path('test_svg_with_clippath_url_ref.svg') dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 0, 0, crop=False, optimize=True, prefix='123') svg = file_get_contents(dst) self.assertEqual( '<?xml version="1.0" encoding="utf-8"?>\n' + '<svg data-prefix="b23" id="b23_Layer_1" style="enable-background:new 0 0 64 64;" version="1.1" viewBox="0 0 64 64" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n' + '<clipPath id="b23_clip"><path d="M441.105,324.213l44.837,10.308l13.761,-59.851l-44.837,-10.309l-13.761,59.852l0,0Z"/></clipPath>\n' + '<g clip-path="url(#b23_clip)">\n' + '<path d="36.3,83.7 0.1,62.7 0,20.8 36.1,0 72.2,21.1 72.4,62.9"/>\n' + '</g>\n' + '</svg>', svg ) finally: if os.path.isfile(dst): os.unlink(dst)
def _assert_downsampling(self, filename): src = self.get_test_image_path(filename) dst = os.path.join(tempfile.gettempdir(), 'resized_svg_image.svg') try: resize_svg_image(src, dst, 50, 50) # svg file with bitmap data embedded should NOT be the same # image after resize... self.assertFalse(is_same_file(src, dst)) # image size should be less self.assertTrue(os.path.getsize(dst) < os.path.getsize(src)) # the resulting image should still be an svg image with path # information in it content = file_get_contents(dst) self.assertIn('<svg', content) self.assertIn('<path', content) finally: if os.path.isfile(dst): os.unlink(dst)