Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix exceptions in media converter #121

Merged
merged 1 commit into from
May 2, 2019
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ sudo apt-get install python
```sh
sudo apt-get install sox
```
* Install npm via aptitude (if not already installed):
```sh
sudo apt-get install nodejs
```
* Install svg2png via npm (needed for svg conversion):
```sh
npm install svg2png -g
```
* Jython 2.7 requires java version 1.8 or later. To determine the currently installed java version run the following command on your shell:
```sh
java -version
Expand Down
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: 984
build_number: 985

;-------------------------------------------------------------------------------
[CATROBAT]
Expand Down
42 changes: 22 additions & 20 deletions src/scratchtocatrobat/tools/svgtopng.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
from scratchtocatrobat.tools import common
from scratchtocatrobat.tools import helpers
from java.io import FileOutputStream
from org.apache.batik.transcoder.image import PNGTranscoder
from org.apache.batik.transcoder import TranscoderInput
from org.apache.batik.transcoder import TranscoderOutput
from java.nio.file import Paths
from java.awt.image import BufferedImage
from java.awt import AlphaComposite
Expand All @@ -37,7 +34,7 @@
from javax.swing import ImageIcon
import java.awt.Color
import xml.etree.cElementTree as ET

import subprocess

_BATIK_CLI_JAR = "batik-rasterizer.jar"
_log = logging.getLogger(__name__)
Expand All @@ -62,6 +59,7 @@ def convert(input_svg_path, rotation_x, rotation_y):
output_png_path = "{}_rotX_{}_rotY_{}.png".format(input_file_name, rotation_x, rotation_y)
_log.info(" converting '%s' to Pocket Code compatible png '%s'", input_svg_path, output_png_path)


output_svg_path = input_svg_path.replace(".svg", "_modified.svg")
output_svg_URI = Paths.get(output_svg_path).toUri().toURL().toString()

Expand All @@ -77,15 +75,10 @@ def convert(input_svg_path, rotation_x, rotation_y):
error = None
try:
_parse_and_rewrite_svg_file(input_svg_path, output_svg_path)

input_svg_image = TranscoderInput(output_svg_URI)

output_png_image = TranscoderOutput(FileOutputStream(output_png_path))

command = "svg2png"
out = subprocess.check_output([command, output_svg_path, "-o", output_png_path])
_log.info(" converting '%s' to Pocket Code compatible png '%s'",
input_svg_path, output_png_path)
png_converter = PNGTranscoder()
png_converter.transcode(input_svg_image, output_png_image)
assert os.path.exists(output_png_path)

final_image = _translation(output_png_path, rotation_x, rotation_y)
Expand Down Expand Up @@ -146,7 +139,7 @@ def _translation(output_png_path, rotation_x, rotation_y):
end_x = 2*rotation_x
dst_new_width = start_x + end_x

# non-overlapping x enhancement
# non-overlapping x enhancement
elif rotation_x < 0:
start_x = 2*abs(rotation_x) + end_x
dst_new_width = 2*(abs(rotation_x) + end_x)
Expand All @@ -162,7 +155,7 @@ def _translation(output_png_path, rotation_x, rotation_y):
end_y = start_y + end_y
elif end_y - rotation_y < end_y/2:
end_y = 2*rotation_y
dst_new_height = start_y + end_y
dst_new_height = start_y + end_y

elif rotation_y < 0:
start_y = 2*abs(rotation_y) + end_y
Expand All @@ -172,10 +165,17 @@ def _translation(output_png_path, rotation_x, rotation_y):
elif rotation_y >= end_y:
dst_new_height = 2*rotation_y

new_buffered_image = BufferedImage(dst_new_width + 1, dst_new_height + 1, BufferedImage.TYPE_INT_ARGB)

new_buffered_image = BufferedImage(int(dst_new_width + 1), int(dst_new_height + 1), BufferedImage.TYPE_INT_ARGB)

g2d = new_buffered_image.createGraphics()
g2d.setComposite(AlphaComposite.Clear)
g2d.fillRect(0, 0, dst_new_width, dst_new_height)
g2d.fillRect(0, 0, int(dst_new_width + 1), int(dst_new_height + 1))

start_x = int(start_x)
start_y = int(start_y)
end_x = int(end_x)
end_y = int(end_y)

for row_y in xrange(start_y, end_y + 1):
for column_x in xrange(start_x, end_x + 1):
Expand All @@ -198,12 +198,16 @@ def _create_buffered_image(image):

def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path):
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)
root = tree.getroot()

#exception is thrown if height or width is less or equal zero
if 'height' in root.attrib and float((root.attrib['height']).replace('px', '')) <= 0:
if 'height' in root.attrib and float((root.attrib['height']).strip('px%')) <= 0:
root.attrib['height'] = '1'
if 'width' in root.attrib and float((root.attrib['width']).replace('px', '')) <= 0:
if 'width' in root.attrib and float((root.attrib['width']).strip('px%')) <= 0:
root.attrib['width'] = '1'


Expand Down Expand Up @@ -241,17 +245,15 @@ def findTextInChildren(parent):
namespace_tag = (child.tag).replace('text', '')
dy_value = 0
if 'font-size' in child.attrib:
dy_font_size = int(child.attrib['font-size'])
dy_font_size = int(child.attrib['font-size'].strip('px'))
else:
dy_font_size = 12 # default value
for text_part in list_of_text_parts:
tspan = ET.SubElement(child, namespace_tag + 'tspan', x = '0', dy = str(dy_value))
tspan.text = text_part
dy_value = dy_value + dy_font_size

tree.write(svg_output_path)


def _get_viewbox_values(view_box_str):
view_box_values = []

Expand Down