Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

STCC-219 fixed svg-to-png conversion error with xml register_namespace #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Scratch2Catrobat Converter
short_name: S2CC
version: 0.10.0
build_name: Aegean cat
build_number: 1002
build_number: 1007
build_type: S2CC

;-------------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion src/scratchtocatrobat/converter/mediaconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import shutil
from threading import Thread
from threading import Lock
from java.awt import Color

from scratchtocatrobat.tools import logger
Expand All @@ -37,6 +38,7 @@

MAX_CONCURRENT_THREADS = int(helpers.config.get("MEDIA_CONVERTER", "max_concurrent_threads"))
log = logger.log
ns_registry_lock = Lock()


class MediaType(object):
Expand Down Expand Up @@ -73,7 +75,7 @@ def run(self):

if media_type == MediaType.UNCONVERTED_SVG:
# converting svg to png -> new md5 and filename
new_src_path = svgtopng.convert(old_src_path, info["rotationCenterX"], info["rotationCenterY"])
new_src_path = svgtopng.convert(old_src_path, info["rotationCenterX"], info["rotationCenterY"], ns_registry_lock)
elif media_type == MediaType.UNCONVERTED_WAV:
# converting Android-incompatible wav to compatible wav
new_src_path = wavconverter.convert_to_android_compatible_wav(old_src_path)
Expand Down
6 changes: 4 additions & 2 deletions src/scratchtocatrobat/converter/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import unittest
import re
from threading import Lock

import org.catrobat.catroid.common as catcommon
import org.catrobat.catroid.content as catbase
Expand All @@ -40,6 +41,7 @@

BACKGROUND_LOCALIZED_GERMAN_NAME = "Hintergrund"
BACKGROUND_ORIGINAL_NAME = "Stage"
ns_registry_lock = Lock()


def create_catrobat_sprite_stub(name=None):
Expand Down Expand Up @@ -2643,7 +2645,7 @@ def test_can_rewrite_svg_matrix(self):
if re.search('.*}g', child.tag) != None:
if 'transform' in child.attrib:
assert(child.attrib['transform'] == "matrix(1.5902323722839355, 0, 0, 1.5902323722839355, -0.5, 0.5)")
svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/3.svg","test/res/scratch/Wizard_Spells/3_changed.svg")
svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/3.svg","test/res/scratch/Wizard_Spells/3_changed.svg", ns_registry_lock)
tree = ET.parse("test/res/scratch/Wizard_Spells/3_changed.svg")
root = tree.getroot()
for child in root:
Expand All @@ -2658,7 +2660,7 @@ def test_can_rewrite_svg_text_position(self):
if re.search('.*}text', child.tag) != None:
assert(child.attrib['x'] == '147.5')
assert(child.attrib['y'] == '146.1')
svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/6.svg","test/res/scratch/Wizard_Spells/6_changed.svg")
svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/6.svg","test/res/scratch/Wizard_Spells/6_changed.svg", ns_registry_lock)
tree = ET.parse("test/res/scratch/Wizard_Spells/6_changed.svg")
root = tree.getroot()
for child in root:
Expand Down
13 changes: 9 additions & 4 deletions src/scratchtocatrobat/tools/svgtopng.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.awt.Color
import xml.etree.cElementTree as ET
import subprocess
from threading import Lock

_BATIK_CLI_JAR = "batik-rasterizer.jar"
_log = logging.getLogger(__name__)
Expand All @@ -51,7 +52,7 @@ def _checked_batik_jar_path():
return _batik_jar_path


def convert(input_svg_path, rotation_x, rotation_y):
def convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock):
assert isinstance(input_svg_path, (str, unicode))
assert os.path.splitext(input_svg_path)[1] == ".svg"

Expand All @@ -74,7 +75,7 @@ def convert(input_svg_path, rotation_x, rotation_y):
png_ostream = None
error = None
try:
_parse_and_rewrite_svg_file(input_svg_path, output_svg_path)
_parse_and_rewrite_svg_file(input_svg_path, output_svg_path, ns_registry_lock)
command = "svg2png"
out = subprocess.check_output([command, output_svg_path, "-o", output_png_path])
_log.info(" converting '%s' to Pocket Code compatible png '%s'",
Expand Down Expand Up @@ -196,12 +197,16 @@ def _create_buffered_image(image):



def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path):
def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path, ns_registry_lock):
tree = ET.parse(svg_input_path)

namespaces = dict([node for _, node in ET.iterparse(svg_input_path,events=['start-ns'])])
for prefix, uri in namespaces.items():
ET.register_namespace(prefix, uri)
ns_registry_lock.acquire()
try:
ET.register_namespace(prefix, uri)
finally:
ns_registry_lock.release()
root = tree.getroot()

#exception is thrown if height or width is less or equal zero
Expand Down
17 changes: 10 additions & 7 deletions src/scratchtocatrobat/tools/test_svgtopng.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import os
import shutil
import unittest
from threading import Lock

from scratchtocatrobat.tools import common_testing
from scratchtocatrobat.tools import svgtopng
from scratchtocatrobat.tools import helpers

ns_registry_lock = Lock()

class SvgToPngTest(common_testing.BaseTestCase):

#def test_can_convert_file_from_svg_to_png(self):
Expand All @@ -49,7 +52,7 @@ def test_parse_svgfile_and_convert_to_png_cape(self):

rotation_x, rotation_y = 36, 67

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand All @@ -76,7 +79,7 @@ def test_parse_svgfile_and_convert_to_png_background(self):

rotation_x, rotation_y = 255, 180

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand All @@ -103,7 +106,7 @@ def test_parse_svgfile_and_convert_to_png_antenna(self):

rotation_x, rotation_y = 53, 43

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand Down Expand Up @@ -133,7 +136,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q1(self):
str(rotation_x) + "_rotY_" + str(rotation_y) +
".png")

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand Down Expand Up @@ -163,7 +166,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q2(self):
str(rotation_x) + "_rotY_" + str(rotation_y) +
".png")

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand Down Expand Up @@ -193,7 +196,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q3(self):
str(rotation_x) + "_rotY_" + str(rotation_y) +
".png")

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand Down Expand Up @@ -223,7 +226,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q4(self):
str(rotation_x) + "_rotY_" + str(rotation_y) +
".png")

output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock)

from javax.imageio import ImageIO
from java.io import File
Expand Down