summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
authorsanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-12 16:25:04 +0000
committersanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-12 16:25:04 +0000
commit2aa8e1835e136a1979210121cca1b68f25187a66 (patch)
tree229a5abbc11360cca47c8ed128f8d46f927c3059 /printing
parent98a94ee562ef36213099a153ca6f3fa4b3b5d09c (diff)
downloadchromium_src-2aa8e1835e136a1979210121cca1b68f25187a66.zip
chromium_src-2aa8e1835e136a1979210121cca1b68f25187a66.tar.gz
chromium_src-2aa8e1835e136a1979210121cca1b68f25187a66.tar.bz2
Added StartPage and EndPage methods to the Emf class because the GDI StartPage and EndPage APIs do not work in a metafile DC. We write custom GDICOMMENT records to designate a StartPage and EndPage.
BUG=None TEST=Unit-tests provided. Review URL: http://codereview.chromium.org/2947006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52084 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r--printing/emf_win.cc60
-rw-r--r--printing/emf_win.h6
-rw-r--r--printing/emf_win_unittest.cc41
3 files changed, 107 insertions, 0 deletions
diff --git a/printing/emf_win.cc b/printing/emf_win.cc
index d8e03af..e93acf2 100644
--- a/printing/emf_win.cc
+++ b/printing/emf_win.cc
@@ -14,6 +14,24 @@
#include "gfx/rect.h"
#include "third_party/skia/include/core/SkBitmap.h"
+namespace {
+const int kCustomGdiCommentSignature = 0xdeadbabe;
+struct PageBreakRecord {
+ int signature;
+ enum PageBreakType {
+ START_PAGE,
+ END_PAGE,
+ } type;
+ explicit PageBreakRecord(PageBreakType type_in)
+ : signature(kCustomGdiCommentSignature), type(type_in) {
+ }
+ bool IsValid() const {
+ return (signature == kCustomGdiCommentSignature) &&
+ (type >= START_PAGE) && (type <= END_PAGE);
+ }
+};
+}
+
namespace printing {
bool DIBFormatNativelySupported(HDC dc, uint32 escape, const BYTE* bits,
@@ -232,6 +250,8 @@ bool Emf::Record::SafePlayback(const XFORM* base_matrix) const {
// device.
// TODO(sanjeevr): We should also add JPEG/PNG support for SetSIBitsToDevice
//
+ // We also process any custom EMR_GDICOMMENT records which are our
+ // placeholders for StartPage and EndPage.
// Note: I should probably care about view ports and clipping, eventually.
bool res;
switch (record()->iType) {
@@ -343,6 +363,27 @@ bool Emf::Record::SafePlayback(const XFORM* base_matrix) const {
// Ignore it.
res = true;
break;
+ case EMR_GDICOMMENT: {
+ const EMRGDICOMMENT* comment_record =
+ reinterpret_cast<const EMRGDICOMMENT*>(record());
+ if (comment_record->cbData == sizeof(PageBreakRecord)) {
+ const PageBreakRecord* page_break_record =
+ reinterpret_cast<const PageBreakRecord*>(comment_record->Data);
+ if (page_break_record && page_break_record->IsValid()) {
+ if (page_break_record->type == PageBreakRecord::START_PAGE) {
+ res = !!::StartPage(context_->hdc);
+ } else if (page_break_record->type == PageBreakRecord::END_PAGE) {
+ res = !!::EndPage(context_->hdc);
+ } else {
+ res = false;
+ NOTREACHED();
+ }
+ } else {
+ res = Play();
+ }
+ }
+ break;
+ }
default: {
res = Play();
break;
@@ -351,6 +392,25 @@ bool Emf::Record::SafePlayback(const XFORM* base_matrix) const {
return res;
}
+bool Emf::StartPage() {
+ DCHECK(hdc_);
+ if (!hdc_)
+ return false;
+ PageBreakRecord record(PageBreakRecord::START_PAGE);
+ return !!GdiComment(hdc_, sizeof(record),
+ reinterpret_cast<const BYTE *>(&record));
+}
+
+bool Emf::EndPage() {
+ DCHECK(hdc_);
+ if (!hdc_)
+ return false;
+ PageBreakRecord record(PageBreakRecord::END_PAGE);
+ return !!GdiComment(hdc_, sizeof(record),
+ reinterpret_cast<const BYTE *>(&record));
+}
+
+
Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) {
context_.handle_table = NULL;
context_.objects_count = 0;
diff --git a/printing/emf_win.h b/printing/emf_win.h
index 9f3dffe..9581d77 100644
--- a/printing/emf_win.h
+++ b/printing/emf_win.h
@@ -84,6 +84,12 @@ class Emf {
return hdc_;
}
+ // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
+ // (since StartPage and EndPage do not work in a metafile DC). Only valid
+ // when hdc_ is non-NULL.
+ bool StartPage();
+ bool EndPage();
+
// Saves the EMF data to a file as-is. It is recommended to use the .emf file
// extension but it is not enforced. This function synchronously writes to the
// file. For testing only.
diff --git a/printing/emf_win_unittest.cc b/printing/emf_win_unittest.cc
index fb85282..3b4c473 100644
--- a/printing/emf_win_unittest.cc
+++ b/printing/emf_win_unittest.cc
@@ -6,11 +6,13 @@
// For quick access.
#include <wingdi.h>
+#include <winspool.h>
#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
+#include "base/scoped_handle_win.h"
#include "printing/printing_context.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -113,3 +115,42 @@ TEST_F(EmfPrintingTest, Enumerate) {
context.DocumentDone();
}
+// Disabled if no "UnitTest printer" exists.
+TEST_F(EmfPrintingTest, PageBreak) {
+ ScopedHDC dc(CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
+ if (!dc.Get())
+ return;
+ printing::Emf emf;
+ EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL));
+ EXPECT_TRUE(emf.hdc() != NULL);
+ int pages = 3;
+ while (pages) {
+ EXPECT_TRUE(emf.StartPage());
+ ::Rectangle(emf.hdc(), 10, 10, 190, 190);
+ EXPECT_TRUE(emf.EndPage());
+ --pages;
+ }
+ EXPECT_TRUE(emf.CloseDc());
+ uint32 size = emf.GetDataSize();
+ std::vector<BYTE> data;
+ EXPECT_TRUE(emf.GetData(&data));
+ EXPECT_EQ(data.size(), size);
+ emf.CloseEmf();
+
+ // Playback the data.
+ DOCINFO di = {0};
+ di.cbSize = sizeof(DOCINFO);
+ di.lpszDocName = L"Test Job";
+ int job_id = ::StartDoc(dc.Get(), &di);
+ EXPECT_TRUE(emf.CreateFromData(&data.front(), size));
+ EXPECT_TRUE(emf.SafePlayback(dc.Get()));
+ ::EndDoc(dc.Get());
+ // Since presumably the printer is not real, let us just delete the job from
+ // the queue.
+ HANDLE printer = NULL;
+ if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) {
+ ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE);
+ ClosePrinter(printer);
+ }
+}
+