2007-04-05  Oliver Hunt  <oliver@apple.com>

        Reviewed by Adam.

WebCore:
        Fix rdar://problem/5030934 -- landscape PDFs embedded as an image are cut off
        also fixes scaling of pdf images

        PDFDocumentImage was not accounting for page rotation in a number of places

        * platform/graphics/cg/PDFDocumentImage.cpp:
        (WebCore::PDFDocumentImage::size):
          we need to account for rotation of the pdf image effecting our bounds
        (WebCore::PDFDocumentImage::draw):
          When compensating for flipped coords we need to use the correct rect for the 
          coordinate space.  We can't pass size() to CGContextDrawPDFDocument as it
          returns the rotated bounds now -- this is also save the old 
          FloatSize -> IntSize -> FloatSize conversions.
LayoutTests:
        Testcase for landscape pdfs as image

        rdar://problem/5030934 -- landscape PDFs embedded as an image are cut off

        * fast/images/pdf-as-image-landscape-expected.txt: Added.
        * fast/images/pdf-as-image-landscape.html: Added.
        * fast/images/resources/pdf_test_landscape.pdf: Added.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@20720 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index c19acaf..dd5b583 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,15 @@
+2007-04-05  Oliver Hunt  <oliver@apple.com>
+
+        Reviewed by Adam.
+
+        Testcase for landscape pdfs as image
+
+        rdar://problem/5030934 -- landscape PDFs embedded as an image are cut off
+
+        * fast/images/pdf-as-image-landscape-expected.txt: Added.
+        * fast/images/pdf-as-image-landscape.html: Added.
+        * fast/images/resources/pdf_test_landscape.pdf: Added.
+
 2007-04-04  Alexey Proskuryakov  <ap@webkit.org>
 
         Reviewed by Darin.
diff --git a/LayoutTests/fast/images/pdf-as-image-landscape-expected.txt b/LayoutTests/fast/images/pdf-as-image-landscape-expected.txt
new file mode 100644
index 0000000..b5b4fd4
--- /dev/null
+++ b/LayoutTests/fast/images/pdf-as-image-landscape-expected.txt
@@ -0,0 +1,14 @@
+layer at (0,0) size 850x653
+  RenderView at (0,0) size 785x585
+layer at (0,0) size 850x653
+  RenderBlock {HTML} at (0,0) size 785x653
+    RenderBody {BODY} at (8,8) size 769x629
+      RenderBlock (anonymous) at (0,0) size 769x18
+        RenderText {#text} at (0,0) size 663x18
+          text run at (0,0) width 349: "This test that we correctly orient landscape pdf images. "
+          text run at (349,0) width 314: "The green rectangle should be wider than it is tall."
+      RenderBlock {P} at (0,34) size 769x595
+        RenderImage {IMG} at (0,0) size 842x595
+        RenderText {#text} at (0,0) size 0x0
+        RenderText {#text} at (0,0) size 0x0
+        RenderText {#text} at (0,0) size 0x0
diff --git a/LayoutTests/fast/images/pdf-as-image-landscape.html b/LayoutTests/fast/images/pdf-as-image-landscape.html
new file mode 100644
index 0000000..7d524d3
--- /dev/null
+++ b/LayoutTests/fast/images/pdf-as-image-landscape.html
@@ -0,0 +1,8 @@
+<html>
+<head>
+</head>
+<body>
+This test that we correctly orient landscape pdf images.  The green rectangle should be wider than it is tall.<p />
+<img src="resources/pdf_test_landscape.pdf"/>
+</body>
+</html>
diff --git a/LayoutTests/fast/images/resources/pdf_test_landscape.pdf b/LayoutTests/fast/images/resources/pdf_test_landscape.pdf
new file mode 100644
index 0000000..0345486
--- /dev/null
+++ b/LayoutTests/fast/images/resources/pdf_test_landscape.pdf
Binary files differ
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 2e558bf..4cfc791 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,22 @@
+2007-04-05  Oliver Hunt  <oliver@apple.com>
+
+        Reviewed by Adam.
+
+        Fix rdar://problem/5030934 -- landscape PDFs embedded as an image are cut off
+        also fixes scaling of pdf images
+
+        PDFDocumentImage was not accounting for page rotation in a number of places
+
+        * platform/graphics/cg/PDFDocumentImage.cpp:
+        (WebCore::PDFDocumentImage::size):
+          we need to account for rotation of the pdf image effecting our bounds
+        (WebCore::PDFDocumentImage::draw):
+          When compensating for flipped coords we need to use the correct rect for the 
+          coordinate space.  We can't pass size() to CGContextDrawPDFDocument as it
+          returns the rotated bounds now -- this is also save the old 
+          FloatSize -> IntSize -> FloatSize conversions.
+          
+
 2007-04-04  Alexey Proskuryakov  <ap@webkit.org>
 
         Reviewed by Darin.
diff --git a/WebCore/platform/graphics/cg/PDFDocumentImage.cpp b/WebCore/platform/graphics/cg/PDFDocumentImage.cpp
index 5a0215d..0da2593e 100644
--- a/WebCore/platform/graphics/cg/PDFDocumentImage.cpp
+++ b/WebCore/platform/graphics/cg/PDFDocumentImage.cpp
@@ -50,7 +50,14 @@
 
 IntSize PDFDocumentImage::size() const
 {
-    return IntSize((int)m_mediaBox.size().width(), (int)m_mediaBox.size().height());
+    const float sina = sinf(-m_rotation);
+    const float cosa = cosf(-m_rotation);
+    const float width = m_mediaBox.size().width();
+    const float height = m_mediaBox.size().height();
+    const float rotWidth = width * cosa - height * sina;
+    const float rotHeight = width * sina + height * cosa;
+    
+    return IntSize((int)(fabsf(rotWidth) + 0.5f), (int)(fabsf(rotHeight) + 0.5f));
 }
 
 bool PDFDocumentImage::dataChanged(bool allDataReceived)
@@ -150,14 +157,14 @@
     CGContextTranslateCTM(context->platformContext(), dstRect.x() - srcRect.x() * hScale, dstRect.y() - srcRect.y() * vScale);
     CGContextScaleCTM(context->platformContext(), hScale, vScale);
     CGContextScaleCTM(context->platformContext(), 1, -1);
-    CGContextTranslateCTM(context->platformContext(), 0, -dstRect.height());
+    CGContextTranslateCTM(context->platformContext(), 0, -srcRect.height());
     CGContextClipToRect(context->platformContext(), CGRectIntegral(srcRect));
 
     // Rotate translate image into position according to doc properties.
     adjustCTM(context);
 
     // Media box may have non-zero origin which we ignore. Pass 1 for the page number.
-    CGContextDrawPDFDocument(context->platformContext(), FloatRect(FloatPoint(), size()),
+    CGContextDrawPDFDocument(context->platformContext(), FloatRect(FloatPoint(), m_mediaBox.size()),
         m_document, m_currentPage + 1);
 
     context->restore();