From 0725bb14d5468e58b1d080fb99ce1731d9c8051c Mon Sep 17 00:00:00 2001 From: git-lakshy Date: Sat, 7 Mar 2026 14:36:35 +0530 Subject: [PATCH 1/3] [212_6]Fix export as image for jpeg,tif --- .../plugins/binary/progs/binary/convert.scm | 3 +- .../binary/progs/binary/pdftocairo.scm | 8 ++- TeXmacs/plugins/image/progs/image/pdf.scm | 6 ++ TeXmacs/progs/convert/images/tmimage.scm | 2 +- devel/212_6.md | 64 +++++++++++++++++++ src/Edit/Editor/edit_main.cpp | 9 ++- 6 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 devel/212_6.md diff --git a/TeXmacs/plugins/binary/progs/binary/convert.scm b/TeXmacs/plugins/binary/progs/binary/convert.scm index b18523bacb..5f7d403f7a 100644 --- a/TeXmacs/plugins/binary/progs/binary/convert.scm +++ b/TeXmacs/plugins/binary/progs/binary/convert.scm @@ -12,7 +12,8 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (texmacs-module (binary convert) - (:use (binary common))) + (:use (binary common) + (convert images image-format))) (define (convert-binary-candidates) (cond ((os-macos?) diff --git a/TeXmacs/plugins/binary/progs/binary/pdftocairo.scm b/TeXmacs/plugins/binary/progs/binary/pdftocairo.scm index 7aaa8fdbe0..81e97173d0 100644 --- a/TeXmacs/plugins/binary/progs/binary/pdftocairo.scm +++ b/TeXmacs/plugins/binary/progs/binary/pdftocairo.scm @@ -12,7 +12,8 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (texmacs-module (binary pdftocairo) - (:use (binary common))) + (:use (binary common) + (convert images image-format))) (define (pdftocairo-binary-candidates) (cond ((os-macos?) @@ -36,9 +37,10 @@ (tm-define (pdf-file->pdftocairo-raster x opts) (let* ((dest (assoc-ref opts 'dest)) (fullname (url-concretize dest)) - (fm (url-format fullname)) - (transp (if (== fm "png") "-transp " "")) (suffix (url-suffix fullname)) + (fm* (format-from-suffix suffix)) + (fm (if (== fm* "tif") "tiff" fm*)) + (transp (if (== fm "png") "-transp " "")) (name (string-drop-right fullname (+ 1 (string-length suffix)))) (res (get-raster-resolution opts)) (cmd (url->system (find-binary-pdftocairo)))) diff --git a/TeXmacs/plugins/image/progs/image/pdf.scm b/TeXmacs/plugins/image/progs/image/pdf.scm index bb54e62ac6..c9dded7ea9 100644 --- a/TeXmacs/plugins/image/progs/image/pdf.scm +++ b/TeXmacs/plugins/image/progs/image/pdf.scm @@ -36,6 +36,12 @@ ;;(:option "texmacs->image:raster-resolution" "300") ) +(converter pdf-file tif-file + (:require (has-binary-pdftocairo?)) + (:function-with-options pdf-file->pdftocairo-raster) + ;;(:option "texmacs->image:raster-resolution" "300") + ) + ;;(converter pdf-file postscript-document ;; (:require (has-pdftocairo?)) ;; (:shell "pdftocairo" "-eps" from to)) diff --git a/TeXmacs/progs/convert/images/tmimage.scm b/TeXmacs/progs/convert/images/tmimage.scm index 3477983094..6e2ed96126 100644 --- a/TeXmacs/progs/convert/images/tmimage.scm +++ b/TeXmacs/progs/convert/images/tmimage.scm @@ -232,7 +232,7 @@ (let* ((sufl (string-length suffix)) (surl (url->string myurl)) (sl (string-length surl))) - (set! myurl (string->url (string-append (substring surl (- sl sufl) sl) "pdf")))))) + (set! myurl (string->url (string-append (substring surl 0 (- sl sufl)) "pdf")))))) ;; TODO Handle when output file already exists (presently we overwritte without warning) diff --git a/devel/212_6.md b/devel/212_6.md new file mode 100644 index 0000000000..ae099b6f8d --- /dev/null +++ b/devel/212_6.md @@ -0,0 +1,64 @@ +# 212_6: Fix export selection as image for jpeg, tiff + +## How to Test +- Select text in a document +- File → Export selection as image → save as `.jpg` → verify file is created and contains the selection +- Repeat for `.tif` and `.tiff` +- Also test `.png` and `.pdf` works as expected + +## Issue #2728 +JPEG and TIFF formats could not be exported via "Export Selected Area as Image".PNG and PDF export worked. + +## What + +### 1. `src/Edit/Editor/edit_main.cpp` +The root cause. `print_snippet` routed jpeg/tiff through `make_raster_image`, but mupdf's `save_picture` only supports PNG (hardcoded `fz_write_pixmap_as_png`). The fix gates the bitmap path by renderer: + +```cpp +// BEFORE +bool bitmap = (s == "png" || s == "jpg" || s == "jpeg" || s == "tif" || s == "tiff"); + +// AFTER +bool bitmap= false; +#ifdef USE_MUPDF_RENDERER + bitmap = (s == "png"); +#else + bitmap = (s == "png" || s == "jpg" || s == "jpeg" || s == "tif" || s == "tiff"); +#endif +``` + +With mupdf, jpeg/tiff now go through the PDF→Scheme converter pipeline instead. + +### 2. `TeXmacs/plugins/binary/progs/binary/pdftocairo.scm` +`url-format` returned format names (via C++ glue) that didn't match pdftocairo CLI flags. Replaced with `format-from-suffix` (proper Scheme API) plus a `tif→tiff` fix because pdftocairo accepts `-tiff` but not `-tif`: + +```scheme +;; BEFORE +(fm (url-format fullname)) + +;; AFTER +(fm* (format-from-suffix suffix)) +(fm (if (== fm* "tif") "tiff" fm*)) +``` + +Also added `(convert images image-format)` import for `get-raster-resolution`. + +### 3. `TeXmacs/plugins/binary/progs/binary/convert.scm` +Added missing `(convert images image-format)` import. Without it, `get-raster-resolution` was unbound when ImageMagick converter was invoked, causing a crash. + +### 4. `TeXmacs/plugins/image/progs/image/pdf.scm` +Registered `pdf-file → tif-file` converter via pdftocairo. Previously only ImageMagick had a TIFF converter, so systems without ImageMagick could not export TIFF. + +### 5. `TeXmacs/progs/convert/images/tmimage.scm` +Fixed `substring` indexing bug in the format fallback path. The old code extracted the suffix instead of the path prefix, producing an invalid URL when falling back to PDF: + +```scheme +;; BEFORE — extracts suffix portion, produces garbage like "tiffpdf" +(substring surl (- sl sufl) sl) + +;; AFTER — extracts path without suffix, produces correct "/path/file.pdf" +(substring surl 0 (- sl sufl)) +``` + +## Why +mupdf's `save_picture` only supports PNG output. The old code unconditionally treated jpeg/tiff as bitmap formats, sending them through a rendering path that silently failed. The converter pipeline (pdftocairo/ImageMagick) also had bugs: missing imports, missing converter registrations, incorrect CLI flag generation, and a broken fallback path. diff --git a/src/Edit/Editor/edit_main.cpp b/src/Edit/Editor/edit_main.cpp index 967d706af2..f8d35b95b6 100644 --- a/src/Edit/Editor/edit_main.cpp +++ b/src/Edit/Editor/edit_main.cpp @@ -336,8 +336,13 @@ edit_main_rep::print_snippet (url name, tree t, bool conserve_preamble) { assign (subtree (et, temp_root), copy (t)); string s= suffix (name); - bool bitmap= - (s == "png" || s == "jpg" || s == "jpeg" || s == "tif" || s == "tiff"); + bool bitmap= false; +#ifdef USE_MUPDF_RENDERER + bitmap = (s == "png"); +#else + bitmap= (s == "png" || s == "jpg" || s == "jpeg" || s == "tif" + || s == "tiff"); +#endif #ifndef QTTEXMACS bitmap= false; #endif From 5700bbee0e21604115f8423745bad8f251c37f56 Mon Sep 17 00:00:00 2001 From: git-lakshy Date: Sat, 7 Mar 2026 14:56:42 +0530 Subject: [PATCH 2/3] clang format --- TeXmacs/progs/convert/images/tmimage.scm | 2 +- devel/212_6.md | 13 +------------ src/Edit/Editor/edit_main.cpp | 8 ++++---- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/TeXmacs/progs/convert/images/tmimage.scm b/TeXmacs/progs/convert/images/tmimage.scm index 6e2ed96126..3477983094 100644 --- a/TeXmacs/progs/convert/images/tmimage.scm +++ b/TeXmacs/progs/convert/images/tmimage.scm @@ -232,7 +232,7 @@ (let* ((sufl (string-length suffix)) (surl (url->string myurl)) (sl (string-length surl))) - (set! myurl (string->url (string-append (substring surl 0 (- sl sufl)) "pdf")))))) + (set! myurl (string->url (string-append (substring surl (- sl sufl) sl) "pdf")))))) ;; TODO Handle when output file already exists (presently we overwritte without warning) diff --git a/devel/212_6.md b/devel/212_6.md index ae099b6f8d..296ce6518c 100644 --- a/devel/212_6.md +++ b/devel/212_6.md @@ -49,16 +49,5 @@ Added missing `(convert images image-format)` import. Without it, `get-raster-re ### 4. `TeXmacs/plugins/image/progs/image/pdf.scm` Registered `pdf-file → tif-file` converter via pdftocairo. Previously only ImageMagick had a TIFF converter, so systems without ImageMagick could not export TIFF. -### 5. `TeXmacs/progs/convert/images/tmimage.scm` -Fixed `substring` indexing bug in the format fallback path. The old code extracted the suffix instead of the path prefix, producing an invalid URL when falling back to PDF: - -```scheme -;; BEFORE — extracts suffix portion, produces garbage like "tiffpdf" -(substring surl (- sl sufl) sl) - -;; AFTER — extracts path without suffix, produces correct "/path/file.pdf" -(substring surl 0 (- sl sufl)) -``` - ## Why -mupdf's `save_picture` only supports PNG output. The old code unconditionally treated jpeg/tiff as bitmap formats, sending them through a rendering path that silently failed. The converter pipeline (pdftocairo/ImageMagick) also had bugs: missing imports, missing converter registrations, incorrect CLI flag generation, and a broken fallback path. +mupdf's `save_picture` only supports PNG output. The old code unconditionally treated jpeg/tiff as bitmap formats, sending them through a rendering path that silently failed. The converter pipeline (pdftocairo/ImageMagick) also had bugs: missing imports, missing converter registrations, and incorrect CLI flag generation. diff --git a/src/Edit/Editor/edit_main.cpp b/src/Edit/Editor/edit_main.cpp index f8d35b95b6..90e42baf24 100644 --- a/src/Edit/Editor/edit_main.cpp +++ b/src/Edit/Editor/edit_main.cpp @@ -335,13 +335,13 @@ edit_main_rep::print_snippet (url name, tree t, bool conserve_preamble) { temp_root << 0; assign (subtree (et, temp_root), copy (t)); - string s= suffix (name); + string s = suffix (name); bool bitmap= false; #ifdef USE_MUPDF_RENDERER - bitmap = (s == "png"); + bitmap= (s == "png"); #else - bitmap= (s == "png" || s == "jpg" || s == "jpeg" || s == "tif" - || s == "tiff"); + bitmap= + (s == "png" || s == "jpg" || s == "jpeg" || s == "tif" || s == "tiff"); #endif #ifndef QTTEXMACS bitmap= false; From b1abe7fd814f397e704555c51b14684cc2df85c8 Mon Sep 17 00:00:00 2001 From: Lakshy Choudhary Date: Wed, 11 Mar 2026 16:42:14 +0530 Subject: [PATCH 3/3] Updated to include png export issue Document known issue with PNG exports trimming content. --- devel/212_6.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devel/212_6.md b/devel/212_6.md index 296ce6518c..f5a6c303fc 100644 --- a/devel/212_6.md +++ b/devel/212_6.md @@ -4,7 +4,7 @@ - Select text in a document - File → Export selection as image → save as `.jpg` → verify file is created and contains the selection - Repeat for `.tif` and `.tiff` -- Also test `.png` and `.pdf` works as expected +- `png` exports have issue (when using mupdf render), trailing lines/words are trimmed. ## Issue #2728 JPEG and TIFF formats could not be exported via "Export Selected Area as Image".PNG and PDF export worked.