Change pdfTransform to asAffine.
http://codereview.appspot.com/4704044/



git-svn-id: http://skia.googlecode.com/svn/trunk@1851 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkMatrix.h b/include/core/SkMatrix.h
index 533e534..a8c50fc 100644
--- a/include/core/SkMatrix.h
+++ b/include/core/SkMatrix.h
@@ -92,6 +92,18 @@
         kMPersp2
     };
     
+    /** Affine arrays are in column major order
+        because that's how PDF and XPS like it.
+     */
+    enum {
+        kAScaleX,
+        kASkewY,
+        kASkewX,
+        kAScaleY,
+        kATransX,
+        kATransY
+    };
+
     SkScalar operator[](int index) const {
         SkASSERT((unsigned)index < 9);
         return fMat[index];
@@ -318,12 +330,19 @@
     */
     bool invert(SkMatrix* inverse) const;
 
-    /** Fills the passed array with the tranform values in the right order
-        for PDFs.  If the matrix is a perspective transform, returns false
-        and fills the array with an identity transform.
-        @param transform  The array to fill in.
+    /** Fills the passed array with affine identity values
+        in column major order.
+        @param affine  The array to fill with affine identity values.
+        Must not be NULL.
     */
-    bool pdfTransform(SkScalar transform[6]) const;
+    static void SetAffineIdentity(SkScalar affine[6]);
+
+    /** Fills the passed array with the affine values in column major order.
+        If the matrix is a perspective transform, returns false
+        and does not change the passed array.
+        @param affine  The array to fill with affine values. Ignored if NULL.
+    */
+    bool asAffine(SkScalar affine[6]) const;
 
     /** Apply this matrix to the array of points specified by src, and write
         the transformed points into the array of points specified by dst.
diff --git a/src/core/SkMatrix.cpp b/src/core/SkMatrix.cpp
index da66a68..8a08bca 100644
--- a/src/core/SkMatrix.cpp
+++ b/src/core/SkMatrix.cpp
@@ -760,21 +760,27 @@
     }
 #endif
 
-bool SkMatrix::pdfTransform(SkScalar transform[6]) const {
-    SkMatrix identity;
-    const SkMatrix* use = this;
-    bool ret = true;
+void SkMatrix::SetAffineIdentity(SkScalar affine[6]) {
+    affine[kAScaleX] = SK_Scalar1;
+    affine[kASkewY] = 0;
+    affine[kASkewX] = 0;
+    affine[kAScaleY] = SK_Scalar1;
+    affine[kATransX] = 0;
+    affine[kATransY] = 0;
+}
+
+bool SkMatrix::asAffine(SkScalar affine[6]) const {
     if (this->hasPerspective()) {
-        identity.reset();
-        use = &identity;
-        ret = false;
+        return false;
     }
-    transform[0] = use->fMat[kMScaleX];
-    transform[1] = use->fMat[kMSkewY];
-    transform[2] = use->fMat[kMSkewX];
-    transform[3] = use->fMat[kMScaleY];
-    transform[4] = use->fMat[kMTransX];
-    transform[5] = use->fMat[kMTransY];
+    if (affine) {
+        affine[kAScaleX] = this->fMat[kMScaleX];
+        affine[kASkewY] = this->fMat[kMSkewY];
+        affine[kASkewX] = this->fMat[kMSkewX];
+        affine[kAScaleY] = this->fMat[kMScaleY];
+        affine[kATransX] = this->fMat[kMTransX];
+        affine[kATransY] = this->fMat[kMTransY];
+    }
     return true;
 }
 
diff --git a/src/pdf/SkPDFUtils.cpp b/src/pdf/SkPDFUtils.cpp
index fcad8a6..6c78a8e 100644
--- a/src/pdf/SkPDFUtils.cpp
+++ b/src/pdf/SkPDFUtils.cpp
@@ -24,7 +24,9 @@
 // static
 SkPDFArray* SkPDFUtils::MatrixToArray(const SkMatrix& matrix) {
     SkScalar values[6];
-    SkAssertResult(matrix.pdfTransform(values));
+    if (!matrix.asAffine(values)) {
+        SkMatrix::SetAffineIdentity(values);
+    }
 
     SkPDFArray* result = new SkPDFArray;
     result->reserve(6);
@@ -37,7 +39,9 @@
 // static
 void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) {
     SkScalar values[6];
-    SkAssertResult(matrix.pdfTransform(values));
+    if (!matrix.asAffine(values)) {
+        SkMatrix::SetAffineIdentity(values);
+    }
     for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
         SkPDFScalar::Append(values[i], content);
         content->writeText(" ");
diff --git a/tests/MatrixTest.cpp b/tests/MatrixTest.cpp
index 4125f9f..5eada0e 100644
--- a/tests/MatrixTest.cpp
+++ b/tests/MatrixTest.cpp
@@ -117,6 +117,30 @@
                     m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
         }
     }
+
+    mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
+    mat.set(SkMatrix::kMSkewX,  SkIntToScalar(2));
+    mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
+    mat.set(SkMatrix::kMSkewY,  SkIntToScalar(4));
+    mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
+    mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
+    mat.set(SkMatrix::kMPersp0, SkIntToScalar(0));
+    mat.set(SkMatrix::kMPersp1, SkIntToScalar(0));
+    mat.set(SkMatrix::kMPersp2, SkIntToScalar(1));
+    SkScalar affine[6];
+    REPORTER_ASSERT(reporter, mat.asAffine(affine));
+
+    #define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
+    REPORTER_ASSERT(reporter, affineEqual(ScaleX));
+    REPORTER_ASSERT(reporter, affineEqual(SkewY));
+    REPORTER_ASSERT(reporter, affineEqual(SkewX));
+    REPORTER_ASSERT(reporter, affineEqual(ScaleY));
+    REPORTER_ASSERT(reporter, affineEqual(TransX));
+    REPORTER_ASSERT(reporter, affineEqual(TransY));
+    #undef affineEqual
+
+    mat.set(SkMatrix::kMPersp1, SkIntToScalar(1));
+    REPORTER_ASSERT(reporter, !mat.asAffine(affine));
 }
 
 #include "TestClassDef.h"