Re: [PATCH] cairolatex: respect transparency setting

Henri Menke <[email protected]>
Newsgroups gmane.comp.graphics.gnuplot.devel
Message-ID <20200821054750.5tvj35v4y6gm7yj3@vento15post8>
On 20/08/20, 21:31, Ethan A Merritt wrote:
> On Thursday, 20 August 2020 20:17:44 PDT Henri Menke wrote:
> > On 20/08/20, 20:01, Ethan A Merritt wrote:
> > > On Thursday, 20 August 2020 18:34:54 PDT Henri Menke wrote:
> > > > Currently the cairolatex terminal does not respect transparency options
> > > > given in the terminal setting.  In this minimal example the
> > > > notransparent option is ignored:
> > > > 
> > > >     set terminal cairolatex png notransparent
> > > >     set output "test.tex"
> > > >     plot sin(x)
> > > > 
> > > > Running this through gnuplot and then using `file' reveals that the
> > > > alpha channel is still present:
> > > > 
> > > >     $ gnuplot test.gnuplot && file test.png
> > > >     test.png: PNG image data, 1500 x 900, 8-bit/color RGBA, non-interlace
> > > 
> > > I think the raionale for the current code is that the png image created for
> > > inclusion in a LaTeX document must have a transparent background in order
> > > to support the "back" and "behind" attributes for text elements, since they
> > > will be drawn by the latex engine and then covered by inclusion of the
> > > graphics image.  The structure of the latex processing is:
> > > 
> > >     render text elements that are "behind" or "back"
> > >     \includegraphics{gnuplot_figure}
> > >     render text elements that are "front"
> > > 
> > > If you use a solid color background in the graphics image, it will hide
> > > any previously rendered text elements.
> > 
> > The reason why I'm looking into this is that currently it is impossible
> > to generate PDF/A when using plots generated by gnuplot.  PDF/A doesn't
> > allow transparency, so I'm trying to remove all of it (or at least make
> > it optional).  As noted in the patch, the default behaviour of using
> > transparency remains, so this is entirely optional.
> 
> I am researching this as I type, so please bear with me.
> 
> From Wikipedia I see
> 	Transparent objects and layers (Optional Content Groups) are 
> 	forbidden in PDF/A-1, but are allowed in PDF/A-2.
> PDF/A-1 was published in 2005; PDF/A-2 in 2011; PDF/A-3 in 2012;
> So maybe this is no longer a limitation?
> 
> From my own knowledge, there are commonly available tools that 
> render the transparency in a PDF document once, then save the result
> as an embedded image that no longer contains transparency.
> For example if you use gnuplot to create a PostScript file directly it cannot
> use transparency because PostScript itself does not support transparency.
> But if you use gnuplot to create a PDF file with transparency and then
> convert it
> 	pdf2ps input.pdf output.ps
> you get a PostScript file in which the transparency is "baked in" rather
> than interpreted.   Logically then you could take that PostScript file
> and convert it back to PDF, keeping the pre-rendered image that no
> longer uses an alpha channel. 
> 	ps2pdf output.ps new.pdf
> Whether that particular pair of tools produces a PDF/A compliant
> file I do not know.

This is actually a pretty good proposal.  In my actual plot I now have
added

    set terminal cairolatex pdf standalone header '\usepackage[a-1b]{pdfx}'
    set output "test.tex"
    # ...
    unset output
    !pdf2ps test-inc.pdf test-inc.ps
    !ps2pdf -dPDFA=1 test-inc.ps test-inc.pdf
    !pdflatex --interaction=nonstopmode test.tex

and the resulting document validates correctly with VeraPDF.  It's a bit
ugly and I don't know whether the ghostscript roundtrip would correctly
preserve fonts, but since the background image doesn't contain fonts, I
don't care.  Note that adding the -dPDFA=1 flag will make the PDF
structurally PDF/A compliant, but will not add metadata or color
profiles, so test-inc.pdf does not validate.  These extras are handled
by the pdfx LaTeX package.

To get my plots with PNG background compliant I use the patch from this
thread together with your suggestion `set tics front'.  Another option
would be to run mogrify to remove the transparency.

    set terminal cairolatex png standalone header '\usepackage[a-1b]{pdfx}'
    set output "test.tex"
    # ...
    unset output
    !mogrify -alpha remove test-inc.png
    !pdflatex --interaction=nonstopmode test.tex

So bottomlined my thesis now validates as PDF/A-1b, which is really
nice.

Cheers, Henri

> 
>    best,
> 
> 	Ethan
> 
> > 
> > Kind regards,
> > Henri
> > 
> > > 
> > > 	Ethan
> > > 
> > > > 
> > > > With this patch the transparency setting will be honoured.  The current
> > > > default setting of implicitly assuming transparency is unaltered.
> > > > ---
> > > >  term/cairo.trm | 2 +-
> > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > > 
> > > > diff --git a/term/cairo.trm b/term/cairo.trm
> > > > index ee748dfe2..3ef76ecc6 100644
> > > > --- a/term/cairo.trm
> > > > +++ b/term/cairo.trm
> > > > @@ -849,7 +849,7 @@ void cairotrm_graphics()
> > > >  	plot.background.g = cairo_params->background.g;
> > > >  	plot.background.b = cairo_params->background.b;
> > > >  	gp_cairo_set_background(cairo_params->background);
> > > > -	if (ISCAIROLATEX || cairo_params->transparent)
> > > > +	if (cairo_params->transparent)
> > > >  		gp_cairo_clear_background(&plot);
> > > >  	else
> > > >  		gp_cairo_solid_background(&plot);
> > > > 
> > > 
> > > 
> > > 
> > > 
> 
> 
> 


_______________________________________________
gnuplot-beta mailing list
[email protected]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.