@@ -849,6 +849,15 @@ func postProcess(ctx context.Context, plugin d2plugin.Plugin, in []byte) ([]byte
849849}
850850
851851func render (ctx context.Context , ms * xmain.State , compileDur time.Duration , plugin d2plugin.Plugin , opts d2svg.RenderOpts , inputPath , outputPath string , bundle , forceAppendix bool , ruler * textmeasure.Ruler , diagram * d2target.Diagram , ext exportExtension , asciiMode string , wantPreview bool ) (_ [][]byte , written bool , _ error ) {
852+ if ext == PNG {
853+ var encoder rasterPNGEncoder
854+ defer encoder .close ()
855+ return renderWithPNGEncoder (ctx , ms , compileDur , plugin , opts , inputPath , outputPath , bundle , forceAppendix , ruler , diagram , ext , asciiMode , wantPreview , & encoder )
856+ }
857+ return renderWithPNGEncoder (ctx , ms , compileDur , plugin , opts , inputPath , outputPath , bundle , forceAppendix , ruler , diagram , ext , asciiMode , wantPreview , nil )
858+ }
859+
860+ func renderWithPNGEncoder (ctx context.Context , ms * xmain.State , compileDur time.Duration , plugin d2plugin.Plugin , opts d2svg.RenderOpts , inputPath , outputPath string , bundle , forceAppendix bool , ruler * textmeasure.Ruler , diagram * d2target.Diagram , ext exportExtension , asciiMode string , wantPreview bool , pngEncoder * rasterPNGEncoder ) (_ [][]byte , written bool , _ error ) {
852861 if diagram .Name != "" {
853862 ext := filepath .Ext (outputPath )
854863 outputPath = strings .TrimSuffix (outputPath , ext )
@@ -895,7 +904,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
895904 var boards [][]byte
896905 for _ , dl := range diagram .Layers {
897906 childPreview := wantPreview && diagram .IsFolderOnly && len (boards ) == 0
898- childrenBoards , childWritten , err := render (ctx , ms , compileDur , plugin , opts , inputPath , layersOutputPath , bundle , forceAppendix , ruler , dl , ext , asciiMode , childPreview )
907+ childrenBoards , childWritten , err := renderWithPNGEncoder (ctx , ms , compileDur , plugin , opts , inputPath , layersOutputPath , bundle , forceAppendix , ruler , dl , ext , asciiMode , childPreview , pngEncoder )
899908 written = written || childWritten
900909 if err != nil {
901910 return boards , written , err
@@ -904,7 +913,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
904913 }
905914 for _ , dl := range diagram .Scenarios {
906915 childPreview := wantPreview && diagram .IsFolderOnly && len (boards ) == 0
907- childrenBoards , childWritten , err := render (ctx , ms , compileDur , plugin , opts , inputPath , scenariosOutputPath , bundle , forceAppendix , ruler , dl , ext , asciiMode , childPreview )
916+ childrenBoards , childWritten , err := renderWithPNGEncoder (ctx , ms , compileDur , plugin , opts , inputPath , scenariosOutputPath , bundle , forceAppendix , ruler , dl , ext , asciiMode , childPreview , pngEncoder )
908917 written = written || childWritten
909918 if err != nil {
910919 return boards , written , err
@@ -913,7 +922,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
913922 }
914923 for _ , dl := range diagram .Steps {
915924 childPreview := wantPreview && diagram .IsFolderOnly && len (boards ) == 0
916- childrenBoards , childWritten , err := render (ctx , ms , compileDur , plugin , opts , inputPath , stepsOutputPath , bundle , forceAppendix , ruler , dl , ext , asciiMode , childPreview )
925+ childrenBoards , childWritten , err := renderWithPNGEncoder (ctx , ms , compileDur , plugin , opts , inputPath , stepsOutputPath , bundle , forceAppendix , ruler , dl , ext , asciiMode , childPreview , pngEncoder )
917926 written = written || childWritten
918927 if err != nil {
919928 return boards , written , err
@@ -923,7 +932,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
923932
924933 if ! diagram .IsFolderOnly {
925934 start := time .Now ()
926- out , boardWritten , err := _render (ctx , ms , plugin , opts , inputPath , boardOutputPath , bundle , forceAppendix , ruler , diagram , ext , asciiMode , wantPreview )
935+ out , boardWritten , err := _renderWithPNGEncoder (ctx , ms , plugin , opts , inputPath , boardOutputPath , bundle , forceAppendix , ruler , diagram , ext , asciiMode , wantPreview , pngEncoder )
927936 written = written || boardWritten
928937 if err != nil {
929938 return boards , written , err
@@ -940,7 +949,7 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
940949
941950func renderSingle (ctx context.Context , ms * xmain.State , compileDur time.Duration , plugin d2plugin.Plugin , opts d2svg.RenderOpts , inputPath , outputPath string , bundle , forceAppendix bool , ruler * textmeasure.Ruler , diagram * d2target.Diagram , outputFormat exportExtension , asciiMode string , wantPreview bool ) ([][]byte , bool , error ) {
942951 start := time .Now ()
943- out , written , err := _render (ctx , ms , plugin , opts , inputPath , outputPath , bundle , forceAppendix , ruler , diagram , outputFormat , asciiMode , wantPreview )
952+ out , written , err := _renderWithPNGEncoder (ctx , ms , plugin , opts , inputPath , outputPath , bundle , forceAppendix , ruler , diagram , outputFormat , asciiMode , wantPreview , nil )
944953 if err != nil {
945954 return [][]byte {}, written , err
946955 }
@@ -952,6 +961,10 @@ func renderSingle(ctx context.Context, ms *xmain.State, compileDur time.Duration
952961}
953962
954963func _render (ctx context.Context , ms * xmain.State , plugin d2plugin.Plugin , opts d2svg.RenderOpts , inputPath , outputPath string , bundle , forceAppendix bool , ruler * textmeasure.Ruler , diagram * d2target.Diagram , outputFormat exportExtension , asciiMode string , wantPreview bool ) ([]byte , bool , error ) {
964+ return _renderWithPNGEncoder (ctx , ms , plugin , opts , inputPath , outputPath , bundle , forceAppendix , ruler , diagram , outputFormat , asciiMode , wantPreview , nil )
965+ }
966+
967+ func _renderWithPNGEncoder (ctx context.Context , ms * xmain.State , plugin d2plugin.Plugin , opts d2svg.RenderOpts , inputPath , outputPath string , bundle , forceAppendix bool , ruler * textmeasure.Ruler , diagram * d2target.Diagram , outputFormat exportExtension , asciiMode string , wantPreview bool , pngEncoder * rasterPNGEncoder ) ([]byte , bool , error ) {
955968 if outputFormat == TXT {
956969 var charsetType charset.Type
957970 switch asciiMode {
@@ -1005,14 +1018,14 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
10051018 return svg , false , err
10061019 }
10071020 cacheImages := ms .Env .Getenv ("IMG_CACHE" ) == "1"
1008- out , err := renderPNG (ctx , inputPath , cacheImages , diagram , * renderOpts )
1021+ out , err := renderPNGWithEncoder (ctx , inputPath , cacheImages , diagram , * renderOpts , pngEncoder )
10091022 if err != nil {
10101023 return svg , false , err
10111024 }
10121025 var withExif []byte
10131026 err = runFinalizer (ctx , func () error {
10141027 var finalizeErr error
1015- withExif , finalizeErr = png .AddExif (out )
1028+ withExif , finalizeErr = png .AddExifToEncoderOutputInPlace (out )
10161029 return finalizeErr
10171030 })
10181031 if err != nil {
0 commit comments