Skip to content

Commit cb955f8

Browse files
AntiDogPhilipp Fleischhacker
authored and
Philipp Fleischhacker
committed
PNGs get now moved by rotation center, some remove of dead code, SVG Texts now onscreen (#80)
1 parent 6b386b3 commit cb955f8

File tree

4 files changed

+25
-147
lines changed

4 files changed

+25
-147
lines changed

config/default.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name: Scratch2Catrobat Converter
44
short_name: S2CC
55
version: 0.9.1
66
build_name: Aegean cat
7-
build_number: 927
7+
build_number: 928
88

99
;-------------------------------------------------------------------------------
1010
[CATROBAT]

src/scratchtocatrobat/converter/converter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ def _add_default_behaviour_to(sprite, sprite_context, catrobat_scene, catrobat_p
10891089

10901090
object_relative_scale = scratch_object.get_scale() or 1
10911091
if costume_resolution is not None:
1092-
object_scale = object_relative_scale * 100.0 / costume_resolution
1092+
object_scale = object_relative_scale * 100.0
10931093
if object_scale != 100.0:
10941094
implicit_bricks_to_add += [catbricks.SetSizeToBrick(object_scale)]
10951095

src/scratchtocatrobat/converter/mediaconverter.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def convert(self, progress_bar = None):
129129
"Unsupported image file extension: %s" % costume_src_path
130130
ispng = file_ext == ".png"
131131
is_unconverted = file_ext == ".svg"
132+
132133
resource_info = {
133134
"scratch_md5_name": costume_file_name,
134135
"src_path": costume_src_path,
@@ -146,8 +147,7 @@ def convert(self, progress_bar = None):
146147
#TODO: background gets scaled too, shouldn't be the case
147148
if ispng:
148149
isStageCostume = scratch_object.name == "Stage"
149-
if not isStageCostume and JsonKeys.COSTUME_RESOLUTION in costume_info:
150-
self.resize_png(costume_src_path, costume_src_path, costume_info[JsonKeys.COSTUME_RESOLUTION])
150+
self.convertPNG(isStageCostume, costume_info,costume_src_path, costume_src_path)
151151
converted_media_resources_paths.add(costume_src_path)
152152
progress_bar.update(ProgressType.CONVERT_MEDIA_FILE)
153153

@@ -313,3 +313,9 @@ def resize(img, height, width):
313313
ImageIO.write(resized, "png", output)
314314
return path_out
315315

316+
def convertPNG(self, isStageCostume, costume_info, costume_src_path , costume_dest_path):
317+
import java.io.File
318+
new_image = svgtopng._translation(costume_src_path, costume_info["rotationCenterX"], costume_info["rotationCenterY"])
319+
ImageIO.write(new_image, "png", java.io.File(costume_dest_path))
320+
if JsonKeys.COSTUME_RESOLUTION in costume_info:
321+
self.resize_png(costume_dest_path, costume_dest_path, costume_info[JsonKeys.COSTUME_RESOLUTION])

src/scratchtocatrobat/tools/svgtopng.py

+15-143
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646

4747
# TODO: refactor to single mediaconverter class together with wavconverter
4848
def _checked_batik_jar_path():
49-
# if _BATIK_ENVIRONMENT_HOME not in os.environ:
50-
# raise EnvironmentError("Environment variable '{}' must be set to batik library location.".format(_BATIK_ENVIRONMENT_HOME))
5149
batik_home_dir = helpers.config.get("PATHS", "batik_home")
5250
batik_jar_path = os.path.join(batik_home_dir, _BATIK_CLI_JAR)
5351
if not os.path.exists(batik_jar_path):
@@ -89,7 +87,6 @@ def convert(input_svg_path, rotation_x, rotation_y):
8987
png_converter.transcode(input_svg_image, output_png_image)
9088
assert os.path.exists(output_png_path)
9189

92-
# TODO: uncomment this once all remaining bugs have been fixed!
9390
final_image = _translation(output_png_path, rotation_x, rotation_y)
9491

9592
if final_image is None:
@@ -126,22 +123,7 @@ def _translation(output_png_path, rotation_x, rotation_y):
126123
buffered_image_matrix = [[buffered_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)]
127124
buffered_image_matrix = _transpose_matrix(buffered_image_matrix)
128125
m, n = len(buffered_image_matrix), len(buffered_image_matrix[0])
129-
'''
130-
x_coords_list, y_coords_list = [], []
131-
132-
for y in xrange(m):
133-
for x in xrange(n):
134-
pixel = buffered_image_matrix[y][x]
135-
if (pixel >> 24) != 0x00:
136-
x_coords_list.append(x)
137-
y_coords_list.append(y)
138-
'''
139126
_log.info("Output path: {}".format(output_png_path))
140-
#_log.info("height, width: ({}, {})".format(m, n))
141-
# start_x = min(x_coords_list) if len(x_coords_list) > 0 else 0
142-
# end_x = max(x_coords_list) if len(x_coords_list) > 0 else 0
143-
# start_y = min(y_coords_list) if len(y_coords_list) > 0 else 0
144-
# end_y = max(y_coords_list) if len(y_coords_list) > 0 else 0
145127

146128
start_x, start_y = 0, 0
147129
end_x, end_y = n, m
@@ -150,27 +132,11 @@ def _translation(output_png_path, rotation_x, rotation_y):
150132
_log.info("ANTENNA-ERROR")
151133
return buffered_image
152134

153-
#_log.info("start y, start x: ({}, {})".format(start_y, start_x))
154-
#_log.info("end y, end x: ({}, {})".format(end_y, end_x))
155-
156-
# if start_x > rotation_x:
157-
# start_x = rotation_x
158-
# if end_x < rotation_x:
159-
# end_x = rotation_x
160-
# if start_y > rotation_y:
161-
# start_y = rotation_y
162-
# if end_y < rotation_y:
163-
# end_y = rotation_y
164-
165-
#org_st_x = start_x
166-
#org_st_y = start_y
167-
168135
dst_new_width = end_x
169136
dst_new_height = end_y
170137

171138
# overlapping x enhancement
172139
if (rotation_x < end_x) and (rotation_x > 0):
173-
#_log.info("x overlap")
174140
if end_x - rotation_x > end_x/2:
175141
dst_new_width = (end_x - rotation_x) * 2
176142
start_x = dst_new_width/2 - rotation_x
@@ -181,18 +147,14 @@ def _translation(output_png_path, rotation_x, rotation_y):
181147

182148
# non-overlapping x enhancement
183149
elif rotation_x < 0:
184-
#_log.info("2nd quadrant x rotation")
185150
start_x = 2*abs(rotation_x) + end_x
186151
dst_new_width = 2*(abs(rotation_x) + end_x)
187-
#_log.info("({})".format(dst_new_width))
188152
end_x = start_x + end_x
189153

190154
elif rotation_x >= end_x:
191-
#_log.info("huge x rotation")
192-
dst_new_width = 2*rotation_x #+ 2*org_st_x
155+
dst_new_width = 2*rotation_x
193156

194157
if (rotation_y < end_y) and (rotation_y > 0):
195-
#_log.info("y overlap")
196158
if end_y - rotation_y > end_y/2:
197159
dst_new_height = (end_y - rotation_y) * 2
198160
start_y = dst_new_height/2 - rotation_y
@@ -202,112 +164,24 @@ def _translation(output_png_path, rotation_x, rotation_y):
202164
dst_new_height = start_y + end_y
203165

204166
elif rotation_y < 0:
205-
#_log.info("4th quadrant y rotation")
206167
start_y = 2*abs(rotation_y) + end_y
207168
dst_new_height = 2*(abs(rotation_y) + end_y)
208169
end_y = start_y + end_y
209170

210171
elif rotation_y >= end_y:
211-
#_log.info("huge y rotation")
212-
dst_new_height = 2*rotation_y #+ 2*org_st_y
172+
dst_new_height = 2*rotation_y
213173

214-
#_log.info("start y, start x: ({}, {})".format(start_y, start_x))
215-
#_log.info("end y, end x: ({}, {})".format(end_y, end_x))
216-
#_log.info("dwidth, dheight: ({}, {})".format(dst_new_width, dst_new_height))
217-
#_log.info("-" * 80)
218-
219-
#new_buffered_image = BufferedImage(dst_new_width + org_st_x + 1, dst_new_height + org_st_y + 1, BufferedImage.TYPE_INT_ARGB)
220-
new_buffered_image = BufferedImage(dst_new_width + 1, dst_new_height + 1, BufferedImage.TYPE_INT_ARGB)
174+
new_buffered_image = BufferedImage(dst_new_width + 1, dst_new_height + 1, BufferedImage.TYPE_INT_ARGB)
221175
g2d = new_buffered_image.createGraphics()
222176
g2d.setComposite(AlphaComposite.Clear)
223177
g2d.fillRect(0, 0, dst_new_width, dst_new_height)
224178

225-
#try to assemble image
226179
for row_y in xrange(start_y, end_y + 1):
227180
for column_x in xrange(start_x, end_x + 1):
228181
if row_y - start_y < buffered_image.getHeight() and column_x - start_x < buffered_image.getWidth():
229182
new_buffered_image.setRGB(column_x,row_y, buffered_image_matrix[row_y-start_y][column_x-start_x])
230-
231-
#rgb = java.awt.Color(255,20,147)
232-
#new_buffered_image.setRGB(dst_new_width/2, dst_new_height/2, rgb.getRGB())
233183
return new_buffered_image
234184

235-
# def _translation_to_rotation_point(img, rotation_x, rotation_y):
236-
#
237-
# dst_new_width, dst_new_height = None, None
238-
# half_old_width, half_old_height = None, None
239-
# while True:
240-
# dst_new_width, dst_new_height = rotation_x * 2, rotation_y * 2
241-
# half_old_width, half_old_height = img.getWidth() / 2, img.getHeight() / 2
242-
# start_x, start_y = rotation_x - half_old_height, rotation_y - half_old_width
243-
# end_x, end_y = rotation_x + img.getHeight() - half_old_height, rotation_y + img.getWidth() - half_old_width
244-
#
245-
# if start_x >= 0 and start_y >= 0 and end_x >= 0 and end_y >= 0:
246-
# break
247-
# else:
248-
# rotation_x *= 2
249-
# rotation_y *= 2
250-
#
251-
#
252-
# bufferedImage = BufferedImage(dst_new_height, dst_new_width, BufferedImage.TYPE_INT_ARGB)
253-
# g2d = bufferedImage.createGraphics()
254-
# g2d.setComposite(AlphaComposite.Clear)
255-
# g2d.fillRect(0, 0, dst_new_width, dst_new_height)
256-
#
257-
# img_matrix = [[img.getRGB(row_index, column_index) for column_index in xrange(img.getHeight())] for row_index in xrange(img.getWidth())]
258-
#
259-
# transposed_img_matrix = _transpose_matrix(img_matrix)
260-
#
261-
# for row_index, old_row_index in zip(xrange(start_x, end_x + 1), xrange(len(transposed_img_matrix))):
262-
# for column_index, old_column_index in zip(xrange(start_y, end_y + 1), xrange(len(transposed_img_matrix[0]))):
263-
# bufferedImage.setRGB(column_index, row_index, transposed_img_matrix[old_row_index][old_column_index])
264-
#
265-
# return bufferedImage
266-
#
267-
# def _scale_image(output_png_path):
268-
# bufferd_image = _create_buffered_image(ImageIcon(output_png_path).getImage())
269-
#
270-
# width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
271-
#
272-
# bufferd_image_matrix = [[bufferd_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)]
273-
#
274-
# bufferd_image_matrix = _transpose_matrix(bufferd_image_matrix)
275-
#
276-
# x_coords_list, y_coords_list = [], []
277-
#
278-
# m, n = len(bufferd_image_matrix), len(bufferd_image_matrix[0])
279-
# for y in xrange(m):
280-
# for x in xrange(n):
281-
# pixel = bufferd_image_matrix[y][x]
282-
# if (pixel >> 24) != 0x00:
283-
# x_coords_list.append(x)
284-
# y_coords_list.append(y)
285-
#
286-
# if len(x_coords_list) == 0 or len(y_coords_list) == 0:
287-
# return
288-
#
289-
# start_x, start_y = min(x_coords_list), min(y_coords_list)
290-
# max_x, max_y = max(x_coords_list), max(y_coords_list)
291-
#
292-
# m, n = (max_y - start_y + 1), (max_x + 1)
293-
#
294-
# old_y_limit = len(bufferd_image_matrix)
295-
# old_x_limit = len(bufferd_image_matrix[0])
296-
#
297-
# new_image_matrix = [[bufferd_image_matrix[old_y][old_x]for _, old_x in zip(xrange(n), xrange(start_x, old_x_limit))] for _, old_y in zip(xrange(m),xrange(start_y,old_y_limit))]
298-
#
299-
# new_image_matrix = _transpose_matrix(new_image_matrix)
300-
#
301-
# m, n = len(new_image_matrix), len(new_image_matrix[0])
302-
#
303-
# final_image = BufferedImage(m, n, bufferd_image.getType())
304-
#
305-
# for x in xrange(m):
306-
# for y in xrange(n):
307-
# final_image.setRGB(x, y, new_image_matrix[x][y])
308-
#
309-
# return final_image
310-
311185
def _transpose_matrix(matrix):
312186
m, n = len(matrix), len(matrix[0])
313187
transposed_matrix = [[matrix[y][x] for y in xrange(m)] for x in xrange(n)]
@@ -330,21 +204,18 @@ def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path):
330204
if 'width' in root.attrib and float((root.attrib['width']).replace('px', '')) <= 0:
331205
root.attrib['width'] = '1'
332206

333-
if 'viewBox' not in root.attrib:
334-
for child in root:
335-
if re.search('.*}g', child.tag) != None:
336-
if 'transform' in child.attrib:
337-
matrix_transform_attrib = child.attrib['transform']
338-
matrix_transform_attrib = re.sub(r"matrix\((\s?-?[0-9]+(\.[0-9]*)?,){4}", "matrix(1, 0, 0, 1,", matrix_transform_attrib)
339-
child.attrib['transform'] = matrix_transform_attrib
340-
break
341-
if 'viewBox' in root.attrib:
342-
del root.attrib['viewBox']
343-
344-
for child in root:
207+
208+
for child in tree.iter():
209+
if re.search('.*}g', child.tag) != None:
210+
if 'transform' in child.attrib:
211+
matrix_transform_attrib = child.attrib['transform']
212+
matrix_transform_attrib = re.sub(r"matrix\((\s?-?[0-9]+(\.[0-9]*)?,){4}", "matrix(1, 0, 0, 1,", matrix_transform_attrib)
213+
child.attrib['transform'] = matrix_transform_attrib
214+
215+
for child in tree.iter():
345216
if re.search('.*}text', child.tag) != None:
346217
child.attrib['x'] = '3'
347-
child.attrib['y'] = '22'
218+
child.attrib['y'] = '23'
348219

349220
list_of_text_parts = (child.text).split('\n')
350221
child.text = (child.text).replace(child.text, '')
@@ -355,11 +226,12 @@ def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path):
355226
else:
356227
dy_font_size = 12 # default value
357228
for text_part in list_of_text_parts:
358-
ET.SubElement(child, namespace_tag + 'tspan', x = '0', dy = str(dy_value))
229+
ET.SubElement(child, namespace_tag + 'tspan', x = '3', dy = str(dy_value))
359230
dy_value = dy_value + dy_font_size
360231
tspan_list = child.findall(namespace_tag + 'tspan')
361232
for index, tspan_element in enumerate(tspan_list):
362233
tspan_element.text = list_of_text_parts[index]
234+
363235
tree.write(svg_output_path)
364236

365237

0 commit comments

Comments
 (0)