Bug 150551 - Exporting shape with gradient fill as PDF results in wrong output
Summary: Exporting shape with gradient fill as PDF results in wrong output
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Impress (show other bugs)
Version:
(earliest affected)
4.2.0.4 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:7.5.0
Keywords:
: 73526 (view as bug list)
Depends on:
Blocks: Impress-Gradient
  Show dependency treegraph
 
Reported: 2022-08-22 20:38 UTC by Rafael Lima
Modified: 2024-01-24 14:20 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments
Screenshot showing the problem (459.16 KB, image/png)
2022-08-22 20:38 UTC, Rafael Lima
Details
FOR THIS IMAGE :BEFORE EXPORT TO PDF (153.31 KB, image/png)
2022-09-11 07:50 UTC, Manikandan C
Details
FOR THIS IMAGE:AFTER EXPORT TO PDF (454.99 KB, image/png)
2022-09-11 07:51 UTC, Manikandan C
Details
Sample ODP file (45.49 KB, application/vnd.oasis.opendocument.presentation)
2022-09-20 12:42 UTC, Rafael Lima
Details
Sample PDF file (264.17 KB, application/pdf)
2022-09-20 12:43 UTC, Rafael Lima
Details
Sample PDF File (with patch applied) (24.63 KB, application/pdf)
2022-09-21 13:05 UTC, Rafael Lima
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rafael Lima 2022-08-22 20:38:40 UTC
Created attachment 181956 [details]
Screenshot showing the problem

Export of shapes with gradient fill to PDF does not seem to work.

Steps to reproduce:
1) Create a blank presentation; insert a single shape to the slide
2) Right-click the shape - Area
3) Select "Gradient"; Type "Radial"; Increment "Automatic"; Center X/Y: 50% for both; From color "Dark Blue 1"; To color "Dark Blue 3" (other colors show the same behavior)
4) Click OK; notice that the gradient will look fine in Impress
5) Export to PDF; open the PDF file and notice that the gradient was not exported correctly

See attached image comparing what I see in Impress and what I get as the PDF output.

This bug is probably related to bug 150545.

System info

