svgs_2_pngs.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. # Basic exporter for svg icons
  3. from os import listdir
  4. from os.path import isfile, join, dirname, realpath
  5. import subprocess
  6. import sys
  7. import rsvg
  8. import cairo
  9. last_svg_path = None
  10. last_svg_data = None
  11. SCRIPT_FOLDER = dirname(realpath(__file__)) + '/'
  12. theme_dir_base = SCRIPT_FOLDER + '../../scene/resources/default_theme/'
  13. theme_dir_source = theme_dir_base + 'source/'
  14. icons_dir_base = SCRIPT_FOLDER + '../editor/icons/'
  15. icons_dir_2x = icons_dir_base + '2x/'
  16. icons_dir_source = icons_dir_base + 'source/'
  17. def svg_to_png(svg_path, png_path, dpi):
  18. global last_svg_path, last_svg_data
  19. zoom = int(dpi / 90)
  20. if last_svg_path != svg_path:
  21. last_svg_data = open(svg_path, 'r').read()
  22. last_svg_path = svg_path
  23. svg = rsvg.Handle(data=last_svg_data)
  24. img = cairo.ImageSurface(
  25. cairo.FORMAT_ARGB32,
  26. svg.props.width * zoom,
  27. svg.props.height * zoom
  28. )
  29. ctx = cairo.Context(img)
  30. ctx.set_antialias(cairo.ANTIALIAS_DEFAULT)
  31. ctx.scale(zoom, zoom)
  32. svg.render_cairo(ctx)
  33. img.write_to_png('%s.png' % png_path)
  34. svg.close()
  35. def export_icons():
  36. svgs_path = icons_dir_source
  37. file_names = [f for f in listdir(svgs_path) if isfile(join(svgs_path, f))]
  38. for file_name in file_names:
  39. # name without extensions
  40. name_only = file_name.replace('.svg', '')
  41. out_icon_names = [name_only] # export to a png with the same file name
  42. theme_out_icon_names = []
  43. # special cases
  44. if special_icons.has_key(name_only):
  45. special_icon = special_icons[name_only]
  46. if type(special_icon) is dict:
  47. if special_icon.get('avoid_self'):
  48. out_icon_names = []
  49. if special_icon.has_key('output_names'):
  50. out_icon_names += special_icon['output_names']
  51. if special_icon.has_key('theme_output_names'):
  52. theme_out_icon_names += special_icon['theme_output_names']
  53. source_path = '%s%s.svg' % (svgs_path, name_only)
  54. for out_icon_name in out_icon_names:
  55. svg_to_png(source_path, icons_dir_base + out_icon_name, 90)
  56. svg_to_png(source_path, icons_dir_2x + out_icon_name, 180)
  57. for theme_out_icon_name in theme_out_icon_names:
  58. svg_to_png(source_path, theme_dir_base + theme_out_icon_name, 90)
  59. def export_theme():
  60. svgs_path = theme_dir_source
  61. file_names = [f for f in listdir(svgs_path) if isfile(join(svgs_path, f))]
  62. for file_name in file_names:
  63. # name without extensions
  64. name_only = file_name.replace('.svg', '')
  65. out_icon_names = [name_only] # export to a png with the same file name
  66. # special cases
  67. if theme_icons.has_key(name_only):
  68. special_icon = theme_icons[name_only]
  69. if type(special_icon) is dict:
  70. if special_icon.has_key('output_names'):
  71. out_icon_names += special_icon['output_names']
  72. source_path = '%s%s.svg' % (svgs_path, name_only)
  73. for out_icon_name in out_icon_names:
  74. svg_to_png(source_path, theme_dir_base + out_icon_name, 90)
  75. # special cases for icons that will be exported to multiple target pngs or that require transforms.
  76. special_icons = {
  77. 'icon_add_track': dict(
  78. output_names=['icon_add'],
  79. theme_output_names=['icon_add', 'icon_zoom_more']
  80. ),
  81. 'icon_new': dict(output_names=['icon_file']),
  82. 'icon_animation_tree_player': dict(output_names=['icon_animation_tree']),
  83. 'icon_tool_rotate': dict(
  84. output_names=['icon_reload'],
  85. theme_output_names=['icon_reload']
  86. ),
  87. 'icon_multi_edit': dict(output_names=['icon_multi_node_edit']),
  88. 'icon_folder': dict(
  89. output_names=['icon_load', 'icon_open'],
  90. theme_output_names=['icon_folder']
  91. ),
  92. 'icon_file_list': dict(output_names=['icon_enum']),
  93. 'icon_collision_2d': dict(output_names=['icon_collision_polygon_2d', 'icon_polygon_2d']),
  94. 'icon_class_list': dict(output_names=['icon_filesystem']),
  95. 'icon_color_ramp': dict(output_names=['icon_graph_color_ramp']),
  96. 'icon_translation': dict(output_names=['icon_p_hash_translation']),
  97. 'icon_shader': dict(output_names=['icon_shader_material', 'icon_material_shader']),
  98. 'icon_canvas_item_shader_graph': dict(output_names=['icon_material_shader_graph']),
  99. 'icon_color_pick': dict(theme_output_names=['icon_color_pick'], avoid_self=True),
  100. 'icon_play': dict(theme_output_names=['icon_play']),
  101. 'icon_stop': dict(theme_output_names=['icon_stop']),
  102. 'icon_zoom_less': dict(theme_output_names=['icon_zoom_less'], avoid_self=True),
  103. 'icon_zoom_reset': dict(theme_output_names=['icon_zoom_reset'], avoid_self=True)
  104. }
  105. theme_icons = {
  106. 'icon_close': dict(output_names=['close', 'close_hl']),
  107. 'tab_menu': dict(output_names=['tab_menu_hl'])
  108. }
  109. export_icons()
  110. export_theme()