fix "BoundingBox" in generated postscript files

10 views
Skip to first unread message

Qian Yun

unread,
Apr 13, 2024, 5:25:22 AM4/13/24
to fricas-devel
One of the problems of postscript files generated by "draw" is that
it is not proper "embedded postscript file", aka "eps".

When trying to include such image files in latex document, there are
huge padding around image.

The problem is caused by "BoundingBox". We have "BoundingBox" in
generated PS file, but almost all utilities such as "ps2pdf",
"epstopdf" only recognize "BoundingBox" in the header comment section.

FriCAS generates "BoundingBox" in the middle of the PS file.

Following patch is a proof of concept to fix this issue.

After this patch, I can use "epstopdf" to convert PS file to
properly cropped pdf file.

I think for a proper fix, a better way is to split "headerps"
into two and add a "boundingbox" patch in between?


- Qian

diff --git a/src/graph/Gdraws/Gfun.c b/src/graph/Gdraws/Gfun.c
index 808822aa..027e1dbe 100644
--- a/src/graph/Gdraws/Gfun.c
+++ b/src/graph/Gdraws/Gfun.c
@@ -98,6 +98,11 @@ PSCreateFile(
return (psError);
}
else {
+ fprintf(ofp, "%%!PS-Adobe-2.0\n");
+ XWindowAttributes vwInfo;
+ XGetWindowAttributes(dsply, vw, &vwInfo);
+ /* Write a Bounding Box for psfig etc. */
+ fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", vwInfo.height,
vwInfo.width);
i = 1;
while (i < psDrawNo) { /* loops through each file/procedure */
if (psData[i].flag) { /* if set, procedure/file is used */
@@ -226,10 +231,6 @@ Gdraws_setDimension(
fprintf(fp, "\t%d\t%d\t%d\tsetDim\n", twInfo.height - vwInfo.height,
vwInfo.height, vwInfo.width);

- /* Write a Bounding Box for psfig etc. */
-
- fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", vwInfo.height, vwInfo.width);
-
fprintf(fp, "\tmaxX maxY\t0 0\trectangle\tclip\t%% set clip path\n\n");
return (fclose(fp));
}
diff --git a/src/graph/Gdraws/ps_files/header.ps
b/src/graph/Gdraws/ps_files/header.ps
index e945bec4..037914ac 100644
--- a/src/graph/Gdraws/ps_files/header.ps
+++ b/src/graph/Gdraws/ps_files/header.ps
@@ -1,4 +1,3 @@
-%!PS-Adobe-2.0
%%DocumentFonts: Times-Roman
%%Creator: FriCAS
%%CreationDate: today

Waldek Hebisch

unread,
Apr 13, 2024, 9:14:38 AM4/13/24
to fricas...@googlegroups.com
On Sat, Apr 13, 2024 at 05:25:17PM +0800, Qian Yun wrote:
> One of the problems of postscript files generated by "draw" is that
> it is not proper "embedded postscript file", aka "eps".
>
> When trying to include such image files in latex document, there are
> huge padding around image.
>
> The problem is caused by "BoundingBox". We have "BoundingBox" in
> generated PS file, but almost all utilities such as "ps2pdf",
> "epstopdf" only recognize "BoundingBox" in the header comment section.
>
> FriCAS generates "BoundingBox" in the middle of the PS file.
>
> Following patch is a proof of concept to fix this issue.
>
> After this patch, I can use "epstopdf" to convert PS file to
> properly cropped pdf file.

Thanks for looking into this.

> I think for a proper fix, a better way is to split "headerps"
> into two and add a "boundingbox" patch in between?

Hmm, I would remove the '%!PS-Adobe-2.0' comment from 'header.ps'
(as you do). IIUC 'BoundingBox' comment is supposed to appear
just after '%!PS-Adobe-2.0', so it is natural to print both
together, like you do.

I have some doubts concerning use 'vwInfo.height' and 'vwInfo.width'.
Namely, 'Gdraws_setDimension' clearly make some effort to fit
image onto page (it put appropriate rescaling operation into .ps file).
So, it seems that 'Gdraws_setDimension' computes _real_ bounding
box and we should use it in 'BoundingBox' comment. Currently,
as you noted 'BoundingBox' comment is ignored and typical
images fit inside page, so nothing bad happens due to possibly
incorrect bouding box. But for people with big screens and
in case we make 'Gdraws_setDimension' smarter we should use
dimensions computed in 'Gdraws_setDimension' when emiting
'BoundingBox' comment. Which probably means that we should
propagate information from 'Gdraws_setDimension' to
'PSCreateFile'.
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/613e7b4e-d8b1-4283-9339-d57765b29cde%40gmail.com.

--
Waldek Hebisch

Qian Yun

unread,
Apr 13, 2024, 10:17:45 PM4/13/24
to fricas...@googlegroups.com
OK, in attachment are 2 patch files, please review.

First one fixes several problems:
1. The most outstanding bug is that the width and height should
exchange positions in BoundingBox!!

2. The logic to determine scale factor is a mess, including
several minor logical problems.

And I extract it into a separate function, because it will be
used in both Gdraws_setDimension and PSCreateFile.


Second patch is to adjust the position of BoundingBox.

- Qian
fix-boundingbox-2.patch
fix-boundingbox-1.patch

Waldek Hebisch

unread,
Apr 14, 2024, 11:24:47 AM4/14/24
to fricas...@googlegroups.com
On Sun, Apr 14, 2024 at 10:17:39AM +0800, Qian Yun wrote:
> OK, in attachment are 2 patch files, please review.

Just a little remark: '(int)' truncates the number. For bounding
box slightly bigger is not a problem, but I am affraid that too
small may be problematic. So I would rather use '(int)ceil(...)'
intead of '(int)(...)'.

Also, those changes are really single thing, so put them in a
single commit.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages