46
46
47
47
# TODO: refactor to single mediaconverter class together with wavconverter
48
48
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))
51
49
batik_home_dir = helpers .config .get ("PATHS" , "batik_home" )
52
50
batik_jar_path = os .path .join (batik_home_dir , _BATIK_CLI_JAR )
53
51
if not os .path .exists (batik_jar_path ):
@@ -89,7 +87,6 @@ def convert(input_svg_path, rotation_x, rotation_y):
89
87
png_converter .transcode (input_svg_image , output_png_image )
90
88
assert os .path .exists (output_png_path )
91
89
92
- # TODO: uncomment this once all remaining bugs have been fixed!
93
90
final_image = _translation (output_png_path , rotation_x , rotation_y )
94
91
95
92
if final_image is None :
@@ -126,22 +123,7 @@ def _translation(output_png_path, rotation_x, rotation_y):
126
123
buffered_image_matrix = [[buffered_image .getRGB (i , j ) for j in xrange (height )] for i in xrange (width )]
127
124
buffered_image_matrix = _transpose_matrix (buffered_image_matrix )
128
125
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
- '''
139
126
_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
145
127
146
128
start_x , start_y = 0 , 0
147
129
end_x , end_y = n , m
@@ -150,27 +132,11 @@ def _translation(output_png_path, rotation_x, rotation_y):
150
132
_log .info ("ANTENNA-ERROR" )
151
133
return buffered_image
152
134
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
-
168
135
dst_new_width = end_x
169
136
dst_new_height = end_y
170
137
171
138
# overlapping x enhancement
172
139
if (rotation_x < end_x ) and (rotation_x > 0 ):
173
- #_log.info("x overlap")
174
140
if end_x - rotation_x > end_x / 2 :
175
141
dst_new_width = (end_x - rotation_x ) * 2
176
142
start_x = dst_new_width / 2 - rotation_x
@@ -181,18 +147,14 @@ def _translation(output_png_path, rotation_x, rotation_y):
181
147
182
148
# non-overlapping x enhancement
183
149
elif rotation_x < 0 :
184
- #_log.info("2nd quadrant x rotation")
185
150
start_x = 2 * abs (rotation_x ) + end_x
186
151
dst_new_width = 2 * (abs (rotation_x ) + end_x )
187
- #_log.info("({})".format(dst_new_width))
188
152
end_x = start_x + end_x
189
153
190
154
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
193
156
194
157
if (rotation_y < end_y ) and (rotation_y > 0 ):
195
- #_log.info("y overlap")
196
158
if end_y - rotation_y > end_y / 2 :
197
159
dst_new_height = (end_y - rotation_y ) * 2
198
160
start_y = dst_new_height / 2 - rotation_y
@@ -202,112 +164,24 @@ def _translation(output_png_path, rotation_x, rotation_y):
202
164
dst_new_height = start_y + end_y
203
165
204
166
elif rotation_y < 0 :
205
- #_log.info("4th quadrant y rotation")
206
167
start_y = 2 * abs (rotation_y ) + end_y
207
168
dst_new_height = 2 * (abs (rotation_y ) + end_y )
208
169
end_y = start_y + end_y
209
170
210
171
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
213
173
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 )
221
175
g2d = new_buffered_image .createGraphics ()
222
176
g2d .setComposite (AlphaComposite .Clear )
223
177
g2d .fillRect (0 , 0 , dst_new_width , dst_new_height )
224
178
225
- #try to assemble image
226
179
for row_y in xrange (start_y , end_y + 1 ):
227
180
for column_x in xrange (start_x , end_x + 1 ):
228
181
if row_y - start_y < buffered_image .getHeight () and column_x - start_x < buffered_image .getWidth ():
229
182
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())
233
183
return new_buffered_image
234
184
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
-
311
185
def _transpose_matrix (matrix ):
312
186
m , n = len (matrix ), len (matrix [0 ])
313
187
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):
330
204
if 'width' in root .attrib and float ((root .attrib ['width' ]).replace ('px' , '' )) <= 0 :
331
205
root .attrib ['width' ] = '1'
332
206
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 ():
345
216
if re .search ('.*}text' , child .tag ) != None :
346
217
child .attrib ['x' ] = '3'
347
- child .attrib ['y' ] = '22 '
218
+ child .attrib ['y' ] = '23 '
348
219
349
220
list_of_text_parts = (child .text ).split ('\n ' )
350
221
child .text = (child .text ).replace (child .text , '' )
@@ -355,11 +226,12 @@ def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path):
355
226
else :
356
227
dy_font_size = 12 # default value
357
228
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 ))
359
230
dy_value = dy_value + dy_font_size
360
231
tspan_list = child .findall (namespace_tag + 'tspan' )
361
232
for index , tspan_element in enumerate (tspan_list ):
362
233
tspan_element .text = list_of_text_parts [index ]
234
+
363
235
tree .write (svg_output_path )
364
236
365
237
0 commit comments