Hi Marc-Antoine,

I've managed to fix the printing scale problem by using the routines
provided in SkCanvas.  Below is the updated (working) code for
TestShell::PrintPage().   I'll look into developing a commit-able patch
based on your feedback, but that will likely take some time -- feel free not
to wait for me if these changes interest you :-).

Thanks again for your assistance.

Regards,
Marshall


void TestShell::PrintPage(int page_number, WebFrame* frame) {
  if (printed_document_width_ <= 0) {
    NOTREACHED();
    return;
  }

  ViewMsg_Print_Params params;
  const printing::PrintSettings &settings = print_context_.settings();
  settings.RenderParams(&params);

  gfx::Rect rect;
  frame->GetPageRect(page_number-1, &rect);
  DCHECK(rect.height());
  DCHECK(rect.width());
  double shrink = static_cast<double>(printed_document_width_) /
                  params.printable_size.width();
  // This check would fire each time the page would get truncated on the
  // right. This is not worth a DCHECK() but should be looked into, for
  // example, wouldn't be worth trying in landscape?
  DCHECK_LE(rect.width(), printed_document_width_);

  double scale = settings.dpi() / settings.desired_dpi;
  double margin = .1 * printed_document_width_;

  // Buffer one page at a time.
  int src_size_x = printed_document_width_;
  int src_size_y =
      static_cast<int>(ceil(params.printable_size.height() *
                            shrink));


  print_context_.NewPage();

  HDC hDC = print_context_.context();
  BOOL res;

  // Save the state to make sure the context this function call does not
modify
  // the device context.
  int saved_state = SaveDC(hDC);
  DCHECK_NE(saved_state, 0);

#if 0
  gfx::PlatformDeviceWin::InitializeDC(hDC);

  // Setup the matrix to translate and scale to the right place. Take in
  // account the actual shrinking factor.
  XFORM xform = { 0 };
  xform.eDx = static_cast<float>(src_size_x);
  xform.eDy = static_cast<float>(src_size_y);
  xform.eM11 = static_cast<float>(1. / shrink);
  xform.eM22 = static_cast<float>(1. / shrink);
  res = ModifyWorldTransform(hDC, &xform, MWT_LEFTMULTIPLY);
  DCHECK_NE(res, 0);

  // Mix of Skia and GDI based.
  gfx::PlatformCanvasWin canvas(src_size_x, src_size_y, true);
  canvas.drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode);
  PlatformContextSkia context(&canvas);
  if (!frame->SpoolPage(page_number-1, &context)) {
    NOTREACHED() << "Printing page " << page_number << " failed.";
    return;
  }

  // Create a BMP v4 header that we can serialize.
  BITMAPV4HEADER bitmap_header;
  gfx::CreateBitmapV4Header(src_size_x, src_size_y, &bitmap_header);
  const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true);
  SkAutoLockPixels src_lock(src_bmp);
  int retval = StretchDIBits(hDC,
                             0,
                             0,
                             src_size_x, src_size_y,
                             0, 0,
                             src_size_x, src_size_y,
                             src_bmp.getPixels(),
                             reinterpret_cast<BITMAPINFO*>(&bitmap_header),
                             DIB_RGB_COLORS,
                             SRCCOPY);
  DCHECK(retval != GDI_ERROR);
#else
  // 100% GDI based.
  gfx::VectorCanvas canvas(hDC, static_cast<int>(ceil(scale * src_size_x)),
    static_cast<int>(ceil(scale * src_size_y)));
  canvas.scale(SkDoubleToScalar(scale - scale * (margin /
printed_document_width_) * 2.),
    SkDoubleToScalar(scale - scale * (margin / printed_document_width_) *
2.));
  canvas.translate(SkDoubleToScalar(margin), SkDoubleToScalar(margin));
  PlatformContextSkia context(&canvas);

  // Set the clipping region to be sure to not overflow.
  SkRect clip_rect;
  clip_rect.set(0, 0, SkDoubleToScalar(src_size_x),
SkDoubleToScalar(src_size_y));
  canvas.clipRect(clip_rect);

  if (!frame->SpoolPage(page_number-1, &context)) {
    NOTREACHED() << "Printing page " << page_number << " failed.";
    return;
  }
#endif

  res = RestoreDC(hDC, saved_state);
  DCHECK_NE(res, 0);

  print_context_.PageDone();
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Chromium-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/chromium-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to