From 141f2458d272e2fc650f43a68a9917797850dea0 Mon Sep 17 00:00:00 2001 From: Christopher Albert Date: Sat, 23 Aug 2025 21:52:42 +0200 Subject: [PATCH 1/2] fix: replace binary characters with [binary] placeholder in ASCII output docs - Replace %???? binary characters with %[binary] placeholder in scale_examples.md - Add BACKLOG.md to track current work on ASCII backend PDF output issue - Addresses issue where ASCII backend incorrectly generates PDF binary data fixes #220 --- BACKLOG.md | 8 ++++++++ doc/example/scale_examples.md | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 BACKLOG.md diff --git a/BACKLOG.md b/BACKLOG.md new file mode 100644 index 00000000..6116c6b1 --- /dev/null +++ b/BACKLOG.md @@ -0,0 +1,8 @@ +# Development Backlog + +## TODO (Ordered by Priority) + +## DOING (Current Work) +- [x] #220: Fix ASCII backend generating PDF binary content instead of text output (branch: fix/comprehensive-ascii-cleaning) + +## DONE (Completed) \ No newline at end of file diff --git a/doc/example/scale_examples.md b/doc/example/scale_examples.md index 4536cc5c..c9613778 100644 --- a/doc/example/scale_examples.md +++ b/doc/example/scale_examples.md @@ -89,7 +89,7 @@ end program scale_examples ASCII output: ``` %PDF-1.4 -%???? +%[binary] 2 0 obj << /Type /Catalog @@ -173,7 +173,7 @@ startxref ASCII output: ``` %PDF-1.4 -%???? +%[binary] 2 0 obj << /Type /Catalog From 8e8cd3dc50e1a9e2b965cf27cd395544253a2cfe Mon Sep 17 00:00:00 2001 From: Christopher Albert Date: Sat, 23 Aug 2025 22:03:02 +0200 Subject: [PATCH 2/2] fix: ASCII backend generating PDF binary content instead of text output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes issue #220 where .txt files generated PDF binary data instead of ASCII text. Root cause: The switch_backend_if_needed() function was hardcoding current_backend='ascii' instead of detecting the actual backend type. When a figure switched from ASCII->PDF->ASCII, the backend remained PDF but the switching logic incorrectly assumed it was ASCII, skipping the necessary switch back to ASCII backend. Solution: Use polymorphic type checking (select type) to properly detect the current backend type by examining the allocated backend object. Changes: - Replace hardcoded backend assumption with runtime type detection - Add proper imports for all backend context types - Maintain existing PNG backend workaround for segfault prevention Testing: - Verified with scale_examples.f90: .txt files now contain ASCII text - File type detection confirms files are "ASCII text" not "PDF document" - Sequential backend switching (ASCII->PDF->ASCII) works correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/fortplot_rendering.f90 | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/fortplot_rendering.f90 b/src/fortplot_rendering.f90 index 8bf2157c..dfc4e81d 100644 --- a/src/fortplot_rendering.f90 +++ b/src/fortplot_rendering.f90 @@ -279,13 +279,36 @@ end subroutine clear_streamlines subroutine switch_backend_if_needed(self, target_backend) !! Switch backend if current doesn't match target use fortplot_utils, only: initialize_backend + use fortplot_ascii, only: ascii_context + use fortplot_pdf, only: pdf_context + use fortplot_png, only: png_context + use fortplot_raster, only: raster_context + use fortplot_gltf, only: gltf_context class(figure_t), intent(inout) :: self character(len=*), intent(in) :: target_backend character(len=20) :: current_backend - ! Get current backend type (stub implementation) - current_backend = 'ascii' ! Default assumption + ! Detect current backend type using polymorphic type checking + current_backend = 'unknown' + if (allocated(self%backend)) then + select type (backend => self%backend) + type is (ascii_context) + current_backend = 'ascii' + type is (pdf_context) + current_backend = 'pdf' + type is (png_context) + current_backend = 'png' + type is (raster_context) + current_backend = 'png' ! Treat base raster as PNG + type is (gltf_context) + current_backend = 'gltf' + class default + current_backend = 'ascii' ! Default fallback + end select + else + current_backend = 'ascii' ! Default for unallocated backend + end if ! For now, avoid switching to PNG backend to prevent segfaults ! This is a temporary workaround for the raster backend corruption issue