pdfviewer: fix font rotation issues
authoredisonn@google.com <edisonn@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 7 Aug 2013 18:04:15 +0000 (18:04 +0000)
committeredisonn@google.com <edisonn@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 7 Aug 2013 18:04:15 +0000 (18:04 +0000)
Review URL: https://codereview.chromium.org/22407005

git-svn-id: http://skia.googlecode.com/svn/trunk@10617 2bbb7eff-a529-9590-31e7-b0007b416f81

experimental/PdfViewer/SkPdfFont.h
experimental/PdfViewer/SkPdfRenderer.cpp
experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp

index d5641b2..bbbaa7a 100644 (file)
@@ -172,9 +172,14 @@ public:
 
     void drawText(const SkDecodedText& text, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) {
         for (int i = 0 ; i < text.size(); i++) {
+            canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
+#ifdef PDF_TRACE
+            SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(0));
+            pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1);
+            printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()), SkScalarToDouble(point.y()));
+#endif  // PDF_TRACE
             double width = drawOneChar(text[i], paint, pdfContext, canvas);
             pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(width), SkDoubleToScalar(0.0));
-            canvas->translate(SkDoubleToScalar(width), SkDoubleToScalar(0.0));
         }
     }
 
index 4f36c0b..e2197b0 100644 (file)
@@ -390,26 +390,6 @@ static PdfResult DrawText(PdfContext* pdfContext,
 
     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 @@ static PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfT
 
     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 @@ PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObjec
 
     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 @@ static PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoop
     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 @@ static PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoop
 
     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_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLoope
 
 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;
 }
index 333b0a2..2e7e670 100644 (file)
@@ -90,21 +90,22 @@ SkNativeParsedPDF::SkNativeParsedPDF(const char* path)
         , fRootCatalog(NULL) {
     gDoc = this;
     FILE* file = fopen(path, "r");
-    size_t size = getFileSize(path);
-    void* content = sk_malloc_throw(size);
-    bool ok = (0 != fread(content, size, 1, file));
-    fclose(file);
-    file = NULL;
+    // TODO(edisonn): put this in a function that can return NULL
+    if (file) {
+        size_t size = getFileSize(path);
+        void* content = sk_malloc_throw(size);
+        bool ok = (0 != fread(content, size, 1, file));
+        fclose(file);
+        if (!ok) {
+            sk_free(content);
+            // TODO(edisonn): report read error
+            // TODO(edisonn): not nice to return like this from constructor, create a static
+            // function that can report NULL for failures.
+            return;  // Doc will have 0 pages
+        }
 
-    if (!ok) {
-        sk_free(content);
-        // TODO(edisonn): report read error
-        // TODO(edisonn): not nice to return like this from constructor, create a static
-        // function that can report NULL for failures.
-        return;  // Doc will have 0 pages
+        init(content, size);
     }
-
-    init(content, size);
 }
 
 void SkNativeParsedPDF::init(const void* bytes, size_t length) {