1 /* 2 * Copyright (C) 2007 Alp Toker <alp (at) atoker.com> 3 * Copyright (C) 2007 Apple Inc. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21 #include "config.h" 22 #include "core/page/PrintContext.h" 23 24 #include "core/frame/FrameView.h" 25 #include "core/frame/LocalFrame.h" 26 #include "core/rendering/RenderView.h" 27 #include "platform/graphics/GraphicsContext.h" 28 29 namespace blink { 30 31 // By imaging to a width a little wider than the available pixels, 32 // thin pages will be scaled down a little, matching the way they 33 // print in IE and Camino. This lets them use fewer sheets than they 34 // would otherwise, which is presumably why other browsers do this. 35 // Wide pages will be scaled down more than this. 36 const float printingMinimumShrinkFactor = 1.25f; 37 38 // This number determines how small we are willing to reduce the page content 39 // in order to accommodate the widest line. If the page would have to be 40 // reduced smaller to make the widest line fit, we just clip instead (this 41 // behavior matches MacIE and Mozilla, at least) 42 const float printingMaximumShrinkFactor = 2; 43 44 PrintContext::PrintContext(LocalFrame* frame) 45 : m_frame(frame) 46 , m_isPrinting(false) 47 , m_linkAndLinkedDestinationsValid(false) 48 { 49 } 50 51 PrintContext::~PrintContext() 52 { 53 if (m_isPrinting) 54 end(); 55 } 56 57 void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight) 58 { 59 m_pageRects.clear(); 60 outPageHeight = 0; 61 62 if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderView()) 63 return; 64 65 if (userScaleFactor <= 0) { 66 WTF_LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor); 67 return; 68 } 69 70 RenderView* view = m_frame->document()->renderView(); 71 const IntRect& documentRect = view->documentRect(); 72 FloatSize pageSize = m_frame->resizePageRectsKeepingRatio(FloatSize(printRect.width(), printRect.height()), FloatSize(documentRect.width(), documentRect.height())); 73 float pageWidth = pageSize.width(); 74 float pageHeight = pageSize.height(); 75 76 outPageHeight = pageHeight; // this is the height of the page adjusted by margins 77 pageHeight -= headerHeight + footerHeight; 78 79 if (pageHeight <= 0) { 80 WTF_LOG_ERROR("pageHeight has bad value %.2f", pageHeight); 81 return; 82 } 83 84 computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), false); 85 } 86 87 void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling) 88 { 89 m_pageRects.clear(); 90 computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalTiling); 91 } 92 93 void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowInlineDirectionTiling) 94 { 95 if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderView()) 96 return; 97 98 RenderView* view = m_frame->document()->renderView(); 99 100 IntRect docRect = view->documentRect(); 101 102 int pageWidth = pageSizeInPixels.width(); 103 int pageHeight = pageSizeInPixels.height(); 104 105 bool isHorizontal = view->style()->isHorizontalWritingMode(); 106 107 int docLogicalHeight = isHorizontal ? docRect.height() : docRect.width(); 108 int pageLogicalHeight = isHorizontal ? pageHeight : pageWidth; 109 int pageLogicalWidth = isHorizontal ? pageWidth : pageHeight; 110 111 int inlineDirectionStart; 112 int inlineDirectionEnd; 113 int blockDirectionStart; 114 int blockDirectionEnd; 115 if (isHorizontal) { 116 if (view->style()->isFlippedBlocksWritingMode()) { 117 blockDirectionStart = docRect.maxY(); 118 blockDirectionEnd = docRect.y(); 119 } else { 120 blockDirectionStart = docRect.y(); 121 blockDirectionEnd = docRect.maxY(); 122 } 123 inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.x() : docRect.maxX(); 124 inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxX() : docRect.x(); 125 } else { 126 if (view->style()->isFlippedBlocksWritingMode()) { 127 blockDirectionStart = docRect.maxX(); 128 blockDirectionEnd = docRect.x(); 129 } else { 130 blockDirectionStart = docRect.x(); 131 blockDirectionEnd = docRect.maxX(); 132 } 133 inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.y() : docRect.maxY(); 134 inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxY() : docRect.y(); 135 } 136 137 unsigned pageCount = ceilf((float)docLogicalHeight / pageLogicalHeight); 138 for (unsigned i = 0; i < pageCount; ++i) { 139 int pageLogicalTop = blockDirectionEnd > blockDirectionStart ? 140 blockDirectionStart + i * pageLogicalHeight : 141 blockDirectionStart - (i + 1) * pageLogicalHeight; 142 if (allowInlineDirectionTiling) { 143 for (int currentInlinePosition = inlineDirectionStart; 144 inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition < inlineDirectionEnd : currentInlinePosition > inlineDirectionEnd; 145 currentInlinePosition += (inlineDirectionEnd > inlineDirectionStart ? pageLogicalWidth : -pageLogicalWidth)) { 146 int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition : currentInlinePosition - pageLogicalWidth; 147 IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight); 148 if (!isHorizontal) 149 pageRect = pageRect.transposedRect(); 150 m_pageRects.append(pageRect); 151 } 152 } else { 153 int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? inlineDirectionStart : inlineDirectionStart - pageLogicalWidth; 154 IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight); 155 if (!isHorizontal) 156 pageRect = pageRect.transposedRect(); 157 m_pageRects.append(pageRect); 158 } 159 } 160 } 161 162 void PrintContext::begin(float width, float height) 163 { 164 // This function can be called multiple times to adjust printing parameters without going back to screen mode. 165 m_isPrinting = true; 166 167 FloatSize originalPageSize = FloatSize(width, height); 168 FloatSize minLayoutSize = m_frame->resizePageRectsKeepingRatio(originalPageSize, FloatSize(width * printingMinimumShrinkFactor, height * printingMinimumShrinkFactor)); 169 170 // This changes layout, so callers need to make sure that they don't paint to screen while in printing mode. 171 m_frame->setPrinting(true, minLayoutSize, originalPageSize, printingMaximumShrinkFactor / printingMinimumShrinkFactor); 172 } 173 174 void PrintContext::end() 175 { 176 ASSERT(m_isPrinting); 177 m_isPrinting = false; 178 m_frame->setPrinting(false, FloatSize(), FloatSize(), 0); 179 m_linkedDestinations.clear(); 180 m_linkDestinations.clear(); 181 m_linkAndLinkedDestinationsValid = false; 182 } 183 184 static RenderBoxModelObject* enclosingBoxModelObject(RenderObject* object) 185 { 186 187 while (object && !object->isBoxModelObject()) 188 object = object->parent(); 189 if (!object) 190 return 0; 191 return toRenderBoxModelObject(object); 192 } 193 194 int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels) 195 { 196 // Make sure the element is not freed during the layout. 197 RefPtrWillBeRawPtr<Element> protect(element); 198 element->document().updateLayout(); 199 200 RenderBoxModelObject* box = enclosingBoxModelObject(element->renderer()); 201 if (!box) 202 return -1; 203 204 LocalFrame* frame = element->document().frame(); 205 FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels); 206 PrintContext printContext(frame); 207 printContext.begin(pageRect.width(), pageRect.height()); 208 FloatSize scaledPageSize = pageSizeInPixels; 209 scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width()); 210 printContext.computePageRectsWithPageSize(scaledPageSize, false); 211 212 int top = box->pixelSnappedOffsetTop(); 213 int left = box->pixelSnappedOffsetLeft(); 214 size_t pageNumber = 0; 215 for (; pageNumber < printContext.pageCount(); pageNumber++) { 216 const IntRect& page = printContext.pageRect(pageNumber); 217 if (page.x() <= left && left < page.maxX() && page.y() <= top && top < page.maxY()) 218 return pageNumber; 219 } 220 return -1; 221 } 222 223 void PrintContext::collectLinkAndLinkedDestinations(Node* node) 224 { 225 for (Node* i = node->firstChild(); i; i = i->nextSibling()) 226 collectLinkAndLinkedDestinations(i); 227 228 if (!node->isLink() || !node->isElementNode()) 229 return; 230 const AtomicString& href = toElement(node)->getAttribute(HTMLNames::hrefAttr); 231 if (href.isNull()) 232 return; 233 KURL url = node->document().completeURL(href); 234 if (!url.isValid()) 235 return; 236 237 bool linkIsValid = true; 238 if (url.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(url, node->document().baseURL())) { 239 String name = url.fragmentIdentifier(); 240 Element* element = node->document().findAnchor(name); 241 if (element) 242 m_linkedDestinations.set(name, element); 243 else 244 linkIsValid = false; 245 } 246 247 if (linkIsValid) 248 m_linkDestinations.set(toElement(node), url); 249 } 250 251 void PrintContext::outputLinkAndLinkedDestinations(GraphicsContext& graphicsContext, Node* node, const IntRect& pageRect) 252 { 253 if (!m_linkAndLinkedDestinationsValid) { 254 collectLinkAndLinkedDestinations(node); 255 m_linkAndLinkedDestinationsValid = true; 256 } 257 258 WillBeHeapHashMap<RawPtrWillBeMember<Element>, KURL>::const_iterator endOfLinkDestinations = m_linkDestinations.end(); 259 for (WillBeHeapHashMap<RawPtrWillBeMember<Element>, KURL>::const_iterator it = m_linkDestinations.begin(); it != endOfLinkDestinations; ++it) { 260 RenderObject* renderer = it->key->renderer(); 261 if (!renderer) 262 continue; 263 KURL url = it->value; 264 IntRect boundingBox = renderer->absoluteBoundingBoxRect(); 265 if (!pageRect.intersects(boundingBox)) 266 continue; 267 if (url.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(url, renderer->document().baseURL())) { 268 String name = url.fragmentIdentifier(); 269 ASSERT(renderer->document().findAnchor(name)); 270 graphicsContext.setURLFragmentForRect(name, boundingBox); 271 } else { 272 graphicsContext.setURLForRect(url, boundingBox); 273 } 274 } 275 276 WillBeHeapHashMap<String, RawPtrWillBeMember<Element> >::const_iterator endOfLinkedDestinations = m_linkedDestinations.end(); 277 for (WillBeHeapHashMap<String, RawPtrWillBeMember<Element> >::const_iterator it = m_linkedDestinations.begin(); it != endOfLinkedDestinations; ++it) { 278 RenderObject* renderer = it->value->renderer(); 279 if (!renderer) 280 continue; 281 IntRect boundingBox = renderer->absoluteBoundingBoxRect(); 282 if (!pageRect.intersects(boundingBox)) 283 continue; 284 IntPoint point = boundingBox.minXMinYCorner(); 285 point.clampNegativeToZero(); 286 graphicsContext.addURLTargetAtPoint(it->key, point); 287 } 288 } 289 290 String PrintContext::pageProperty(LocalFrame* frame, const char* propertyName, int pageNumber) 291 { 292 Document* document = frame->document(); 293 PrintContext printContext(frame); 294 printContext.begin(800); // Any width is OK here. 295 document->updateLayout(); 296 RefPtr<RenderStyle> style = document->styleForPage(pageNumber); 297 298 // Implement formatters for properties we care about. 299 if (!strcmp(propertyName, "margin-left")) { 300 if (style->marginLeft().isAuto()) 301 return String("auto"); 302 return String::number(style->marginLeft().value()); 303 } 304 if (!strcmp(propertyName, "line-height")) 305 return String::number(style->lineHeight().value()); 306 if (!strcmp(propertyName, "font-size")) 307 return String::number(style->fontDescription().computedPixelSize()); 308 if (!strcmp(propertyName, "font-family")) 309 return style->fontDescription().family().family().string(); 310 if (!strcmp(propertyName, "size")) 311 return String::number(style->pageSize().width().value()) + ' ' + String::number(style->pageSize().height().value()); 312 313 return String("pageProperty() unimplemented for: ") + propertyName; 314 } 315 316 bool PrintContext::isPageBoxVisible(LocalFrame* frame, int pageNumber) 317 { 318 return frame->document()->isPageBoxVisible(pageNumber); 319 } 320 321 String PrintContext::pageSizeAndMarginsInPixels(LocalFrame* frame, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft) 322 { 323 IntSize pageSize(width, height); 324 frame->document()->pageSizeAndMarginsInPixels(pageNumber, pageSize, marginTop, marginRight, marginBottom, marginLeft); 325 326 return "(" + String::number(pageSize.width()) + ", " + String::number(pageSize.height()) + ") " + 327 String::number(marginTop) + ' ' + String::number(marginRight) + ' ' + String::number(marginBottom) + ' ' + String::number(marginLeft); 328 } 329 330 int PrintContext::numberOfPages(LocalFrame* frame, const FloatSize& pageSizeInPixels) 331 { 332 frame->document()->updateLayout(); 333 334 FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels); 335 PrintContext printContext(frame); 336 printContext.begin(pageRect.width(), pageRect.height()); 337 // Account for shrink-to-fit. 338 FloatSize scaledPageSize = pageSizeInPixels; 339 scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width()); 340 printContext.computePageRectsWithPageSize(scaledPageSize, false); 341 return printContext.pageCount(); 342 } 343 344 void PrintContext::trace(Visitor* visitor) 345 { 346 #if ENABLE(OILPAN) 347 visitor->trace(m_frame); 348 visitor->trace(m_linkDestinations); 349 visitor->trace(m_linkedDestinations); 350 #endif 351 } 352 353 } 354