Bug 692914 - Postscript MediaType lost in tranformation to PCL
Summary: Postscript MediaType lost in tranformation to PCL
Status: RESOLVED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: PXL Driver (show other bugs)
Version: 9.01
Hardware: PC Windows 7
: P4 normal
Assignee: Henry Stiles
URL:
Keywords: bountiable
Depends on:
Blocks:
 
Reported: 2012-03-12 18:15 UTC by ernst.scheithauer
Modified: 2013-01-30 21:15 UTC (History)
4 users (show)

See Also:
Customer:
Word Size: ---


Attachments
Postscript with varying media types and duplex information (430.91 KB, application/postscript)
2012-03-12 18:16 UTC, ernst.scheithauer
Details
Multiple postscript jobs with MediaPosition set (430.93 KB, application/postscript)
2012-03-16 10:42 UTC, ernst.scheithauer
Details
A single postscript job with MediaPosition set (143.51 KB, application/postscript)
2012-03-16 10:43 UTC, ernst.scheithauer
Details
Multiple postscript jobs with MediaPosition set on first and third job and automatically selection on second job (430.94 KB, application/postscript)
2012-03-16 10:49 UTC, ernst.scheithauer
Details
Patch to implement Mediatype (9.06 KB, patch)
2012-05-20 00:20 UTC, Hin-Tak Leung
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description ernst.scheithauer 2012-03-12 18:15:16 UTC
When a postscript file including paper tray selection using MediaType is processed by Ghostscript and converted using the pxlmono or pxlcolor then there is no MediaSource specified in the PCL output. This can be reproduced using the following command:

"C:\Program Files\gs\gs9.01\bin\gswin64c.exe" -dNOPAUSE -dBATCH -sDEVICE=pxlmono -sOutputFile="c:\Temp\output.pcl" "c:\Temp\MediaTypesAndDuplex.ps"

While Duplex information is preserved, MediaType is not preserved. I figure this should be possible because MediaSource in PCL is the equivalent to MediaType in Postscript.

Is this a bug in Ghostscript? Can this be fixed?
Comment 1 ernst.scheithauer 2012-03-12 18:16:10 UTC
Created attachment 8413 [details]
Postscript with varying media types and duplex information
Comment 2 Hin-Tak Leung 2012-03-16 06:03:37 UTC
The current code maps MediaPosition (from postscript?) to  PXL's MediaSource, and it appears to be a tray number, or autoselect, or manual feed tray.

Is that what it is supposed to do?

However, please also note that quite a few of these parameters only have effect on a per-job basis, and may not be able to switch between pages, I believe.
Comment 3 ernst.scheithauer 2012-03-16 08:31:37 UTC
You are correct that MediaSource in PCL is an integer value that corresponds to MediaPosition in Postscript. I will test mapping of MediaPosition to MediaSource.

However, there also is the concept of MediaType in PCL which ought to be the same as MediaType in Postscript. Could this be mapped as well?

MediaPosition and MediaType can be set on the sheet level (so every page with simplex and every other page with duplex printing), although the common postscript printer drivers set it on the document/job level. If multiple postscript jobs are concatenated as in the attached file, the value can change within the postscript file as well.
Comment 4 ernst.scheithauer 2012-03-16 10:42:00 UTC
Created attachment 8427 [details]
Multiple postscript jobs with MediaPosition set
Comment 5 ernst.scheithauer 2012-03-16 10:43:57 UTC
Created attachment 8428 [details]
A single postscript job with MediaPosition set
Comment 6 ernst.scheithauer 2012-03-16 10:49:18 UTC
Created attachment 8429 [details]
Multiple postscript jobs with MediaPosition set on first and third job and automatically selection on second job
Comment 7 ernst.scheithauer 2012-03-16 10:53:07 UTC
I have tested MediaPosition and come to the following conclusion:

MediaSource is set according to a MediaPosition specified on the command line with a "-dMediaType=2" switch.

MediaSource is not set according to a MediaPosition specified in the postscript file => I think this is a bug, isn't it? 

I have attached three additional test files with Media Position:
- MediaPositionSingleJob.ps
- MediaPositionConcatenated.ps
- MediaPositionConcatenated_AutomaticallySelectBetween.ps

The implementation should work generically for all three test files. Therefore, if the full postscript stack is cleared and no MediaPosition is given in the second job the MediaSource should be set to AutomaticallySelect.

