From 61a530742f70e59d7fbd7c35a22292176ca554fa Mon Sep 17 00:00:00 2001 From: "Philip P. Moltmann" Date: Tue, 31 May 2016 14:07:54 -0700 Subject: [PATCH] Use SkMatrix to compute pdf transformation. Bug: 28051413 Change-Id: Id17db12801a33955426d7e01d0af950b1c6468cf --- core/jni/android/graphics/pdf/PdfEditor.cpp | 35 ++++++++++--------- core/jni/android/graphics/pdf/PdfRenderer.cpp | 16 +++++---- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/core/jni/android/graphics/pdf/PdfEditor.cpp b/core/jni/android/graphics/pdf/PdfEditor.cpp index 0468b644862db..21f3c46b4c3d2 100644 --- a/core/jni/android/graphics/pdf/PdfEditor.cpp +++ b/core/jni/android/graphics/pdf/PdfEditor.cpp @@ -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(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(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(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()); + FS_MATRIX transform = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY], + transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY], + transformValues[SkMatrix::kATransX], + transformValues[SkMatrix::kATransY]}; - // Apply the transformation what was created in our coordinates. - matrix.Concat(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()); - - 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); diff --git a/core/jni/android/graphics/pdf/PdfRenderer.cpp b/core/jni/android/graphics/pdf/PdfRenderer.cpp index 43550ac9ed72b..3ef2c027f85ab 100644 --- a/core/jni/android/graphics/pdf/PdfRenderer.cpp +++ b/core/jni/android/graphics/pdf/PdfRenderer.cpp @@ -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);