#1 2016-03-01 13:17:48

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Latest SynPdf version breaks the clipping

Hello,

I use the following code to apply a clipping region on my PDF page

    HRGN pClipRegion = NULL;

    try
    {
        ::SelectClipRgn(pCanvas->Handle, NULL);

        pClipRegion = ::CreateRectRgn(rect.Left, rect.Top, rect.Right, rect.Bottom);

        if (::SelectClipRgn(pCanvas->Handle, pClipRegion) == ERROR)
        {
            DWORD error = ::GetLastError();
            ...
        }
        else
            ::SetMetaRgn(pCanvas->Handle);
    }
    __finally
    {
        if (pClipRegion)
            ::DeleteObject(pClipRegion);
    }

Where pCanvas is the canvas provided ny the VCLCanvas property. This code works well using the SynPdf version 1.18. But I recently tried the latest SynPdf version, and I noticed that the above code no longer works, as my page is drawn without clipping at all. What is the reason?

Regards

Offline

#2 2016-04-13 20:30:22

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: Latest SynPdf version breaks the clipping

I found that the issue is in relation with this function in SynPdf.pas:

procedure TPdfEnum.ExtSelectClipRgn(data: PRgnDataHeader; iMode: DWord);
var ExtClip: TRect;
begin
  try
    ExtClip := data^.rcBound;
    with DC[nDC] do
    case iMode of
      RGN_COPY: begin
        ClipRgn := MetaRgn;
        ClipRgnNull := False;
      end;
    end;
  except
    on E: Exception do ; // ignore any error (continue EMF enumeration)
  end;
end;

when I revert to:

procedure TPdfEnum.ExtSelectClipRgn(data: PEMRExtSelectClipRgn);
var RGNs: PRgnData;
    i: Integer;
    RCT: TRect;
    ClipRect: TPdfBox;
begin // see http://www.codeproject.com/Articles/1944/Guide-to-WIN-Regions
  if not DC[nDC].ClipRgnNull then begin
    Canvas.GRestore;
    Canvas.NewPath;
    Canvas.fNewPath := False;
    DC[nDC].ClipRgnNull := True;
    fFillColor := -1;
  end;
  if Data^.cbRgnData>0 then begin
    Canvas.GSave;
    Canvas.NewPath;
    DC[nDC].ClipRgnNull := False;
    RGNs := @Data^.RgnData;
    for i := 0 to RGNs^.rdh.nCount-1 do begin
      Move(Rgns^.Buffer[i*SizeOf(TRect)], RCT, SizeOf(RCT));
      Inc(RCT.Bottom);
      ClipRect := Canvas.BoxI(RCT, False);
      Canvas.Rectangle(ClipRect.Left,ClipRect.Top,ClipRect.Width,ClipRect.Height);
    end;
    Canvas.Closepath;
    Canvas.Clip;
    Canvas.NewPath;
    Canvas.FNewPath := False;
  end;
end;

all my clipping works fine again. Would it be possible to fix the bug, or revert to the previous version of this function in the library?

Regards

Offline

#3 2016-04-14 08:52:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,131
Website

Re: Latest SynPdf version breaks the clipping

Are you sure you retrieved the latest version from trunk?

The one from http://synopse.info/fossil/finfo?name=SynPdf.pas or https://github.com/synopse/mORMot/blob/ … SynPdf.pas do include the working TPdfEnum.ExtSelectClipRgn() method.

IMHO the version included in the mORMot trunk is always to be checked... it is updated/fixed more other than the SynPdf archive or github dedicated repository itself.

Offline

#4 2016-04-14 12:54:15

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: Latest SynPdf version breaks the clipping

Hi ab,

Thanks for your reply.

The library version is written inside the SynopseCommit.inc file, right? In this case, the latest stable version is the '1.18.1417', and this version contains the above mentioned issue.

I verified again my working version and is the '1.18.2217', so it's a more recent version, and effectively the clipping is working in it. In conclusion, if the code existing in the '1.18.2217' version is destined to replace the code in the latest stable '1.18.1417' version, then then above mentioned issue is effectively resolved.

Regards

Offline

#5 2016-04-14 16:05:17

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,131
Website

Re: Latest SynPdf version breaks the clipping

Indeed.

Offline

#6 2016-11-16 11:23:55

MtwStark
Member
From: Italy
Registered: 2016-01-05
Posts: 27

Re: Latest SynPdf version breaks the clipping

Hi jeanmilost,
please take a look at my post for the same problem here

I have solved adding this test to the beginning of ExtSelectClipRgn procedure:

  // we are handling RGN_COPY (5) only..
  if data^.iMode <> RGN_COPY then
    exit;

Last edited by MtwStark (2016-11-16 11:42:32)

Offline

#7 2017-05-19 14:15:10

kalwados
Member
Registered: 2011-04-18
Posts: 15

Re: Latest SynPdf version breaks the clipping

Today I updated my local copy of SynPDF to the latest trunk version, mostly to get the PDF/A fix; but also to find out that my ExtSelectClipRgn() code was reverted in SVN revision 150.

My code was able to handle non-rectangular clipping areas by interpreting the Region arrays from Windows. Now the old code is active again, which can handle a single rectangle clipping area only. And I have the feeling that even this simple code is wrong, because Data.rcBounds is never used.

I've created a local branch and merged my code back into SynPDF.pas; with some small adjustments as mentioned where by MtwStark: Test for iMode == RGN_COPY. Now everything works fine, again.

AB, can you please dig into your version control software to find out if there was a specific bug, which lead to reverting my enhancements?!

TIA
Achim

BTW:
What would be needed to support PDF/A-3 with attachments (ZUGFeRD, an ugly german acronym for embedding invoice data as XML into PDF documents): http://www.ferd-net.de/front_content.php?idcat=231

Offline

#8 2017-05-19 19:46:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,131
Website

Re: Latest SynPdf version breaks the clipping

Afair some regression about clipping were reported...

Offline

#9 2017-08-15 15:17:16

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: Latest SynPdf version breaks the clipping

Hello

For info: tried the latest nightly build today (1.18.3764), the issue still exists. Do you think you will deal with it soon?

Regards

Offline

#10 2017-08-16 06:52:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,131
Website

Re: Latest SynPdf version breaks the clipping

All this back & forth with diverse feedbacks about clipping is confusing.
I didn't code this part (I didn't need it), so included third-party patches, which work in some cases, but not for everyone...

Can anyone contribute some clean code for clipping?

Offline

Board footer

Powered by FluxBB