int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels) { // Make sure the element is not freed during the layout. RefPtrWillBeRawPtr<Element> protect(element); element->document().updateLayout(); RenderBoxModelObject* box = enclosingBoxModelObject(element->renderer()); if (!box) return -1; LocalFrame* frame = element->document().frame(); FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels); PrintContext printContext(frame); printContext.begin(pageRect.width(), pageRect.height()); FloatSize scaledPageSize = pageSizeInPixels; scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width()); printContext.computePageRectsWithPageSize(scaledPageSize, false); int top = box->pixelSnappedOffsetTop(); int left = box->pixelSnappedOffsetLeft(); size_t pageNumber = 0; for (; pageNumber < printContext.pageCount(); pageNumber++) { const IntRect& page = printContext.pageRect(pageNumber); if (page.x() <= left && left < page.maxX() && page.y() <= top && top < page.maxY()) return pageNumber; } return -1; }
int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels) { element->document().updateStyleAndLayout(); LocalFrame* frame = element->document().frame(); FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels); PrintContext printContext(frame); printContext.begin(pageRect.width(), pageRect.height()); LayoutBoxModelObject* box = enclosingBoxModelObject(element->layoutObject()); if (!box) return -1; FloatSize scaledPageSize = pageSizeInPixels; scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width()); printContext.computePageRectsWithPageSize(scaledPageSize); int top = box->pixelSnappedOffsetTop(box->offsetParent()); int left = box->pixelSnappedOffsetLeft(box->offsetParent()); size_t pageNumber = 0; for (; pageNumber < printContext.pageCount(); pageNumber++) { const IntRect& page = printContext.pageRect(pageNumber); if (page.x() <= left && left < page.maxX() && page.y() <= top && top < page.maxY()) return pageNumber; } return -1; }