export_task.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Can optimise the output (ie, output to svgo) if requested.
  12. """
  13. if license_enabled:
  14. final_svg = svg.add_license(emoji_svg, license)
  15. else:
  16. final_svg = emoji_svg
  17. # write SVG out to file
  18. if not optimise: # (svg)
  19. files.try_write(final_svg, out_path, "final SVG")
  20. else: # (svgo)
  21. tmp_svg_path = '.tmp' + name + '.svg'
  22. files.try_write(final_svg, tmp_svg_path, "temporary SVG")
  23. image_proc.optimise_svg(tmp_svg_path, out_path)
  24. os.remove(tmp_svg_path)
  25. def to_raster(emoji_svg, out_path, renderer, format, size, name):
  26. """
  27. Raster exporting function. Can export to any of orxporter's supported raster formats.
  28. Creates and deletes temporary SVG files. Might also create and delete temporary PNG files depending on the format.
  29. """
  30. tmp_svg_path = '.tmp' + name + '.svg'
  31. tmp_png_path = '.tmp' + name + '.png'
  32. # try to write a temporary SVG.
  33. files.try_write(emoji_svg, tmp_svg_path, "temporary SVG")
  34. if format == "png":
  35. # one-step process
  36. image_proc.render_svg(tmp_svg_path, out_path, renderer, size)
  37. else:
  38. # two-step process
  39. image_proc.render_svg(tmp_svg_path, tmp_png_path, renderer, size)
  40. if format == "pngc":
  41. image_proc.crush_png(tmp_png_path, out_path)
  42. elif format == "webp":
  43. image_proc.convert_webp(tmp_png_path, out_path)
  44. elif format == "avif":
  45. image_proc.convert_avif(tmp_png_path, out_path)
  46. elif format == "flif":
  47. image_proc.convert_flif(tmp_png_path, out_path)
  48. else:
  49. os.remove(tmp_svg_path)
  50. os.remove(tmp_png_path)
  51. raise ValueError(f"This function wasn't given a correct format! ({format})")
  52. # delete temporary files
  53. os.remove(tmp_svg_path)
  54. if format != "png":
  55. os.remove(tmp_png_path)