Can this be fixed in Ghostscript?
Comment 8 Hin-Tak Leung 2012-03-16 17:39:44 UTC
(In reply to comment #7)
<snipped>
> MediaSource is not set according to a MediaPosition specified in the postscript
> file => I think this is a bug, isn't it? 

I (well, somebody else...) will need to look at the postscript files. But any such parameters must be set before any drawing instructions to have an effect.

> Can this be fixed in Ghostscript?

Caveat comment above, possibly yes.
Comment 9 Hin-Tak Leung 2012-04-12 17:15:04 UTC
Oh. Some of this appears to be another problem coming out of the ESP ghostscript merge back in 2008.

There seems to be some strangeness around the -dNOMEDIAATTRS switch which was introduced then; that broke MediaPosition, and bug 690182 was partly a result of that, and to fix bug 690182, further hackery seemed to be introduced.

According to the git/svn log, MediaPosition via setpagedevice did work in 2005 (or was implemented then); anyhow, the current behavior is that MediaPosition via setpagedevice is only passed to the driver if -dNOMEDIAATTRS is specified. This seems strange.

OTOH, I can see the need of CUPS (media being fixed per queue and not modifiable by job content) being opposite to the need of the reporter (media set by job itself, and changes within job).

Till: can you explain better what -dNOMEDIAATTRS does? It also seem to do the opposite of what it says?

BTW, I have media-type working, I think, if -dNOMEDIAATTRS is specified.
Comment 10 Hin-Tak Leung 2012-04-16 21:24:40 UTC
commit 2bce78bcea74dd4d08a6fb59df978b78af959c39 'Finally merged in extra functionality of ESP Ghostscript (from branch "gs-esp-gpl-merger")' in 2007 removed the initialization "MediaPosition_set = false" and its setting true on options, then fix it with commit ff84ce4f8b7f644a9f02092217b59b905c570865 in 2008... which set it to true; but MediaPosition_set is never set back to false, which is required for the present reporter for multiple setpagedevice to work.

In a nut-shell,  6d040291e55b8c633bd8f7bbf5780a90f448c866 from 2005 seems to do the correct thing with MediaPosition, which was broken in 2007, then partially restored in 2008.
Comment 11 Hin-Tak Leung 2012-04-16 21:29:43 UTC
Oh, meant to add Till's e-mail address to the CC the last time and for some reason never did.

Till: can you explain better what -dNOMEDIAATTRS does? It also seem to do the
opposite of what it says? and other questions in the last two comments.
Comment 12 Ray Johnston 2012-04-17 00:09:30 UTC
The PostScript definition of 'setpagedevice' requires that ALL options
be "sticky", so according to that definition MediaPosition _should_ be
set on the second job to the same value as on the first job.

Until a "<< /MediaPosition ___ >> setpagedevice" overrides the value set
by the first job, it must be retained.

If Ghostscript (before or after the ESP Ghostscript merge) is not retaining
the MediaPosition setting from job 1 for job 2, then that is a bug. If the
first job setting _is_ being retained for the second job, then that is the
correct (according to the PLRM) behavior.

Note that running Ghostscript with -dJOBSERVER=1 will perform a full 'restore'
between jobs (delimited by ^D or 'false 0 startjob pop') but the command line
and init_with_args interface INTENTIONALLY treats all files as a single job.
Comment 13 Till Kamppeter 2012-04-17 07:12:18 UTC
Hin-Tak, I do not really know for what this -dNOMEDIAATTRS is good for. It was introduced by Mike Sweet and seems to be used only with the CUPS Raster driver. Perhaps one could modify Ghostscript so that -dNOMEDIAATTRS is ignored when the output device is not "cups"?
Comment 14 Hin-Tak Leung 2012-04-17 15:04:31 UTC
(In reply to comment #12)
> The PostScript definition of 'setpagedevice' requires that ALL options
> be "sticky", so according to that definition MediaPosition _should_ be
> set on the second job to the same value as on the first job.
> 
> Until a "<< /MediaPosition ___ >> setpagedevice" overrides the value set
> by the first job, it must be retained.
> 
> If Ghostscript (before or after the ESP Ghostscript merge) is not retaining
> the MediaPosition setting from job 1 for job 2, then that is a bug. If the
> first job setting _is_ being retained for the second job, then that is the
> correct (according to the PLRM) behavior.
> 
> Note that running Ghostscript with -dJOBSERVER=1 will perform a full 'restore'
> between jobs (delimited by ^D or 'false 0 startjob pop') but the command line
> and init_with_args interface INTENTIONALLY treats all files as a single job.

I understand and agree with most (all?) of that - setpagedevice's effect is persistent until another invocation to the contrary. The current behavior is bizarre: information from "<< /MediaPosition ___ >> setpagedevice" is stripped, invisible and not passed to the driver, unless -dNOMEDIAATTRS is added.

So the default behavior currently, is to ignore "<< /MediaPosition ___ >> setpagedevice". This is wrong, and "-dNOMEDIAATTRS" does the opposite - this seems to be the opposite of what it is documented to do.

I.e. the current default behavior seems to be what -dNOMEDIAATTRS does (i.e. ignore media attributes), and specifying "-dNOMEDIAATTRS" seems to do the opposite (i.e. honouring media attributes within job). This is very confusing.
Comment 15 Hin-Tak Leung 2012-04-17 15:16:39 UTC
(In reply to comment #13)
> Hin-Tak, I do not really know for what this -dNOMEDIAATTRS is good for. It was
> introduced by Mike Sweet and seems to be used only with the CUPS Raster driver.
> Perhaps one could modify Ghostscript so that -dNOMEDIAATTRS is ignored when the
> output device is not "cups"?

How is -dNOMEDIAATTRS usually used? hooked up to scripts?

I understand why Mike Sweet/CUPS would want a feature of this sort - i.e. ignoring within-job instructions of changing tray/media and switching them mid-job, since those are essentially per queue characteristics in the typical cups usage. But that's

1. is contrary to documented postscript behavior.
2. should not be the default behavior.

The pre-esp-merge behavior, according to svn r5995, is to pass postscript MediaPosition onto the driver and make it HP PXL's MediaSource; this is broken since the merge, and now needs -dNOMEDIAATTRS .
Comment 16 ernst.scheithauer 2012-05-08 17:10:59 UTC
(In reply to comment #12)
> The PostScript definition of 'setpagedevice' requires that ALL options
> be "sticky", so according to that definition MediaPosition _should_ be
> set on the second job to the same value as on the first job.
> 
> Until a "<< /MediaPosition ___ >> setpagedevice" overrides the value set
> by the first job, it must be retained.

In this case there is a script executed in between that clears up the stacks and everything so here the value needs to be cleared. It also seems that Ghostscript does that because when duplex was previously specified and duplex is no longer specified in the next document then the PCL driver emits simplex printing.
Comment 17 Hin-Tak Leung 2012-05-20 00:20:51 UTC
Created attachment 8596 [details]
Patch to implement Mediatype

Patch to implement MediaType pass-through.

There is also some extra code to decide on what to do if both media position and media type are specified.

And a fix to a old bug introduced by the CUPS merge in 2007 - a bogus MediaPosition can be copied into the language core where none had specified under some usage. (See also bug 690182 comment 11 for more detailed discussion).

Note this patch (and this functionalty of changing media position/type) has no effect unless in use together with -dNOMEDIAATTR, or together with a InputAttributes dictionary.
Comment 18 ernst.scheithauer 2012-06-11 17:45:10 UTC
I have tried out this patch and it works and solves the problem. Can you please include this patch in the next version of Ghostscript?
Comment 19 Henry Stiles 2013-01-14 20:03:01 UTC
Hello Hin-Tak, It looks like I lost track of this one.  Can you verify the patch works with the current code, I'll integrate it and get you the bounty, asap.
Comment 20 Hin-Tak Leung 2013-01-15 11:57:45 UTC
(In reply to comment #19)
> Hello Hin-Tak, It looks like I lost track of this one.  Can you verify the
> patch works with the current code, I'll integrate it and get you the bounty,
> asap.

Yes, it does. You might want to remove the debugging code or change the '|' to something more approriate; since the additional debugging codes are compiled out in release builds, there is no necessity for doing so.
Comment 21 Hin-Tak Leung 2013-01-30 20:21:27 UTC
Any chance of getting this patch in before 9.07? I suggest a small adjustment from changing '|' to 'd' for the debugging designation. (from 'experimental' to 'dictionary put/undef' - I think that's the closest descriptions of what area this change about the input tray dictionary belongs to).
Comment 22 Henry Stiles 2013-01-30 20:27:25 UTC
I intentionally waited so it wouldn't be included.  It'll be in the next release (August) or the user can use the git source.
Comment 23 Henry Stiles 2013-01-30 20:39:51 UTC
(In reply to comment #22)
> I intentionally waited so it wouldn't be included.  It'll be in the next
> release (August) or the user can use the git source.

Till has requested this also, so I'll get it in trunk and it will be Chris' call if it should be cherry picked from trunk to the 9.07 branch.
Comment 24 Henry Stiles 2013-01-30 21:15:18 UTC
Out of my hands ;-)

The ghostpdl branch, master has been updated
       via  140bfcf881a24e4f915e467a89b8a96592551846 (commit)
      from  9fb635f0a6e1695b7a88822c75e22d40a9ba5116 (commit)

----------------------------------------------------------------------
commit 140bfcf881a24e4f915e467a89b8a96592551846
Author: Henry Stiles <henry.stiles@artifex.com>
Date:   Wed Jan 30 14:07:41 2013 -0700

    Fix Bug #692914 - Postscript MediaType lost.

    Thanks to Hin-Tak Leung for adding support for MediaType to PXL output
    drivers.