export_task.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import pathlib
  3. import subprocess
  4. import files
  5. import svg
  6. import image_proc
  7. def to_svg(emoji_svg, out_path, name, license=None, license_enabled=True, optimise=False):
  8. """
  9. SVG exporting function. Doesn't create temporary files.
  10. Will append license <metadata> if requested.
  11. """
  12. if license_enabled:
  13. final_svg = svg.add_license(emoji_svg, license)
  14. else:
  15. final_svg = emoji_svg
  16. # write SVG out to file
  17. files.try_write(final_svg, out_path, "final SVG")
  18. def to_raster(emoji_svg, out_path, renderer, format, size, name):
  19. """
  20. Raster exporting function. Can export to any of orxporter's supported raster formats.
  21. Creates and deletes temporary SVG files. Might also create and delete temporary PNG files depending on the format.
  22. """
  23. tmp_svg_path = '.tmp' + name + '.svg'
  24. tmp_png_path = '.tmp' + name + '.png'
  25. # try to write a temporary SVG.
  26. files.try_write(emoji_svg, tmp_svg_path, "temporary SVG")
  27. if format == "png":
  28. # one-step process
  29. image_proc.render_svg(tmp_svg_path, out_path, renderer, size)
  30. else:
  31. # two-step process
  32. image_proc.render_svg(tmp_svg_path, tmp_png_path, renderer, size)
  33. if format == "pngc":
  34. image_proc.crush_png(tmp_png_path, out_path)
  35. elif format == "webp":
  36. image_proc.convert_webp(tmp_png_path, out_path)
  37. elif format == "jxl":
  38. image_proc.convert_jxl(tmp_png_path, out_path)
  39. else:
  40. os.remove(tmp_svg_path)
  41. os.remove(tmp_png_path)
  42. raise ValueError(f"This function wasn't given a correct format! ({format})")
  43. # delete temporary files
  44. os.remove(tmp_svg_path)
  45. if format != "png":
  46. os.remove(tmp_png_path)