MCPcopy Create free account
hub / github.com/TeXworks/texworks / getContentBoundingBox

Method getContentBoundingBox

modules/QtPDF/src/PDFBackend.cpp:344–389  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

342}
343
344QRectF Page::getContentBoundingBox() const
345{
346 QSizeF pageSize(pageSizeF());
347 // render the page into a 100x100 px image (this should be fast and will allow
348 // estimating the content bounding box to about 1% of the page size)
349 QImage img = renderToImage(100. * 72 / pageSize.width(), 100. * 72 / pageSize.height());
350
351 if (img.isNull())
352 return QRectF();
353
354 // Make sure the image is in a format we can handle here
355 switch (img.format()) {
356 case QImage::Format_ARGB32:
357 case QImage::Format_ARGB32_Premultiplied:
358 case QImage::Format_RGB32:
359 break;
360 default:
361 img = img.convertToFormat(QImage::Format_ARGB32);
362 break;
363 }
364
365 // Get the background color (assumed to be the color of the top left pixel)
366 QRgb bg = img.pixel(0, 0);
367
368 // Make sure the same color is used in the other three corners (otherwise we
369 // can't be sure it's really the global background color; in that case we
370 // assume that everything is content)
371 if (bg != img.pixel(img.width() - 1, 0) || bg != img.pixel(0, img.height() - 1 || bg != img.pixel(img.width() - 1, img.height() - 1)))
372 return QRectF(QPointF(0, 0), pageSize);
373
374 // Find the bounding box (min/max values for x and y) of the content
375 int x0 = img.width(), x1 = 0, y0 = img.height(), y1 = 0;
376 for (int y = 0; y < img.height(); ++y) {
377 const QRgb * row = reinterpret_cast<const QRgb*>(img.constScanLine(y));
378 for (int x = 0; x < img.width(); ++x) {
379 if (row[x] != bg) {
380 if (x0 > x) x0 = x;
381 if (x1 < x) x1 = x;
382 if (y0 > y) y0 = y;
383 if (y1 < y) y1 = y;
384 }
385 }
386 }
387
388 return QRectF(x0 * pageSize.width() / 100., y0 * pageSize.height() / 100., (x1 - x0 + 1) * pageSize.width() / 100., (y1 - y0 + 1) * pageSize.height() / 100.);
389}
390
391QSharedPointer<QImage> Page::getCachedImage(double xres, double yres, QRect render_box /* = QRect() */, PDFPageCache::TileStatus * status /* = nullptr */)
392{

Callers 2

abstractBaseClassesMethod · 0.80
zoomFitContentWidthMethod · 0.80

Calls 1

isNullMethod · 0.80

Tested by 1

abstractBaseClassesMethod · 0.64