Comment #28 on issue 849 by
andr...@airsoftsoftwair.de: FPDF_RenderPageBitmapWithMatrix() still not behaving properly
https://bugs.chromium.org/p/pdfium/issues/detail?id=849#c28I've now fixed this issue but it's not a general fix. It merely just makes it work for me. For the record, here's what I did: CPDF_Page::GetDisplayMatrixWithTransformation() does a lot of stuff to the transformation matrix that makes no sense for my purposes so I just killed it and replaced it with the following three lines:
CFX_Matrix matrix = m_PageMatrix;
matrix.Concat(transformation);
return matrix;
My host program which calls FPDF_RenderPageBitmapWithMatrix() then first flips the page upside down by applying a matrix of (1,0,0,-1,0,0), like so:
t.a = 1 * transform->a + 0 * transform->c;
t.b = 1 * transform->b + 0 * transform->d;
t.c = 0 * transform->a + -1 * transform->c;
t.d = 0 * transform->b + -1 * transform->d;
Then it uses the matrix in "t" to transform a rectangle of the page dimensions, i.e (0,0,pageWidth,pageHeight):
transformrect(0, 0, pageWidth, pageHeight, &t, &bx, &by, &bwidth, &bheight);
The transformrect() function returns the transformed rectangle's bounding box in (bx,by,bwidth,bheight). The last step that is necessary is to translate the "t" matrix by the bounding box offsets, like so:
t.e = (float) -bx;
t.f = (float) -by;
And that's it. I can now pass "t" to FPDF_RenderPageBitmapWithMatrix() and it will correctly render the transformed page to my bitmap. The bitmap has to be of the size (bwidth,bheight).
In order to get a white background in the background of the transformed page I have added a function called FPDFPage_PrependObject(). This does the same as FPDFPage_InsertObject() but it inserts the object at the bottom of the stack, i.e. all the way in the background. I then use FPDFPage_PrependObject() to insert a filled white rectangle that covers the whole page dimensions as the very first object. This is then nicely transformed with the whole page when calling FPDF_RenderPageBitmapWithMatrix().
So finally things work like they should with FPDF_RenderPageBitmapWithMatrix(). But I'm still hoping for an official fix because my one is rather hackish of course ;)
Also note that my PDFium snapshot is from 14th September 2017 so maybe things have changed in the meantime...