+ const long perLine = long(width * perPixel);
+ const long maxstep = (memAvail / perLine).GetValue();
+ const long step = wxMin(height, maxstep);
+
+ wxLogTrace("printing",
+ "using %liMB of RAM (%li lines) for preview, %li %lipx fragments",
+ long((memAvail >> 20).GetValue()),
+ maxstep,
+ (height+step-1) / step,
+ step);
+
+ return step;
+}
+
+} // anonymous namespace
+
+
+bool wxWindowsPrintPreview::RenderPageFragment(float scaleX, float scaleY,
+ int *nextFinalLine,
+ wxPrinterDC& printer,
+ wxMemoryDC& finalDC,
+ const wxRect& rect,
+ int pageNum)
+{
+ // compute 'rect' equivalent in the small final bitmap:
+ const wxRect smallRect(wxPoint(0, *nextFinalLine),
+ wxPoint(int(rect.GetRight() * scaleX),
+ int(rect.GetBottom() * scaleY)));
+ wxLogTrace("printing",
+ "rendering fragment of page %i: [%i,%i,%i,%i] scaled down to [%i,%i,%i,%i]",
+ pageNum,
+ rect.x, rect.y, rect.GetRight(), rect.GetBottom(),
+ smallRect.x, smallRect.y, smallRect.GetRight(), smallRect.GetBottom()
+ );
+
+ // create DC and bitmap compatible with printer DC:
+ wxBitmap large(rect.width, rect.height, printer);
+ if ( !large.IsOk() )
+ return false;
+
+ // render part of the page into it:
+ {
+ PageFragmentDC memoryDC(&printer, large,
+ rect.GetPosition(),
+ wxSize(m_pageWidth, m_pageHeight));
+ if ( !memoryDC.IsOk() )
+ return false;
+
+ memoryDC.Clear();
+
+ if ( !RenderPageIntoDC(memoryDC, pageNum) )
+ return false;
+ } // release bitmap from memoryDC
+
+ // now scale the rendered part down and blit it into final output:
+
+ wxImage img;
+ {
+ wxDIB dib(large);
+ if ( !dib.IsOk() )
+ return false;
+ large = wxNullBitmap; // free memory a.s.a.p.
+ img = dib.ConvertToImage();
+ } // free the DIB now that it's no longer needed, too
+
+ if ( !img.IsOk() )
+ return false;
+
+ img.Rescale(smallRect.width, smallRect.height, wxIMAGE_QUALITY_HIGH);
+ if ( !img.IsOk() )
+ return false;
+
+ wxBitmap bmp(img);
+ if ( !bmp.IsOk() )
+ return false;
+
+ img = wxNullImage;
+ finalDC.DrawBitmap(bmp, smallRect.x, smallRect.y);
+ if ( bmp.IsOk() )
+ {
+ *nextFinalLine += smallRect.height;
+ return true;
+ }
+
+ return false;
+}
+
+bool wxWindowsPrintPreview::RenderPageIntoBitmapHQ(wxBitmap& bmp, int pageNum)
+{
+ wxLogTrace("printing", "rendering HQ preview of page %i", pageNum);
+
+ wxPrinterDC printerDC(m_printDialogData.GetPrintData());
+ if ( !printerDC.IsOk() )
+ return false;
+
+ // compute scale factor
+ const float scaleX = float(bmp.GetWidth()) / float(m_pageWidth);
+ const float scaleY = float(bmp.GetHeight()) / float(m_pageHeight);
+
+ wxMemoryDC bmpDC;
+ bmpDC.SelectObject(bmp);
+ bmpDC.Clear();
+
+ const int initialStep = ComputeFragmentSize(printerDC.GetDepth(),
+ m_pageWidth, m_pageHeight);
+
+ wxRect todo(0, 0, m_pageWidth, initialStep); // rect to render
+ int nextFinalLine = 0; // first not-yet-rendered output line
+
+ while ( todo.y < m_pageHeight )
+ {
+ todo.SetBottom(wxMin(todo.GetBottom(), m_pageHeight - 1));
+
+ if ( !RenderPageFragment(scaleX, scaleY,
+ &nextFinalLine,
+ printerDC,
+ bmpDC,
+ todo,
+ pageNum) )
+ {
+ if ( todo.height < 20 )
+ {
+ // something is very wrong if we can't render even at this
+ // slow space, let's bail out and fall back to low quality
+ // preview
+ wxLogTrace("printing",
+ "it seems that HQ preview doesn't work at all");
+ return false;
+ }
+
+ // it's possible our memory calculation was off, or conditions
+ // changed, or there's not enough _bitmap_ resources; try if using
+ // smaller bitmap would help:
+ todo.height /= 2;
+
+ wxLogTrace("printing",
+ "preview of fragment failed, reducing height to %ipx",
+ todo.height);
+
+ continue; // retry at the same position again
+ }
+
+ // move to the next segment
+ todo.Offset(0, todo.height);
+ }
+
+ return true;
+}
+
+bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
+{
+ // try high quality rendering first:
+ if ( !m_hqPreviewFailed )
+ {
+ if ( RenderPageIntoBitmapHQ(bmp, pageNum) )
+ {
+ return true;
+ }
+ else
+ {
+ wxLogTrace("printing",
+ "high-quality preview failed, falling back to normal");
+ m_hqPreviewFailed = true; // don't bother re-trying
+ }
+ }
+
+ // if it fails, use the generic method that is less resource intensive,
+ // but inexact
+ return wxPrintPreviewBase::RenderPageIntoBitmap(bmp, pageNum);