pdfviewer: fix font rotation issues

Review URL: https://codereview.chromium.org/22407005

git-svn-id: http://skia.googlecode.com/svn/trunk@10617 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/PdfViewer/SkPdfRenderer.cpp b/experimental/PdfViewer/SkPdfRenderer.cpp
index 4f36c0b..e2197b0 100644
--- a/experimental/PdfViewer/SkPdfRenderer.cpp
+++ b/experimental/PdfViewer/SkPdfRenderer.cpp
@@ -390,26 +390,6 @@
 
     canvas->save();
 
-#if 1
-    SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm;
-
-    SkPoint point1;
-    pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1);
-
-    SkMatrix mirror;
-    mirror.setTranslate(0, -point1.y());
-    // TODO(edisonn): fix rotated text, and skewed too
-    mirror.postScale(SK_Scalar1, -SK_Scalar1);
-    // TODO(edisonn): post rotate, skew
-    mirror.postTranslate(0, point1.y());
-
-    matrix.postConcat(mirror);
-
-    canvas->setMatrix(matrix);
-
-    SkTraceMatrix(matrix, "mirrored");
-#endif
-
     skfont->drawText(decoded, &paint, pdfContext, canvas);
     canvas->restore();
 
@@ -756,8 +736,10 @@
 
     if (skobj->has_Matrix()) {
         pdfContext->fGraphicsState.fCTM.preConcat(skobj->Matrix(pdfContext->fPdfDoc));
-        pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
-        pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
+        SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
+        matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
+        pdfContext->fGraphicsState.fMatrixTm = matrix;
+        pdfContext->fGraphicsState.fMatrixTlm = matrix;
         // TODO(edisonn) reset matrixTm and matricTlm also?
     }
 
@@ -870,9 +852,10 @@
 
     pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
     pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize));
+    pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrixTm;
 
     pdfContext->fGraphicsState.fCTM = pdfContext->fGraphicsState.fMatrixTm;
-    pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
+    pdfContext->fGraphicsState.fCTM.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
 
     SkTraceMatrix(pdfContext->fGraphicsState.fCTM, "Total matrix");
 
@@ -1074,7 +1057,7 @@
     obj = obj;
     double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop();
 
-    double array[6] = {1, 0, 0, 1, tx, ty};
+    double array[6] = {1, 0, 0, 1, tx, -ty};
     SkMatrix matrix = SkMatrixFromPdfMatrix(array);
 
     pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix);
@@ -1125,6 +1108,7 @@
 
     SkMatrix matrix = SkMatrixFromPdfMatrix(array);
     matrix.postConcat(pdfContext->fGraphicsState.fCTM);
+    matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
 
     // TODO(edisonn): Text positioning.
     pdfContext->fGraphicsState.fMatrixTm = matrix;
@@ -1459,8 +1443,10 @@
 
 static PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) {
     pdfContext->fGraphicsState.fTextBlock   = true;
-    pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fCTM;
-    pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fCTM;
+    SkMatrix matrix = pdfContext->fGraphicsState.fCTM;
+    matrix.preScale(SkDoubleToScalar(1), SkDoubleToScalar(-1));
+    pdfContext->fGraphicsState.fMatrixTm = matrix;
+    pdfContext->fGraphicsState.fMatrixTlm = matrix;
 
     return kPartial_PdfResult;
 }