Skip to content

Commit 8e8cd3d

Browse files
krystophnyclaude
andcommitted
fix: ASCII backend generating PDF binary content instead of text output
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 <noreply@anthropic.com>
1 parent 141f245 commit 8e8cd3d

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/fortplot_rendering.f90

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,36 @@ end subroutine clear_streamlines
279279
subroutine switch_backend_if_needed(self, target_backend)
280280
!! Switch backend if current doesn't match target
281281
use fortplot_utils, only: initialize_backend
282+
use fortplot_ascii, only: ascii_context
283+
use fortplot_pdf, only: pdf_context
284+
use fortplot_png, only: png_context
285+
use fortplot_raster, only: raster_context
286+
use fortplot_gltf, only: gltf_context
282287
class(figure_t), intent(inout) :: self
283288
character(len=*), intent(in) :: target_backend
284289

285290
character(len=20) :: current_backend
286291

287-
! Get current backend type (stub implementation)
288-
current_backend = 'ascii' ! Default assumption
292+
! Detect current backend type using polymorphic type checking
293+
current_backend = 'unknown'
294+
if (allocated(self%backend)) then
295+
select type (backend => self%backend)
296+
type is (ascii_context)
297+
current_backend = 'ascii'
298+
type is (pdf_context)
299+
current_backend = 'pdf'
300+
type is (png_context)
301+
current_backend = 'png'
302+
type is (raster_context)
303+
current_backend = 'png' ! Treat base raster as PNG
304+
type is (gltf_context)
305+
current_backend = 'gltf'
306+
class default
307+
current_backend = 'ascii' ! Default fallback
308+
end select
309+
else
310+
current_backend = 'ascii' ! Default for unallocated backend
311+
end if
289312

290313
! For now, avoid switching to PNG backend to prevent segfaults
291314
! This is a temporary workaround for the raster backend corruption issue

0 commit comments

Comments
 (0)