Version: 7.3.5.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 12; OS: Linux 5.15; UI render: default; VCL: kf5 (cairo+xcb)
Locale: pt-BR (pt_BR.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.5-0ubuntu0.22.04.1
Calc: threaded
Comment 1 Manikandan C 2022-09-11 07:49:08 UTC
NOT REPRODUCED..

ENVIRONMENT:LibreOfficeDev-7.4.0.0.alpha0_2022-01-23-x86_64 

VERSION: TESTED IN ---Linux Mint 21 Cinnamon
Comment 2 Manikandan C 2022-09-11 07:50:52 UTC
Created attachment 182378 [details]
FOR THIS IMAGE :BEFORE EXPORT TO PDF
Comment 3 Manikandan C 2022-09-11 07:51:35 UTC
Created attachment 182379 [details]
FOR THIS IMAGE:AFTER EXPORT TO PDF
Comment 4 Rafael Lima 2022-09-12 13:11:56 UTC
(In reply to Manikandan C from comment #1)
> NOT REPRODUCED..

Hi Manikandan, why do you say this is not reproducible?

The second screenshot you posted (attachment 182379 [details]) clearly shows the error in gradient after being exported to PDF. Notice the circular lines that are visible in the PDF, which are not visible in Impress (see your attachment 182378 [details]).
Comment 5 raal 2022-09-17 20:09:31 UTC
Confirm with Version: 7.5.0.0.alpha0+ / LibreOffice Community
Build ID: e46f9cc4b506c325cbe1060777bbc81fd1630f49
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: cs-CZ (cs_CZ.UTF-8); UI: en-US
Calc: threaded

and Version 4.1.0.0.alpha0+ (Build ID: efca6f15609322f62a35619619a6d5fe5c9bd5a)
Comment 6 Rafael Lima 2022-09-20 12:42:54 UTC
Created attachment 182570 [details]
Sample ODP file

Here's a sample ODP file with a shape with radial gradient.
Comment 7 Rafael Lima 2022-09-20 12:43:44 UTC
Created attachment 182571 [details]
Sample PDF file

Here's the PDF generated from the ODP file above.
Comment 8 Rafael Lima 2022-09-20 12:44:49 UTC
Hi Armin, given your recent work on a gradient-related patch, maybe you could give your opinion on this one as well.
Comment 9 Armin Le Grand 2022-09-21 09:19:41 UTC
Hi Rafael,
I took a look:

Export starts by recording a Metafle (unfortunately still the base for PDF export, no Primitive processor for that). At drawinglayer/source/processor2d/vclmetafileprocessor2d.cxx:2004 in processPolyPolygonGradientPrimitive2D the fallback to the 'old' vcl-paint is taken.

The PDF exporter in vcl/source/gdi/pdfwriter_impl2.cxx:681 in playMetafile sees the created MetaCommentAction containing the gradient as "XPATHFILL_SEQ_BEGIN", but that is no used (here are two stacked XPATHFILL_SEQ_BEGIN/XPATHFILL_SEQ_END pairs here created from he old former paint). This is no used.

It continues to the 2nd pair at ln 555 and detects the comment as MetaGradientExAction. There ln 581 implWriteGradient is taken. That creates the MetafileAcions temporarily (AddGradientActions) and plays them inside the PDFWriterImpl using playMetafile.

Gradient::AddGradientActions uses Gradient::DrawComplexGradientToMetafile. That (old) stuff creates a stepped bunch of 'donuts'. These get written as filled polyPolygons to the PDF.

What we see is not directly an error, but bad quality of the 'overlapping' of those 'donuts' building the gradient.

The gradient cannot directly be exported to PDF since our old gradient definitions cannot be matched to PDF definitions, so this geometric representation is created and exported.

For non-transparent gradients it is possible (and simpler) to not use 'donuts' but the whole stack of overlapping ellipses.

We could try to balance around with the sizes of the 'donuts' to get a better overlapping - problem is the AntiAliased paint in the PDF renderer(s) here - it sees only a bunch of 'donuts' and does not know that it's supposed to be a gradient. We did that a lot - experience tells that his will probably create another problem somewhere else (Gradient::AddGradientActions is no only used for PDF export).

It's ugly but there is unfortunately no simple, good solution. A bigger redesign would be needed to generally solve that stuff. When debugging you can see many places where fixes/changes were already done, too - very dangerous and unstable (unfortunately).

Sorry, no simple fix available...
Comment 10 Armin Le Grand 2022-09-21 09:30:32 UTC
One more note:

I checked how it looks when forcing to use just the primitive decompose in VclMetafileProcessor2D::processPolyPolygonGradientPrimitive2D - that's much better. That was used (and could be) before the shortcut introduced by 20c09d351ee060bdde13d92d2bf86dd998cdb0cb (as already mentioned in https://bugs.documentfoundation.org/show_bug.cgi?id=151081#c1). That led to many problems. It's intentions to speedup gradient paint are good, but it had many side-effects, which we seem to have to fix now one after the other...
Comment 11 Armin Le Grand 2022-09-21 09:39:18 UTC
Thus, another simple fix for this would be:

--
index 8e221affe978..8ebc653f580f 100644
--- a/drawinglayer/source/processor2d/vclmetafileprocessor2d.cxx
+++ b/drawinglayer/source/processor2d/vclmetafileprocessor2d.cxx
@@ -1938,6 +1938,13 @@ void VclMetafileProcessor2D::processPolyPolygonGradientPrimitive2D(
         return;
     }
 
+    // tdf#150551 for PDF export, use the decomposition for better gradient visualization
+    if(nullptr != mpPDFExtOutDevData)
+    {
+        process(rGradientCandidate);
+        return;
+    }
+
     basegfx::B2DPolyPolygon aLocalPolyPolygon(rGradientCandidate.getB2DPolyPolygon());
 
     if (aLocalPolyPolygon.getB2DRange() != rGradientCandidate.getDefinitionRange())
--

That uses the state before 20c09d351ee060bdde13d92d2bf86dd998cdb0cb for PDF exports, but keeps the fallback to old VCL rendering for other cases.

As I explained in https://bugs.documentfoundation.org/show_bug.cgi?id=151081#c1 I would prefer a different approach to speedup gradient paint for pixel targets.
Comment 12 Rafael Lima 2022-09-21 13:05:53 UTC
Created attachment 182601 [details]
Sample PDF File (with patch applied)

(In reply to Armin Le Grand from comment #11)
> Thus, another simple fix for this would be:

Hi Armin... here's the PDF file generated with the patch you proposed.

It is not a perfect fix, since one can still see the donuts. However it is a lot better than the current state.

Since a definitive fix would be too complicated, maybe we could go with your proposed patch for the time being. What do you think?
Comment 13 Armin Le Grand 2022-09-21 16:15:09 UTC
Why not, it's on gerrit now:

https://gerrit.libreoffice.org/c/core/+/140354

This puts it at least to the state before the change leaded to this regression. To do more I would need to know more why the two ::processPolyPolygonGradientPrimitive2D methods were added.

The one at VclPixelProcessor2D only replaces the case that start/end color are the same, but not even calls older (faster?) vcl methods, so I see no gain there, but may overlook something. BTW that useful check/optimization should go to PolyPolygonGradientPrimitive2D::create2DDecomposition, so all renderers (now and in the future) would use that optimization. In the end the decomposition of the resulting create2DDecomposition creates a single polygon, but it's embedded in a MaskPrimitive2D in PolyPolygonGradientPrimitive2D::create2DDecomposition, so the optimization in VclPixelProcessor2D::processPolyPolygonGradientPrimitive2D is better here, but - as explained - will only have effect in VclPixelProcessor2D since not added to the decompose.

The one at VclMetafileProcessor2D should not be time-critical at all since it's for creating metafiles. It leads to regressions, as we see...
Comment 14 Commit Notification 2022-09-23 07:45:56 UTC
Armin Le Grand (allotropia) committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/2914316bcffc890887ca577d2753149c07005815

tdf#150551 for PDF export, use the decomposition

It will be available in 7.5.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 15 Gabor Kelemen (allotropia) 2024-01-24 14:20:14 UTC
*** Bug 73526 has been marked as a duplicate of this bug. ***