Skip to content

Commit

Permalink
Added StartPage and EndPage methods to the Emf class because the GDI …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sanjeevr@chromium.org committed Jul 12, 2010
1 parent 98a94ee commit 2aa8e18
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
60 changes: 60 additions & 0 deletions printing/emf_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions printing/emf_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions printing/emf_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2aa8e18

Please sign in to comment.