code: def _spec_to_mimebundle_with_engine(
      spec: dict,
      format: Literal["png", "svg", "pdf", "vega"],
      mode: Literal["vega-lite", "vega"],
      format_locale: Optional[Union[str, dict]] = None,
      time_format_locale: Optional[Union[str, dict]] = None,
      **kwargs,
  ) -> Union[dict, Tuple[dict, dict]]:
      """Helper for Vega-Lite to mimebundle conversions that require an engine
  
      Parameters
      ----------
      spec : dict
          a dictionary representing a vega-lite plot spec
      format : string {'png', 'svg', 'pdf', 'vega'}
          the format of the mimebundle to be returned
      mode : string {'vega-lite', 'vega'}
          The rendering mode.
      engine: string {'vl-convert', 'altair_saver'}
          the conversion engine to use
      format_locale : str or dict
          d3-format locale name or dictionary. Defaults to "en-US" for United States English.
          See https://github.com/d3/d3-format/tree/main/locale for available names and example
          definitions.
      time_format_locale : str or dict
          d3-time-format locale name or dictionary. Defaults to "en-US" for United States English.
          See https://github.com/d3/d3-time-format/tree/main/locale for available names and example
          definitions.
      **kwargs :
          Additional arguments will be passed to the conversion function
      """
      # Normalize the engine string (if any) by lower casing
      # and removing underscores and hyphens
      engine = kwargs.pop("engine", None)
      normalized_engine = _validate_normalize_engine(engine, format)
  
      if normalized_engine == "vlconvert":
          vlc = import_vl_convert()
          vl_version = vl_version_for_vl_convert()
          if format == "vega":
              if mode == "vega":
                  vg = spec
              else:
                  vg = vlc.vegalite_to_vega(spec, vl_version=vl_version)
              return {"application/vnd.vega.v5+json": vg}
          elif format == "svg":
              if mode == "vega":
                  svg = vlc.vega_to_svg(
                      spec,
                      format_locale=format_locale,
                      time_format_locale=time_format_locale,
                  )
              else:
                  svg = vlc.vegalite_to_svg(
                      spec,
                      vl_version=vl_version,
                      format_locale=format_locale,
                      time_format_locale=time_format_locale,
                  )
              return {"image/svg+xml": svg}
          elif format == "png":
              scale = kwargs.get("scale_factor", 1)
              # The default ppi for a PNG file is 72
              default_ppi = 72
              ppi = kwargs.get("ppi", default_ppi)
              if mode == "vega":
                  png = vlc.vega_to_png(
                      spec,
                      scale=scale,
                      ppi=ppi,
                      format_locale=format_locale,
                      time_format_locale=time_format_locale,
                  )
              else:
                  png = vlc.vegalite_to_png(
                      spec,
                      vl_version=vl_version,
                      scale=scale,
                      ppi=ppi,
                      format_locale=format_locale,
                      time_format_locale=time_format_locale,
                  )
              factor = ppi / default_ppi
              w, h = _pngxy(png)
              return {"image/png": png}, {
                  "image/png": {
                      "width": w / factor,
                      "height": h / factor,
                  }
              }
          elif format == "pdf":
              scale = kwargs.get("scale_factor", 1)
              if mode == "vega":
                  pdf = vlc.vega_to_pdf(
                      spec,
                      scale=scale,
                      format_locale=format_locale,
                      time_format_locale=time_format_locale,
                  )
              else:
                  pdf = vlc.vegalite_to_pdf(
                      spec,
                      vl_version=vl_version,
                      scale=scale,
                      format_locale=format_locale,
                      time_format_locale=time_format_locale,
                  )
              return {"application/pdf": pdf}
          else:
              # This should be validated above
              # but raise exception for the sake of future development
              raise ValueError(
                  "Unexpected format {fmt!r}".format(fmt=format)
              )
      elif normalized_engine == "altairsaver":
          warnings.warn(
              "The altair_saver export engine is deprecated and will be removed in a future version.\n"
              "Please migrate to the vl-convert engine",
              AltairDeprecationWarning,
              stacklevel=1,
          )
          import altair_saver
  
          return altair_saver.render(spec, format, mode=mode, **kwargs)
      else:
          # This should be validated above
          # but raise exception for the sake of future development
          raise ValueError(
              "Unexpected normalized_engine {eng!r}".format(
                  eng=normalized_engine
              )
          )
  
file_path: altair/altair/utils/mimebundle.py
imports: from typing import Literal, Optional, Union, cast, Tuple
  from .deprecation import AltairDeprecationWarning
  from .html import spec_to_html
  from ._importers import import_vl_convert, vl_version_for_vl_convert
  import struct
  import warnings
label_desc: Use image node_id 2 for calling a global function with example usage: _spec_to_mimebundle_with_engine(spec, format, mode, format_locale, time_format_locale) and returns: dict, altair_saver, dict, dict,  dict, dict