1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/emf_win.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/win/scoped_gdi_object.h"
11 #include "base/win/scoped_hdc.h"
12 #include "base/win/scoped_select_object.h"
13 #include "skia/ext/vector_platform_device_emf_win.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/codec/jpeg_codec.h"
16 #include "ui/gfx/codec/png_codec.h"
17 #include "ui/gfx/gdi_util.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/size.h"
23 const int kCustomGdiCommentSignature = 0xdeadbabe;
24 struct PageBreakRecord {
30 explicit PageBreakRecord(PageBreakType type_in)
31 : signature(kCustomGdiCommentSignature), type(type_in) {
33 bool IsValid() const {
34 return (signature == kCustomGdiCommentSignature) &&
35 (type >= START_PAGE) && (type <= END_PAGE);
39 int CALLBACK IsAlphaBlendUsedEnumProc(HDC,
41 const ENHMETARECORD *record,
44 bool* result = reinterpret_cast<bool*>(data);
47 switch (record->iType) {
48 case EMR_ALPHABLEND: {
57 int CALLBACK RasterizeAlphaBlendProc(HDC metafile_dc,
58 HANDLETABLE* handle_table,
59 const ENHMETARECORD *record,
62 HDC bitmap_dc = *reinterpret_cast<HDC*>(data);
63 // Play this command to the bitmap DC.
64 ::PlayEnhMetaFileRecord(bitmap_dc, handle_table, record, num_objects);
65 switch (record->iType) {
66 case EMR_ALPHABLEND: {
67 const EMRALPHABLEND* alpha_blend =
68 reinterpret_cast<const EMRALPHABLEND*>(record);
69 // Don't modify transformation here.
70 // Old implementation did reset transformations for DC to identity matrix.
71 // That was not correct and cause some bugs, like unexpected cropping.
72 // EMRALPHABLEND is rendered into bitmap and metafile contexts with
73 // current transformation. If we don't touch them here BitBlt will copy
86 case EMR_CREATEBRUSHINDIRECT:
87 case EMR_CREATECOLORSPACE:
88 case EMR_CREATECOLORSPACEW:
89 case EMR_CREATEDIBPATTERNBRUSHPT:
90 case EMR_CREATEMONOBRUSH:
91 case EMR_CREATEPALETTE:
93 case EMR_DELETECOLORSPACE:
94 case EMR_DELETEOBJECT:
95 case EMR_EXTCREATEFONTINDIRECTW:
96 // Play object creation command only once.
100 // Play this command to the metafile DC.
101 ::PlayEnhMetaFileRecord(metafile_dc, handle_table, record, num_objects);
104 return 1; // Continue enumeration
107 // Bitmapt for rasterization.
110 explicit RasterBitmap(const gfx::Size& raster_size)
111 : saved_object_(NULL) {
112 context_.Set(::CreateCompatibleDC(NULL));
114 NOTREACHED() << "Bitmap DC creation failed";
117 ::SetGraphicsMode(context_, GM_ADVANCED);
119 gfx::Rect bitmap_rect(raster_size);
120 gfx::CreateBitmapHeader(raster_size.width(), raster_size.height(),
122 bitmap_.Set(::CreateDIBSection(context_, &header_, DIB_RGB_COLORS, &bits,
125 NOTREACHED() << "Raster bitmap creation for printing failed";
127 saved_object_ = ::SelectObject(context_, bitmap_);
128 RECT rect = bitmap_rect.ToRECT();
129 ::FillRect(context_, &rect,
130 static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH)));
135 ::SelectObject(context_, saved_object_);
138 HDC context() const {
142 base::win::ScopedCreateDC context_;
144 base::win::ScopedBitmap bitmap_;
145 HGDIOBJ saved_object_;
148 DISALLOW_COPY_AND_ASSIGN(RasterBitmap);
157 bool DIBFormatNativelySupported(HDC dc, uint32 escape, const BYTE* bits,
159 BOOL supported = FALSE;
160 if (ExtEscape(dc, QUERYESCSUPPORT, sizeof(escape),
161 reinterpret_cast<LPCSTR>(&escape), 0, 0) > 0) {
162 ExtEscape(dc, escape, size, reinterpret_cast<LPCSTR>(bits),
163 sizeof(supported), reinterpret_cast<LPSTR>(&supported));
168 Emf::Emf() : emf_(NULL), hdc_(NULL), page_count_(0) {
178 DeleteEnhMetaFile(emf_);
182 bool Emf::InitToFile(const base::FilePath& metafile_path) {
183 DCHECK(!emf_ && !hdc_);
184 hdc_ = CreateEnhMetaFile(NULL, metafile_path.value().c_str(), NULL, NULL);
189 bool Emf::InitFromFile(const base::FilePath& metafile_path) {
190 DCHECK(!emf_ && !hdc_);
191 emf_ = GetEnhMetaFile(metafile_path.value().c_str());
197 DCHECK(!emf_ && !hdc_);
198 hdc_ = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
203 bool Emf::InitFromData(const void* src_buffer, uint32 src_buffer_size) {
204 DCHECK(!emf_ && !hdc_);
205 emf_ = SetEnhMetaFileBits(src_buffer_size,
206 reinterpret_cast<const BYTE*>(src_buffer));
210 bool Emf::FinishDocument() {
211 DCHECK(!emf_ && hdc_);
212 emf_ = CloseEnhMetaFile(hdc_);
218 bool Emf::Playback(HDC hdc, const RECT* rect) const {
219 DCHECK(emf_ && !hdc_);
222 // Get the natural bounds of the EMF buffer.
223 bounds = GetPageBounds(1).ToRECT();
226 return PlayEnhMetaFile(hdc, emf_, rect) != 0;
229 bool Emf::SafePlayback(HDC context) const {
230 DCHECK(emf_ && !hdc_);
232 if (!GetWorldTransform(context, &base_matrix)) {
236 Emf::EnumerationContext playback_context;
237 playback_context.base_matrix = &base_matrix;
238 RECT rect = GetPageBounds(1).ToRECT();
239 return EnumEnhMetaFile(context,
241 &Emf::SafePlaybackProc,
242 reinterpret_cast<void*>(&playback_context),
246 gfx::Rect Emf::GetPageBounds(unsigned int page_number) const {
247 DCHECK(emf_ && !hdc_);
248 DCHECK_EQ(1U, page_number);
249 ENHMETAHEADER header;
250 if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) {
254 // Add 1 to right and bottom because it's inclusive rectangle.
255 // See ENHMETAHEADER.
256 return gfx::Rect(header.rclBounds.left,
257 header.rclBounds.top,
258 header.rclBounds.right - header.rclBounds.left + 1,
259 header.rclBounds.bottom - header.rclBounds.top + 1);
262 uint32 Emf::GetDataSize() const {
263 DCHECK(emf_ && !hdc_);
264 return GetEnhMetaFileBits(emf_, 0, NULL);
267 bool Emf::GetData(void* buffer, uint32 size) const {
268 DCHECK(emf_ && !hdc_);
269 DCHECK(buffer && size);
271 GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer));
272 DCHECK(size2 == size);
273 return size2 == size && size2 != 0;
276 bool Emf::GetDataAsVector(std::vector<uint8>* buffer) const {
277 uint32 size = GetDataSize();
281 buffer->resize(size);
282 if (!GetData(&buffer->front(), size))
287 bool Emf::SaveTo(const base::FilePath& file_path) const {
288 HANDLE file = CreateFile(file_path.value().c_str(), GENERIC_WRITE,
289 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
290 CREATE_ALWAYS, 0, NULL);
291 if (file == INVALID_HANDLE_VALUE)
294 bool success = false;
295 std::vector<uint8> buffer;
296 if (GetDataAsVector(&buffer)) {
298 if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()),
300 written == buffer.size()) {
308 int CALLBACK Emf::SafePlaybackProc(HDC hdc,
309 HANDLETABLE* handle_table,
310 const ENHMETARECORD* record,
313 Emf::EnumerationContext* context =
314 reinterpret_cast<Emf::EnumerationContext*>(param);
315 context->handle_table = handle_table;
316 context->objects_count = objects_count;
318 Record record_instance(record);
319 bool success = record_instance.SafePlayback(context);
324 Emf::EnumerationContext::EnumerationContext() {
325 memset(this, 0, sizeof(*this));
328 Emf::Record::Record(const ENHMETARECORD* record)
333 bool Emf::Record::Play(Emf::EnumerationContext* context) const {
334 return 0 != PlayEnhMetaFileRecord(context->hdc,
335 context->handle_table,
337 context->objects_count);
340 bool Emf::Record::SafePlayback(Emf::EnumerationContext* context) const {
341 // For EMF field description, see [MS-EMF] Enhanced Metafile Format
344 // This is the second major EMF breakage I get; the first one being
345 // SetDCBrushColor/SetDCPenColor/DC_PEN/DC_BRUSH being silently ignored.
347 // This function is the guts of the fix for bug 1186598. Some printer drivers
348 // somehow choke on certain EMF records, but calling the corresponding
349 // function directly on the printer HDC is fine. Still, playing the EMF record
352 // The main issue is that SetLayout is totally unsupported on these printers
353 // (HP 4500/4700). I used to call SetLayout and I stopped. I found out this is
354 // not sufficient because GDI32!PlayEnhMetaFile internally calls SetLayout(!)
357 // So I resorted to manually parse the EMF records and play them one by one.
358 // The issue with this method compared to using PlayEnhMetaFile to play back
359 // an EMF buffer is that the later silently fixes the matrix to take in
360 // account the matrix currently loaded at the time of the call.
361 // The matrix magic is done transparently when using PlayEnhMetaFile but since
362 // I'm processing one field at a time, I need to do the fixup myself. Note
363 // that PlayEnhMetaFileRecord doesn't fix the matrix correctly even when
364 // called inside an EnumEnhMetaFile loop. Go figure (bis).
366 // So when I see a EMR_SETWORLDTRANSFORM and EMR_MODIFYWORLDTRANSFORM, I need
367 // to fix the matrix according to the matrix previously loaded before playing
368 // back the buffer. Otherwise, the previously loaded matrix would be ignored
369 // and the EMF buffer would always be played back at its native resolution.
372 // I also use this opportunity to skip over eventual EMR_SETLAYOUT record that
375 // Another tweak we make is for JPEGs/PNGs in calls to StretchDIBits.
376 // (Our Pepper plugin code uses a JPEG). If the printer does not support
377 // JPEGs/PNGs natively we decompress the JPEG/PNG and then set it to the
379 // TODO(sanjeevr): We should also add JPEG/PNG support for SetSIBitsToDevice
381 // We also process any custom EMR_GDICOMMENT records which are our
382 // placeholders for StartPage and EndPage.
383 // Note: I should probably care about view ports and clipping, eventually.
385 const XFORM* base_matrix = context->base_matrix;
386 switch (record()->iType) {
387 case EMR_STRETCHDIBITS: {
388 const EMRSTRETCHDIBITS * sdib_record =
389 reinterpret_cast<const EMRSTRETCHDIBITS*>(record());
390 const BYTE* record_start = reinterpret_cast<const BYTE *>(record());
391 const BITMAPINFOHEADER *bmih =
392 reinterpret_cast<const BITMAPINFOHEADER *>(record_start +
393 sdib_record->offBmiSrc);
394 const BYTE* bits = record_start + sdib_record->offBitsSrc;
395 bool play_normally = true;
397 HDC hdc = context->hdc;
398 scoped_ptr<SkBitmap> bitmap;
399 if (bmih->biCompression == BI_JPEG) {
400 if (!DIBFormatNativelySupported(hdc, CHECKJPEGFORMAT, bits,
401 bmih->biSizeImage)) {
402 play_normally = false;
403 bitmap.reset(gfx::JPEGCodec::Decode(bits, bmih->biSizeImage));
405 } else if (bmih->biCompression == BI_PNG) {
406 if (!DIBFormatNativelySupported(hdc, CHECKPNGFORMAT, bits,
407 bmih->biSizeImage)) {
408 play_normally = false;
409 bitmap.reset(new SkBitmap());
410 gfx::PNGCodec::Decode(bits, bmih->biSizeImage, bitmap.get());
413 if (!play_normally) {
414 DCHECK(bitmap.get());
416 SkAutoLockPixels lock(*bitmap.get());
417 DCHECK_EQ(bitmap->colorType(), kN32_SkColorType);
418 const uint32_t* pixels =
419 static_cast<const uint32_t*>(bitmap->getPixels());
420 if (pixels == NULL) {
424 BITMAPINFOHEADER bmi = {0};
425 gfx::CreateBitmapHeader(bitmap->width(), bitmap->height(), &bmi);
426 res = (0 != StretchDIBits(hdc, sdib_record->xDest, sdib_record->yDest,
428 sdib_record->cyDest, sdib_record->xSrc,
430 sdib_record->cxSrc, sdib_record->cySrc,
432 reinterpret_cast<const BITMAPINFO *>(&bmi),
433 sdib_record->iUsageSrc,
434 sdib_record->dwRop));
441 case EMR_SETWORLDTRANSFORM: {
442 DCHECK_EQ(record()->nSize, sizeof(DWORD) * 2 + sizeof(XFORM));
443 const XFORM* xform = reinterpret_cast<const XFORM*>(record()->dParm);
444 HDC hdc = context->hdc;
446 res = 0 != SetWorldTransform(hdc, base_matrix) &&
447 ModifyWorldTransform(hdc, xform, MWT_LEFTMULTIPLY);
449 res = 0 != SetWorldTransform(hdc, xform);
453 case EMR_MODIFYWORLDTRANSFORM: {
454 DCHECK_EQ(record()->nSize,
455 sizeof(DWORD) * 2 + sizeof(XFORM) + sizeof(DWORD));
456 const XFORM* xform = reinterpret_cast<const XFORM*>(record()->dParm);
457 const DWORD* option = reinterpret_cast<const DWORD*>(xform + 1);
458 HDC hdc = context->hdc;
462 res = 0 != SetWorldTransform(hdc, base_matrix);
464 res = 0 != ModifyWorldTransform(hdc, xform, MWT_IDENTITY);
467 case MWT_LEFTMULTIPLY:
468 case MWT_RIGHTMULTIPLY:
469 res = 0 != ModifyWorldTransform(hdc, xform, *option);
473 res = 0 != SetWorldTransform(hdc, base_matrix) &&
474 ModifyWorldTransform(hdc, xform, MWT_LEFTMULTIPLY);
476 res = 0 != SetWorldTransform(hdc, xform);
489 case EMR_GDICOMMENT: {
490 const EMRGDICOMMENT* comment_record =
491 reinterpret_cast<const EMRGDICOMMENT*>(record());
492 if (comment_record->cbData == sizeof(PageBreakRecord)) {
493 const PageBreakRecord* page_break_record =
494 reinterpret_cast<const PageBreakRecord*>(comment_record->Data);
495 if (page_break_record && page_break_record->IsValid()) {
496 if (page_break_record->type == PageBreakRecord::START_PAGE) {
497 res = !!::StartPage(context->hdc);
498 DCHECK_EQ(0, context->dc_on_page_start);
499 context->dc_on_page_start = ::SaveDC(context->hdc);
500 } else if (page_break_record->type == PageBreakRecord::END_PAGE) {
501 DCHECK_NE(0, context->dc_on_page_start);
502 ::RestoreDC(context->hdc, context->dc_on_page_start);
503 context->dc_on_page_start = 0;
504 res = !!::EndPage(context->hdc);
525 SkBaseDevice* Emf::StartPageForVectorCanvas(
526 const gfx::Size& page_size, const gfx::Rect& content_area,
527 const float& scale_factor) {
528 if (!StartPage(page_size, content_area, scale_factor))
531 return skia::VectorPlatformDeviceEmf::CreateDevice(page_size.width(),
536 bool Emf::StartPage(const gfx::Size& /*page_size*/,
537 const gfx::Rect& /*content_area*/,
538 const float& /*scale_factor*/) {
543 PageBreakRecord record(PageBreakRecord::START_PAGE);
544 return !!GdiComment(hdc_, sizeof(record),
545 reinterpret_cast<const BYTE *>(&record));
548 bool Emf::FinishPage() {
552 PageBreakRecord record(PageBreakRecord::END_PAGE);
553 return !!GdiComment(hdc_, sizeof(record),
554 reinterpret_cast<const BYTE *>(&record));
557 Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) {
559 if (!EnumEnhMetaFile(context,
561 &Emf::Enumerator::EnhMetaFileProc,
562 reinterpret_cast<void*>(this),
567 DCHECK_EQ(context_.hdc, context);
570 Emf::Enumerator::const_iterator Emf::Enumerator::begin() const {
571 return items_.begin();
574 Emf::Enumerator::const_iterator Emf::Enumerator::end() const {
578 int CALLBACK Emf::Enumerator::EnhMetaFileProc(HDC hdc,
579 HANDLETABLE* handle_table,
580 const ENHMETARECORD* record,
583 Enumerator& emf = *reinterpret_cast<Enumerator*>(param);
584 if (!emf.context_.handle_table) {
585 DCHECK(!emf.context_.handle_table);
586 DCHECK(!emf.context_.objects_count);
587 emf.context_.handle_table = handle_table;
588 emf.context_.objects_count = objects_count;
589 emf.context_.hdc = hdc;
591 DCHECK_EQ(emf.context_.handle_table, handle_table);
592 DCHECK_EQ(emf.context_.objects_count, objects_count);
593 DCHECK_EQ(emf.context_.hdc, hdc);
595 emf.items_.push_back(Record(record));
599 bool Emf::IsAlphaBlendUsed() const {
601 ::EnumEnhMetaFile(NULL,
603 &IsAlphaBlendUsedEnumProc,
609 Emf* Emf::RasterizeMetafile(int raster_area_in_pixels) const {
610 gfx::Rect page_bounds = GetPageBounds(1);
611 gfx::Size page_size(page_bounds.size());
612 if (page_size.GetArea() <= 0) {
613 NOTREACHED() << "Metafile is empty";
614 page_bounds = gfx::Rect(1, 1);
617 float scale = sqrt(float(raster_area_in_pixels) / page_size.GetArea());
618 page_size.set_width(std::max<int>(1, page_size.width() * scale));
619 page_size.set_height(std::max<int>(1, page_size.height() * scale));
622 RasterBitmap bitmap(page_size);
624 gfx::Rect bitmap_rect(page_size);
625 RECT rect = bitmap_rect.ToRECT();
626 Playback(bitmap.context(), &rect);
628 scoped_ptr<Emf> result(new Emf);
630 HDC hdc = result->context();
632 skia::InitializeDC(hdc);
634 // Params are ignored.
635 result->StartPage(page_bounds.size(), page_bounds, 1);
637 ::ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
639 float(page_bounds.width()) / bitmap_rect.width(), 0,
640 0, float(page_bounds.height()) / bitmap_rect.height(),
644 ::SetWorldTransform(hdc, &xform);
645 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(),
646 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY);
648 result->FinishPage();
649 result->FinishDocument();
651 return result.release();
654 Emf* Emf::RasterizeAlphaBlend() const {
655 gfx::Rect page_bounds = GetPageBounds(1);
656 if (page_bounds.size().GetArea() <= 0) {
657 NOTREACHED() << "Metafile is empty";
658 page_bounds = gfx::Rect(1, 1);
661 RasterBitmap bitmap(page_bounds.size());
663 // Map metafile page_bounds.x(), page_bounds.y() to bitmap 0, 0.
664 XFORM xform = { 1, 0, 0, 1, -page_bounds.x(), -page_bounds.y()};
665 ::SetWorldTransform(bitmap.context(), &xform);
667 scoped_ptr<Emf> result(new Emf);
669 HDC hdc = result->context();
671 skia::InitializeDC(hdc);
673 HDC bitmap_dc = bitmap.context();
674 RECT rect = page_bounds.ToRECT();
675 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect);
677 result->FinishDocument();
679 return result.release();
683 } // namespace printing