Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7025a133 authored by Philip P. Moltmann's avatar Philip P. Moltmann Committed by Android (Google) Code Review
Browse files

Merge "Use SkMatrix to compute pdf transformation."

parents 026e5173 61a53074
Loading
Loading
Loading
Loading
+18 −17
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@
#include "fpdfview.h"
#include "fpdf_edit.h"
#include "fpdf_save.h"
#include "fsdk_rendercontext.h"
#include "fpdf_transformpage.h"
#pragma GCC diagnostic pop

@@ -176,7 +175,7 @@ static void nativeSetTransformAndClip(JNIEnv* env, jclass thiz, jlong documentPt
        jlong transformPtr, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom) {
    FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);

    CPDF_Page* page = (CPDF_Page*) FPDF_LoadPage(document, pageIndex);
    FPDF_PAGE* page = (FPDF_PAGE*) FPDF_LoadPage(document, pageIndex);
    if (!page) {
        jniThrowException(env, "java/lang/IllegalStateException",
                "cannot open page");
@@ -193,30 +192,32 @@ static void nativeSetTransformAndClip(JNIEnv* env, jclass thiz, jlong documentPt
        return;
    }

    CFX_Matrix matrix;

    SkMatrix* skTransform = reinterpret_cast<SkMatrix*>(transformPtr);
    // PDF's coordinate system origin is left-bottom while in graphics it
    // is the top-left. So, translate the PDF coordinates to ours.
    SkMatrix reflectOnX = SkMatrix::MakeScale(1, -1);
    SkMatrix moveUp = SkMatrix::MakeTrans(0, FPDF_GetPageHeight(page));
    SkMatrix coordinateChange = SkMatrix::Concat(moveUp, reflectOnX);

    // Apply the transformation what was created in our coordinates.
    SkMatrix matrix = SkMatrix::Concat(*reinterpret_cast<SkMatrix*>(transformPtr),
            coordinateChange);

    // Translate the result back to PDF coordinates.
    matrix.setConcat(coordinateChange, matrix);

    SkScalar transformValues[6];
    if (!skTransform->asAffine(transformValues)) {
    if (!matrix.asAffine(transformValues)) {
        jniThrowException(env, "java/lang/IllegalArgumentException",
                "transform matrix has perspective. Only affine matrices are allowed.");
        return;
    }

    // PDF's coordinate system origin is left-bottom while in graphics it
    // is the top-left. So, translate the PDF coordinates to ours.
    matrix.Set(1, 0, 0, -1, 0, page->GetPageHeight());

    // Apply the transformation what was created in our coordinates.
    matrix.Concat(transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
    FS_MATRIX transform = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
                           transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
            transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]);

    // Translate the result back to PDF coordinates.
    matrix.Concat(1, 0, 0, -1, 0, page->GetPageHeight());
                           transformValues[SkMatrix::kATransX],
                           transformValues[SkMatrix::kATransY]};

    FS_MATRIX transform = {matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f};
    FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom};

    FPDFPage_TransFormWithClip(page, &transform, &clip);
+10 −6
Original line number Diff line number Diff line
@@ -212,19 +212,23 @@ static void renderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page, int destLeft, i
    } else {
        // PDF's coordinate system origin is left-bottom while
        // in graphics it is the top-left, so remap the origin.
        matrix.Set(1, 0, 0, -1, 0, pPage->GetPageHeight());
        SkMatrix reflectOnX = SkMatrix::MakeScale(1, -1);
        SkMatrix moveUp = SkMatrix::MakeTrans(0, FPDF_GetPageHeight(page));
        SkMatrix m = SkMatrix::Concat(moveUp, reflectOnX);

        // Concatenate transformation and origin transformation
        m.setConcat(*transform, m);

        SkScalar transformValues[6];
        if (transform->asAffine(transformValues)) {
            matrix.Concat(transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
                    transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
                    transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]);
        } else {
        if (!m.asAffine(transformValues)) {
            // Already checked for a return value of false in the caller, so this should never
            // happen.
            ALOGE("Error rendering page!");
        }

        matrix = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
                  transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
                  transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]};
    }
    pageContext->AppendObjectList(pPage, &matrix);